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.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.io.util.Dictionary;
32  import org.apache.hadoop.hbase.io.util.LRUDictionary;
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   * Test our compressor class.
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  }