View Javadoc

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