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.io.hfile.Compression;
25  import org.apache.hadoop.io.DataOutputBuffer;
26  import org.apache.hadoop.io.compress.CompressionCodec;
27  import org.apache.hadoop.io.compress.CompressionOutputStream;
28  import org.apache.hadoop.util.NativeCodeLoader;
29  import org.apache.hadoop.util.ReflectionUtils;
30  import org.junit.Test;
31  
32  import java.io.BufferedOutputStream;
33  import java.io.DataOutputStream;
34  import java.io.IOException;
35  
36  import static org.junit.Assert.*;
37  
38  public class TestCompressionTest {
39  
40    @Test
41    public void testTestCompression() {
42  
43      // This test will fail if you run the tests with LZO compression available.
44      try {
45        CompressionTest.testCompression(Compression.Algorithm.LZO);
46        fail(); // always throws
47      } catch (IOException e) {
48        // there should be a 'cause'.
49        assertNotNull(e.getCause());
50      }
51  
52      // this is testing the caching of the test results.
53      try {
54        CompressionTest.testCompression(Compression.Algorithm.LZO);
55        fail(); // always throws
56      } catch (IOException e) {
57        // there should be NO cause because it's a direct exception not wrapped
58        assertNull(e.getCause());
59      }
60  
61  
62      assertFalse(CompressionTest.testCompression("LZO"));
63      assertTrue(CompressionTest.testCompression("NONE"));
64      assertTrue(CompressionTest.testCompression("GZ"));
65  
66      if (isCompressionAvailable("org.apache.hadoop.io.compress.SnappyCodec")) {
67        if (NativeCodeLoader.isNativeCodeLoaded()) {
68          try {
69            System.loadLibrary("snappy");
70  
71            try {
72              Configuration conf = new Configuration();
73              CompressionCodec codec = (CompressionCodec)
74                ReflectionUtils.newInstance(
75                  conf.getClassByName("org.apache.hadoop.io.compress.SnappyCodec"), conf);
76  
77              DataOutputBuffer compressedDataBuffer = new DataOutputBuffer();
78              CompressionOutputStream deflateFilter =
79                codec.createOutputStream(compressedDataBuffer);
80  
81              byte[] data = new byte[1024];
82              DataOutputStream deflateOut = new DataOutputStream(
83                new BufferedOutputStream(deflateFilter));
84              deflateOut.write(data, 0, data.length);
85              deflateOut.flush();
86              deflateFilter.finish();
87  
88              // Snappy Codec class, Snappy nativelib and Hadoop nativelib with 
89              // Snappy JNIs are present
90              assertTrue(CompressionTest.testCompression("SNAPPY"));
91            }
92            catch (UnsatisfiedLinkError ex) {
93              // Hadoop nativelib does not have Snappy JNIs
94              
95              // cannot assert the codec here because the current logic of 
96              // CompressionTest checks only classloading, not the codec
97              // usage.
98            }
99            catch (Exception ex) {
100           }
101         }
102         catch (UnsatisfiedLinkError ex) {
103           // Snappy nativelib is not available
104           assertFalse(CompressionTest.testCompression("SNAPPY"));
105         }
106       }
107       else {
108         // Hadoop nativelib is not available
109         assertFalse(CompressionTest.testCompression("SNAPPY"));
110       }
111     }
112     else {
113       // Snappy Codec class is not available
114       assertFalse(CompressionTest.testCompression("SNAPPY"));
115     }
116   }
117 
118   private boolean isCompressionAvailable(String codecClassName) {
119     try {
120       Thread.currentThread().getContextClassLoader().loadClass(codecClassName);
121       return true;
122     }
123     catch (Exception ex) {
124       return false;
125     }
126   }
127 
128 }