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