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