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