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