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 * <p>
010 * http://www.apache.org/licenses/LICENSE-2.0
011 * <p>
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 org.apache.hadoop.hbase.HBaseClassTestRule;
021import org.apache.hadoop.hbase.testclassification.SmallTests;
022import org.junit.Assert;
023import org.junit.ClassRule;
024import org.junit.Rule;
025import org.junit.Test;
026import org.junit.experimental.categories.Category;
027import org.junit.rules.ExpectedException;
028
029@Category({SmallTests.class})
030public class TestStrings {
031
032  @Rule
033  public final ExpectedException thrown = ExpectedException.none();
034
035  @ClassRule
036  public static final HBaseClassTestRule CLASS_RULE =
037          HBaseClassTestRule.forClass(TestStrings.class);
038
039  @Test
040  public void testAppendKeyValue() {
041    Assert.assertEquals("foo, bar=baz", Strings.appendKeyValue(
042            new StringBuilder("foo"), "bar", "baz").toString());
043    Assert.assertEquals("bar->baz", Strings.appendKeyValue(
044            new StringBuilder(), "bar", "baz", "->", "| ").toString());
045    Assert.assertEquals("foo, bar=baz", Strings.appendKeyValue(
046            new StringBuilder("foo"), "bar", "baz", "=", ", ").toString());
047    Assert.assertEquals("foo| bar->baz", Strings.appendKeyValue(
048            new StringBuilder("foo"), "bar", "baz", "->", "| ").toString());
049  }
050
051  @Test
052  public void testDomainNamePointerToHostName() {
053    Assert.assertNull(Strings.domainNamePointerToHostName(null));
054    Assert.assertEquals("foo",
055            Strings.domainNamePointerToHostName("foo"));
056    Assert.assertEquals("foo.com",
057            Strings.domainNamePointerToHostName("foo.com"));
058    Assert.assertEquals("foo.bar.com",
059            Strings.domainNamePointerToHostName("foo.bar.com"));
060    Assert.assertEquals("foo.bar.com",
061            Strings.domainNamePointerToHostName("foo.bar.com."));
062  }
063
064  @Test
065  public void testPadFront() {
066    Assert.assertEquals("ddfoo", Strings.padFront("foo", 'd', 5));
067
068    thrown.expect(IllegalArgumentException.class);
069    Strings.padFront("foo", 'd', 1);
070  }
071}