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.Cell;
030import org.apache.hadoop.hbase.CellUtil;
031import org.apache.hadoop.hbase.HBaseClassTestRule;
032import org.apache.hadoop.hbase.KeyValue;
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 return false
091    dis.close();
092    assertEquals(offset, cis.getCount());
093  }
094
095  @Test
096  public void testThree() throws IOException {
097    ByteArrayOutputStream baos = new ByteArrayOutputStream();
098    CountingOutputStream cos = new CountingOutputStream(baos);
099    DataOutputStream dos = new DataOutputStream(cos);
100    MessageCodec cmc = new MessageCodec();
101    Codec.Encoder encoder = cmc.getEncoder(dos);
102    final KeyValue kv1 = new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("1"), Bytes.toBytes("1"));
103    final KeyValue kv2 = new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("2"), Bytes.toBytes("2"));
104    final KeyValue kv3 = new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("3"), Bytes.toBytes("3"));
105    encoder.write(kv1);
106    encoder.write(kv2);
107    encoder.write(kv3);
108    encoder.flush();
109    dos.close();
110    long offset = cos.getCount();
111    CountingInputStream cis = new CountingInputStream(new ByteArrayInputStream(baos.toByteArray()));
112    DataInputStream dis = new DataInputStream(cis);
113    Codec.Decoder decoder = cmc.getDecoder(dis);
114    assertTrue(decoder.advance());
115    Cell c = decoder.current();
116    assertTrue(CellUtil.equals(c, kv1));
117    assertTrue(decoder.advance());
118    c = decoder.current();
119    assertTrue(CellUtil.equals(c, kv2));
120    assertTrue(decoder.advance());
121    c = decoder.current();
122    assertTrue(CellUtil.equals(c, kv3));
123    assertFalse(decoder.advance());
124    dis.close();
125    assertEquals(offset, cis.getCount());
126  }
127}