View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.types;
19  
20  import static org.junit.Assert.assertArrayEquals;
21  import static org.junit.Assert.assertEquals;
22  
23  import java.util.Arrays;
24  
25  import org.apache.hadoop.hbase.SmallTests;
26  import org.apache.hadoop.hbase.util.Bytes;
27  import org.apache.hadoop.hbase.util.Order;
28  import org.apache.hadoop.hbase.util.PositionedByteRange;
29  import org.apache.hadoop.hbase.util.SimplePositionedByteRange;
30  import org.junit.Test;
31  import org.junit.experimental.categories.Category;
32  
33  @Category(SmallTests.class)
34  public class TestFixedLengthWrapper {
35  
36    static final byte[][] VALUES = new byte[][] {
37      Bytes.toBytes(""), Bytes.toBytes("1"), Bytes.toBytes("22"), Bytes.toBytes("333"),
38      Bytes.toBytes("4444"), Bytes.toBytes("55555"), Bytes.toBytes("666666"),
39      Bytes.toBytes("7777777"), Bytes.toBytes("88888888"), Bytes.toBytes("999999999"),
40    };
41  
42    /**
43     * all values of {@code limit} are >= max length of a member of
44     * {@code VALUES}.
45     */
46    static final int[] limits = { 9, 12, 15 };
47  
48    @Test
49    public void testReadWrite() {
50      for (int limit : limits) {
51        PositionedByteRange buff = new SimplePositionedByteRange(limit);
52        for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
53          for (byte[] val : VALUES) {
54            buff.setPosition(0);
55            DataType<byte[]> type = new FixedLengthWrapper<byte[]>(new RawBytes(ord), limit);
56            assertEquals(limit, type.encode(buff, val));
57            byte[] expected = Arrays.copyOf(val, limit);
58            buff.setPosition(0);
59            byte[] actual = type.decode(buff);
60            assertArrayEquals(expected, actual);
61            buff.setPosition(0);
62            assertEquals(limit, type.skip(buff));
63          }
64        }
65      }
66    }
67  
68    @Test(expected = IllegalArgumentException.class)
69    public void testInsufficientRemainingRead() {
70      PositionedByteRange buff = new SimplePositionedByteRange(0);
71      DataType<byte[]> type = new FixedLengthWrapper<byte[]>(new RawBytes(), 3);
72      type.decode(buff);
73    }
74  
75    @Test(expected = IllegalArgumentException.class)
76    public void testInsufficientRemainingWrite() {
77      PositionedByteRange buff = new SimplePositionedByteRange(0);
78      DataType<byte[]> type = new FixedLengthWrapper<byte[]>(new RawBytes(), 3);
79      type.encode(buff, Bytes.toBytes(""));
80    }
81  
82    @Test(expected = IllegalArgumentException.class)
83    public void testOverflowPassthrough() {
84      PositionedByteRange buff = new SimplePositionedByteRange(3);
85      DataType<byte[]> type = new FixedLengthWrapper<byte[]>(new RawBytes(), 0);
86      type.encode(buff, Bytes.toBytes("foo"));
87    }
88  }