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.assertTrue;
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.Bytes;
027import org.apache.hadoop.hbase.util.Order;
028import org.apache.hadoop.hbase.util.PositionedByteRange;
029import org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033
034@Category({MiscTests.class, SmallTests.class})
035public class TestFixedLengthWrapper {
036
037  @ClassRule
038  public static final HBaseClassTestRule CLASS_RULE =
039      HBaseClassTestRule.forClass(TestFixedLengthWrapper.class);
040
041  static final byte[][] VALUES = new byte[][] {
042    Bytes.toBytes(""), Bytes.toBytes("1"), Bytes.toBytes("22"), Bytes.toBytes("333"),
043    Bytes.toBytes("4444"), Bytes.toBytes("55555"), Bytes.toBytes("666666"),
044    Bytes.toBytes("7777777"), Bytes.toBytes("88888888"), Bytes.toBytes("999999999"),
045  };
046
047  /**
048   * all values of {@code limit} are >= max length of a member of
049   * {@code VALUES}.
050   */
051  static final int[] limits = { 9, 12, 15 };
052
053  @Test
054  public void testReadWrite() {
055    for (int limit : limits) {
056      PositionedByteRange buff = new SimplePositionedMutableByteRange(limit);
057      for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
058        for (byte[] val : VALUES) {
059          buff.setPosition(0);
060          DataType<byte[]> type = new FixedLengthWrapper<>(new RawBytes(ord), limit);
061          assertEquals(limit, type.encode(buff, val));
062          buff.setPosition(0);
063          byte[] actual = type.decode(buff);
064          assertTrue("Decoding output differs from expected",
065            Bytes.equals(val, 0, val.length, actual, 0, val.length));
066          buff.setPosition(0);
067          assertEquals(limit, type.skip(buff));
068        }
069      }
070    }
071  }
072
073  @Test(expected = IllegalArgumentException.class)
074  public void testInsufficientRemainingRead() {
075    PositionedByteRange buff = new SimplePositionedMutableByteRange(0);
076    DataType<byte[]> type = new FixedLengthWrapper<>(new RawBytes(), 3);
077    type.decode(buff);
078  }
079
080  @Test(expected = IllegalArgumentException.class)
081  public void testInsufficientRemainingWrite() {
082    PositionedByteRange buff = new SimplePositionedMutableByteRange(0);
083    DataType<byte[]> type = new FixedLengthWrapper<>(new RawBytes(), 3);
084    type.encode(buff, Bytes.toBytes(""));
085  }
086
087  @Test(expected = IllegalArgumentException.class)
088  public void testOverflowPassthrough() {
089    PositionedByteRange buff = new SimplePositionedMutableByteRange(3);
090    DataType<byte[]> type = new FixedLengthWrapper<>(new RawBytes(), 0);
091    type.encode(buff, Bytes.toBytes("foo"));
092  }
093}