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.fail;
10  
11  import org.mule.api.lifecycle.InitialisationException;
12  import org.mule.api.transformer.Transformer;
13  import org.mule.util.SerializationUtils;
14  
15  import java.io.Serializable;
16  
17  /**
18   * Tests {@link GZipCompressTransformer} and its counterpart, the {@link GZipUncompressTransformer} with an object as an input.
19   */
20  public class GZipTransformerObjectTestCase extends GZipTransformerTestCase
21  {
22      private static final TestObject TEST_OBJECT = new TestObject(15, TEST_DATA);
23  
24      @Override
25      public Object getResultData()
26      {
27          try
28          {
29              return strat.compressByteArray(SerializationUtils.serialize(TEST_OBJECT));
30          }
31          catch (Exception e)
32          {
33              fail(e.getMessage());
34              return null;
35          }
36      }
37  
38      @Override
39      public Object getTestData()
40      {
41          return TEST_OBJECT;
42      }
43  
44      @Override
45      public Transformer getRoundTripTransformer()
46      {
47          GZipUncompressTransformer transformer = new GZipUncompressTransformer();
48          transformer.setMuleContext(muleContext);
49  
50          try
51          {
52              transformer.initialise();
53          }
54          catch (InitialisationException e)
55          {
56              fail(e.getMessage());
57          }
58  
59          return transformer;
60      }
61  
62      /**
63       * A class representing an arbitrary object.
64       */
65      private static class TestObject implements Serializable
66      {
67          private int intAttribute;
68          private String stringAttribute;
69  
70          public TestObject(int intAttribute, String stringAttribute)
71          {
72              this.intAttribute = intAttribute;
73              this.stringAttribute = stringAttribute;
74          }
75  
76          @Override
77          public boolean equals(Object o)
78          {
79              if (this == o)
80              {
81                  return true;
82              }
83              if (o == null || getClass() != o.getClass())
84              {
85                  return false;
86              }
87  
88              TestObject that = (TestObject) o;
89              if (intAttribute != that.intAttribute)
90              {
91                  return false;
92              }
93              if (stringAttribute != null ? !stringAttribute.equals(that.stringAttribute) : that.stringAttribute != null)
94              {
95                  return false;
96              }
97  
98              return true;
99          }
100 
101         @Override
102         public int hashCode()
103         {
104             int result = intAttribute;
105             result = 31 * result + (stringAttribute != null ? stringAttribute.hashCode() : 0);
106             return result;
107         }
108     }
109 }