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.codec; 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.io.ByteArrayInputStream; 025import java.io.ByteArrayOutputStream; 026import java.io.DataInputStream; 027import java.io.DataOutputStream; 028import java.io.IOException; 029import org.apache.hadoop.hbase.ExtendedCell; 030import org.apache.hadoop.hbase.KeyValue; 031import org.apache.hadoop.hbase.PrivateCellUtil; 032import org.apache.hadoop.hbase.testclassification.MiscTests; 033import org.apache.hadoop.hbase.testclassification.SmallTests; 034import org.apache.hadoop.hbase.util.Bytes; 035import org.junit.jupiter.api.Tag; 036import org.junit.jupiter.api.Test; 037 038import org.apache.hbase.thirdparty.com.google.common.io.CountingInputStream; 039import org.apache.hbase.thirdparty.com.google.common.io.CountingOutputStream; 040 041@Tag(MiscTests.TAG) 042@Tag(SmallTests.TAG) 043public class TestCellCodec { 044 045 @Test 046 public void testEmptyWorks() throws IOException { 047 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 048 CountingOutputStream cos = new CountingOutputStream(baos); 049 DataOutputStream dos = new DataOutputStream(cos); 050 Codec codec = new CellCodec(); 051 Codec.Encoder encoder = codec.getEncoder(dos); 052 encoder.flush(); 053 dos.close(); 054 long offset = cos.getCount(); 055 assertEquals(0, offset); 056 CountingInputStream cis = new CountingInputStream(new ByteArrayInputStream(baos.toByteArray())); 057 DataInputStream dis = new DataInputStream(cis); 058 Codec.Decoder decoder = codec.getDecoder(dis); 059 assertFalse(decoder.advance()); 060 dis.close(); 061 assertEquals(0, cis.getCount()); 062 } 063 064 @Test 065 public void testOne() throws IOException { 066 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 067 CountingOutputStream cos = new CountingOutputStream(baos); 068 DataOutputStream dos = new DataOutputStream(cos); 069 Codec codec = new CellCodec(); 070 Codec.Encoder encoder = codec.getEncoder(dos); 071 final KeyValue kv = 072 new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("q"), Bytes.toBytes("v")); 073 kv.setSequenceId(Long.MAX_VALUE); 074 encoder.write(kv); 075 encoder.flush(); 076 dos.close(); 077 long offset = cos.getCount(); 078 CountingInputStream cis = new CountingInputStream(new ByteArrayInputStream(baos.toByteArray())); 079 DataInputStream dis = new DataInputStream(cis); 080 Codec.Decoder decoder = codec.getDecoder(dis); 081 assertTrue(decoder.advance()); // First read should pull in the KV 082 // Second read should trip over the end-of-stream marker and return false 083 assertFalse(decoder.advance()); 084 dis.close(); 085 assertEquals(offset, cis.getCount()); 086 } 087 088 @Test 089 public void testThree() throws IOException { 090 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 091 CountingOutputStream cos = new CountingOutputStream(baos); 092 DataOutputStream dos = new DataOutputStream(cos); 093 Codec codec = new CellCodec(); 094 Codec.Encoder encoder = codec.getEncoder(dos); 095 final KeyValue kv1 = 096 new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("1"), Bytes.toBytes("1")); 097 final KeyValue kv2 = 098 new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("2"), Bytes.toBytes("2")); 099 final KeyValue kv3 = 100 new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("3"), Bytes.toBytes("3")); 101 encoder.write(kv1); 102 encoder.write(kv2); 103 encoder.write(kv3); 104 encoder.flush(); 105 dos.close(); 106 long offset = cos.getCount(); 107 CountingInputStream cis = new CountingInputStream(new ByteArrayInputStream(baos.toByteArray())); 108 DataInputStream dis = new DataInputStream(cis); 109 Codec.Decoder decoder = codec.getDecoder(dis); 110 assertTrue(decoder.advance()); 111 ExtendedCell c = decoder.current(); 112 assertTrue(PrivateCellUtil.equals(c, kv1)); 113 assertTrue(decoder.advance()); 114 c = decoder.current(); 115 assertTrue(PrivateCellUtil.equals(c, kv2)); 116 assertTrue(decoder.advance()); 117 c = decoder.current(); 118 assertTrue(PrivateCellUtil.equals(c, kv3)); 119 assertFalse(decoder.advance()); 120 dis.close(); 121 assertEquals(offset, cis.getCount()); 122 } 123}