View Javadoc

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