View Javadoc
1   package org.apache.commons.jcs.utils.zip;
2   
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,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.TestCase;
23  
24  import java.io.ByteArrayOutputStream;
25  import java.io.IOException;
26  import java.util.zip.GZIPOutputStream;
27  
28  /** Unit tests for the compression util */
29  public class CompressionUtilUnitTest
30      extends TestCase
31  {
32      /** Test method for decompressByteArray. */
33      public final void testDecompressByteArray_failure()
34      {
35          try
36          {
37              // DO WORK
38              CompressionUtil.decompressByteArray( null );
39  
40              // VERIFY
41              fail( "excepted an IllegalArgumentException" );
42          }
43          catch ( IllegalArgumentException exception )
44          {
45              // expected
46              return;
47          }
48      }
49  
50      /**
51       * Test method for decompressByteArray.
52       * <p>
53       * @throws IOException
54       */
55      public final void testCompressDecompressByteArray_success()
56          throws IOException
57      {
58          // SETUP
59          String text = "This is some text to compress, not a lot, just a bit ";
60  
61          // DO WORK
62          byte[] compressedText = CompressionUtil.compressByteArray( text.getBytes() );
63          byte[] output = CompressionUtil.decompressByteArray( compressedText );
64  
65          // VERIFY
66          String result = new String( output );
67          assertNotNull( "decompressed output stream shouldn't have been null ", output );
68          assertEquals( text, result );
69      }
70  
71      /**
72       * Test method for decompressByteArray.
73       * <p>
74       * @throws IOException
75       */
76      public final void testCompressDecompressGzipByteArray_success()
77          throws IOException
78      {
79          // SETUP
80          String text = " This is some text to compress, not a lot, just a bit ";
81  
82          ByteArrayOutputStream baos = new ByteArrayOutputStream();
83          GZIPOutputStream os = new GZIPOutputStream( baos );
84  
85          os.write( text.getBytes() );
86          os.flush();
87          os.close();
88  
89          // DO WORK
90          byte[] output = CompressionUtil.decompressGzipByteArray( baos.toByteArray() );
91  
92          // VERIFY
93          String result = new String( output );
94          assertNotNull( "decompressed output stream shouldn't have been null ", output );
95          assertEquals( text, result );
96      }
97  }