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.assertTrue;
024
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<ServerName>();
048    sns.add(sn2);
049    sns.add(sn1);
050    sns.add(sn1);
051    assertEquals(2, sns.size());
052  }
053
054  @Test
055  public void testGetHostNameMinusDomain() {
056    assertEquals("2607:f0d0:1002:51::4",
057      ServerName.getHostNameMinusDomain("2607:f0d0:1002:51::4"));
058    assertEquals("2607:f0d0:1002:0051:0000:0000:0000:0004",
059        ServerName.getHostNameMinusDomain("2607:f0d0:1002:0051:0000:0000:0000:0004"));
060    assertEquals("1.1.1.1", ServerName.getHostNameMinusDomain("1.1.1.1"));
061    assertEquals("x", ServerName.getHostNameMinusDomain("x"));
062    assertEquals("x", ServerName.getHostNameMinusDomain("x.y.z"));
063    assertEquals("asf000", ServerName.getHostNameMinusDomain("asf000.sp2.ygridcore.net"));
064    ServerName sn = ServerName.valueOf("asf000.sp2.ygridcore.net", 1, 1);
065    assertEquals("asf000.sp2.ygridcore.net,1,1", sn.toString());
066  }
067
068  @Test
069  public void testShortString() {
070    ServerName sn = ServerName.valueOf("asf000.sp2.ygridcore.net", 1, 1);
071    assertEquals("asf000:1", sn.toShortString());
072    sn = ServerName.valueOf("2607:f0d0:1002:0051:0000:0000:0000:0004", 1, 1);
073    assertEquals("2607:f0d0:1002:0051:0000:0000:0000:0004:1", sn.toShortString());
074    sn = ServerName.valueOf("1.1.1.1", 1, 1);
075    assertEquals("1.1.1.1:1", sn.toShortString());
076  }
077
078  @Test
079  public void testRegexPatterns() {
080    assertTrue(Pattern.matches(Addressing.VALID_PORT_REGEX, "123"));
081    assertFalse(Pattern.matches(Addressing.VALID_PORT_REGEX, ""));
082    assertTrue(ServerName.SERVERNAME_PATTERN.matcher("www1.example.org,1234,567").matches());
083    ServerName.parseServerName("a.b.c,58102,1319771740322");
084    ServerName.parseServerName("192.168.1.199,58102,1319771740322");
085    ServerName.parseServerName("a.b.c:58102");
086    ServerName.parseServerName("192.168.1.199:58102");
087  }
088
089  @Test public void testParseOfBytes() {
090    final String snStr = "www.EXAMPLE.org,1234,5678";
091    ServerName sn = ServerName.valueOf(snStr);
092    byte [] versionedBytes = sn.getVersionedBytes();
093    ServerName parsedSn = ServerName.parseVersionedServerName(versionedBytes);
094    assertEquals(sn.toString(), parsedSn.toString());
095    assertEquals(sn.getHostnameLowerCase(), parsedSn.getHostnameLowerCase());
096    assertEquals(sn.getPort(), parsedSn.getPort());
097    assertEquals(sn.getStartcode(), parsedSn.getStartcode());
098
099    final String hostnamePortStr = sn.getAddress().toString();
100    byte [] bytes = Bytes.toBytes(hostnamePortStr);
101    parsedSn = ServerName.parseVersionedServerName(bytes);
102    assertEquals(sn.getHostnameLowerCase(), parsedSn.getHostnameLowerCase());
103    assertEquals(sn.getPort(), parsedSn.getPort());
104    assertEquals(ServerName.NON_STARTCODE, parsedSn.getStartcode());
105  }
106
107  @Test
108  public void testServerName() {
109    ServerName sn = ServerName.valueOf("www.example.org", 1234, 5678);
110    ServerName sn2 = ServerName.valueOf("www.example.org", 1234, 5678);
111    ServerName sn3 = ServerName.valueOf("www.example.org", 1234, 56789);
112    assertTrue(sn.equals(sn2));
113    assertFalse(sn.equals(sn3));
114    assertEquals(sn.hashCode(), sn2.hashCode());
115    assertNotSame(sn.hashCode(), sn3.hashCode());
116    assertEquals(sn.toString(),
117      ServerName.valueOf("www.example.org", 1234, 5678).toString());
118    assertEquals(sn.toString(),
119      ServerName.valueOf("www.example.org:1234", 5678).toString());
120    assertEquals("www.example.org" + ServerName.SERVERNAME_SEPARATOR + "1234"
121        + ServerName.SERVERNAME_SEPARATOR + "5678", sn.toString());
122  }
123
124  @Test
125  public void testHostNameCaseSensitivity() {
126    ServerName lower = ServerName.valueOf("www.example.org", 1234, 5678);
127    ServerName upper = ServerName.valueOf("www.EXAMPLE.org", 1234, 5678);
128    assertEquals(0, lower.compareTo(upper));
129    assertEquals(0, upper.compareTo(lower));
130    assertEquals(lower.hashCode(), upper.hashCode());
131    assertTrue(lower.equals(upper));
132    assertTrue(upper.equals(lower));
133    assertTrue(ServerName.isSameAddress(lower, upper));
134  }
135}
136