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.types;
019
020import static org.junit.Assert.assertArrayEquals;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertFalse;
023import static org.junit.Assert.assertTrue;
024
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.testclassification.MiscTests;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.apache.hadoop.hbase.util.Bytes;
029import org.apache.hadoop.hbase.util.Order;
030import org.apache.hadoop.hbase.util.PositionedByteRange;
031import org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange;
032import org.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035
036@Category({ MiscTests.class, SmallTests.class })
037public class TestRawString {
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040    HBaseClassTestRule.forClass(TestRawString.class);
041
042  private static final String[] VALUES = new String[] { "", "1", "22", "333", "4444", "55555",
043    "666666", "7777777", "88888888", "999999999", };
044
045  @Test
046  public void testIsOrderPreservingIsTrue() {
047    final DataType<String> type = new RawString(Order.ASCENDING);
048
049    assertTrue(type.isOrderPreserving());
050  }
051
052  @Test
053  public void testGetOrderIsCorrectOrder() {
054    final DataType<String> type = new RawString(Order.ASCENDING);
055
056    assertEquals(Order.ASCENDING, type.getOrder());
057  }
058
059  @Test
060  public void testIsNullableIsFalse() {
061    final DataType<String> type = new RawString(Order.ASCENDING);
062
063    assertFalse(type.isNullable());
064  }
065
066  @Test
067  public void testIsSkippableIsFalse() {
068    final DataType<String> type = new RawString(Order.ASCENDING);
069
070    assertFalse(type.isSkippable());
071  }
072
073  @Test
074  public void testEncodedClassIsString() {
075    final DataType<String> type = new RawString(Order.ASCENDING);
076
077    assertEquals(String.class, type.encodedClass());
078  }
079
080  @Test
081  public void testReadWrite() {
082    for (final Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
083      final RawString type =
084        Order.ASCENDING == ord ? new RawString(Order.ASCENDING) : new RawString(Order.DESCENDING);
085      for (final String val : VALUES) {
086        final PositionedByteRange buff =
087          new SimplePositionedMutableByteRange(Bytes.toBytes(val).length);
088        assertEquals(buff.getLength(), type.encode(buff, val));
089        final byte[] expected = Bytes.toBytes(val);
090        ord.apply(expected);
091        assertArrayEquals(expected, buff.getBytes());
092        buff.setPosition(0);
093        assertEquals(val, type.decode(buff));
094        buff.setPosition(0);
095        assertEquals(buff.getLength(), type.skip(buff));
096        assertEquals(buff.getLength(), buff.getPosition());
097      }
098    }
099  }
100}