1 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 protected void doSetUp() throws Exception 24 { 25 RequestContext.setEvent(getTestEvent("test")); 27 } 28 29 protected void doTearDown() throws Exception 30 { 31 RequestContext.setEvent(null); 32 } 33 34 protected String normalizeString(String rawString) 35 { 36 return rawString.replaceAll("\r\n", "\n"); 37 } 38 39 public void testTransform() throws Exception 40 { 41 Object result = getTransformer().transform(getTestData()); 42 assertNotNull(result); 43 44 Object expectedResult = getResultData(); 45 if (result instanceof String && expectedResult instanceof String ) 47 { 48 assertEquals(normalizeString((String )expectedResult), normalizeString((String )result)); 49 } 50 else 51 { 52 boolean b = compareResults(expectedResult, result); 53 assertTrue(b); 54 } 55 } 56 57 public void testRoundtripTransform() throws Exception 58 { 59 if (getRoundTripTransformer() != null) 60 { 61 Object result = getRoundTripTransformer().transform(getResultData()); 62 assertNotNull(result); 63 boolean b = compareRoundtripResults(getTestData(), result); 64 assertTrue(b); 65 } 66 } 67 68 public void testBadReturnType() throws Exception 69 { 70 doTestBadReturnType(getTransformer(), getTestData()); 71 } 72 73 public void testRoundtripBadReturnType() throws Exception 74 { 75 if (getRoundTripTransformer() != null) 76 { 77 doTestBadReturnType(getRoundTripTransformer(), getResultData()); 78 } 79 } 80 81 public void testRoundTrip() throws Exception 82 { 83 if (getRoundTripTransformer() != null) 84 { 85 UMOTransformer trans = getTransformer(); 86 trans.setNextTransformer(getRoundTripTransformer()); 87 Object result = trans.transform(getTestData()); 88 compareRoundtripResults(getTestData(), result); 89 } 90 } 91 92 public void doTestBadReturnType(UMOTransformer tran, Object src) throws Exception 93 { 94 tran.setReturnClass(InvalidSatsuma.class); 95 try 96 { 97 tran.transform(src); 98 fail("Should throw exception for bad return type"); 99 } 100 catch (TransformerException e) 101 { 102 } 104 } 105 106 public abstract UMOTransformer getTransformer() throws Exception ; 107 108 public abstract UMOTransformer getRoundTripTransformer() throws Exception ; 109 110 public abstract Object getTestData(); 111 112 public abstract Object getResultData(); 113 114 public boolean compareResults(Object src, Object result) 115 { 116 if (src == null && result == null) 117 { 118 return true; 119 } 120 if (src == null || result == null) 121 { 122 return false; 123 } 124 if (src instanceof Object [] && result instanceof Object []) 125 { 126 return Arrays.equals((Object [])src, (Object [])result); 127 } 131 else if (src instanceof byte[] && result instanceof byte[]) 132 { 133 return Arrays.equals((byte[])src, (byte[])result); 134 } 135 else 136 { 137 return src.equals(result); 138 } 139 } 140 141 public boolean compareRoundtripResults(Object src, Object result) 142 { 143 return compareResults(src, result); 144 } 145 } 146 | Popular Tags |