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.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  
23  import org.apache.hadoop.hbase.SmallTests;
24  import org.apache.hadoop.hbase.util.Bytes;
25  import org.apache.hadoop.hbase.util.Order;
26  import org.apache.hadoop.hbase.util.PositionedByteRange;
27  import org.apache.hadoop.hbase.util.SimplePositionedByteRange;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  @Category(SmallTests.class)
32  public class TestFixedLengthWrapper {
33  
34    static final byte[][] VALUES = new byte[][] {
35      Bytes.toBytes(""), Bytes.toBytes("1"), Bytes.toBytes("22"), Bytes.toBytes("333"),
36      Bytes.toBytes("4444"), Bytes.toBytes("55555"), Bytes.toBytes("666666"),
37      Bytes.toBytes("7777777"), Bytes.toBytes("88888888"), Bytes.toBytes("999999999"),
38    };
39  
40    /**
41     * all values of {@code limit} are >= max length of a member of
42     * {@code VALUES}.
43     */
44    static final int[] limits = { 9, 12, 15 };
45  
46    @Test
47    public void testReadWrite() {
48      for (int limit : limits) {
49        PositionedByteRange buff = new SimplePositionedByteRange(limit);
50        for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
51          for (byte[] val : VALUES) {
52            buff.setPosition(0);
53            DataType<byte[]> type = new FixedLengthWrapper<byte[]>(new RawBytes(ord), limit);
54            assertEquals(limit, type.encode(buff, val));
55            buff.setPosition(0);
56            byte[] actual = type.decode(buff);
57            assertTrue("Decoding output differs from expected", 
58              Bytes.equals(val, 0, val.length, actual, 0, val.length));
59            buff.setPosition(0);
60            assertEquals(limit, type.skip(buff));
61          }
62        }
63      }
64    }
65  
66    @Test(expected = IllegalArgumentException.class)
67    public void testInsufficientRemainingRead() {
68      PositionedByteRange buff = new SimplePositionedByteRange(0);
69      DataType<byte[]> type = new FixedLengthWrapper<byte[]>(new RawBytes(), 3);
70      type.decode(buff);
71    }
72  
73    @Test(expected = IllegalArgumentException.class)
74    public void testInsufficientRemainingWrite() {
75      PositionedByteRange buff = new SimplePositionedByteRange(0);
76      DataType<byte[]> type = new FixedLengthWrapper<byte[]>(new RawBytes(), 3);
77      type.encode(buff, Bytes.toBytes(""));
78    }
79  
80    @Test(expected = IllegalArgumentException.class)
81    public void testOverflowPassthrough() {
82      PositionedByteRange buff = new SimplePositionedByteRange(3);
83      DataType<byte[]> type = new FixedLengthWrapper<byte[]>(new RawBytes(), 0);
84      type.encode(buff, Bytes.toBytes("foo"));
85    }
86  }