1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.regionserver.wal;
19
20
21 import static org.junit.Assert.assertEquals;
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.io.util.Dictionary;
31 import org.apache.hadoop.hbase.io.util.LRUDictionary;
32 import org.apache.hadoop.hbase.testclassification.SmallTests;
33 import org.apache.hadoop.hbase.util.Bytes;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.junit.experimental.categories.Category;
37
38
39
40
41 @Category(SmallTests.class)
42 public class TestCompressor {
43 @BeforeClass
44 public static void setUpBeforeClass() throws Exception {
45 }
46
47 @Test
48 public void testToShort() {
49 short s = 1;
50 assertEquals(s, Compressor.toShort((byte)0, (byte)1));
51 s <<= 8;
52 assertEquals(s, Compressor.toShort((byte)1, (byte)0));
53 }
54
55 @Test (expected = IllegalArgumentException.class)
56 public void testNegativeToShort() {
57 Compressor.toShort((byte)0xff, (byte)0xff);
58 }
59
60 @Test
61 public void testCompressingWithNullDictionaries() throws IOException {
62 ByteArrayOutputStream baos = new ByteArrayOutputStream();
63 DataOutputStream dos = new DataOutputStream(baos);
64 byte [] blahBytes = Bytes.toBytes("blah");
65 Compressor.writeCompressed(blahBytes, 0, blahBytes.length, dos, null);
66 dos.close();
67 byte [] dosbytes = baos.toByteArray();
68 DataInputStream dis =
69 new DataInputStream(new ByteArrayInputStream(dosbytes));
70 byte [] product = Compressor.readCompressed(dis, null);
71 assertTrue(Bytes.equals(blahBytes, product));
72 }
73
74 @Test
75 public void testCompressingWithClearDictionaries() throws IOException {
76 ByteArrayOutputStream baos = new ByteArrayOutputStream();
77 DataOutputStream dos = new DataOutputStream(baos);
78 Dictionary dictionary = new LRUDictionary();
79 dictionary.init(Short.MAX_VALUE);
80 byte [] blahBytes = Bytes.toBytes("blah");
81 Compressor.writeCompressed(blahBytes, 0, blahBytes.length, dos, dictionary);
82 dos.close();
83 byte [] dosbytes = baos.toByteArray();
84 DataInputStream dis =
85 new DataInputStream(new ByteArrayInputStream(dosbytes));
86 dictionary = new LRUDictionary();
87 dictionary.init(Short.MAX_VALUE);
88 byte [] product = Compressor.readCompressed(dis, dictionary);
89 assertTrue(Bytes.equals(blahBytes, product));
90 }
91 }