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