View Javadoc

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