001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver.wal;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.io.ByteArrayInputStream;
024import java.io.ByteArrayOutputStream;
025import java.io.DataInputStream;
026import java.io.DataOutputStream;
027import java.io.IOException;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.io.util.Dictionary;
030import org.apache.hadoop.hbase.io.util.LRUDictionary;
031import org.apache.hadoop.hbase.testclassification.RegionServerTests;
032import org.apache.hadoop.hbase.testclassification.SmallTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.junit.BeforeClass;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038
039/**
040 * Test our compressor class.
041 */
042@Category({RegionServerTests.class, SmallTests.class})
043public class TestCompressor {
044
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047      HBaseClassTestRule.forClass(TestCompressor.class);
048
049  @BeforeClass
050  public static void setUpBeforeClass() throws Exception {
051  }
052
053  @Test
054  public void testToShort() {
055    short s = 1;
056    assertEquals(s, Compressor.toShort((byte)0, (byte)1));
057    s <<= 8;
058    assertEquals(s, Compressor.toShort((byte)1, (byte)0));
059  }
060
061  @Test (expected = IllegalArgumentException.class)
062  public void testNegativeToShort() {
063    Compressor.toShort((byte)0xff, (byte)0xff);
064  }
065
066  @Test
067  public void testCompressingWithNullDictionaries() throws IOException {
068    ByteArrayOutputStream baos = new ByteArrayOutputStream();
069    DataOutputStream dos = new DataOutputStream(baos);
070    byte [] blahBytes = Bytes.toBytes("blah");
071    Compressor.writeCompressed(blahBytes, 0, blahBytes.length, dos, null);
072    dos.close();
073    byte [] dosbytes = baos.toByteArray();
074    DataInputStream dis =
075      new DataInputStream(new ByteArrayInputStream(dosbytes));
076    byte [] product = Compressor.readCompressed(dis, null);
077    assertTrue(Bytes.equals(blahBytes, product));
078  }
079
080  @Test
081  public void testCompressingWithClearDictionaries() throws IOException {
082    ByteArrayOutputStream baos = new ByteArrayOutputStream();
083    DataOutputStream dos = new DataOutputStream(baos);
084    Dictionary dictionary = new LRUDictionary();
085    dictionary.init(Short.MAX_VALUE);
086    byte [] blahBytes = Bytes.toBytes("blah");
087    Compressor.writeCompressed(blahBytes, 0, blahBytes.length, dos, dictionary);
088    dos.close();
089    byte [] dosbytes = baos.toByteArray();
090    DataInputStream dis =
091      new DataInputStream(new ByteArrayInputStream(dosbytes));
092    dictionary = new LRUDictionary();
093    dictionary.init(Short.MAX_VALUE);
094    byte [] product = Compressor.readCompressed(dis, dictionary);
095    assertTrue(Bytes.equals(blahBytes, product));
096  }
097}