001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertFalse; 022import static org.junit.Assert.assertNotSame; 023import static org.junit.Assert.assertSame; 024import static org.junit.Assert.assertTrue; 025 026import java.util.HashSet; 027import java.util.Set; 028import java.util.regex.Pattern; 029import org.apache.hadoop.hbase.testclassification.MiscTests; 030import org.apache.hadoop.hbase.testclassification.SmallTests; 031import org.apache.hadoop.hbase.util.Addressing; 032import org.apache.hadoop.hbase.util.Bytes; 033import org.junit.ClassRule; 034import org.junit.Test; 035import org.junit.experimental.categories.Category; 036 037@Category({ MiscTests.class, SmallTests.class }) 038public class TestServerName { 039 040 @ClassRule 041 public static final HBaseClassTestRule CLASS_RULE = 042 HBaseClassTestRule.forClass(TestServerName.class); 043 044 @Test 045 public void testHash() { 046 ServerName sn1 = ServerName.parseServerName("asf903.gq1.ygridcore.net,52690,1517835491385"); 047 ServerName sn2 = ServerName.parseServerName("asf903.gq1.ygridcore.net,42231,1517835491329"); 048 Set<ServerName> sns = new HashSet<>(); 049 sns.add(sn2); 050 sns.add(sn1); 051 sns.add(sn1); 052 assertEquals(2, sns.size()); 053 } 054 055 @Test 056 public void testShortString() { 057 ServerName sn = ServerName.valueOf("asf000.sp2.ygridcore.net", 1, 1); 058 assertEquals("asf000:1", sn.toShortString()); 059 sn = ServerName.valueOf("2607:f0d0:1002:0051:0000:0000:0000:0004", 1, 1); 060 assertEquals("2607:f0d0:1002:0051:0000:0000:0000:0004:1", sn.toShortString()); 061 sn = ServerName.valueOf("1.1.1.1", 1, 1); 062 assertEquals("1.1.1.1:1", sn.toShortString()); 063 } 064 065 @Test 066 public void testRegexPatterns() { 067 assertTrue(Pattern.matches(Addressing.VALID_PORT_REGEX, "123")); 068 assertFalse(Pattern.matches(Addressing.VALID_PORT_REGEX, "")); 069 assertTrue(ServerName.SERVERNAME_PATTERN.matcher("www1.example.org,1234,567").matches()); 070 ServerName.parseServerName("a.b.c,58102,1319771740322"); 071 ServerName.parseServerName("192.168.1.199,58102,1319771740322"); 072 ServerName.parseServerName("a.b.c:58102"); 073 ServerName.parseServerName("192.168.1.199:58102"); 074 } 075 076 @Test 077 public void testParseOfBytes() { 078 final String snStr = "www.EXAMPLE.org,1234,5678"; 079 ServerName sn = ServerName.valueOf(snStr); 080 byte[] versionedBytes = sn.getVersionedBytes(); 081 ServerName parsedSn = ServerName.parseVersionedServerName(versionedBytes); 082 assertEquals(sn.toString(), parsedSn.toString()); 083 assertEquals(sn.getHostnameLowerCase(), parsedSn.getHostnameLowerCase()); 084 assertEquals(sn.getPort(), parsedSn.getPort()); 085 assertEquals(sn.getStartCode(), parsedSn.getStartCode()); 086 087 final String hostnamePortStr = sn.getAddress().toString(); 088 byte[] bytes = Bytes.toBytes(hostnamePortStr); 089 parsedSn = ServerName.parseVersionedServerName(bytes); 090 assertEquals(sn.getHostnameLowerCase(), parsedSn.getHostnameLowerCase()); 091 assertEquals(sn.getPort(), parsedSn.getPort()); 092 assertEquals(ServerName.NON_STARTCODE, parsedSn.getStartCode()); 093 } 094 095 @Test 096 public void testServerName() { 097 ServerName sn = ServerName.valueOf("www.example.org", 1234, 5678); 098 ServerName sn2 = ServerName.valueOf("www.example.org", 1234, 5678); 099 ServerName sn3 = ServerName.valueOf("www.example.org", 1234, 56789); 100 assertTrue(sn.equals(sn2)); 101 assertFalse(sn.equals(sn3)); 102 assertEquals(sn.hashCode(), sn2.hashCode()); 103 assertNotSame(sn.hashCode(), sn3.hashCode()); 104 assertEquals(sn.toString(), ServerName.valueOf("www.example.org", 1234, 5678).toString()); 105 assertEquals(sn.toString(), ServerName.valueOf("www.example.org:1234", 5678).toString()); 106 assertEquals("www.example.org" + ServerName.SERVERNAME_SEPARATOR + "1234" 107 + ServerName.SERVERNAME_SEPARATOR + "5678", sn.toString()); 108 } 109 110 @Test 111 public void testHostNameCaseSensitivity() { 112 ServerName lower = ServerName.valueOf("www.example.org", 1234, 5678); 113 ServerName upper = ServerName.valueOf("www.EXAMPLE.org", 1234, 5678); 114 assertEquals(0, lower.compareTo(upper)); 115 assertEquals(0, upper.compareTo(lower)); 116 assertEquals(lower.hashCode(), upper.hashCode()); 117 assertTrue(lower.equals(upper)); 118 assertTrue(upper.equals(lower)); 119 assertTrue(ServerName.isSameAddress(lower, upper)); 120 } 121 122 @Test 123 public void testInterning() { 124 ServerName sn1 = ServerName.valueOf("www.example.org", 1234, 5671); 125 assertSame(sn1, ServerName.valueOf("www.example.org", 1234, 5671)); 126 } 127 128 @Test 129 public void testInterningDoesWeakReferences() { 130 for (int i = 0; i < 5000; i++) { 131 final int startcode = i++; 132 final ServerName sn1 = ServerName.valueOf("www.example.org", 1234, startcode); 133 assertSame(sn1, ServerName.valueOf("www.example.org", 1234, startcode)); 134 } 135 } 136}