Coverage Report - org.mule.tck.AbstractTransformerTestCase
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractTransformerTestCase
0%
0/55
0%
0/9
1.765
 
 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  0
 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  0
         RequestContext.setEvent(getTestEvent("test"));
 28  0
     }
 29  
 
 30  
     // @Override
 31  
     protected void doTearDown() throws Exception
 32  
     {
 33  0
         RequestContext.setEvent(null);
 34  0
     }
 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  0
         rawString = rawString.replaceAll("\r", "");
 41  0
         rawString = rawString.replaceAll("\n", "");
 42  0
         return rawString.replaceAll("\t", "");
 43  
     }
 44  
 
 45  
     public void testTransform() throws Exception
 46  
     {
 47  0
         Object result = this.getTransformer().transform(getTestData());
 48  0
         assertNotNull(result);
 49  
 
 50  0
         Object expectedResult = this.getResultData();
 51  0
         assertNotNull(expectedResult);
 52  
 
 53  0
         assertTrue(this.compareResults(expectedResult, result));
 54  0
     }
 55  
 
 56  
     public void testRoundtripTransform() throws Exception
 57  
     {
 58  0
         if (this.getRoundTripTransformer() != null)
 59  
         {
 60  0
             Object result = this.getRoundTripTransformer().transform(this.getResultData());
 61  0
             assertNotNull(result);
 62  
 
 63  0
             assertTrue(this.compareRoundtripResults(this.getTestData(), result));
 64  
         }
 65  0
     }
 66  
 
 67  
     public void testBadReturnType() throws Exception
 68  
     {
 69  0
         this.doTestBadReturnType(this.getTransformer(), this.getTestData());
 70  0
     }
 71  
 
 72  
     public void testRoundtripBadReturnType() throws Exception
 73  
     {
 74  0
         if (this.getRoundTripTransformer() != null)
 75  
         {
 76  0
             this.doTestBadReturnType(this.getRoundTripTransformer(), this.getResultData());
 77  
         }
 78  0
     }
 79  
 
 80  
     public void testRoundTrip() throws Exception
 81  
     {
 82  0
         if (this.getRoundTripTransformer() != null)
 83  
         {
 84  0
             UMOTransformer trans = this.getTransformer();
 85  0
             trans.setNextTransformer(this.getRoundTripTransformer());
 86  0
             Object result = trans.transform(this.getTestData());
 87  0
             this.compareRoundtripResults(this.getTestData(), result);
 88  
         }
 89  0
     }
 90  
 
 91  
     public void doTestBadReturnType(UMOTransformer tran, Object src) throws Exception
 92  
     {
 93  0
         tran.setReturnClass(InvalidSatsuma.class);
 94  
         try
 95  
         {
 96  0
             tran.transform(src);
 97  0
             fail("Should throw exception for bad return type");
 98  
         }
 99  0
         catch (TransformerException e)
 100  
         {
 101  
             // expected
 102  0
         }
 103  0
     }
 104  
 
 105  
     public void testClone() throws Exception
 106  
     {
 107  0
         UMOTransformer original = this.getTransformer();
 108  0
         UMOTransformer clone = (UMOTransformer) original.clone();
 109  0
         this.doTestClone(original, clone);
 110  0
     }
 111  
 
 112  
     protected void doTestClone(UMOTransformer original, UMOTransformer clone) throws Exception
 113  
     {
 114  0
         assertNotSame(original, clone);
 115  0
     }
 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  0
         if (expected == null && result == null)
 128  
         {
 129  0
             return true;
 130  
         }
 131  
 
 132  0
         if (expected == null || result == null)
 133  
         {
 134  0
             return false;
 135  
         }
 136  
 
 137  0
         if (expected instanceof Object[] && result instanceof Object[])
 138  
         {
 139  0
             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  0
         else if (expected instanceof byte[] && result instanceof byte[])
 145  
         {
 146  0
             return Arrays.equals((byte[]) expected, (byte[]) result);
 147  
         }
 148  
 
 149  
         // Special case for Strings: normalize comparison arguments
 150  0
         if (expected instanceof String && result instanceof String)
 151  
         {
 152  0
             expected = this.normalizeString((String) expected);
 153  0
             result = this.normalizeString((String) result);
 154  
         }
 155  
 
 156  0
         return expected.equals(result);
 157  
     }
 158  
 
 159  
     public boolean compareRoundtripResults(Object expected, Object result)
 160  
     {
 161  0
         return compareResults(expected, result);
 162  
     }
 163  
 
 164  
 }