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