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