1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertNotSame;
23 import static org.junit.Assert.assertTrue;
24
25 import java.util.regex.Pattern;
26
27 import org.apache.hadoop.hbase.testclassification.SmallTests;
28 import org.apache.hadoop.hbase.util.Addressing;
29 import org.apache.hadoop.hbase.util.Bytes;
30 import org.junit.Test;
31 import org.junit.experimental.categories.Category;
32
33 @Category(SmallTests.class)
34 public class TestServerName {
35 @Test
36 public void testGetHostNameMinusDomain() {
37 assertEquals("2607:f0d0:1002:51::4",
38 ServerName.getHostNameMinusDomain("2607:f0d0:1002:51::4"));
39 assertEquals("2607:f0d0:1002:0051:0000:0000:0000:0004",
40 ServerName.getHostNameMinusDomain("2607:f0d0:1002:0051:0000:0000:0000:0004"));
41 assertEquals("1.1.1.1", ServerName.getHostNameMinusDomain("1.1.1.1"));
42 assertEquals("x", ServerName.getHostNameMinusDomain("x"));
43 assertEquals("x", ServerName.getHostNameMinusDomain("x.y.z"));
44 assertEquals("asf000", ServerName.getHostNameMinusDomain("asf000.sp2.ygridcore.net"));
45 ServerName sn = ServerName.valueOf("asf000.sp2.ygridcore.net", 1, 1);
46 assertEquals("asf000.sp2.ygridcore.net,1,1", sn.toString());
47 }
48
49 @Test
50 public void testShortString() {
51 ServerName sn = ServerName.valueOf("asf000.sp2.ygridcore.net", 1, 1);
52 assertEquals("asf000:1", sn.toShortString());
53 sn = ServerName.valueOf("2607:f0d0:1002:0051:0000:0000:0000:0004", 1, 1);
54 assertEquals("2607:f0d0:1002:0051:0000:0000:0000:0004:1", sn.toShortString());
55 sn = ServerName.valueOf("1.1.1.1", 1, 1);
56 assertEquals("1.1.1.1:1", sn.toShortString());
57 }
58
59 @Test
60 public void testRegexPatterns() {
61 assertTrue(Pattern.matches(Addressing.VALID_PORT_REGEX, "123"));
62 assertFalse(Pattern.matches(Addressing.VALID_PORT_REGEX, ""));
63 assertTrue(ServerName.SERVERNAME_PATTERN.matcher("www1.example.org,1234,567").matches());
64 ServerName.parseServerName("a.b.c,58102,1319771740322");
65 ServerName.parseServerName("192.168.1.199,58102,1319771740322");
66 ServerName.parseServerName("a.b.c:58102");
67 ServerName.parseServerName("192.168.1.199:58102");
68 }
69
70 @Test public void testParseOfBytes() {
71 final String snStr = "www.example.org,1234,5678";
72 ServerName sn = ServerName.valueOf(snStr);
73 byte [] versionedBytes = sn.getVersionedBytes();
74 assertEquals(sn.toString(), ServerName.parseVersionedServerName(versionedBytes).toString());
75 final String hostnamePortStr = sn.getHostAndPort();
76 byte [] bytes = Bytes.toBytes(hostnamePortStr);
77 String expecting =
78 hostnamePortStr.replace(":", ServerName.SERVERNAME_SEPARATOR) +
79 ServerName.SERVERNAME_SEPARATOR + ServerName.NON_STARTCODE;
80 assertEquals(expecting, ServerName.parseVersionedServerName(bytes).toString());
81 }
82
83 @Test
84 public void testServerName() {
85 ServerName sn = ServerName.valueOf("www.example.org", 1234, 5678);
86 ServerName sn2 = ServerName.valueOf("www.example.org", 1234, 5678);
87 ServerName sn3 = ServerName.valueOf("www.example.org", 1234, 56789);
88 assertTrue(sn.equals(sn2));
89 assertFalse(sn.equals(sn3));
90 assertEquals(sn.hashCode(), sn2.hashCode());
91 assertNotSame(sn.hashCode(), sn3.hashCode());
92 assertEquals(sn.toString(),
93 ServerName.getServerName("www.example.org", 1234, 5678));
94 assertEquals(sn.toString(),
95 ServerName.getServerName("www.example.org:1234", 5678));
96 assertEquals(sn.toString(),
97 "www.example.org" + ServerName.SERVERNAME_SEPARATOR + "1234" +
98 ServerName.SERVERNAME_SEPARATOR + "5678");
99 }
100
101 @Test
102 public void getServerStartcodeFromServerName() {
103 ServerName sn = ServerName.valueOf("www.example.org", 1234, 5678);
104 assertEquals(5678,
105 ServerName.getServerStartcodeFromServerName(sn.toString()));
106 assertNotSame(5677,
107 ServerName.getServerStartcodeFromServerName(sn.toString()));
108 }
109
110 @Test
111 public void testHostNameCaseSensitivity() {
112 ServerName lower = ServerName.valueOf("www.example.org", 1234, 5678);
113 ServerName upper = ServerName.valueOf("www.EXAMPLE.org", 1234, 5678);
114 assertEquals(0, lower.compareTo(upper));
115 assertEquals(0, upper.compareTo(lower));
116 assertEquals(lower.hashCode(), upper.hashCode());
117 assertTrue(lower.equals(upper));
118 assertTrue(upper.equals(lower));
119 assertTrue(ServerName.isSameHostnameAndPort(lower, upper));
120 }
121 }
122