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 testGetHostNameMinusDomain() { 057 assertEquals("2607:f0d0:1002:51::4", ServerName.getHostNameMinusDomain("2607:f0d0:1002:51::4")); 058 assertEquals("2607:f0d0:1002:0051:0000:0000:0000:0004", 059 ServerName.getHostNameMinusDomain("2607:f0d0:1002:0051:0000:0000:0000:0004")); 060 assertEquals("1.1.1.1", ServerName.getHostNameMinusDomain("1.1.1.1")); 061 assertEquals("x", ServerName.getHostNameMinusDomain("x")); 062 assertEquals("x", ServerName.getHostNameMinusDomain("x.y.z")); 063 assertEquals("asf000", ServerName.getHostNameMinusDomain("asf000.sp2.ygridcore.net")); 064 ServerName sn = ServerName.valueOf("asf000.sp2.ygridcore.net", 1, 1); 065 assertEquals("asf000.sp2.ygridcore.net,1,1", sn.toString()); 066 } 067 068 @Test 069 public void testShortString() { 070 ServerName sn = ServerName.valueOf("asf000.sp2.ygridcore.net", 1, 1); 071 assertEquals("asf000:1", sn.toShortString()); 072 sn = ServerName.valueOf("2607:f0d0:1002:0051:0000:0000:0000:0004", 1, 1); 073 assertEquals("2607:f0d0:1002:0051:0000:0000:0000:0004:1", sn.toShortString()); 074 sn = ServerName.valueOf("1.1.1.1", 1, 1); 075 assertEquals("1.1.1.1:1", sn.toShortString()); 076 } 077 078 @Test 079 public void testRegexPatterns() { 080 assertTrue(Pattern.matches(Addressing.VALID_PORT_REGEX, "123")); 081 assertFalse(Pattern.matches(Addressing.VALID_PORT_REGEX, "")); 082 assertTrue(ServerName.SERVERNAME_PATTERN.matcher("www1.example.org,1234,567").matches()); 083 ServerName.parseServerName("a.b.c,58102,1319771740322"); 084 ServerName.parseServerName("192.168.1.199,58102,1319771740322"); 085 ServerName.parseServerName("a.b.c:58102"); 086 ServerName.parseServerName("192.168.1.199:58102"); 087 } 088 089 @Test 090 public void testParseOfBytes() { 091 final String snStr = "www.EXAMPLE.org,1234,5678"; 092 ServerName sn = ServerName.valueOf(snStr); 093 byte[] versionedBytes = sn.getVersionedBytes(); 094 ServerName parsedSn = ServerName.parseVersionedServerName(versionedBytes); 095 assertEquals(sn.toString(), parsedSn.toString()); 096 assertEquals(sn.getHostnameLowerCase(), parsedSn.getHostnameLowerCase()); 097 assertEquals(sn.getPort(), parsedSn.getPort()); 098 assertEquals(sn.getStartCode(), parsedSn.getStartCode()); 099 100 final String hostnamePortStr = sn.getAddress().toString(); 101 byte[] bytes = Bytes.toBytes(hostnamePortStr); 102 parsedSn = ServerName.parseVersionedServerName(bytes); 103 assertEquals(sn.getHostnameLowerCase(), parsedSn.getHostnameLowerCase()); 104 assertEquals(sn.getPort(), parsedSn.getPort()); 105 assertEquals(ServerName.NON_STARTCODE, parsedSn.getStartCode()); 106 } 107 108 @Test 109 public void testServerName() { 110 ServerName sn = ServerName.valueOf("www.example.org", 1234, 5678); 111 ServerName sn2 = ServerName.valueOf("www.example.org", 1234, 5678); 112 ServerName sn3 = ServerName.valueOf("www.example.org", 1234, 56789); 113 assertTrue(sn.equals(sn2)); 114 assertFalse(sn.equals(sn3)); 115 assertEquals(sn.hashCode(), sn2.hashCode()); 116 assertNotSame(sn.hashCode(), sn3.hashCode()); 117 assertEquals(sn.toString(), ServerName.valueOf("www.example.org", 1234, 5678).toString()); 118 assertEquals(sn.toString(), ServerName.valueOf("www.example.org:1234", 5678).toString()); 119 assertEquals("www.example.org" + ServerName.SERVERNAME_SEPARATOR + "1234" 120 + ServerName.SERVERNAME_SEPARATOR + "5678", sn.toString()); 121 } 122 123 @Test 124 public void testHostNameCaseSensitivity() { 125 ServerName lower = ServerName.valueOf("www.example.org", 1234, 5678); 126 ServerName upper = ServerName.valueOf("www.EXAMPLE.org", 1234, 5678); 127 assertEquals(0, lower.compareTo(upper)); 128 assertEquals(0, upper.compareTo(lower)); 129 assertEquals(lower.hashCode(), upper.hashCode()); 130 assertTrue(lower.equals(upper)); 131 assertTrue(upper.equals(lower)); 132 assertTrue(ServerName.isSameAddress(lower, upper)); 133 } 134 135 @Test 136 public void testInterning() { 137 ServerName sn1 = ServerName.valueOf("www.example.org", 1234, 5671); 138 assertSame(sn1, ServerName.valueOf("www.example.org", 1234, 5671)); 139 } 140 141 @Test 142 public void testInterningDoesWeakReferences() { 143 for (int i = 0; i < 5000; i++) { 144 final int startcode = i++; 145 final ServerName sn1 = ServerName.valueOf("www.example.org", 1234, startcode); 146 assertSame(sn1, ServerName.valueOf("www.example.org", 1234, startcode)); 147 } 148 } 149}