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