1
2
3
4
5
6
7
8
9
10
11 package org.mule.registry;
12
13 import org.mule.DefaultMuleMessage;
14 import org.mule.OptimizedRequestContext;
15 import org.mule.RequestContext;
16 import org.mule.api.MuleContext;
17 import org.mule.api.MuleEvent;
18 import org.mule.api.MuleException;
19 import org.mule.api.MuleMessage;
20 import org.mule.api.MuleSession;
21 import org.mule.api.ThreadSafeAccess;
22 import org.mule.api.construct.FlowConstruct;
23 import org.mule.api.endpoint.ImmutableEndpoint;
24 import org.mule.api.security.Credentials;
25 import org.mule.api.transformer.DataType;
26 import org.mule.api.transformer.TransformerException;
27 import org.mule.message.DefaultExceptionPayload;
28 import org.mule.tck.AbstractMuleTestCase;
29
30 import java.io.OutputStream;
31
32 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
33
34 public class RequestContextTestCase extends AbstractMuleTestCase
35 {
36
37 private boolean threadSafeEvent;
38
39 public void testSetExceptionPayloadAcrossThreads() throws InterruptedException
40 {
41 threadSafeEvent = true;
42 MuleEvent event = new DummyEvent();
43 runThread(event, false);
44 runThread(event, true);
45 }
46
47 public void testFailureWithoutThreadSafeEvent() throws InterruptedException
48 {
49 threadSafeEvent = false;
50 MuleEvent event = new DummyEvent();
51 runThread(event, false);
52 runThread(event, true);
53 }
54
55 protected void runThread(MuleEvent event, boolean doTest) throws InterruptedException
56 {
57 AtomicBoolean success = new AtomicBoolean(false);
58 Thread thread = new Thread(new SetExceptionPayload(event, success));
59 thread.start();
60 thread.join();
61 if (doTest)
62 {
63 assertEquals(threadSafeEvent, success.get());
64 }
65 }
66
67 private class SetExceptionPayload implements Runnable
68 {
69
70 private MuleEvent event;
71 private AtomicBoolean success;
72
73 public SetExceptionPayload(MuleEvent event, AtomicBoolean success)
74 {
75 this.event = event;
76 this.success = success;
77 }
78
79 public void run()
80 {
81 try
82 {
83 OptimizedRequestContext.unsafeSetEvent(event);
84 RequestContext.setExceptionPayload(new DefaultExceptionPayload(new Exception()));
85 success.set(true);
86 }
87 catch (RuntimeException e)
88 {
89 logger.error("error in thread", e);
90 }
91 }
92
93 }
94
95 private class DummyEvent implements MuleEvent, ThreadSafeAccess
96 {
97
98 private MuleMessage message = new DefaultMuleMessage(null, muleContext);
99
100 public MuleMessage getMessage()
101 {
102 return message;
103 }
104
105 public Credentials getCredentials()
106 {
107 return null;
108 }
109
110 public byte[] getMessageAsBytes() throws MuleException
111 {
112 return new byte[0];
113 }
114
115 public Object transformMessage() throws TransformerException
116 {
117 return null;
118 }
119
120 public Object transformMessage(Class outputType) throws TransformerException
121 {
122 return null;
123 }
124
125 @Deprecated
126 public byte[] transformMessageToBytes() throws TransformerException
127 {
128 return new byte[0];
129 }
130
131 public String transformMessageToString() throws TransformerException
132 {
133 return null;
134 }
135
136 public String getMessageAsString() throws MuleException
137 {
138 return null;
139 }
140
141 public <T> T transformMessage(DataType<T> outputType) throws TransformerException
142 {
143 return null;
144 }
145
146 public String getTransformedMessageAsString(String encoding) throws TransformerException
147 {
148 return null;
149 }
150
151 public String getMessageAsString(String encoding) throws MuleException
152 {
153 return null;
154 }
155
156 public String getId()
157 {
158 return null;
159 }
160
161 public Object getProperty(String name)
162 {
163 return null;
164 }
165
166 public Object getProperty(String name, Object defaultValue)
167 {
168 return null;
169 }
170
171 public ImmutableEndpoint getEndpoint()
172 {
173 return null;
174 }
175
176 public MuleSession getSession()
177 {
178 return null;
179 }
180
181 public FlowConstruct getFlowConstruct()
182 {
183 return null;
184 }
185
186 public boolean isStopFurtherProcessing()
187 {
188 return false;
189 }
190
191 public void setStopFurtherProcessing(boolean stopFurtherProcessing)
192 {
193
194 }
195
196 public boolean isSynchronous()
197 {
198 return false;
199 }
200
201 public void setSynchronous(boolean value)
202 {
203
204 }
205
206 public int getTimeout()
207 {
208 return 0;
209 }
210
211 public void setTimeout(int timeout)
212 {
213
214 }
215
216 public OutputStream getOutputStream()
217 {
218 return null;
219 }
220
221 public String getEncoding()
222 {
223 return null;
224 }
225
226 public MuleContext getMuleContext()
227 {
228 return null;
229 }
230
231 public void assertAccess(boolean write)
232 {
233
234 }
235
236 public void resetAccessControl()
237 {
238
239 }
240
241 public ThreadSafeAccess newThreadCopy()
242 {
243 if (threadSafeEvent)
244 {
245 return new DummyEvent();
246 }
247 else
248 {
249 return this;
250 }
251 }
252 }
253
254 }