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.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.io.ByteArrayInputStream;
25  import java.io.ByteArrayOutputStream;
26  import java.io.DataInputStream;
27  import java.io.DataOutputStream;
28  import java.io.IOException;
29  
30  import org.apache.hadoop.hbase.KeyValue;
31  import org.apache.hadoop.hbase.SmallTests;
32  import org.apache.hadoop.hbase.codec.Codec;
33  import org.apache.hadoop.hbase.codec.KeyValueCodec;
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 TestKeyValueCodec {
43    @Test
44    public void testEmptyWorks() throws IOException {
45      ByteArrayOutputStream baos = new ByteArrayOutputStream();
46      CountingOutputStream cos = new CountingOutputStream(baos);
47      DataOutputStream dos = new DataOutputStream(cos);
48      KeyValueCodec kvc = new KeyValueCodec();
49      Codec.Encoder encoder = kvc.getEncoder(dos);
50      encoder.flush();
51      dos.close();
52      long offset = cos.getCount();
53      assertEquals(0, offset);
54      CountingInputStream cis =
55        new CountingInputStream(new ByteArrayInputStream(baos.toByteArray()));
56      DataInputStream dis = new DataInputStream(cis);
57      Codec.Decoder decoder = kvc.getDecoder(dis);
58      assertFalse(decoder.advance());
59      dis.close();
60      assertEquals(0, cis.getCount());
61    }
62  
63    @Test
64    public void testOne() throws IOException {
65      ByteArrayOutputStream baos = new ByteArrayOutputStream();
66      CountingOutputStream cos = new CountingOutputStream(baos);
67      DataOutputStream dos = new DataOutputStream(cos);
68      KeyValueCodec kvc = new KeyValueCodec();
69      Codec.Encoder encoder = kvc.getEncoder(dos);
70      final KeyValue kv =
71        new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("q"), Bytes.toBytes("v"));
72      final long length = kv.getLength() + Bytes.SIZEOF_INT; 
73      encoder.write(kv);
74      encoder.flush();
75      dos.close();
76      long offset = cos.getCount();
77      assertEquals(length, offset);
78      CountingInputStream cis =
79        new CountingInputStream(new ByteArrayInputStream(baos.toByteArray()));
80      DataInputStream dis = new DataInputStream(cis);
81      Codec.Decoder decoder = kvc.getDecoder(dis);
82      assertTrue(decoder.advance()); // First read should pull in the KV
83      // Second read should trip over the end-of-stream  marker and return false
84      assertFalse(decoder.advance());
85      dis.close();
86      assertEquals(length, cis.getCount());
87    }
88  
89    @Test
90    public void testThree() throws IOException {
91      ByteArrayOutputStream baos = new ByteArrayOutputStream();
92      CountingOutputStream cos = new CountingOutputStream(baos);
93      DataOutputStream dos = new DataOutputStream(cos);
94      KeyValueCodec kvc = new KeyValueCodec();
95      Codec.Encoder encoder = kvc.getEncoder(dos);
96      final KeyValue kv1 =
97        new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("1"), Bytes.toBytes("1"));
98      final KeyValue kv2 =
99        new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("2"), Bytes.toBytes("2"));
100     final KeyValue kv3 =
101       new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("3"), Bytes.toBytes("3"));
102     final long length = kv1.getLength() + Bytes.SIZEOF_INT; 
103     encoder.write(kv1);
104     encoder.write(kv2);
105     encoder.write(kv3);
106     encoder.flush();
107     dos.close();
108     long offset = cos.getCount();
109     assertEquals(length * 3, offset);
110     CountingInputStream cis =
111       new CountingInputStream(new ByteArrayInputStream(baos.toByteArray()));
112     DataInputStream dis = new DataInputStream(cis);
113     Codec.Decoder decoder = kvc.getDecoder(dis);
114     assertTrue(decoder.advance());
115     KeyValue kv = (KeyValue)decoder.current();
116     assertTrue(kv1.equals(kv));
117     assertTrue(decoder.advance());
118     kv = (KeyValue)decoder.current();
119     assertTrue(kv2.equals(kv));
120     assertTrue(decoder.advance());
121     kv = (KeyValue)decoder.current();
122     assertTrue(kv3.equals(kv));
123     assertFalse(decoder.advance());
124     dis.close();
125     assertEquals((length * 3), cis.getCount());
126   }
127 }