1   /*
2    * $Id: JmsTransformerTestCase.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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.providers.jms;
12  
13  import org.mule.impl.MuleEvent;
14  import org.mule.impl.MuleMessage;
15  import org.mule.impl.RequestContext;
16  import org.mule.providers.jms.transformers.ObjectToJMSMessage;
17  import org.mule.tck.AbstractMuleTestCase;
18  import org.mule.tck.MuleTestUtils;
19  import org.mule.umo.UMOMessage;
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 UMOMessage. 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          UMOMessage msg = new MuleMessage(new JmsMessageAdapter(mockTextMessage));
72  
73          // Now we set a custom "JMS-like" property on the UMOMessage
74          msg.setProperty("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          RequestContext.setEvent(new MuleEvent(msg, MuleTestUtils.getTestEvent("previous")));
80  
81          // The transformer we are going to use is ObjectToJMSMessage, which will
82          // return the same (but mockingly modified!) JMS message that is used as
83          // input.
84          ObjectToJMSMessage transformer = new ObjectToJMSMessage();
85          Message transformed = (Message)transformer.transform(msg.getPayload());
86  
87          // Finally we can assert that the setProperty done to the UMOMessage actually
88          // made it through to the wrapped JMS Message. Yay!
89          assertEquals("customValue", transformed.getObjectProperty("JMS_CUSTOM_PROPERTY"));
90  
91          // note that we don't verify() the mock since we have no way of knowing
92          // whether toString was actually called (environment dependency)
93      }
94  
95  }