View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.jms;
8   
9   import org.mule.DefaultMuleEvent;
10  import org.mule.DefaultMuleMessage;
11  import org.mule.RequestContext;
12  import org.mule.api.MuleMessage;
13  import org.mule.tck.MuleTestUtils;
14  import org.mule.tck.junit4.AbstractMuleContextTestCase;
15  import org.mule.transport.jms.transformers.ObjectToJMSMessage;
16  
17  import com.mockobjects.constraint.Constraint;
18  import com.mockobjects.constraint.IsEqual;
19  import com.mockobjects.dynamic.ConstraintMatcher;
20  import com.mockobjects.dynamic.FullConstraintMatcher;
21  import com.mockobjects.dynamic.Mock;
22  
23  import javax.jms.Message;
24  import javax.jms.TextMessage;
25  
26  import org.apache.commons.collections.IteratorUtils;
27  import org.junit.Test;
28  
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertNotNull;
31  
32  public class JmsTransformerTestCase extends AbstractMuleContextTestCase
33  {
34  
35      @Test
36      public void testCustomJMSProperty() throws Exception
37      {
38          // Warning: this test is REALLY complicated :)
39          // The purpose is to test whether custom JMS message properties survive
40          // transformations when their name begins with "JMS" (MULE-1120).
41  
42          // First we need a JMS message wrapped into a MuleMessage. This turned out to
43          // be trickier than expected (ha ha) since mocking a Message depends on the
44          // specific calls made to the mocked class.
45          Mock mockMessage = new Mock(TextMessage.class);
46          mockMessage.expectAndReturn("getJMSCorrelationID", null);
47          mockMessage.expectAndReturn("getJMSMessageID", "1234567890");
48          mockMessage.expectAndReturn("getJMSDeliveryMode", new Integer(1));
49          mockMessage.expectAndReturn("getJMSDestination", null);
50          mockMessage.expectAndReturn("getJMSPriority", new Integer(4));
51          mockMessage.expectAndReturn("getJMSRedelivered", Boolean.FALSE);
52          mockMessage.expectAndReturn("getJMSReplyTo", null);
53          mockMessage.expectAndReturn("getJMSExpiration", new Long(0));
54          mockMessage.expectAndReturn("getJMSTimestamp", new Long(0));
55          mockMessage.expectAndReturn("getJMSType", null);
56  
57          mockMessage.expect("toString");
58          mockMessage.expect("toString");
59  
60          mockMessage.expect("clearProperties");
61  
62          mockMessage.expectAndReturn("getPropertyNames",
63              IteratorUtils.asEnumeration(IteratorUtils.emptyIterator()));
64  
65          mockMessage.expectAndReturn("getObjectProperty", "JMS_CUSTOM_PROPERTY", "customValue");
66  
67          ConstraintMatcher setPropertyMatcher = new FullConstraintMatcher(new Constraint[]{
68              new IsEqual("JMS_CUSTOM_PROPERTY"), new IsEqual("customValue")});
69          mockMessage.expect("setObjectProperty", setPropertyMatcher);
70  
71          Message mockTextMessage = (Message)mockMessage.proxy();
72          MuleMessage msg = new DefaultMuleMessage(mockTextMessage, muleContext);
73  
74          // Now we set a custom "JMS-like" property on the MuleMessage
75          msg.setOutboundProperty("JMS_CUSTOM_PROPERTY", "customValue");
76  
77          // The AbstractJMSTransformer will only apply JMS properties to the
78          // underlying message when a "current event" is available, so we need to set
79          // one.
80          assertNotNull("The test hasn't been configured properly, no muleContext available", muleContext);
81          RequestContext.setEvent(new DefaultMuleEvent(msg, MuleTestUtils.getTestEvent("previous", muleContext)));
82  
83          // The transformer we are going to use is ObjectToJMSMessage, which will
84          // return the same (but mockingly modified!) JMS message that is used as
85          // input.
86          ObjectToJMSMessage transformer = createObject(ObjectToJMSMessage.class);
87          Message transformed = (Message)transformer.transform(msg.getPayload());
88  
89          // Finally we can assert that the setProperty done to the MuleMessage actually
90          // made it through to the wrapped JMS Message. Yay!
91          assertEquals("customValue", transformed.getObjectProperty("JMS_CUSTOM_PROPERTY"));
92  
93          // note that we don't verify() the mock since we have no way of knowing
94          // whether toString was actually called (environment dependency)
95      }
96  
97  }