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.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertThrows; 022import static org.junit.jupiter.api.Assertions.assertTrue; 023 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.jupiter.api.Tag; 031import org.junit.jupiter.api.Test; 032 033@Tag(MiscTests.TAG) 034@Tag(SmallTests.TAG) 035public class TestFixedLengthWrapper { 036 037 static final byte[][] VALUES = 038 new byte[][] { Bytes.toBytes(""), Bytes.toBytes("1"), Bytes.toBytes("22"), Bytes.toBytes("333"), 039 Bytes.toBytes("4444"), Bytes.toBytes("55555"), Bytes.toBytes("666666"), 040 Bytes.toBytes("7777777"), Bytes.toBytes("88888888"), Bytes.toBytes("999999999"), }; 041 042 /** 043 * all values of {@code limit} are >= max length of a member of {@code VALUES}. 044 */ 045 static final int[] limits = { 9, 12, 15 }; 046 047 @Test 048 public void testReadWrite() { 049 for (int limit : limits) { 050 PositionedByteRange buff = new SimplePositionedMutableByteRange(limit); 051 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { 052 for (byte[] val : VALUES) { 053 buff.setPosition(0); 054 DataType<byte[]> type = new FixedLengthWrapper<>(new RawBytes(ord), limit); 055 assertEquals(limit, type.encode(buff, val)); 056 buff.setPosition(0); 057 byte[] actual = type.decode(buff); 058 assertTrue(Bytes.equals(val, 0, val.length, actual, 0, val.length), 059 "Decoding output differs from expected"); 060 buff.setPosition(0); 061 assertEquals(limit, type.skip(buff)); 062 } 063 } 064 } 065 } 066 067 @Test 068 public void testInsufficientRemainingRead() { 069 final PositionedByteRange buff = new SimplePositionedMutableByteRange(0); 070 final DataType<byte[]> type = new FixedLengthWrapper<>(new RawBytes(Order.ASCENDING), 3); 071 assertThrows(IllegalArgumentException.class, () -> type.decode(buff)); 072 } 073 074 @Test 075 public void testInsufficientRemainingWrite() { 076 final PositionedByteRange buff = new SimplePositionedMutableByteRange(0); 077 final DataType<byte[]> type = new FixedLengthWrapper<>(new RawBytes(Order.ASCENDING), 3); 078 assertThrows(IllegalArgumentException.class, () -> type.encode(buff, Bytes.toBytes(""))); 079 } 080 081 @Test 082 public void testOverflowPassthrough() { 083 final PositionedByteRange buff = new SimplePositionedMutableByteRange(3); 084 final DataType<byte[]> type = new FixedLengthWrapper<>(new RawBytes(Order.ASCENDING), 0); 085 assertThrows(IllegalArgumentException.class, () -> type.encode(buff, Bytes.toBytes("foo"))); 086 } 087}