001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.types; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertTrue; 022 023import org.apache.hadoop.hbase.HBaseClassTestRule; 024import org.apache.hadoop.hbase.testclassification.MiscTests; 025import org.apache.hadoop.hbase.testclassification.SmallTests; 026import org.apache.hadoop.hbase.util.Bytes; 027import org.apache.hadoop.hbase.util.Order; 028import org.apache.hadoop.hbase.util.PositionedByteRange; 029import org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange; 030import org.junit.ClassRule; 031import org.junit.Test; 032import org.junit.experimental.categories.Category; 033 034@Category({ MiscTests.class, SmallTests.class }) 035public class TestFixedLengthWrapper { 036 037 @ClassRule 038 public static final HBaseClassTestRule CLASS_RULE = 039 HBaseClassTestRule.forClass(TestFixedLengthWrapper.class); 040 041 static final byte[][] VALUES = 042 new byte[][] { Bytes.toBytes(""), Bytes.toBytes("1"), Bytes.toBytes("22"), Bytes.toBytes("333"), 043 Bytes.toBytes("4444"), Bytes.toBytes("55555"), Bytes.toBytes("666666"), 044 Bytes.toBytes("7777777"), Bytes.toBytes("88888888"), Bytes.toBytes("999999999"), }; 045 046 /** 047 * all values of {@code limit} are >= max length of a member of {@code VALUES}. 048 */ 049 static final int[] limits = { 9, 12, 15 }; 050 051 @Test 052 public void testReadWrite() { 053 for (int limit : limits) { 054 PositionedByteRange buff = new SimplePositionedMutableByteRange(limit); 055 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { 056 for (byte[] val : VALUES) { 057 buff.setPosition(0); 058 DataType<byte[]> type = new FixedLengthWrapper<>(new RawBytes(ord), limit); 059 assertEquals(limit, type.encode(buff, val)); 060 buff.setPosition(0); 061 byte[] actual = type.decode(buff); 062 assertTrue("Decoding output differs from expected", 063 Bytes.equals(val, 0, val.length, actual, 0, val.length)); 064 buff.setPosition(0); 065 assertEquals(limit, type.skip(buff)); 066 } 067 } 068 } 069 } 070 071 @Test(expected = IllegalArgumentException.class) 072 public void testInsufficientRemainingRead() { 073 PositionedByteRange buff = new SimplePositionedMutableByteRange(0); 074 DataType<byte[]> type = new FixedLengthWrapper<>(new RawBytes(), 3); 075 type.decode(buff); 076 } 077 078 @Test(expected = IllegalArgumentException.class) 079 public void testInsufficientRemainingWrite() { 080 PositionedByteRange buff = new SimplePositionedMutableByteRange(0); 081 DataType<byte[]> type = new FixedLengthWrapper<>(new RawBytes(), 3); 082 type.encode(buff, Bytes.toBytes("")); 083 } 084 085 @Test(expected = IllegalArgumentException.class) 086 public void testOverflowPassthrough() { 087 PositionedByteRange buff = new SimplePositionedMutableByteRange(3); 088 DataType<byte[]> type = new FixedLengthWrapper<>(new RawBytes(), 0); 089 type.encode(buff, Bytes.toBytes("foo")); 090 } 091}