View Javadoc

1   /**
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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
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      ServerName parsedSn = ServerName.parseVersionedServerName(versionedBytes);
75      assertEquals(sn.toString(), parsedSn.toString());
76      assertEquals(sn.getHostnameLowerCase(), parsedSn.getHostnameLowerCase());
77      assertEquals(sn.getPort(), parsedSn.getPort());
78      assertEquals(sn.getStartcode(), parsedSn.getStartcode());
79  
80      final String hostnamePortStr = sn.getAddress().toString();
81      byte [] bytes = Bytes.toBytes(hostnamePortStr);
82      parsedSn = ServerName.parseVersionedServerName(bytes);
83      assertEquals(sn.getHostnameLowerCase(), parsedSn.getHostnameLowerCase());
84      assertEquals(sn.getPort(), parsedSn.getPort());
85      assertEquals(ServerName.NON_STARTCODE, parsedSn.getStartcode());
86    }
87  
88    @Test
89    public void testServerName() {
90      ServerName sn = ServerName.valueOf("www.example.org", 1234, 5678);
91      ServerName sn2 = ServerName.valueOf("www.example.org", 1234, 5678);
92      ServerName sn3 = ServerName.valueOf("www.example.org", 1234, 56789);
93      assertTrue(sn.equals(sn2));
94      assertFalse(sn.equals(sn3));
95      assertEquals(sn.hashCode(), sn2.hashCode());
96      assertNotSame(sn.hashCode(), sn3.hashCode());
97      assertEquals(sn.toString(),
98        ServerName.getServerName("www.example.org", 1234, 5678));
99      assertEquals(sn.toString(),
100       ServerName.getServerName("www.example.org:1234", 5678));
101     assertEquals(sn.toString(),
102       "www.example.org" + ServerName.SERVERNAME_SEPARATOR + "1234" +
103       ServerName.SERVERNAME_SEPARATOR + "5678");
104   }
105 
106   @Test
107   public void getServerStartcodeFromServerName() {
108     ServerName sn = ServerName.valueOf("www.example.org", 1234, 5678);
109     assertEquals(5678,
110       ServerName.getServerStartcodeFromServerName(sn.toString()));
111     assertNotSame(5677,
112       ServerName.getServerStartcodeFromServerName(sn.toString()));
113   }
114 
115   @Test
116   public void testHostNameCaseSensitivity() {
117     ServerName lower = ServerName.valueOf("www.example.org", 1234, 5678);
118     ServerName upper = ServerName.valueOf("www.EXAMPLE.org", 1234, 5678);
119     assertEquals(0, lower.compareTo(upper));
120     assertEquals(0, upper.compareTo(lower));
121     assertEquals(lower.hashCode(), upper.hashCode());
122     assertTrue(lower.equals(upper));
123     assertTrue(upper.equals(lower));
124     assertTrue(ServerName.isSameHostnameAndPort(lower, upper));
125   }
126 }
127