View Javadoc

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