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