View Javadoc

1   /*
2    * $Id:XmlMuleMessageTransformersTestCase.java 5937 2007-04-09 22:35:04Z rossmason $
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.transformers.xml;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.config.MuleProperties;
16  import org.mule.module.xml.transformer.ObjectToXml;
17  import org.mule.module.xml.transformer.XmlToObject;
18  import org.mule.tck.AbstractMuleTestCase;
19  import org.mule.tck.testmodels.fruit.Apple;
20  
21  import java.util.Set;
22  
23  public class XmlMuleMessageTransformersTestCase extends AbstractMuleTestCase
24  {
25      public void testMessageSerialization() throws Exception
26      {
27          ObjectToXml t1 = createObject(ObjectToXml.class);
28          t1.setAcceptMuleMessage(true);
29  
30          MuleMessage msg = new DefaultMuleMessage("test", muleContext);
31          msg.setEncoding("UTF-8");
32          msg.setCorrelationId("1234");
33          msg.setInvocationProperty("number", 1);
34          msg.setOutboundProperty("object", new Apple());
35          msg.setOutboundProperty("string", "hello");
36  
37          String xml = (String) t1.transform(msg);
38          assertNotNull(xml);
39  
40          XmlToObject t2 = createObject(XmlToObject.class);
41  
42          Object result = t2.transform(xml);
43          assertNotNull(result);
44          assertTrue(result instanceof MuleMessage);
45  
46          msg = (MuleMessage) result;
47  
48          assertEquals("test", msg.getPayloadAsString());
49          assertEquals(new Apple(), msg.getOutboundProperty("object"));
50          //with different case
51          assertEquals(new Apple(), msg.getOutboundProperty("oBjeCt"));
52          //Make sure we don't have the property in a different scope
53          assertNull(msg.getInboundProperty("oBjeCt"));
54          assertNull(msg.getInvocationProperty("oBjeCt"));
55          assertNull(msg.getSessionProperty("oBjeCt"));
56  
57          assertEquals("hello", msg.getOutboundProperty("string"));
58          //with different case
59          assertEquals("hello", msg.getOutboundProperty("String"));
60          //Make sure we don't have the property in a different scope
61          assertNull(msg.getInboundProperty("string"));
62          assertNull(msg.getInvocationProperty("string"));
63          assertNull(msg.getSessionProperty("string"));
64  
65          assertEquals(1, msg.getInvocationProperty("number"));
66          //with different case
67          assertEquals(1, msg.getInvocationProperty("NUMBER"));
68          //Make sure we don't have the property in a different scope
69          assertNull(msg.getInboundProperty("number"));
70          assertNull(msg.getOutboundProperty("number"));
71          assertNull(msg.getSessionProperty("number"));
72  
73          assertEquals("1234", msg.getCorrelationId());
74          assertEquals("UTF-8", msg.getEncoding());
75  
76  
77          assertEquals(1, msg.getInvocationPropertyNames().size());
78          Set<String> outboundProps = msg.getOutboundPropertyNames();
79          assertEquals(4, outboundProps.size());
80  
81          //Remove Mule properties
82          outboundProps.remove(MuleProperties.MULE_CORRELATION_ID_PROPERTY);
83          outboundProps.remove(MuleProperties.MULE_ENCODING_PROPERTY);
84  
85          for (String key : outboundProps)
86          {
87              assertTrue(key.equals("number") || key.equals("string") || key.equals("object"));
88              assertFalse(key.equals("NUMBER") || key.equals("STRING") || key.equals("OBJECT"));
89          }
90      }
91  }