View Javadoc

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