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.util.Addressing;
28  import org.apache.hadoop.hbase.util.Bytes;
29  import org.junit.Test;
30  import org.junit.experimental.categories.Category;
31  
32  @Category(SmallTests.class)
33  public class TestServerName {
34    @Test
35    public void testGetHostNameMinusDomain() {
36      assertEquals("2607:f0d0:1002:51::4",
37        ServerName.getHostNameMinusDomain("2607:f0d0:1002:51::4"));
38      assertEquals("2607:f0d0:1002:0051:0000:0000:0000:0004",
39          ServerName.getHostNameMinusDomain("2607:f0d0:1002:0051:0000:0000:0000:0004"));
40      assertEquals("1.1.1.1", ServerName.getHostNameMinusDomain("1.1.1.1"));
41      assertEquals("x", ServerName.getHostNameMinusDomain("x"));
42      assertEquals("x", ServerName.getHostNameMinusDomain("x.y.z"));
43      assertEquals("asf000", ServerName.getHostNameMinusDomain("asf000.sp2.ygridcore.net"));
44      ServerName sn = ServerName.valueOf("asf000.sp2.ygridcore.net", 1, 1);
45      assertEquals("asf000.sp2.ygridcore.net,1,1", sn.toString());
46    }
47  
48    @Test
49    public void testShortString() {
50      ServerName sn = ServerName.valueOf("asf000.sp2.ygridcore.net", 1, 1);
51      assertEquals("asf000:1", sn.toShortString());
52      sn = ServerName.valueOf("2607:f0d0:1002:0051:0000:0000:0000:0004", 1, 1);
53      assertEquals("2607:f0d0:1002:0051:0000:0000:0000:0004:1", sn.toShortString());
54      sn = ServerName.valueOf("1.1.1.1", 1, 1);
55      assertEquals("1.1.1.1:1", sn.toShortString());
56    }
57  
58    @Test
59    public void testRegexPatterns() {
60      assertTrue(Pattern.matches(Addressing.VALID_PORT_REGEX, "123"));
61      assertFalse(Pattern.matches(Addressing.VALID_PORT_REGEX, ""));
62      assertTrue(ServerName.SERVERNAME_PATTERN.matcher("www1.example.org,1234,567").matches());
63      ServerName.parseServerName("a.b.c,58102,1319771740322");
64      ServerName.parseServerName("192.168.1.199,58102,1319771740322");
65      ServerName.parseServerName("a.b.c:58102");
66      ServerName.parseServerName("192.168.1.199:58102");
67    }
68  
69    @Test public void testParseOfBytes() {
70      final String snStr = "www.example.org,1234,5678";
71      ServerName sn = ServerName.valueOf(snStr);
72      byte [] versionedBytes = sn.getVersionedBytes();
73      assertEquals(sn.toString(), ServerName.parseVersionedServerName(versionedBytes).toString());
74      final String hostnamePortStr = sn.getHostAndPort();
75      byte [] bytes = Bytes.toBytes(hostnamePortStr);
76      String expecting =
77        hostnamePortStr.replace(":", ServerName.SERVERNAME_SEPARATOR) +
78        ServerName.SERVERNAME_SEPARATOR + ServerName.NON_STARTCODE;
79      assertEquals(expecting, ServerName.parseVersionedServerName(bytes).toString());
80    }
81  
82    @Test
83    public void testServerName() {
84      ServerName sn = ServerName.valueOf("www.example.org", 1234, 5678);
85      ServerName sn2 = ServerName.valueOf("www.example.org", 1234, 5678);
86      ServerName sn3 = ServerName.valueOf("www.example.org", 1234, 56789);
87      assertTrue(sn.equals(sn2));
88      assertFalse(sn.equals(sn3));
89      assertEquals(sn.hashCode(), sn2.hashCode());
90      assertNotSame(sn.hashCode(), sn3.hashCode());
91      assertEquals(sn.toString(),
92        ServerName.getServerName("www.example.org", 1234, 5678));
93      assertEquals(sn.toString(),
94        ServerName.getServerName("www.example.org:1234", 5678));
95      assertEquals(sn.toString(),
96        "www.example.org" + ServerName.SERVERNAME_SEPARATOR + "1234" +
97        ServerName.SERVERNAME_SEPARATOR + "5678");
98    }
99  
100   @Test
101   public void getServerStartcodeFromServerName() {
102     ServerName sn = ServerName.valueOf("www.example.org", 1234, 5678);
103     assertEquals(5678,
104       ServerName.getServerStartcodeFromServerName(sn.toString()));
105     assertNotSame(5677,
106       ServerName.getServerStartcodeFromServerName(sn.toString()));
107   }
108 
109 }
110