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.util; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertFalse; 022import static org.junit.jupiter.api.Assertions.assertNull; 023import static org.junit.jupiter.api.Assertions.assertThrows; 024import static org.junit.jupiter.api.Assertions.assertTrue; 025 026import java.net.URI; 027import java.net.URLEncoder; 028import java.nio.charset.StandardCharsets; 029import java.util.Map; 030import org.apache.hadoop.conf.Configuration; 031import org.apache.hadoop.hbase.testclassification.MiscTests; 032import org.apache.hadoop.hbase.testclassification.SmallTests; 033import org.junit.jupiter.api.Tag; 034import org.junit.jupiter.api.Test; 035 036@Tag(MiscTests.TAG) 037@Tag(SmallTests.TAG) 038public class TestStrings { 039 040 @Test 041 public void testAppendKeyValue() { 042 assertEquals("foo, bar=baz", 043 Strings.appendKeyValue(new StringBuilder("foo"), "bar", "baz").toString()); 044 assertEquals("bar->baz", 045 Strings.appendKeyValue(new StringBuilder(), "bar", "baz", "->", "| ").toString()); 046 assertEquals("foo, bar=baz", 047 Strings.appendKeyValue(new StringBuilder("foo"), "bar", "baz", "=", ", ").toString()); 048 assertEquals("foo| bar->baz", 049 Strings.appendKeyValue(new StringBuilder("foo"), "bar", "baz", "->", "| ").toString()); 050 } 051 052 @Test 053 public void testDomainNamePointerToHostName() { 054 assertNull(Strings.domainNamePointerToHostName(null)); 055 assertEquals("foo", Strings.domainNamePointerToHostName("foo")); 056 assertEquals("foo.com", Strings.domainNamePointerToHostName("foo.com")); 057 assertEquals("foo.bar.com", Strings.domainNamePointerToHostName("foo.bar.com")); 058 assertEquals("foo.bar.com", Strings.domainNamePointerToHostName("foo.bar.com.")); 059 } 060 061 @Test 062 public void testHostnamesEqual() { 063 assertTrue(Strings.hostnamesEqual("HOST.example.com", "host.example.com")); 064 assertTrue(Strings.hostnamesEqual("rs1", "RS1")); 065 assertFalse(Strings.hostnamesEqual("host-a.example.com", "host-b.example.com")); 066 assertTrue(Strings.hostnamesEqual("10.0.0.1", "10.0.0.1")); 067 assertFalse(Strings.hostnamesEqual("10.0.0.1", "10.0.0.2")); 068 assertFalse(Strings.hostnamesEqual("HOST.example.com", "10.0.0.1")); 069 assertTrue(Strings.hostnamesEqual("::1", "0:0:0:0:0:0:0:1")); 070 assertTrue(Strings.hostnamesEqual("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]", 071 "2001:0db8:85a3:0000:0000:8a2e:0370:7334")); 072 assertThrows(NullPointerException.class, () -> Strings.hostnamesEqual(null, "host")); 073 assertThrows(NullPointerException.class, () -> Strings.hostnamesEqual("host", null)); 074 } 075 076 @Test 077 public void testPadFront() { 078 assertEquals("ddfoo", Strings.padFront("foo", 'd', 5)); 079 assertThrows(IllegalArgumentException.class, () -> Strings.padFront("foo", 'd', 1)); 080 } 081 082 @Test 083 public void testParseURIQueries() throws Exception { 084 Map<String, 085 String> queries = Strings.parseURIQueries(new URI("hbase+rpc://server01:123?a=1&b=2&a=3&" 086 + URLEncoder.encode("& ?", StandardCharsets.UTF_8.name()) + "=&" 087 + URLEncoder.encode("===", StandardCharsets.UTF_8.name()))); 088 assertEquals("1", queries.get("a")); 089 assertEquals("2", queries.get("b")); 090 assertEquals("", queries.get("& ?")); 091 assertEquals("", queries.get("===")); 092 assertEquals(4, queries.size()); 093 094 assertTrue(Strings.parseURIQueries(new URI("hbase+zk://zk1:2181/")).isEmpty()); 095 assertTrue(Strings.parseURIQueries(new URI("hbase+zk://zk1:2181/?")).isEmpty()); 096 assertTrue(Strings.parseURIQueries(new URI("hbase+zk://zk1:2181/?#anchor")).isEmpty()); 097 } 098 099 @Test 100 public void testApplyURIQueriesToConf() throws Exception { 101 Configuration conf = new Configuration(); 102 Strings.applyURIQueriesToConf(new URI("hbase+zk://aaa:2181/root?a=1&b=2&c"), conf); 103 assertEquals("1", conf.get("a")); 104 assertEquals("2", conf.get("b")); 105 assertEquals("", conf.get("c")); 106 } 107}