View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transformer.wire;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.transformer.wire.WireFormat;
12  import org.mule.tck.junit4.AbstractMuleContextTestCase;
13  import org.mule.tck.testmodels.fruit.Orange;
14  import org.mule.transformer.simple.ObjectToString;
15  
16  import java.io.ByteArrayInputStream;
17  import java.io.ByteArrayOutputStream;
18  import java.util.HashMap;
19  import java.util.Map;
20  import java.util.Properties;
21  
22  import org.junit.Test;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertNotNull;
26  import static org.junit.Assert.assertTrue;
27  
28  public abstract class AbstractWireFormatTestCase extends AbstractMuleContextTestCase
29  {
30  
31      @Test
32      public void testWriteReadMessage() throws Exception
33      {
34          // Create message to send over wire
35          Map<String, Object> messageProerties = new HashMap<String, Object>();
36          messageProerties.put("key1", "val1");
37          MuleMessage inMessage = new DefaultMuleMessage("testMessage", messageProerties, muleContext);
38  
39          Object outMessage = readWrite(inMessage);
40  
41          // NOTE: Since we are not using SerializedMuleMessageWireFormat we only get
42          // the payload back and not the MuleMessage.
43          assertTrue(outMessage instanceof String);
44          assertEquals("testMessage", outMessage);
45      }
46  
47      @Test
48      public void testWriteReadPayload() throws Exception
49      {
50          // Create orange to send over the wire
51          Properties messageProerties = new Properties();
52          messageProerties.put("key1", "val1");
53          Orange inOrange = new Orange();
54          inOrange.setBrand("Walmart");
55          inOrange.setMapProperties(messageProerties);
56  
57          Object outObject = readWrite(inOrange);
58  
59          // Test deserialized Fruit
60          assertTrue(outObject instanceof Orange);
61          assertEquals("Walmart", ((Orange) outObject).getBrand());
62          assertEquals("val1", ((Orange) outObject).getMapProperties().get("key1"));
63      }
64  
65      protected Object readWrite(Object inObject) throws Exception
66      {
67          // Serialize
68          WireFormat wireFormat = getWireFormat();
69          ByteArrayOutputStream out = new ByteArrayOutputStream();
70          wireFormat.write(out, inObject, "UTF-8");
71  
72          // De-serialize
73          ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
74          Object outMessage = wireFormat.read(in);
75          assertNotNull(outMessage);
76          return outMessage;
77      }
78  
79      @Test
80      public void testSetInboundTransformer() throws Exception
81      {
82          TransformerPairWireFormat transPairWireFormat = (TransformerPairWireFormat) getWireFormat();
83          transPairWireFormat.setInboundTransformer(new ObjectToString());
84          assertTrue(transPairWireFormat.getInboundTransformer() instanceof ObjectToString);
85      }
86  
87      @Test
88      public void testSetOutboundTransformer() throws Exception
89      {
90          TransformerPairWireFormat transPairWireFormat = (TransformerPairWireFormat) getWireFormat();
91          transPairWireFormat.setInboundTransformer(new ObjectToString());
92          assertTrue(transPairWireFormat.getInboundTransformer() instanceof ObjectToString);
93      }
94  
95      @Test
96      public abstract void testGetDefaultInboundTransformer() throws Exception;
97  
98      @Test
99      public abstract void testGetDefaultOutboundTransformer() throws Exception;
100 
101     protected abstract WireFormat getWireFormat() throws Exception;
102 
103 }