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