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