View Javadoc

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