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.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertThrows;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.io.ByteArrayInputStream;
025import java.io.ByteArrayOutputStream;
026import java.io.DataInputStream;
027import java.io.DataOutputStream;
028import java.io.IOException;
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.jupiter.api.BeforeAll;
035import org.junit.jupiter.api.Tag;
036import org.junit.jupiter.api.Test;
037
038/**
039 * Test our compressor class.
040 */
041@Tag(RegionServerTests.TAG)
042@Tag(SmallTests.TAG)
043public class TestCompressor {
044
045  @BeforeAll
046  public static void setUpBeforeClass() throws Exception {
047  }
048
049  @Test
050  public void testToShort() {
051    short s = 1;
052    assertEquals(s, Compressor.toShort((byte) 0, (byte) 1));
053    s <<= 8;
054    assertEquals(s, Compressor.toShort((byte) 1, (byte) 0));
055  }
056
057  @Test
058  public void testNegativeToShort() {
059    assertThrows(IllegalArgumentException.class,
060      () -> Compressor.toShort((byte) 0xff, (byte) 0xff));
061  }
062
063  @Test
064  public void testCompressingWithNullDictionaries() throws IOException {
065    ByteArrayOutputStream baos = new ByteArrayOutputStream();
066    DataOutputStream dos = new DataOutputStream(baos);
067    byte[] blahBytes = Bytes.toBytes("blah");
068    Compressor.writeCompressed(blahBytes, 0, blahBytes.length, dos, null);
069    dos.close();
070    byte[] dosbytes = baos.toByteArray();
071    DataInputStream dis = new DataInputStream(new ByteArrayInputStream(dosbytes));
072    byte[] product = Compressor.readCompressed(dis, null);
073    assertTrue(Bytes.equals(blahBytes, product));
074  }
075
076  @Test
077  public void testCompressingWithClearDictionaries() throws IOException {
078    ByteArrayOutputStream baos = new ByteArrayOutputStream();
079    DataOutputStream dos = new DataOutputStream(baos);
080    Dictionary dictionary = new LRUDictionary();
081    dictionary.init(Short.MAX_VALUE);
082    byte[] blahBytes = Bytes.toBytes("blah");
083    Compressor.writeCompressed(blahBytes, 0, blahBytes.length, dos, dictionary);
084    dos.close();
085    byte[] dosbytes = baos.toByteArray();
086    DataInputStream dis = new DataInputStream(new ByteArrayInputStream(dosbytes));
087    dictionary = new LRUDictionary();
088    dictionary.init(Short.MAX_VALUE);
089    byte[] product = Compressor.readCompressed(dis, dictionary);
090    assertTrue(Bytes.equals(blahBytes, product));
091  }
092}