1
2
3
4
5
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
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
63 assertNotSame(asyncEvent, event);
64 assertFalse(asyncEvent.equals(event));
65 assertSame(asyncEvent.getSession(), event.getSession());
66
67
68 assertEquals("value", message.getInvocationProperty("key"));
69
70
71
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
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
95 assertNotSame(deserializedEvent, event);
96 assertFalse(deserializedEvent.equals(event));
97 assertNotSame(deserializedEvent.getSession(), event.getSession());
98 assertFalse(deserializedEvent.getSession().equals(event.getSession()));
99
100
101 assertEquals("value", deserializedEvent.getMessage().getInvocationProperty("key"));
102
103
104
105
106 deserializedEvent.getMessage().setInvocationProperty("newKey", "newValue");
107 assertEquals("newValue", deserializedEvent.getMessage().getInvocationProperty("newKey"));
108 assertNull(event.getMessage().getInvocationProperty("newKey"));
109 }
110
111
112
113
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
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
176 assertNotSame(processedEvent, event);
177 assertEquals(processedEvent, event);
178 assertNotSame(processedEvent.getSession(), event.getSession());
179
180
181 assertEquals("value", processedEvent.getMessage().getInvocationProperty("key"));
182 assertEquals(nonSerializable, processedEvent.getMessage().getInvocationProperty("key2"));
183
184
185
186 assertEquals("newValue", event.getMessage().getInvocationProperty("newKey"));
187 assertEquals("newValue", processedEvent.getMessage().getInvocationProperty("newKey"));
188
189 flow.stop();
190 flow.dispose();
191 }
192
193 }