View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.codec;
19  
20  import static org.junit.Assert.*;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.DataInputStream;
25  import java.io.DataOutputStream;
26  import java.io.IOException;
27  
28  import org.apache.hadoop.hbase.Cell;
29  import org.apache.hadoop.hbase.CellComparator;
30  import org.apache.hadoop.hbase.KeyValue;
31  import org.apache.hadoop.hbase.SmallTests;
32  import org.apache.hadoop.hbase.codec.CellCodec;
33  import org.apache.hadoop.hbase.codec.Codec;
34  import org.apache.hadoop.hbase.util.Bytes;
35  import org.junit.Test;
36  import org.junit.experimental.categories.Category;
37  
38  import com.google.common.io.CountingInputStream;
39  import com.google.common.io.CountingOutputStream;
40  
41  @Category(SmallTests.class)
42  public class TestCellCodec {
43  
44    @Test
45    public void testEmptyWorks() throws IOException {
46      ByteArrayOutputStream baos = new ByteArrayOutputStream();
47      CountingOutputStream cos = new CountingOutputStream(baos);
48      DataOutputStream dos = new DataOutputStream(cos);
49      Codec codec = new CellCodec();
50      Codec.Encoder encoder = codec.getEncoder(dos);
51      encoder.flush();
52      dos.close();
53      long offset = cos.getCount();
54      assertEquals(0, offset);
55      CountingInputStream cis =
56        new CountingInputStream(new ByteArrayInputStream(baos.toByteArray()));
57      DataInputStream dis = new DataInputStream(cis);
58      Codec.Decoder decoder = codec.getDecoder(dis);
59      assertFalse(decoder.advance());
60      dis.close();
61      assertEquals(0, cis.getCount());
62    }
63  
64    @Test
65    public void testOne() throws IOException {
66      ByteArrayOutputStream baos = new ByteArrayOutputStream();
67      CountingOutputStream cos = new CountingOutputStream(baos);
68      DataOutputStream dos = new DataOutputStream(cos);
69      Codec codec = new CellCodec();
70      Codec.Encoder encoder = codec.getEncoder(dos);
71      final KeyValue kv =
72        new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("q"), Bytes.toBytes("v"));
73      encoder.write(kv);
74      encoder.flush();
75      dos.close();
76      long offset = cos.getCount();
77      CountingInputStream cis =
78        new CountingInputStream(new ByteArrayInputStream(baos.toByteArray()));
79      DataInputStream dis = new DataInputStream(cis);
80      Codec.Decoder decoder = codec.getDecoder(dis);
81      assertTrue(decoder.advance()); // First read should pull in the KV
82      // Second read should trip over the end-of-stream marker and return false
83      assertFalse(decoder.advance());
84      dis.close();
85      assertEquals(offset, cis.getCount());
86    }
87  
88    @Test
89    public void testThree() throws IOException {
90      ByteArrayOutputStream baos = new ByteArrayOutputStream();
91      CountingOutputStream cos = new CountingOutputStream(baos);
92      DataOutputStream dos = new DataOutputStream(cos);
93      Codec codec = new CellCodec();
94      Codec.Encoder encoder = codec.getEncoder(dos);
95      final KeyValue kv1 =
96        new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("1"), Bytes.toBytes("1"));
97      final KeyValue kv2 =
98        new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("2"), Bytes.toBytes("2"));
99      final KeyValue kv3 =
100       new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("3"), Bytes.toBytes("3"));
101     encoder.write(kv1);
102     encoder.write(kv2);
103     encoder.write(kv3);
104     encoder.flush();
105     dos.close();
106     long offset = cos.getCount();
107     CountingInputStream cis =
108       new CountingInputStream(new ByteArrayInputStream(baos.toByteArray()));
109     DataInputStream dis = new DataInputStream(cis);
110     Codec.Decoder decoder = codec.getDecoder(dis);
111     assertTrue(decoder.advance());
112     Cell c = decoder.current();
113     assertTrue(CellComparator.equals(c, kv1));
114     assertTrue(decoder.advance());
115     c = decoder.current();
116     assertTrue(CellComparator.equals(c, kv2));
117     assertTrue(decoder.advance());
118     c = decoder.current();
119     assertTrue(CellComparator.equals(c, kv3));
120     assertFalse(decoder.advance());
121     dis.close();
122     assertEquals(offset, cis.getCount());
123   }
124 }