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