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.testclassification.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 String[] VALUES_STRINGS = new String[] {
35      "", "1", "22", "333", "4444", "55555", "666666", "7777777", "88888888", "999999999",
36    };
37  
38    static final byte[][] VALUES_BYTES = new byte[VALUES_STRINGS.length][];
39    static {
40      for (int i = 0; i < VALUES_STRINGS.length; i++) {
41        VALUES_BYTES[i] = Bytes.toBytes(VALUES_STRINGS[i]);
42      }
43    }
44  
45    static final byte[][] TERMINATORS = new byte[][] { new byte[] { -2 }, Bytes.toBytes("foo") };
46  
47    @Test(expected = IllegalArgumentException.class)
48    public void testEmptyDelimiter() {
49      new TerminatedWrapper<byte[]>(new RawBytes(), "");
50    }
51  
52    @Test(expected = IllegalArgumentException.class)
53    public void testNullDelimiter() {
54      new RawBytesTerminated((byte[]) null);
55      // new TerminatedWrapper<byte[]>(new RawBytes(), (byte[]) null);
56    }
57  
58    @Test(expected = IllegalArgumentException.class)
59    public void testEncodedValueContainsTerm() {
60      DataType<byte[]> type = new TerminatedWrapper<byte[]>(new RawBytes(), "foo");
61      PositionedByteRange buff = new SimplePositionedByteRange(16);
62      type.encode(buff, Bytes.toBytes("hello foobar!"));
63    }
64  
65    @Test
66    public void testReadWriteSkippable() {
67      PositionedByteRange buff = new SimplePositionedByteRange(14);
68      for (OrderedString t : new OrderedString[] {
69          OrderedString.ASCENDING, OrderedString.DESCENDING
70      }) {
71        for (byte[] term : TERMINATORS) {
72          for (String val : VALUES_STRINGS) {
73            buff.setPosition(0);
74            DataType<String> type = new TerminatedWrapper<String>(t, term);
75            assertEquals(val.length() + 2 + term.length, type.encode(buff, val));
76            buff.setPosition(0);
77            assertEquals(val, type.decode(buff));
78            assertEquals(val.length() + 2 + term.length, buff.getPosition());
79          }
80        }
81      }
82    }
83  
84    @Test
85    public void testReadWriteNonSkippable() {
86      PositionedByteRange buff = new SimplePositionedByteRange(12);
87      for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
88        for (byte[] term : TERMINATORS) {
89          for (byte[] val : VALUES_BYTES) {
90            buff.setPosition(0);
91            DataType<byte[]> type = new TerminatedWrapper<byte[]>(new RawBytes(ord), term);
92            assertEquals(val.length + term.length, type.encode(buff, val));
93            buff.setPosition(0);
94            assertArrayEquals(val, type.decode(buff));
95            assertEquals(val.length + term.length, buff.getPosition());
96          }
97        }
98      }
99    }
100 
101   @Test
102   public void testSkipSkippable() {
103     PositionedByteRange buff = new SimplePositionedByteRange(14);
104     for (OrderedString t : new OrderedString[] {
105         OrderedString.ASCENDING, OrderedString.DESCENDING
106     }) {
107       for (byte[] term : TERMINATORS) {
108         for (String val : VALUES_STRINGS) {
109           buff.setPosition(0);
110           DataType<String> type = new TerminatedWrapper<String>(t, term);
111           int expected = val.length() + 2 + term.length;
112           assertEquals(expected, type.encode(buff, val));
113           buff.setPosition(0);
114           assertEquals(expected, type.skip(buff));
115           assertEquals(expected, buff.getPosition());
116         }
117       }
118     }
119   }
120 
121   @Test
122   public void testSkipNonSkippable() {
123     PositionedByteRange buff = new SimplePositionedByteRange(12);
124     for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
125       for (byte[] term : TERMINATORS) {
126         for (byte[] val : VALUES_BYTES) {
127           buff.setPosition(0);
128           DataType<byte[]> type = new TerminatedWrapper<byte[]>(new RawBytes(ord), term);
129           int expected = type.encode(buff, val);
130           buff.setPosition(0);
131           assertEquals(expected, type.skip(buff));
132           assertEquals(expected, buff.getPosition());
133         }
134       }
135     }
136   }
137 
138   @Test(expected = IllegalArgumentException.class)
139   public void testInvalidSkip() {
140     PositionedByteRange buff = new SimplePositionedByteRange(Bytes.toBytes("foo"));
141     DataType<byte[]> type = new TerminatedWrapper<byte[]>(new RawBytes(), new byte[] { 0x00 });
142     type.skip(buff);
143   }
144 }