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