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.util.Addressing;
28 import org.apache.hadoop.hbase.util.Bytes;
29 import org.junit.Test;
30
31 public class TestServerName {
32 @Test
33 public void testRegexPatterns() {
34 assertTrue(Pattern.matches(Addressing.VALID_PORT_REGEX, "123"));
35 assertFalse(Pattern.matches(Addressing.VALID_PORT_REGEX, ""));
36 assertTrue(ServerName.SERVERNAME_PATTERN.matcher("www1.example.org,1234,567").matches());
37 ServerName.parseServerName("a.b.c,58102,1319771740322");
38 ServerName.parseServerName("192.168.1.199,58102,1319771740322");
39 ServerName.parseServerName("a.b.c:58102");
40 ServerName.parseServerName("192.168.1.199:58102");
41 }
42
43 @Test public void testParseOfBytes() {
44 final String snStr = "www.example.org,1234,5678";
45 ServerName sn = new ServerName(snStr);
46 byte [] versionedBytes = sn.getVersionedBytes();
47 assertEquals(snStr, ServerName.parseVersionedServerName(versionedBytes).toString());
48 final String hostnamePortStr = "www.example.org:1234";
49 byte [] bytes = Bytes.toBytes(hostnamePortStr);
50 String expecting =
51 hostnamePortStr.replace(":", ServerName.SERVERNAME_SEPARATOR) +
52 ServerName.SERVERNAME_SEPARATOR + ServerName.NON_STARTCODE;
53 assertEquals(expecting, ServerName.parseVersionedServerName(bytes).toString());
54 }
55
56 @Test
57 public void testServerName() {
58 ServerName sn = new ServerName("www.example.org", 1234, 5678);
59 ServerName sn2 = new ServerName("www.example.org", 1234, 5678);
60 ServerName sn3 = new ServerName("www.example.org", 1234, 56789);
61 assertTrue(sn.equals(sn2));
62 assertFalse(sn.equals(sn3));
63 assertEquals(sn.hashCode(), sn2.hashCode());
64 assertNotSame(sn.hashCode(), sn3.hashCode());
65 assertEquals(sn.toString(),
66 ServerName.getServerName("www.example.org", 1234, 5678));
67 assertEquals(sn.toString(),
68 ServerName.getServerName("www.example.org:1234", 5678));
69 assertEquals(sn.toString(),
70 "www.example.org" + ServerName.SERVERNAME_SEPARATOR +
71 "1234" + ServerName.SERVERNAME_SEPARATOR + "5678");
72 }
73
74 @Test
75 public void getServerStartcodeFromServerName() {
76 ServerName sn = new ServerName("www.example.org", 1234, 5678);
77 assertEquals(5678,
78 ServerName.getServerStartcodeFromServerName(sn.toString()));
79 assertNotSame(5677,
80 ServerName.getServerStartcodeFromServerName(sn.toString()));
81 }
82 }