1   /*
2    * $Id: JmsTransformerTestCase.java 10489 2008-01-23 17:53:38Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.DefaultMuleMessage;
14  import org.mule.DefaultMuleEvent;
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.JmsMessageAdapter;
20  import org.mule.transport.jms.transformers.ObjectToJMSMessage;
21  
22  import com.mockobjects.constraint.Constraint;
23  import com.mockobjects.constraint.IsEqual;
24  import com.mockobjects.dynamic.ConstraintMatcher;
25  import com.mockobjects.dynamic.FullConstraintMatcher;
26  import com.mockobjects.dynamic.Mock;
27  
28  import javax.jms.Message;
29  import javax.jms.TextMessage;
30  
31  import org.apache.commons.collections.IteratorUtils;
32  
33  public class JmsTransformerTestCase extends AbstractMuleTestCase
34  {
35  
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(new JmsMessageAdapter(mockTextMessage));
73  
74          // Now we set a custom "JMS-like" property on the MuleMessage
75          msg.setProperty("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 = new ObjectToJMSMessage();
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  }