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;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.RequestContext;
11  import org.mule.api.MuleMessage;
12  import org.mule.api.transformer.Transformer;
13  import org.mule.api.transformer.TransformerException;
14  import org.mule.tck.junit4.AbstractMuleContextTestCase;
15  import org.mule.tck.testmodels.fruit.InvalidSatsuma;
16  import org.mule.transformer.types.DataTypeFactory;
17  import org.mule.util.IOUtils;
18  
19  import java.io.InputStream;
20  import java.util.Arrays;
21  
22  import org.junit.Test;
23  
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertNotSame;
26  import static org.junit.Assert.assertTrue;
27  import static org.junit.Assert.fail;
28  
29  public abstract class AbstractTransformerTestCase extends AbstractMuleContextTestCase
30  {
31  
32      @Override
33      protected void doSetUp() throws Exception
34      {
35          // setup a dummy context for transformers that are event aware
36          RequestContext.setEvent(getTestEvent("test"));
37      }
38  
39      @Override
40      protected void doTearDown() throws Exception
41      {
42          RequestContext.setEvent(null);
43      }
44  
45      // Remove tabs and line breaks in the passed String; this makes comparison of XML
46      // fragments easier
47      protected String normalizeString(String rawString)
48      {
49          rawString = rawString.replaceAll("\r", "");
50          rawString = rawString.replaceAll("\n", "");
51          return rawString.replaceAll("\t", "");
52      }
53  
54      @Test
55      public void testTransform() throws Exception
56      {
57          Transformer trans = this.getTransformer();
58          Object result = trans.transform(getTestData());
59          assertNotNull("The result of the transform shouldn't be null", result);
60  
61          Object expectedResult = this.getResultData();
62          assertNotNull("The expected result data must not be null", expectedResult);
63  
64          assertTrue("Transformation result does not match expected result", this.compareResults(
65              expectedResult, result));
66      }
67  
68      @Test
69      public void testRoundtripTransform() throws Exception
70      {
71          Transformer roundTripTransformer = this.getRoundTripTransformer();
72          //If null this is just a one way test
73          if (roundTripTransformer != null)
74          {
75              Object result = roundTripTransformer.transform(this.getResultData());
76              assertNotNull("The result of the roundtrip transform shouldn't be null", result);
77  
78              assertTrue("The result of the roundtrip transform does not match the expected result", this.compareRoundtripResults(this.getTestData(), result));
79          }
80      }
81  
82      @Test
83      public void testBadReturnType() throws Exception
84      {
85          this.doTestBadReturnType(this.getTransformer(), this.getTestData());
86      }
87  
88      @Test
89      public void testRoundtripBadReturnType() throws Exception
90      {
91          if (this.getRoundTripTransformer() != null)
92          {
93              this.doTestBadReturnType(this.getRoundTripTransformer(), this.getResultData());
94          }
95      }
96  
97      @Test
98      public void testRoundTrip() throws Exception
99      {
100         if (this.getRoundTripTransformer() != null)
101         {
102             Transformer trans = this.getTransformer();
103             Transformer trans2 = this.getRoundTripTransformer();
104             MuleMessage message = new DefaultMuleMessage(getTestData(), muleContext);
105             message.applyTransformers(null, Arrays.asList(trans, trans2));
106             Object result = message.getPayload();
107             this.compareRoundtripResults(this.getTestData(), result);
108         }
109     }
110 
111     public void doTestBadReturnType(Transformer tran, Object src) throws Exception
112     {
113         tran.setReturnDataType(DataTypeFactory.create(InvalidSatsuma.class));
114         try
115         {
116             tran.transform(src);
117             fail("Should throw exception for bad return type");
118         }
119         catch (TransformerException e)
120         {
121             // expected
122         }
123     }
124 
125     protected void doTestClone(Transformer original, Transformer clone) throws Exception
126     {
127         assertNotSame(original, clone);
128     }
129 
130     public abstract Transformer getTransformer() throws Exception;
131 
132     public abstract Transformer getRoundTripTransformer() throws Exception;
133 
134     public abstract Object getTestData();
135 
136     public abstract Object getResultData();
137 
138     public boolean compareResults(Object expected, Object result)
139     {
140         if (expected == null && result == null)
141         {
142             return true;
143         }
144 
145         if (expected == null || result == null)
146         {
147             return false;
148         }
149 
150         if (expected instanceof Object[] && result instanceof Object[])
151         {
152             return Arrays.equals((Object[]) expected, (Object[]) result);
153         }
154         else if (expected instanceof byte[] && result instanceof byte[])
155         {
156             return Arrays.equals((byte[]) expected, (byte[]) result);
157         }
158 
159         if (expected instanceof InputStream && result instanceof InputStream)
160         {
161             return IOUtils.toString((InputStream) expected).equals(IOUtils.toString((InputStream) result));
162         }
163         else if (expected instanceof InputStream)
164         {
165             expected = IOUtils.toString((InputStream)expected);
166         }
167         
168         // Special case for Strings: normalize comparison arguments
169         if (expected instanceof String && result instanceof String)
170         {
171             expected = this.normalizeString((String) expected);
172             result = this.normalizeString((String) result);
173         }
174 
175         return expected.equals(result);
176     }
177 
178     public boolean compareRoundtripResults(Object expected, Object result)
179     {
180         return compareResults(expected, result);
181     }
182 
183 }