View Javadoc

1   /*
2    * $Id: CompressionTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.util.compression;
12  
13  import org.mule.tck.AbstractMuleTestCase;
14  
15  import java.util.Arrays;
16  
17  public class CompressionTestCase extends AbstractMuleTestCase
18  {
19  
20      public void testCompressDefaultGZip() throws Exception
21      {
22          String temp = "This is a compressed string";
23          CompressionStrategy strategy = CompressionHelper.getDefaultCompressionStrategy();
24          byte[] compressed = strategy.compressByteArray(temp.getBytes());
25  
26          // For small test data the compressed data will be bigger than the real data
27          assertTrue(compressed.length > temp.getBytes().length);
28  
29          byte[] uncompressed = strategy.uncompressByteArray(compressed);
30          assertTrue(uncompressed.length == temp.getBytes().length);
31  
32          assertEquals(temp, new String(uncompressed));
33  
34          String tempLarge = temp;
35          for (int i = 0; i < 100; i++)
36          {
37              tempLarge += temp;
38          }
39  
40          compressed = strategy.compressByteArray(tempLarge.getBytes());
41  
42          assertTrue(compressed.length < tempLarge.getBytes().length);
43  
44          uncompressed = strategy.uncompressByteArray(compressed);
45          assertTrue(uncompressed.length == tempLarge.getBytes().length);
46  
47          assertEquals(tempLarge, new String(uncompressed));
48  
49      }
50  
51      public void testNullIsCompressed() throws Exception
52      {
53          CompressionStrategy strategy = CompressionHelper.getDefaultCompressionStrategy();
54          assertFalse(strategy.isCompressed(null));
55      }
56  
57      public void testEmptyIsCompressed() throws Exception
58      {
59          CompressionStrategy strategy = CompressionHelper.getDefaultCompressionStrategy();
60          assertFalse(strategy.isCompressed(new byte[0]));
61      }
62  
63      public void testCompressNullBytes() throws Exception
64      {
65          CompressionStrategy strategy = CompressionHelper.getDefaultCompressionStrategy();
66          assertNull(strategy.compressByteArray(null));
67      }
68  
69      public void testCompressEmptyBytes() throws Exception
70      {
71          CompressionStrategy strategy = CompressionHelper.getDefaultCompressionStrategy();
72          byte[] bytes = new byte[0];
73          byte[] result = strategy.compressByteArray(bytes);
74  
75          assertTrue(strategy.isCompressed(result));
76      }
77  
78      public void testUncompressNullBytes() throws Exception
79      {
80          CompressionStrategy strategy = CompressionHelper.getDefaultCompressionStrategy();
81          assertNull(strategy.uncompressByteArray(null));
82      }
83  
84      public void testUncompressEmptyBytes() throws Exception
85      {
86          CompressionStrategy strategy = CompressionHelper.getDefaultCompressionStrategy();
87          byte[] bytes = new byte[0];
88  
89          byte[] cmpbytes = strategy.compressByteArray(bytes);
90          assertTrue(strategy.isCompressed(cmpbytes));
91  
92          byte[] result = strategy.uncompressByteArray(cmpbytes);
93          assertTrue(Arrays.equals(bytes, result));
94      }
95  
96  }