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.transformer.compression;
8   
9   import static org.junit.Assert.assertTrue;
10  import static org.junit.Assert.fail;
11  
12  import org.mule.api.lifecycle.InitialisationException;
13  import org.mule.api.transformer.Transformer;
14  import org.mule.transformer.AbstractTransformerTestCase;
15  import org.mule.transformer.types.DataTypeFactory;
16  import org.mule.util.SerializationUtils;
17  import org.mule.util.compression.GZipCompression;
18  
19  import org.junit.Test;
20  
21  /**
22   * Tests {@link GZipCompressTransformer} and its counterpart, the {@link GZipUncompressTransformer}.
23   */
24  public class GZipTransformerTestCase extends AbstractTransformerTestCase
25  {
26      protected static final String TEST_DATA = "the quick brown fox jumped over the lazy dog the quick brown fox jumped over the lazy dog the quick brown fox jumped over the lazy dog";
27      protected GZipCompression strat;
28  
29      @Override
30      protected void doSetUp() throws Exception
31      {
32          strat = new GZipCompression();
33      }
34  
35      @Override
36      public Object getResultData()
37      {
38          try
39          {
40              return strat.compressByteArray(SerializationUtils.serialize(TEST_DATA));
41          }
42          catch (Exception e)
43          {
44              fail(e.getMessage());
45              return null;
46          }
47      }
48  
49      @Override
50      public Object getTestData()
51      {
52          return TEST_DATA;
53      }
54  
55      @Override
56      public Transformer getTransformer()
57      {
58          return new GZipCompressTransformer();
59      }
60  
61      @Override
62      public Transformer getRoundTripTransformer()
63      {
64          GZipUncompressTransformer transformer = new GZipUncompressTransformer();
65          transformer.setMuleContext(muleContext);
66          transformer.setReturnDataType(DataTypeFactory.STRING);
67  
68          try
69          {
70              transformer.initialise();
71          }
72          catch (InitialisationException e)
73          {
74              fail(e.getMessage());
75          }
76  
77          return transformer;
78      }
79  
80      @Test
81      public void testCompressAndDecompress() throws Exception
82      {
83          Transformer compressorTransformer = getTransformer();
84          Transformer decompressorTransformer = getRoundTripTransformer();
85  
86          // Compress the test data.
87          Object compressedData = compressorTransformer.transform(getTestData());
88          // Decompress the test data.
89          Object decompressedData = decompressorTransformer.transform(compressedData);
90  
91          assertTrue(String.format("Compress and decompress process failed. Expected '%s', but got '%s'", getTestData(), decompressedData),
92                     compareResults(getTestData(), decompressedData));
93      }
94  }