1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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());
81
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 }