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;
022
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.testclassification.MiscTests;
025import org.apache.hadoop.hbase.testclassification.SmallTests;
026import org.apache.hadoop.hbase.util.Order;
027import org.apache.hadoop.hbase.util.PositionedByteRange;
028import org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange;
029import org.junit.ClassRule;
030import org.junit.Rule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033import org.junit.rules.ExpectedException;
034
035@Category({ MiscTests.class, SmallTests.class })
036public class TestOrderedInt8 {
037  private static final Byte[] VALUES = new Byte[] { 1, 22 };
038
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041    HBaseClassTestRule.forClass(TestOrderedInt8.class);
042
043  @Rule
044  public ExpectedException exception = ExpectedException.none();
045
046  @Test
047  public void testIsNullableIsFalse() {
048    final DataType<Byte> type = new OrderedInt8(Order.ASCENDING);
049
050    assertFalse(type.isNullable());
051  }
052
053  @Test
054  public void testEncodedClassIsByte() {
055    final DataType<Byte> type = new OrderedInt8(Order.ASCENDING);
056
057    assertEquals(Byte.class, type.encodedClass());
058  }
059
060  @Test
061  public void testEncodedLength() {
062    final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20);
063    for (final DataType<Byte> type : new OrderedInt8[] { new OrderedInt8(Order.ASCENDING),
064      new OrderedInt8(Order.DESCENDING) }) {
065      for (final Byte val : VALUES) {
066        buffer.setPosition(0);
067        type.encode(buffer, val);
068        assertEquals("encodedLength does not match actual, " + val, buffer.getPosition(),
069          type.encodedLength(val));
070      }
071    }
072  }
073
074  @Test
075  public void testEncodeNoSupportForNull() {
076    exception.expect(IllegalArgumentException.class);
077
078    final DataType<Byte> type = new OrderedInt8(Order.ASCENDING);
079
080    type.encode(new SimplePositionedMutableByteRange(20), null);
081  }
082
083  @Test
084  public void testEncodedFloatLength() {
085    final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20);
086    for (final OrderedInt8 type : new OrderedInt8[] { new OrderedInt8(Order.ASCENDING),
087      new OrderedInt8(Order.DESCENDING) }) {
088      for (final Byte val : VALUES) {
089        buffer.setPosition(0);
090        type.encodeByte(buffer, val);
091        assertEquals("encodedLength does not match actual, " + val, buffer.getPosition(),
092          type.encodedLength(val));
093      }
094    }
095  }
096}