View Javadoc

1   /*
2    * $Id: DefaultMuleMessageTestCase.java 19400 2010-09-07 20:20:09Z mike.schilling $
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;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.api.transport.PropertyScope;
15  import org.mule.session.DefaultMuleSession;
16  import org.mule.tck.AbstractMuleTestCase;
17  import org.mule.tck.testmodels.fruit.Apple;
18  import org.mule.tck.testmodels.fruit.Orange;
19  import org.mule.transformer.types.MimeTypes;
20  import org.mule.transport.NullPayload;
21  import org.mule.util.IOUtils;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import javax.activation.DataHandler;
27  
28  public class DefaultMuleMessageTestCase extends AbstractMuleTestCase
29  {
30      //
31      // corner cases/errors
32      //
33      public void testConstructorWithNoMuleContext()
34      {
35          try
36          {
37              new DefaultMuleMessage(TEST_MESSAGE, null);
38              fail("DefaultMuleMessage must fail when created with null MuleContext");
39          }
40          catch (IllegalArgumentException iae)
41          {
42              // this one was expected
43          }
44      }
45  
46      public void testConstructorWithNullPayload()
47      {
48          MuleMessage message = new DefaultMuleMessage(null, muleContext);
49          assertEquals(NullPayload.getInstance(), message.getPayload());
50      }
51  
52      //
53      // payload-only ctor tests
54      //
55      public void testOneArgConstructor()
56      {
57          MuleMessage message = new DefaultMuleMessage(TEST_MESSAGE, muleContext);
58          assertEquals(TEST_MESSAGE, message.getPayload());
59      }
60  
61      public void testOneArgConstructorWithMuleMessageAsPayload()
62      {
63          MuleMessage oldMessage = createMuleMessage();
64  
65          MuleMessage message = new DefaultMuleMessage(oldMessage, muleContext);
66          assertEquals("MULE_MESSAGE", message.getPayload());
67          assertOutboundMessageProperty("MuleMessage", message);
68      }
69  
70      //
71      // ctor with message properties
72      //
73      public void testMessagePropertiesConstructor()
74      {
75          Map<String, Object> properties = createMessageProperties();
76  
77          MuleMessage message = new DefaultMuleMessage(TEST_MESSAGE, properties, muleContext);
78          assertEquals(TEST_MESSAGE, message.getPayload());
79          assertOutboundMessageProperty("MessageProperties", message);
80      }
81  
82      public void testMessagePropertiesAccessors()
83      {
84          Map<String, Object> properties = createMessageProperties();
85  
86          properties.put("number", "24");
87          properties.put("decimal", "24.3");
88          properties.put("boolean", "true");
89          Apple apple = new Apple(true);
90          properties.put("apple", apple);
91          MuleMessage message = new DefaultMuleMessage(TEST_MESSAGE, properties, muleContext);
92          assertTrue(message.getOutboundProperty("boolean", false));
93          assertEquals(new Integer(24), message.getOutboundProperty("number", 0));
94          assertEquals(new Byte((byte) 24), message.getOutboundProperty("number", (byte) 0));
95          assertEquals(new Long(24), message.getOutboundProperty("number", 0l));
96          assertEquals(new Float(24.3), message.getOutboundProperty("decimal", 0f));
97          Double d = message.getOutboundProperty("decimal", 0d);
98          assertEquals(new Double(24.3), d);
99  
100         assertEquals("true", message.getOutboundProperty("boolean", ""));
101 
102         assertEquals(apple, message.getOutboundProperty("apple"));
103         try
104         {
105             message.getOutboundProperty("apple", new Orange());
106             fail("Orange is not assignable to Apple");
107         }
108         catch (IllegalArgumentException e)
109         {
110             //expected
111         }
112 
113         //Test null
114         assertNull(message.getOutboundProperty("banana"));
115         assertNull(message.getOutboundProperty("blah"));
116 
117         //Test default value
118         assertEquals(new Float(24.3), message.getOutboundProperty("blah", 24.3f));
119 
120     }
121 
122     public void testMessagePropertiesConstructorWithMuleMessageAsPayload()
123     {
124         Map<String, Object> properties = createMessageProperties();
125         MuleMessage previousMessage = createMuleMessage();
126 
127         MuleMessage message = new DefaultMuleMessage(previousMessage, properties, muleContext);
128         assertEquals("MULE_MESSAGE", message.getPayload());
129         assertOutboundMessageProperty("MessageProperties", message);
130         assertOutboundMessageProperty("MuleMessage", message);
131     }
132 
133     //
134     // ctor with previous message
135     //
136     public void testPreviousMessageConstructorWithRegularPayloadAndMuleMessageAsPrevious()
137     {
138         MuleMessage previous = createMuleMessage();
139 
140         MuleMessage message = new DefaultMuleMessage(TEST_MESSAGE, previous, muleContext);
141         assertEquals(TEST_MESSAGE, message.getPayload());
142         assertOutboundMessageProperty("MuleMessage", message);
143         assertEquals(previous.getUniqueId(), message.getUniqueId());
144     }
145 
146     public void testPreviousMessageConstructorWithMuleMessageAsPayloadAndMuleMessageAsPrevious()
147     {
148         MuleMessage payload = createMuleMessage();
149         payload.setOutboundProperty("payload", "payload");
150 
151         MuleMessage previous = createMuleMessage();
152         previous.setOutboundProperty("previous", "previous");
153 
154         MuleMessage message = new DefaultMuleMessage(payload, previous, muleContext);
155         assertEquals("MULE_MESSAGE", message.getPayload());
156         assertOutboundMessageProperty("MuleMessage", message);
157         assertOutboundMessageProperty("payload", message);
158         assertEquals(previous.getUniqueId(), message.getUniqueId());
159     }
160 
161     public void testClearProperties()
162     {
163         MuleMessage payload = createMuleMessage();
164         payload.setOutboundProperty("foo", "fooValue");
165         payload.setInvocationProperty("bar", "barValue");
166 
167         assertEquals(1, payload.getInvocationPropertyNames().size());
168         assertEquals(2, payload.getOutboundPropertyNames().size());
169         assertEquals(0, payload.getInboundPropertyNames().size());
170 
171         payload.clearProperties(PropertyScope.INVOCATION);
172         assertEquals(0, payload.getInvocationPropertyNames().size());
173 
174         payload.clearProperties(PropertyScope.OUTBOUND);
175         assertEquals(0, payload.getOutboundPropertyNames().size());
176 
177         //See http://www.mulesoft.org/jira/browse/MULE-4968 for additional test needed here
178     }
179 
180     //
181     // copy ctor
182     //
183     public void testCopyConstructor() throws Exception
184     {
185         DefaultMuleMessage original = (DefaultMuleMessage) createMuleMessage();
186         Map<String, Object> properties = createMessageProperties();
187         original.addInboundProperties(properties);
188         assertInboundAndOutboundMessageProperties(original);
189 
190         MuleMessage copy = new DefaultMuleMessage(original);
191         assertInboundAndOutboundMessageProperties(copy);
192     }
193 
194     private void assertInboundAndOutboundMessageProperties(MuleMessage original)
195     {
196         assertOutboundMessageProperty("MuleMessage", original);
197         assertEquals("MessageProperties", original.getInboundProperty("MessageProperties"));
198     }
199 
200     //
201     // attachments
202     //
203     public void testLegacyAddingAttachment() throws Exception
204     {
205         MuleMessage message = new DefaultMuleMessage(TEST_MESSAGE, muleContext);
206 
207         DataHandler handler = new DataHandler("this is the attachment", "text/plain");
208         message.addOutboundAttachment("attachment", handler);
209 
210         assertTrue(message.getOutboundAttachmentNames().contains("attachment"));
211         assertEquals(handler, message.getOutboundAttachment("attachment"));
212     }
213 
214     public void testAddingOutboundAttachment() throws Exception
215     {
216         MuleMessage message = new DefaultMuleMessage(TEST_MESSAGE, muleContext);
217 
218         DataHandler handler = new DataHandler("this is the attachment", "text/plain");
219         message.addOutboundAttachment("attachment", handler);
220 
221         assertTrue(message.getOutboundAttachmentNames().contains("attachment"));
222         assertEquals(handler, message.getOutboundAttachment("attachment"));
223         assertEquals(0, message.getInboundAttachmentNames().size());
224 
225         message.removeOutboundAttachment("attachment");
226         assertEquals(0, message.getOutboundAttachmentNames().size());
227 
228         //Try with content type set
229         message.addOutboundAttachment("spi-props", IOUtils.getResourceAsUrl("test-spi.properties", getClass()), MimeTypes.TEXT);
230 
231         assertTrue(message.getOutboundAttachmentNames().contains("spi-props"));
232         handler = message.getOutboundAttachment("spi-props");
233         assertEquals(MimeTypes.TEXT, handler.getContentType());
234         assertEquals(1, message.getOutboundAttachmentNames().size());
235 
236         //Try without content type set
237         message.addOutboundAttachment("dummy", IOUtils.getResourceAsUrl("dummy.xml", getClass()), null);
238         handler = message.getOutboundAttachment("dummy");
239         assertEquals(MimeTypes.APPLICATION_XML, handler.getContentType());
240         assertEquals(2, message.getOutboundAttachmentNames().size());
241 
242 
243     }
244 
245     public void testAddingInboundAttachment() throws Exception
246     {
247         Map<String, DataHandler> attachments = new HashMap<String, DataHandler>();
248 
249         DataHandler dh = new DataHandler("this is the attachment", "text/plain");
250         attachments.put("attachment", dh);
251         MuleMessage message = new DefaultMuleMessage(TEST_MESSAGE, null, attachments, muleContext);
252 
253         assertTrue(message.getInboundAttachmentNames().contains("attachment"));
254         assertEquals(dh, message.getInboundAttachment("attachment"));
255 
256         assertEquals(0, message.getOutboundAttachmentNames().size());
257 
258     }
259 
260     public void testNewMuleMessageFromMuleMessageWithAttachment() throws Exception
261     {
262         MuleMessage previous = createMuleMessage();
263         DataHandler handler = new DataHandler("this is the attachment", "text/plain");
264         previous.addOutboundAttachment("attachment", handler);
265 
266         MuleMessage message = new DefaultMuleMessage(TEST_MESSAGE, previous, muleContext);
267         assertTrue(message.getOutboundAttachmentNames().contains("attachment"));
268         assertEquals(handler, message.getOutboundAttachment("attachment"));
269     }
270 
271     public void testFindPropertiesInAnyScope() throws Exception
272     {
273         MuleMessage message = createMuleMessage();
274         //Not sure why this test adds this property
275         message.removeProperty("MuleMessage", PropertyScope.OUTBOUND);
276 
277         //We need a session and current event for this test
278         RequestContext.setEvent(new DefaultMuleEvent(
279                 message,
280                 getTestInboundEndpoint("foo"),
281                 new DefaultMuleSession(muleContext)));
282 
283         message.setOutboundProperty("foo", "fooOutbound");
284         message.setInvocationProperty("bar", "barInvocation");
285         message.setInvocationProperty("foo", "fooInvocation");
286         message.setProperty("foo", "fooInbound", PropertyScope.INBOUND);
287         message.setSessionProperty("foo", "fooSession");
288 
289 
290         assertEquals(2, message.getInvocationPropertyNames().size());
291         assertEquals(1, message.getOutboundPropertyNames().size());
292         assertEquals(1, message.getInboundPropertyNames().size());
293         assertEquals(1, message.getSessionPropertyNames().size());
294 
295         String value = message.findPropertyInAnyScope("foo", null);
296         assertEquals("fooOutbound", value);
297 
298         message.removeProperty("foo", PropertyScope.OUTBOUND);
299 
300         value = message.findPropertyInAnyScope("foo", null);
301         assertEquals("fooInvocation", value);
302 
303         message.removeProperty("foo", PropertyScope.INVOCATION);
304 
305         value = message.findPropertyInAnyScope("foo", null);
306         assertEquals("fooSession", value);
307         message.removeProperty("foo", PropertyScope.SESSION);
308 
309         value = message.findPropertyInAnyScope("foo", null);
310         assertEquals("fooInbound", value);
311 
312         value = message.findPropertyInAnyScope("bar", null);
313         assertEquals("barInvocation", value);
314 
315     }
316 
317     //
318     // helpers
319     //
320     private Map<String, Object> createMessageProperties()
321     {
322         HashMap<String, Object> map = new HashMap<String, Object>();
323         map.put("MessageProperties", "MessageProperties");
324         return map;
325     }
326 
327     private MuleMessage createMuleMessage()
328     {
329         MuleMessage previousMessage = new DefaultMuleMessage("MULE_MESSAGE", muleContext);
330         previousMessage.setOutboundProperty("MuleMessage", "MuleMessage");
331         return previousMessage;
332     }
333 
334     private void assertOutboundMessageProperty(String key, MuleMessage message)
335     {
336         // taking advantage of the fact here that key and value are the same
337         assertEquals(key, message.getOutboundProperty(key));
338     }
339 }