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.assertFalse; 022import static org.junit.jupiter.api.Assertions.assertTrue; 023 024import java.util.Arrays; 025import org.apache.hadoop.hbase.testclassification.MiscTests; 026import org.apache.hadoop.hbase.testclassification.SmallTests; 027import org.apache.hadoop.hbase.util.Bytes; 028import org.apache.hadoop.hbase.util.Order; 029import org.apache.hadoop.hbase.util.PositionedByteRange; 030import org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange; 031import org.junit.jupiter.api.Tag; 032import org.junit.jupiter.api.Test; 033 034@Tag(MiscTests.TAG) 035@Tag(SmallTests.TAG) 036public class TestRawBytes { 037 private 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 @Test 043 public void testIsOrderPreservingIsTrue() { 044 final DataType<byte[]> type = new RawBytes(Order.ASCENDING); 045 046 assertTrue(type.isOrderPreserving()); 047 } 048 049 @Test 050 public void testGetOrderCorrectOrder() { 051 final DataType<byte[]> type = new RawBytes(Order.ASCENDING); 052 053 assertEquals(Order.ASCENDING, type.getOrder()); 054 } 055 056 @Test 057 public void testIsNullableIsFalse() { 058 final DataType<byte[]> type = new RawBytes(Order.ASCENDING); 059 060 assertFalse(type.isNullable()); 061 } 062 063 @Test 064 public void testIsSkippableIsFalse() { 065 final DataType<byte[]> type = new RawBytes(Order.ASCENDING); 066 067 assertFalse(type.isSkippable()); 068 } 069 070 @Test 071 public void testEncodedClassIsByteArray() { 072 final DataType<byte[]> type = new RawBytes(Order.ASCENDING); 073 074 assertEquals(byte[].class, type.encodedClass()); 075 } 076 077 @Test 078 public void testEncodedLength() { 079 final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20); 080 for (final DataType<byte[]> type : new RawBytes[] { new RawBytes(Order.ASCENDING), 081 new RawBytes(Order.DESCENDING) }) { 082 for (final byte[] val : VALUES) { 083 buffer.setPosition(0); 084 type.encode(buffer, val); 085 assertEquals(buffer.getPosition(), type.encodedLength(val), 086 "encodedLength does not match actual, " + Arrays.toString(val)); 087 } 088 } 089 } 090}