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