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