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;
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 = new CountingInputStream(new ByteArrayInputStream(baos.toByteArray()));
062    DataInputStream dis = new DataInputStream(cis);
063    Codec.Decoder decoder = codec.getDecoder(dis);
064    assertFalse(decoder.advance());
065    dis.close();
066    assertEquals(0, cis.getCount());
067  }
068
069  @Test
070  public void testOne() throws IOException {
071    ByteArrayOutputStream baos = new ByteArrayOutputStream();
072    CountingOutputStream cos = new CountingOutputStream(baos);
073    DataOutputStream dos = new DataOutputStream(cos);
074    Codec codec = new CellCodec();
075    Codec.Encoder encoder = codec.getEncoder(dos);
076    final KeyValue kv =
077      new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("q"), Bytes.toBytes("v"));
078    kv.setSequenceId(Long.MAX_VALUE);
079    encoder.write(kv);
080    encoder.flush();
081    dos.close();
082    long offset = cos.getCount();
083    CountingInputStream cis = new CountingInputStream(new ByteArrayInputStream(baos.toByteArray()));
084    DataInputStream dis = new DataInputStream(cis);
085    Codec.Decoder decoder = codec.getDecoder(dis);
086    assertTrue(decoder.advance()); // First read should pull in the KV
087    // Second read should trip over the end-of-stream marker and return false
088    assertFalse(decoder.advance());
089    dis.close();
090    assertEquals(offset, cis.getCount());
091  }
092
093  @Test
094  public void testThree() throws IOException {
095    ByteArrayOutputStream baos = new ByteArrayOutputStream();
096    CountingOutputStream cos = new CountingOutputStream(baos);
097    DataOutputStream dos = new DataOutputStream(cos);
098    Codec codec = new CellCodec();
099    Codec.Encoder encoder = codec.getEncoder(dos);
100    final KeyValue kv1 =
101      new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("1"), Bytes.toBytes("1"));
102    final KeyValue kv2 =
103      new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("2"), Bytes.toBytes("2"));
104    final KeyValue kv3 =
105      new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("3"), Bytes.toBytes("3"));
106    encoder.write(kv1);
107    encoder.write(kv2);
108    encoder.write(kv3);
109    encoder.flush();
110    dos.close();
111    long offset = cos.getCount();
112    CountingInputStream cis = new CountingInputStream(new ByteArrayInputStream(baos.toByteArray()));
113    DataInputStream dis = new DataInputStream(cis);
114    Codec.Decoder decoder = codec.getDecoder(dis);
115    assertTrue(decoder.advance());
116    ExtendedCell c = decoder.current();
117    assertTrue(PrivateCellUtil.equals(c, kv1));
118    assertTrue(decoder.advance());
119    c = decoder.current();
120    assertTrue(PrivateCellUtil.equals(c, kv2));
121    assertTrue(decoder.advance());
122    c = decoder.current();
123    assertTrue(PrivateCellUtil.equals(c, kv3));
124    assertFalse(decoder.advance());
125    dis.close();
126    assertEquals(offset, cis.getCount());
127  }
128}