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.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
42
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 }