001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertNotSame;
023import static org.junit.Assert.assertSame;
024import static org.junit.Assert.assertTrue;
025import java.util.HashSet;
026import java.util.Set;
027import java.util.regex.Pattern;
028import org.apache.hadoop.hbase.testclassification.MiscTests;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.apache.hadoop.hbase.util.Addressing;
031import org.apache.hadoop.hbase.util.Bytes;
032import org.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035
036@Category({MiscTests.class, SmallTests.class})
037public class TestServerName {
038
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041      HBaseClassTestRule.forClass(TestServerName.class);
042
043  @Test
044  public void testHash() {
045    ServerName sn1 = ServerName.parseServerName("asf903.gq1.ygridcore.net,52690,1517835491385");
046    ServerName sn2 = ServerName.parseServerName("asf903.gq1.ygridcore.net,42231,1517835491329");
047    Set<ServerName> sns = new HashSet<>();
048    sns.add(sn2);
049    sns.add(sn1);
050    sns.add(sn1);
051    assertEquals(2, sns.size());
052  }
053
054  @Test
055  public void testShortString() {
056    ServerName sn = ServerName.valueOf("asf000.sp2.ygridcore.net", 1, 1);
057    assertEquals("asf000:1", sn.toShortString());
058    sn = ServerName.valueOf("2607:f0d0:1002:0051:0000:0000:0000:0004", 1, 1);
059    assertEquals("2607:f0d0:1002:0051:0000:0000:0000:0004:1", sn.toShortString());
060    sn = ServerName.valueOf("1.1.1.1", 1, 1);
061    assertEquals("1.1.1.1:1", sn.toShortString());
062  }
063
064  @Test
065  public void testRegexPatterns() {
066    assertTrue(Pattern.matches(Addressing.VALID_PORT_REGEX, "123"));
067    assertFalse(Pattern.matches(Addressing.VALID_PORT_REGEX, ""));
068    assertTrue(ServerName.SERVERNAME_PATTERN.matcher("www1.example.org,1234,567").matches());
069    ServerName.parseServerName("a.b.c,58102,1319771740322");
070    ServerName.parseServerName("192.168.1.199,58102,1319771740322");
071    ServerName.parseServerName("a.b.c:58102");
072    ServerName.parseServerName("192.168.1.199:58102");
073  }
074
075  @Test
076  public void testParseOfBytes() {
077    final String snStr = "www.EXAMPLE.org,1234,5678";
078    ServerName sn = ServerName.valueOf(snStr);
079    byte[] versionedBytes = sn.getVersionedBytes();
080    ServerName parsedSn = ServerName.parseVersionedServerName(versionedBytes);
081    assertEquals(sn.toString(), parsedSn.toString());
082    assertEquals(sn.getHostnameLowerCase(), parsedSn.getHostnameLowerCase());
083    assertEquals(sn.getPort(), parsedSn.getPort());
084    assertEquals(sn.getStartcode(), parsedSn.getStartcode());
085
086    final String hostnamePortStr = sn.getAddress().toString();
087    byte[] bytes = Bytes.toBytes(hostnamePortStr);
088    parsedSn = ServerName.parseVersionedServerName(bytes);
089    assertEquals(sn.getHostnameLowerCase(), parsedSn.getHostnameLowerCase());
090    assertEquals(sn.getPort(), parsedSn.getPort());
091    assertEquals(ServerName.NON_STARTCODE, parsedSn.getStartcode());
092  }
093
094  @Test
095  public void testServerName() {
096    ServerName sn = ServerName.valueOf("www.example.org", 1234, 5678);
097    ServerName sn2 = ServerName.valueOf("www.example.org", 1234, 5678);
098    ServerName sn3 = ServerName.valueOf("www.example.org", 1234, 56789);
099    assertTrue(sn.equals(sn2));
100    assertFalse(sn.equals(sn3));
101    assertEquals(sn.hashCode(), sn2.hashCode());
102    assertNotSame(sn.hashCode(), sn3.hashCode());
103    assertEquals(sn.toString(),
104        ServerName.valueOf("www.example.org", 1234, 5678).toString());
105    assertEquals(sn.toString(),
106        ServerName.valueOf("www.example.org:1234", 5678).toString());
107    assertEquals("www.example.org" + ServerName.SERVERNAME_SEPARATOR + "1234"
108        + ServerName.SERVERNAME_SEPARATOR + "5678", sn.toString());
109  }
110
111  @Test
112  public void testHostNameCaseSensitivity() {
113    ServerName lower = ServerName.valueOf("www.example.org", 1234, 5678);
114    ServerName upper = ServerName.valueOf("www.EXAMPLE.org", 1234, 5678);
115    assertEquals(0, lower.compareTo(upper));
116    assertEquals(0, upper.compareTo(lower));
117    assertEquals(lower.hashCode(), upper.hashCode());
118    assertTrue(lower.equals(upper));
119    assertTrue(upper.equals(lower));
120    assertTrue(ServerName.isSameAddress(lower, upper));
121  }
122
123  @Test
124  public void testInterning() {
125    ServerName sn1 = ServerName.valueOf("www.example.org", 1234, 5671);
126    assertSame(sn1, ServerName.valueOf("www.example.org", 1234, 5671));
127  }
128
129  @Test
130  public void testInterningDoesWeakReferences() {
131    for (int i = 0; i < 5000; i++) {
132      final int startcode = i++;
133      final ServerName sn1 = ServerName.valueOf("www.example.org", 1234, startcode);
134      assertSame(sn1, ServerName.valueOf("www.example.org", 1234, startcode));
135    }
136  }
137}