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