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