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.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertFalse; 022import static org.junit.jupiter.api.Assertions.assertNotSame; 023import static org.junit.jupiter.api.Assertions.assertSame; 024import static org.junit.jupiter.api.Assertions.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.jupiter.api.Tag; 034import org.junit.jupiter.api.Test; 035 036@Tag(MiscTests.TAG) 037@Tag(SmallTests.TAG) 038public class TestServerName { 039 040 @Test 041 public void testHash() { 042 ServerName sn1 = ServerName.parseServerName("asf903.gq1.ygridcore.net,52690,1517835491385"); 043 ServerName sn2 = ServerName.parseServerName("asf903.gq1.ygridcore.net,42231,1517835491329"); 044 Set<ServerName> sns = new HashSet<>(); 045 sns.add(sn2); 046 sns.add(sn1); 047 sns.add(sn1); 048 assertEquals(2, sns.size()); 049 } 050 051 @Test 052 public void testShortString() { 053 ServerName sn = ServerName.valueOf("asf000.sp2.ygridcore.net", 1, 1); 054 assertEquals("asf000:1", sn.toShortString()); 055 sn = ServerName.valueOf("2607:f0d0:1002:0051:0000:0000:0000:0004", 1, 1); 056 assertEquals("2607:f0d0:1002:0051:0000:0000:0000:0004:1", sn.toShortString()); 057 sn = ServerName.valueOf("1.1.1.1", 1, 1); 058 assertEquals("1.1.1.1:1", sn.toShortString()); 059 } 060 061 @Test 062 public void testRegexPatterns() { 063 assertTrue(Pattern.matches(Addressing.VALID_PORT_REGEX, "123")); 064 assertFalse(Pattern.matches(Addressing.VALID_PORT_REGEX, "")); 065 assertTrue(ServerName.SERVERNAME_PATTERN.matcher("www1.example.org,1234,567").matches()); 066 ServerName.parseServerName("a.b.c,58102,1319771740322"); 067 ServerName.parseServerName("192.168.1.199,58102,1319771740322"); 068 ServerName.parseServerName("a.b.c:58102"); 069 ServerName.parseServerName("192.168.1.199:58102"); 070 } 071 072 @Test 073 public void testParseOfBytes() { 074 final String snStr = "www.EXAMPLE.org,1234,5678"; 075 ServerName sn = ServerName.valueOf(snStr); 076 byte[] versionedBytes = sn.getVersionedBytes(); 077 ServerName parsedSn = ServerName.parseVersionedServerName(versionedBytes); 078 assertEquals(sn.toString(), parsedSn.toString()); 079 assertEquals(sn.getHostnameLowerCase(), parsedSn.getHostnameLowerCase()); 080 assertEquals(sn.getPort(), parsedSn.getPort()); 081 assertEquals(sn.getStartCode(), parsedSn.getStartCode()); 082 083 final String hostnamePortStr = sn.getAddress().toString(); 084 byte[] bytes = Bytes.toBytes(hostnamePortStr); 085 parsedSn = ServerName.parseVersionedServerName(bytes); 086 assertEquals(sn.getHostnameLowerCase(), parsedSn.getHostnameLowerCase()); 087 assertEquals(sn.getPort(), parsedSn.getPort()); 088 assertEquals(ServerName.NON_STARTCODE, parsedSn.getStartCode()); 089 } 090 091 @Test 092 public void testServerName() { 093 ServerName sn = ServerName.valueOf("www.example.org", 1234, 5678); 094 ServerName sn2 = ServerName.valueOf("www.example.org", 1234, 5678); 095 ServerName sn3 = ServerName.valueOf("www.example.org", 1234, 56789); 096 assertTrue(sn.equals(sn2)); 097 assertFalse(sn.equals(sn3)); 098 assertEquals(sn.hashCode(), sn2.hashCode()); 099 assertNotSame(sn.hashCode(), sn3.hashCode()); 100 assertEquals(sn.toString(), ServerName.valueOf("www.example.org", 1234, 5678).toString()); 101 assertEquals(sn.toString(), ServerName.valueOf("www.example.org:1234", 5678).toString()); 102 assertEquals("www.example.org" + ServerName.SERVERNAME_SEPARATOR + "1234" 103 + ServerName.SERVERNAME_SEPARATOR + "5678", sn.toString()); 104 } 105 106 @Test 107 public void testHostNameCaseSensitivity() { 108 ServerName lower = ServerName.valueOf("www.example.org", 1234, 5678); 109 ServerName upper = ServerName.valueOf("www.EXAMPLE.org", 1234, 5678); 110 assertEquals(0, lower.compareTo(upper)); 111 assertEquals(0, upper.compareTo(lower)); 112 assertEquals(lower.hashCode(), upper.hashCode()); 113 assertTrue(lower.equals(upper)); 114 assertTrue(upper.equals(lower)); 115 assertTrue(ServerName.isSameAddress(lower, upper)); 116 } 117 118 @Test 119 public void testInterning() { 120 ServerName sn1 = ServerName.valueOf("www.example.org", 1234, 5671); 121 assertSame(sn1, ServerName.valueOf("www.example.org", 1234, 5671)); 122 } 123 124 @Test 125 public void testInterningDoesWeakReferences() { 126 for (int i = 0; i < 5000; i++) { 127 final int startcode = i++; 128 final ServerName sn1 = ServerName.valueOf("www.example.org", 1234, startcode); 129 assertSame(sn1, ServerName.valueOf("www.example.org", 1234, startcode)); 130 } 131 } 132}