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 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 TestTerminatedWrapper {
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    static final byte[][] TERMINATORS = new byte[][] { new byte[] { -1 }, Bytes.toBytes("foo") };
41  
42    @Test(expected = IllegalArgumentException.class)
43    public void testEmptyDelimiter() {
44      new TerminatedWrapper<byte[]>(new RawBytes(), "");
45    }
46  
47    @Test(expected = IllegalArgumentException.class)
48    public void testNullDelimiter() {
49      new RawBytesTerminated((byte[]) null);
50      // new TerminatedWrapper<byte[]>(new RawBytes(), (byte[]) null);
51    }
52  
53    @Test(expected = IllegalArgumentException.class)
54    public void testEncodedValueContainsTerm() {
55      DataType<byte[]> type = new TerminatedWrapper<byte[]>(new RawBytes(), "foo");
56      PositionedByteRange buff = new SimplePositionedByteRange(16);
57      type.encode(buff, Bytes.toBytes("hello foobar!"));
58    }
59  
60    @Test
61    public void testReadWrite() {
62      PositionedByteRange buff = new SimplePositionedByteRange(12);
63      for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
64        for (byte[] term : TERMINATORS) {
65          for (byte[] val : VALUES) {
66            buff.setPosition(0);
67            DataType<byte[]> type = new TerminatedWrapper<byte[]>(new RawBytes(ord), term);
68            assertEquals(val.length + term.length, type.encode(buff, val));
69            buff.setPosition(0);
70            assertArrayEquals(val, type.decode(buff));
71            assertEquals(val.length + term.length, buff.getPosition());
72          }
73        }
74      }
75    }
76  
77    @Test
78    public void testSkip() {
79      PositionedByteRange buff = new SimplePositionedByteRange(12);
80      for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
81        for (byte[] term : TERMINATORS) {
82          for (byte[] val : VALUES) {
83            buff.setPosition(0);
84            DataType<byte[]> type = new TerminatedWrapper<byte[]>(new RawBytes(ord), term);
85            int expected = type.encode(buff, val);
86            buff.setPosition(0);
87            assertEquals(expected, type.skip(buff));
88            assertEquals(expected, buff.getPosition());
89          }
90        }
91      }
92    }
93  
94    @Test(expected = IllegalArgumentException.class)
95    public void testInvalidSkip() {
96      PositionedByteRange buff = new SimplePositionedByteRange(Bytes.toBytes("foo"));
97      DataType<byte[]> type = new TerminatedWrapper<byte[]>(new RawBytes(), new byte[] { 0x00 });
98      type.skip(buff);
99    }
100 }