View Javadoc

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