1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.util;
22  
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.hbase.SmallTests;
25  import org.apache.hadoop.hbase.io.hfile.Compression;
26  import org.apache.hadoop.io.DataOutputBuffer;
27  import org.apache.hadoop.io.compress.CompressionCodec;
28  import org.apache.hadoop.io.compress.CompressionOutputStream;
29  import org.apache.hadoop.util.NativeCodeLoader;
30  import org.apache.hadoop.util.ReflectionUtils;
31  import org.junit.Test;
32  import org.junit.experimental.categories.Category;
33  
34  import java.io.BufferedOutputStream;
35  import java.io.DataOutputStream;
36  import java.io.IOException;
37  
38  import static org.junit.Assert.*;
39  
40  @Category(SmallTests.class)
41  public class TestCompressionTest {
42  
43    @Test
44    public void testTestCompression() {
45  
46      // This test will fail if you run the tests with LZO compression available.
47      try {
48        CompressionTest.testCompression(Compression.Algorithm.LZO);
49        fail(); // always throws
50      } catch (IOException e) {
51        // there should be a 'cause'.
52        assertNotNull(e.getCause());
53      }
54  
55      // this is testing the caching of the test results.
56      try {
57        CompressionTest.testCompression(Compression.Algorithm.LZO);
58        fail(); // always throws
59      } catch (IOException e) {
60        // there should be NO cause because it's a direct exception not wrapped
61        assertNull(e.getCause());
62      }
63  
64  
65      assertFalse(CompressionTest.testCompression("LZO"));
66      assertTrue(CompressionTest.testCompression("NONE"));
67      assertTrue(CompressionTest.testCompression("GZ"));
68  
69      if (isCompressionAvailable("org.apache.hadoop.io.compress.SnappyCodec")) {
70        if (NativeCodeLoader.isNativeCodeLoaded()) {
71          try {
72            System.loadLibrary("snappy");
73  
74            try {
75              Configuration conf = new Configuration();
76              CompressionCodec codec = (CompressionCodec)
77                ReflectionUtils.newInstance(
78                  conf.getClassByName("org.apache.hadoop.io.compress.SnappyCodec"), conf);
79  
80              DataOutputBuffer compressedDataBuffer = new DataOutputBuffer();
81              CompressionOutputStream deflateFilter =
82                codec.createOutputStream(compressedDataBuffer);
83  
84              byte[] data = new byte[1024];
85              DataOutputStream deflateOut = new DataOutputStream(
86                new BufferedOutputStream(deflateFilter));
87              deflateOut.write(data, 0, data.length);
88              deflateOut.flush();
89              deflateFilter.finish();
90  
91              // Snappy Codec class, Snappy nativelib and Hadoop nativelib with 
92              // Snappy JNIs are present
93              assertTrue(CompressionTest.testCompression("SNAPPY"));
94            }
95            catch (UnsatisfiedLinkError ex) {
96              // Hadoop nativelib does not have Snappy JNIs
97              
98              // cannot assert the codec here because the current logic of 
99              // CompressionTest checks only classloading, not the codec
100             // usage.
101           }
102           catch (Exception ex) {
103           }
104         }
105         catch (UnsatisfiedLinkError ex) {
106           // Snappy nativelib is not available
107           assertFalse(CompressionTest.testCompression("SNAPPY"));
108         }
109       }
110       else {
111         // Hadoop nativelib is not available
112         assertFalse(CompressionTest.testCompression("SNAPPY"));
113       }
114     }
115     else {
116       // Snappy Codec class is not available
117       assertFalse(CompressionTest.testCompression("SNAPPY"));
118     }
119   }
120 
121   private boolean isCompressionAvailable(String codecClassName) {
122     try {
123       Thread.currentThread().getContextClassLoader().loadClass(codecClassName);
124       return true;
125     }
126     catch (Exception ex) {
127       return false;
128     }
129   }
130 
131 
132   @org.junit.Rule
133   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
134     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
135 }
136