View Javadoc

1   /*
2    * $Id$
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.processor;
12  
13  import org.mule.api.MuleException;
14  import org.mule.api.MuleRuntimeException;
15  import org.mule.api.lifecycle.Initialisable;
16  import org.mule.api.lifecycle.InitialisationException;
17  import org.mule.api.lifecycle.Lifecycle;
18  import org.mule.api.lifecycle.LifecycleState;
19  import org.mule.api.lifecycle.Startable;
20  import org.mule.api.lifecycle.Stoppable;
21  import org.mule.api.processor.MessageProcessor;
22  import org.mule.config.QueueProfile;
23  import org.mule.management.stats.QueueStatistics;
24  import org.mule.service.Pausable;
25  
26  import java.beans.ExceptionListener;
27  
28  import javax.resource.spi.work.Work;
29  import javax.resource.spi.work.WorkEvent;
30  import javax.resource.spi.work.WorkException;
31  
32  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
33  
34  public class SedaStageInterceptingMessageProcessorTestCase extends
35      OptionalAsyncInterceptingMessageProcessorTestCase implements ExceptionListener
36  {
37  
38      QueueProfile queueProfile = new QueueProfile();
39      int queueTimeout;
40      QueueStatistics queueStatistics;
41      TestLifeCycleState lifeCycleState;
42  
43      @Override
44      protected void doSetUp() throws Exception
45      {
46          queueStatistics = new TestQueueStatistics();
47          queueTimeout = muleContext.getConfiguration().getDefaultQueueTimeout();
48          lifeCycleState = new TestLifeCycleState();
49          super.doSetUp();
50          ((Initialisable) messageProcessor).initialise();
51          ((Startable) messageProcessor).start();
52          lifeCycleState.start();
53      }
54  
55      @Override
56      protected boolean isStartContext()
57      {
58          return false;
59      }
60  
61      @Override
62      protected void doTearDown() throws Exception
63      {
64          super.doTearDown();
65          ((Stoppable) messageProcessor).stop();
66          lifeCycleState.stop();
67          lifeCycleState.dispose();
68  
69      }
70  
71      protected AsyncInterceptingMessageProcessor createAsyncInterceptingMessageProcessor(MessageProcessor listener)
72          throws Exception
73      {
74          SedaStageInterceptingMessageProcessor mp = new SedaStageInterceptingMessageProcessor(
75              null, queueProfile, queueTimeout, new TestWorkManagerSource(), true, lifeCycleState,
76              queueStatistics, muleContext);
77          mp.setListener(listener);
78          return mp;
79      }
80  
81      public void testSpiWorkThrowableHandling() throws Exception
82      {
83          try
84          {
85              createAsyncInterceptingMessageProcessor(getSensingNullMessageProcessor()).handleWorkException(
86                  getTestWorkEvent(), "workRejected");
87          }
88          catch (MuleRuntimeException mrex)
89          {
90              assertNotNull(mrex);
91              assertTrue(mrex.getCause().getClass() == Throwable.class);
92              assertEquals("testThrowable", mrex.getCause().getMessage());
93          }
94      }
95  
96      private WorkEvent getTestWorkEvent()
97      {
98          return new WorkEvent(this, // source
99              WorkEvent.WORK_REJECTED, getTestWork(), new WorkException(new Throwable("testThrowable")));
100     }
101 
102     private Work getTestWork()
103     {
104         return new Work()
105         {
106             public void release()
107             {
108                 // noop
109             }
110 
111             public void run()
112             {
113                 // noop
114             }
115         };
116     }
117 
118     class TestQueueStatistics implements QueueStatistics
119     {
120         int incCount;
121         int decCount;
122 
123         public void decQueuedEvent()
124         {
125             decCount++;
126         }
127 
128         public void incQueuedEvent()
129         {
130             incCount++;
131         }
132 
133         public boolean isEnabled()
134         {
135             return true;
136         }
137     }
138 
139     class TestLifeCycleState implements LifecycleState, Lifecycle
140     {
141 
142         AtomicBoolean started = new AtomicBoolean(false);
143         AtomicBoolean stopped = new AtomicBoolean(true);
144         AtomicBoolean disposed = new AtomicBoolean(false);
145         AtomicBoolean initialised = new AtomicBoolean(false);
146         AtomicBoolean paused = new AtomicBoolean(false);
147 
148         public boolean isDisposed()
149         {
150             return disposed.get();
151         }
152 
153         public boolean isDisposing()
154         {
155             return false;
156         }
157 
158         public boolean isInitialised()
159         {
160             return initialised.get();
161         }
162 
163         public boolean isInitialising()
164         {
165             return false;
166         }
167 
168         public boolean isPhaseComplete(String phase)
169         {
170             if (Pausable.PHASE_NAME.equals(phase))
171             {
172                 return paused.get();
173             }
174             else
175             {
176                 return false;
177             }
178         }
179 
180         public boolean isPhaseExecuting(String phase)
181         {
182             return false;
183         }
184 
185         public boolean isStarted()
186         {
187             return started.get();
188         }
189 
190         public boolean isStarting()
191         {
192             return false;
193         }
194 
195         public boolean isStopped()
196         {
197             return stopped.get();
198         }
199 
200         public boolean isStopping()
201         {
202             return false;
203         }
204 
205         public void initialise() throws InitialisationException
206         {
207             initialised.set(true);
208         }
209 
210         public void start() throws MuleException
211         {
212             initialised.set(false);
213             stopped.set(false);
214             started.set(true);
215         }
216 
217         public void stop() throws MuleException
218         {
219             started.set(false);
220             stopped.set(true);
221         }
222 
223         public void dispose()
224         {
225             stopped.set(true);
226             disposed.set(true);
227         }
228 
229         public boolean isValidTransition(String phase)
230         {
231             return false;
232         }
233     }
234 
235 }