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 = new DataInputStream(new ByteArrayInputStream(dosbytes));
075    byte[] product = Compressor.readCompressed(dis, null);
076    assertTrue(Bytes.equals(blahBytes, product));
077  }
078
079  @Test
080  public void testCompressingWithClearDictionaries() throws IOException {
081    ByteArrayOutputStream baos = new ByteArrayOutputStream();
082    DataOutputStream dos = new DataOutputStream(baos);
083    Dictionary dictionary = new LRUDictionary();
084    dictionary.init(Short.MAX_VALUE);
085    byte[] blahBytes = Bytes.toBytes("blah");
086    Compressor.writeCompressed(blahBytes, 0, blahBytes.length, dos, dictionary);
087    dos.close();
088    byte[] dosbytes = baos.toByteArray();
089    DataInputStream dis = new DataInputStream(new ByteArrayInputStream(dosbytes));
090    dictionary = new LRUDictionary();
091    dictionary.init(Short.MAX_VALUE);
092    byte[] product = Compressor.readCompressed(dis, dictionary);
093    assertTrue(Bytes.equals(blahBytes, product));
094  }
095}