View Javadoc

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