View Javadoc

1   /*
2    * $Id: AbstractWireFormatTestCase.java 22377 2011-07-11 12:41:42Z dirk.olmes $
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.junit4.AbstractMuleContextTestCase;
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  import org.junit.Test;
27  
28  import static org.junit.Assert.assertEquals;
29  import static org.junit.Assert.assertNotNull;
30  import static org.junit.Assert.assertTrue;
31  
32  public abstract class AbstractWireFormatTestCase extends AbstractMuleContextTestCase
33  {
34  
35      @Test
36      public void testWriteReadMessage() throws Exception
37      {
38          // Create message to send over wire
39          Map<String, Object> messageProerties = new HashMap<String, Object>();
40          messageProerties.put("key1", "val1");
41          MuleMessage inMessage = new DefaultMuleMessage("testMessage", messageProerties, muleContext);
42  
43          Object outMessage = readWrite(inMessage);
44  
45          // NOTE: Since we are not using SerializedMuleMessageWireFormat we only get
46          // the payload back and not the MuleMessage.
47          assertTrue(outMessage instanceof String);
48          assertEquals("testMessage", outMessage);
49      }
50  
51      @Test
52      public void testWriteReadPayload() throws Exception
53      {
54          // Create orange to send over the wire
55          Properties messageProerties = new Properties();
56          messageProerties.put("key1", "val1");
57          Orange inOrange = new Orange();
58          inOrange.setBrand("Walmart");
59          inOrange.setMapProperties(messageProerties);
60  
61          Object outObject = readWrite(inOrange);
62  
63          // Test deserialized Fruit
64          assertTrue(outObject instanceof Orange);
65          assertEquals("Walmart", ((Orange) outObject).getBrand());
66          assertEquals("val1", ((Orange) outObject).getMapProperties().get("key1"));
67      }
68  
69      protected Object readWrite(Object inObject) throws Exception
70      {
71          // Serialize
72          WireFormat wireFormat = getWireFormat();
73          ByteArrayOutputStream out = new ByteArrayOutputStream();
74          wireFormat.write(out, inObject, "UTF-8");
75  
76          // De-serialize
77          ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
78          Object outMessage = wireFormat.read(in);
79          assertNotNull(outMessage);
80          return outMessage;
81      }
82  
83      @Test
84      public void testSetInboundTransformer() throws Exception
85      {
86          TransformerPairWireFormat transPairWireFormat = (TransformerPairWireFormat) getWireFormat();
87          transPairWireFormat.setInboundTransformer(new ObjectToString());
88          assertTrue(transPairWireFormat.getInboundTransformer() instanceof ObjectToString);
89      }
90  
91      @Test
92      public void testSetOutboundTransformer() throws Exception
93      {
94          TransformerPairWireFormat transPairWireFormat = (TransformerPairWireFormat) getWireFormat();
95          transPairWireFormat.setInboundTransformer(new ObjectToString());
96          assertTrue(transPairWireFormat.getInboundTransformer() instanceof ObjectToString);
97      }
98  
99      @Test
100     public abstract void testGetDefaultInboundTransformer() throws Exception;
101 
102     @Test
103     public abstract void testGetDefaultOutboundTransformer() throws Exception;
104 
105     protected abstract WireFormat getWireFormat() throws Exception;
106 
107 }