1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
44
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 }