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