2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
;
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
.assertSame
;
24 import static org
.junit
.Assert
.assertTrue
;
26 import java
.util
.HashSet
;
28 import java
.util
.regex
.Pattern
;
30 import org
.apache
.hadoop
.hbase
.testclassification
.MiscTests
;
31 import org
.apache
.hadoop
.hbase
.testclassification
.SmallTests
;
32 import org
.apache
.hadoop
.hbase
.util
.Addressing
;
33 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
34 import org
.junit
.ClassRule
;
35 import org
.junit
.Ignore
;
36 import org
.junit
.Test
;
37 import org
.junit
.experimental
.categories
.Category
;
39 @Category({MiscTests
.class, SmallTests
.class})
40 public class TestServerName
{
43 public static final HBaseClassTestRule CLASS_RULE
=
44 HBaseClassTestRule
.forClass(TestServerName
.class);
47 public void testHash() {
48 ServerName sn1
= ServerName
.parseServerName("asf903.gq1.ygridcore.net,52690,1517835491385");
49 ServerName sn2
= ServerName
.parseServerName("asf903.gq1.ygridcore.net,42231,1517835491329");
50 Set
<ServerName
> sns
= new HashSet
<ServerName
>();
54 assertEquals(2, sns
.size());
58 public void testGetHostNameMinusDomain() {
59 assertEquals("2607:f0d0:1002:51::4",
60 ServerName
.getHostNameMinusDomain("2607:f0d0:1002:51::4"));
61 assertEquals("2607:f0d0:1002:0051:0000:0000:0000:0004",
62 ServerName
.getHostNameMinusDomain("2607:f0d0:1002:0051:0000:0000:0000:0004"));
63 assertEquals("1.1.1.1", ServerName
.getHostNameMinusDomain("1.1.1.1"));
64 assertEquals("x", ServerName
.getHostNameMinusDomain("x"));
65 assertEquals("x", ServerName
.getHostNameMinusDomain("x.y.z"));
66 assertEquals("asf000", ServerName
.getHostNameMinusDomain("asf000.sp2.ygridcore.net"));
67 ServerName sn
= ServerName
.valueOf("asf000.sp2.ygridcore.net", 1, 1);
68 assertEquals("asf000.sp2.ygridcore.net,1,1", sn
.toString());
72 public void testShortString() {
73 ServerName sn
= ServerName
.valueOf("asf000.sp2.ygridcore.net", 1, 1);
74 assertEquals("asf000:1", sn
.toShortString());
75 sn
= ServerName
.valueOf("2607:f0d0:1002:0051:0000:0000:0000:0004", 1, 1);
76 assertEquals("2607:f0d0:1002:0051:0000:0000:0000:0004:1", sn
.toShortString());
77 sn
= ServerName
.valueOf("1.1.1.1", 1, 1);
78 assertEquals("1.1.1.1:1", sn
.toShortString());
82 public void testRegexPatterns() {
83 assertTrue(Pattern
.matches(Addressing
.VALID_PORT_REGEX
, "123"));
84 assertFalse(Pattern
.matches(Addressing
.VALID_PORT_REGEX
, ""));
85 assertTrue(ServerName
.SERVERNAME_PATTERN
.matcher("www1.example.org,1234,567").matches());
86 ServerName
.parseServerName("a.b.c,58102,1319771740322");
87 ServerName
.parseServerName("192.168.1.199,58102,1319771740322");
88 ServerName
.parseServerName("a.b.c:58102");
89 ServerName
.parseServerName("192.168.1.199:58102");
93 public void testParseOfBytes() {
94 final String snStr
= "www.EXAMPLE.org,1234,5678";
95 ServerName sn
= ServerName
.valueOf(snStr
);
96 byte[] versionedBytes
= sn
.getVersionedBytes();
97 ServerName parsedSn
= ServerName
.parseVersionedServerName(versionedBytes
);
98 assertEquals(sn
.toString(), parsedSn
.toString());
99 assertEquals(sn
.getHostnameLowerCase(), parsedSn
.getHostnameLowerCase());
100 assertEquals(sn
.getPort(), parsedSn
.getPort());
101 assertEquals(sn
.getStartcode(), parsedSn
.getStartcode());
103 final String hostnamePortStr
= sn
.getAddress().toString();
104 byte[] bytes
= Bytes
.toBytes(hostnamePortStr
);
105 parsedSn
= ServerName
.parseVersionedServerName(bytes
);
106 assertEquals(sn
.getHostnameLowerCase(), parsedSn
.getHostnameLowerCase());
107 assertEquals(sn
.getPort(), parsedSn
.getPort());
108 assertEquals(ServerName
.NON_STARTCODE
, parsedSn
.getStartcode());
112 public void testServerName() {
113 ServerName sn
= ServerName
.valueOf("www.example.org", 1234, 5678);
114 ServerName sn2
= ServerName
.valueOf("www.example.org", 1234, 5678);
115 ServerName sn3
= ServerName
.valueOf("www.example.org", 1234, 56789);
116 assertTrue(sn
.equals(sn2
));
117 assertFalse(sn
.equals(sn3
));
118 assertEquals(sn
.hashCode(), sn2
.hashCode());
119 assertNotSame(sn
.hashCode(), sn3
.hashCode());
120 assertEquals(sn
.toString(),
121 ServerName
.valueOf("www.example.org", 1234, 5678).toString());
122 assertEquals(sn
.toString(),
123 ServerName
.valueOf("www.example.org:1234", 5678).toString());
124 assertEquals("www.example.org" + ServerName
.SERVERNAME_SEPARATOR
+ "1234"
125 + ServerName
.SERVERNAME_SEPARATOR
+ "5678", sn
.toString());
129 public void testHostNameCaseSensitivity() {
130 ServerName lower
= ServerName
.valueOf("www.example.org", 1234, 5678);
131 ServerName upper
= ServerName
.valueOf("www.EXAMPLE.org", 1234, 5678);
132 assertEquals(0, lower
.compareTo(upper
));
133 assertEquals(0, upper
.compareTo(lower
));
134 assertEquals(lower
.hashCode(), upper
.hashCode());
135 assertTrue(lower
.equals(upper
));
136 assertTrue(upper
.equals(lower
));
137 assertTrue(ServerName
.isSameAddress(lower
, upper
));
141 public void testInterning() {
142 ServerName sn1
= ServerName
.valueOf("www.example.org", 1234, 5671);
143 assertSame(sn1
, ServerName
.valueOf("www.example.org", 1234, 5671));
146 @Ignore // Enable and let fun for hours to make sure weak references working fine.
148 public void testInterningDoesWeakReferences() {
149 for (int i
= 0; i
< Integer
.MAX_VALUE
; i
++) {
150 ServerName
.valueOf("www.example.org", 1234, i
++);