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