1   /*
2    * $Id: AbstractWireFormatTestCase.java 11377 2008-03-16 18:18:33Z dfeist $
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.wire;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.transformer.wire.WireFormat;
16  import org.mule.tck.AbstractMuleTestCase;
17  import org.mule.tck.testmodels.fruit.Orange;
18  import org.mule.transformer.simple.ObjectToString;
19  
20  import java.io.ByteArrayInputStream;
21  import java.io.ByteArrayOutputStream;
22  import java.util.Properties;
23  
24  public abstract class AbstractWireFormatTestCase extends AbstractMuleTestCase
25  {
26  
27      public void testWriteReadMessage() throws Exception
28      {
29          // Create message to send over wire
30          Properties messageProerties = new Properties();
31          messageProerties.put("key1", "val1");
32          MuleMessage inMessage = new DefaultMuleMessage("testMessage", messageProerties);
33  
34          Object outMessage = readWrite(inMessage);
35  
36          // NOTE: Since we are not using SerializedMuleMessageWireFormat we only get
37          // the payload back and not the MuleMessage.
38          assertTrue(outMessage instanceof String);
39          assertEquals("testMessage", outMessage);
40      }
41  
42      public void testWriteReadPayload() throws Exception
43      {
44          // Create orange to send over the wire
45          Properties messageProerties = new Properties();
46          messageProerties.put("key1", "val1");
47          Orange inOrange = new Orange();
48          inOrange.setBrand("Walmart");
49          inOrange.setMapProperties(messageProerties);
50  
51          Object outObject = readWrite(inOrange);
52  
53          // Test deserialized Fruit
54          assertTrue(outObject instanceof Orange);
55          assertEquals("Walmart", ((Orange) outObject).getBrand());
56          assertEquals("val1", ((Orange) outObject).getMapProperties().get("key1"));
57      }
58  
59      protected Object readWrite(Object inObject) throws Exception
60      {
61          // Serialize
62          WireFormat wireFormat = getWireFormat();
63          ByteArrayOutputStream out = new ByteArrayOutputStream();
64          wireFormat.write(out, inObject, "UTF-8");
65  
66          // De-serialize
67          ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
68          Object outMessage = wireFormat.read(in);
69          assertNotNull(outMessage);
70          return outMessage;
71      }
72  
73      public void testSetInboundTransformer() throws Exception
74      {
75          TransformerPairWireFormat transPairWireFormat = (TransformerPairWireFormat) getWireFormat();
76          transPairWireFormat.setInboundTransformer(new ObjectToString());
77          assertTrue(transPairWireFormat.getInboundTransformer() instanceof ObjectToString);
78      }
79  
80      public void testSetOutboundTransformer() throws Exception
81      {
82          TransformerPairWireFormat transPairWireFormat = (TransformerPairWireFormat) getWireFormat();
83          transPairWireFormat.setInboundTransformer(new ObjectToString());
84          assertTrue(transPairWireFormat.getInboundTransformer() instanceof ObjectToString);
85      }
86  
87      public abstract void testGetDefaultInboundTransformer() throws Exception;
88  
89      public abstract void testGetDefaultOutboundTransformer() throws Exception;
90  
91      protected abstract WireFormat getWireFormat() throws Exception;
92  
93  }