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.session;
8   
9   import static org.junit.Assert.assertEquals;
10  import static org.junit.Assert.assertFalse;
11  import static org.junit.Assert.assertNotSame;
12  import static org.junit.Assert.assertNull;
13  import static org.junit.Assert.assertSame;
14  import static org.junit.Assert.fail;
15  
16  import org.mule.DefaultMuleEvent;
17  import org.mule.DefaultMuleMessage;
18  import org.mule.api.MuleEvent;
19  import org.mule.api.MuleException;
20  import org.mule.api.MuleMessage;
21  import org.mule.api.processor.MessageProcessor;
22  import org.mule.api.transport.PropertyScope;
23  import org.mule.construct.SimpleFlowConstruct;
24  import org.mule.processor.AsyncInterceptingMessageProcessor;
25  import org.mule.tck.SensingNullMessageProcessor;
26  import org.mule.tck.junit4.AbstractMuleContextTestCase;
27  import org.mule.util.SerializationUtils;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  import org.apache.commons.lang.SerializationException;
33  import org.junit.Test;
34  
35  public class InvocationPropertiesTestCase extends AbstractMuleContextTestCase
36  {
37  
38      /**
39       * MuleSession is not copied when async intercepting processor is used
40       */
41      @Test
42      public void asyncInterceptingProcessorInvocationPropertyPropagation() throws Exception
43      {
44          AsyncInterceptingMessageProcessor async = new AsyncInterceptingMessageProcessor(
45              muleContext.getDefaultThreadingProfile(), "async", 0);
46          SensingNullMessageProcessor asyncListener = new SensingNullMessageProcessor();
47          async.setListener(asyncListener);
48          async.start();
49  
50          MuleMessage message = new DefaultMuleMessage("data", muleContext);
51          MuleEvent event = new DefaultMuleEvent(message, getTestInboundEndpoint(""), getTestSession(
52              getTestService(), muleContext));
53  
54          message.setInvocationProperty("key", "value");
55  
56          async.process(event);
57          asyncListener.latch.await(RECEIVE_TIMEOUT,
58              edu.emory.mathcs.backport.java.util.concurrent.TimeUnit.MILLISECONDS);
59  
60          MuleEvent asyncEvent = asyncListener.event;
61  
62          // Event is copied, but session isn't
63          assertNotSame(asyncEvent, event);
64          assertFalse(asyncEvent.equals(event));
65          assertSame(asyncEvent.getSession(), event.getSession());
66  
67          // Session properties available before async are available after too
68          assertEquals("value", message.getInvocationProperty("key"));
69  
70          // Session properties set after async are available in message processor
71          // before async
72          message.setProperty("newKey", "newValue", PropertyScope.INVOCATION);
73          assertEquals("newValue", message.getInvocationProperty("newKey"));
74          assertNull(event.getSession().getProperty("newKey"));
75  
76          async.stop();
77      }
78  
79      /**
80       * MuleSession is not copied when async intercepting processor is used
81       */
82      @Test
83      public void serializationInvocationPropertyPropagation() throws Exception
84      {
85          MuleMessage message = new DefaultMuleMessage("data", muleContext);
86          MuleEvent event = new DefaultMuleEvent(message, getTestInboundEndpoint(""), getTestSession(
87              getTestService(), muleContext));
88  
89          message.setInvocationProperty("key", "value");
90  
91          MuleEvent deserializedEvent = (MuleEvent) SerializationUtils.deserialize(
92              SerializationUtils.serialize(event), muleContext);
93  
94          // Event and session are both copied
95          assertNotSame(deserializedEvent, event);
96          assertFalse(deserializedEvent.equals(event));
97          assertNotSame(deserializedEvent.getSession(), event.getSession());
98          assertFalse(deserializedEvent.getSession().equals(event.getSession()));
99  
100         // Session properties available before serialization are available after too
101         assertEquals("value", deserializedEvent.getMessage().getInvocationProperty("key"));
102 
103         // Session properties set after deserialization are not available in message
104         // processor
105         // before serialization
106         deserializedEvent.getMessage().setInvocationProperty("newKey", "newValue");
107         assertEquals("newValue", deserializedEvent.getMessage().getInvocationProperty("newKey"));
108         assertNull(event.getMessage().getInvocationProperty("newKey"));
109     }
110 
111     /**
112      * Serialization of a MuleSession with session properties serializes only
113      * serializable properties
114      */
115     @Test
116     public void serializationNonSerializableInvocationPropertyPropagation() throws Exception
117     {
118         MuleMessage message = new DefaultMuleMessage("data", muleContext);
119         MuleEvent event = new DefaultMuleEvent(message, getTestInboundEndpoint(""), getTestSession(
120             getTestService(), muleContext));
121 
122         Object nonSerializable = new Object();
123         message.setInvocationProperty("key", nonSerializable);
124         message.setInvocationProperty("key2", "value2");
125 
126         try
127         {
128             SerializationUtils.deserialize(SerializationUtils.serialize(event), muleContext);
129             fail("Serialization should have failed.");
130         }
131         catch (Exception e)
132         {
133             assertEquals(SerializationException.class, e.getClass());
134         }
135     }
136 
137     /**
138      * When invoking a Flow directly session properties are preserved
139      */
140     @Test
141     public void processFlowInvocationPropertyPropagation() throws Exception
142     {
143         MuleMessage message = new DefaultMuleMessage("data", muleContext);
144         MuleEvent event = new DefaultMuleEvent(message, getTestInboundEndpoint(""), getTestSession(
145             getTestService(), muleContext));
146 
147         SensingNullMessageProcessor flowListener = new SensingNullMessageProcessor();
148         List<MessageProcessor> processors = new ArrayList<MessageProcessor>();
149         processors.add(new MessageProcessor()
150         {
151             
152             public MuleEvent process(MuleEvent event) throws MuleException
153             {
154                 event.getMessage().setInvocationProperty("newKey", "newValue");
155                 return event;
156             }
157         });
158         processors.add(flowListener);
159         
160         SimpleFlowConstruct flow = new SimpleFlowConstruct("flow", muleContext);
161         flow.setMessageProcessors(processors);
162         flow.initialise();
163         flow.start();
164 
165         Object nonSerializable = new Object();
166         message.setInvocationProperty("key", "value");
167         message.setInvocationProperty("key2", nonSerializable);
168 
169         flow.process(event);
170 
171         flowListener.latch.await(RECEIVE_TIMEOUT,
172             edu.emory.mathcs.backport.java.util.concurrent.TimeUnit.MILLISECONDS);
173         MuleEvent processedEvent = flowListener.event;
174 
175         // Event is copied, but session isn't
176         assertNotSame(processedEvent, event);
177         assertEquals(processedEvent, event);
178         assertNotSame(processedEvent.getSession(), event.getSession());
179 
180         // Session properties available before new flow are available after too
181         assertEquals("value", processedEvent.getMessage().getInvocationProperty("key"));
182         assertEquals(nonSerializable, processedEvent.getMessage().getInvocationProperty("key2"));
183 
184         // Session properties set after new flow are available in message processor
185         // before new flow
186         assertEquals("newValue", event.getMessage().getInvocationProperty("newKey"));
187         assertEquals("newValue", processedEvent.getMessage().getInvocationProperty("newKey"));
188 
189         flow.stop();
190         flow.dispose();
191     }
192 
193 }