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.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   * Test our compressor class.
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  }