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.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023
024import java.util.Arrays;
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.Rule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036import org.junit.rules.ExpectedException;
037
038@Category({ MiscTests.class, SmallTests.class })
039public class TestRawBytes {
040  private static final byte[][] VALUES =
041    new byte[][] { Bytes.toBytes(""), Bytes.toBytes("1"), Bytes.toBytes("22"), Bytes.toBytes("333"),
042      Bytes.toBytes("4444"), Bytes.toBytes("55555"), Bytes.toBytes("666666"),
043      Bytes.toBytes("7777777"), Bytes.toBytes("88888888"), Bytes.toBytes("999999999"), };
044
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047    HBaseClassTestRule.forClass(TestRawBytes.class);
048
049  @Rule
050  public ExpectedException exception = ExpectedException.none();
051
052  @Test
053  public void testIsOrderPreservingIsTrue() {
054    final DataType<byte[]> type = new RawBytes(Order.ASCENDING);
055
056    assertTrue(type.isOrderPreserving());
057  }
058
059  @Test
060  public void testGetOrderCorrectOrder() {
061    final DataType<byte[]> type = new RawBytes(Order.ASCENDING);
062
063    assertEquals(Order.ASCENDING, type.getOrder());
064  }
065
066  @Test
067  public void testIsNullableIsFalse() {
068    final DataType<byte[]> type = new RawBytes(Order.ASCENDING);
069
070    assertFalse(type.isNullable());
071  }
072
073  @Test
074  public void testIsSkippableIsFalse() {
075    final DataType<byte[]> type = new RawBytes(Order.ASCENDING);
076
077    assertFalse(type.isSkippable());
078  }
079
080  @Test
081  public void testEncodedClassIsByteArray() {
082    final DataType<byte[]> type = new RawBytes(Order.ASCENDING);
083
084    assertEquals(byte[].class, type.encodedClass());
085  }
086
087  @Test
088  public void testEncodedLength() {
089    final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20);
090    for (final DataType<byte[]> type : new RawBytes[] { new RawBytes(Order.ASCENDING),
091      new RawBytes(Order.DESCENDING) }) {
092      for (final byte[] val : VALUES) {
093        buffer.setPosition(0);
094        type.encode(buffer, val);
095        assertEquals("encodedLength does not match actual, " + Arrays.toString(val),
096          buffer.getPosition(), type.encodedLength(val));
097      }
098    }
099  }
100}