View Javadoc

1   /*
2    * $Id: ServiceStateTestCase.java 22414 2011-07-14 13:24:46Z dirk.olmes $
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.test.components;
12  
13  import org.mule.api.MuleException;
14  import org.mule.api.lifecycle.LifecycleException;
15  import org.mule.api.service.Service;
16  import org.mule.api.transport.MessageReceiver;
17  import org.mule.tck.junit4.FunctionalTestCase;
18  import org.mule.transport.AbstractConnector;
19  
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertTrue;
26  import static org.junit.Assert.fail;
27  
28  public class ServiceStateTestCase extends FunctionalTestCase
29  {
30  
31      public ServiceStateTestCase()
32      {
33          setStartContext(true);
34      }
35  
36      @Override
37      protected String getConfigResources()
38      {
39          return "org/mule/test/components/component-initial-state.xml";
40      }
41  
42      @Test
43      public void testDefaultInitialState() throws Exception
44      {
45          Service c = muleContext.getRegistry().lookupService("defaultComponent");
46          // Service initially started
47          assertTrue(c.isStarted());
48          assertFalse(c.isPaused());
49          assertFalse(c.isStopped());
50  
51          // The listeners should be registered and started.
52          AbstractConnector connector = (AbstractConnector)muleContext.getRegistry().lookupConnector("connector.test.mule.default");
53          assertNotNull(connector);
54          assertTrue(connector.isStarted());
55          MessageReceiver[] receivers = connector.getReceivers("*default*");
56          assertEquals(1, receivers.length);
57          assertTrue(receivers[0].isConnected());
58      }
59  
60      // MULE-494
61      @Test
62      public void testInitialStateStopped() throws Exception
63      {
64          Service c = muleContext.getRegistry().lookupService("stoppedComponent");
65          assertEquals("stopped", c.getInitialState());
66          // Service initially stopped
67          assertFalse(c.isStarted());
68          assertTrue(c.isStopped());
69          assertFalse(c.isPaused());
70  
71          // The connector should be started, but with no listeners registered.
72          AbstractConnector connector = (AbstractConnector)muleContext.getRegistry().lookupConnector("connector.test.mule.default");
73          assertNotNull(connector);
74          assertTrue(connector.isStarted());
75          MessageReceiver[] receivers = connector.getReceivers("*stopped*");
76          assertEquals(0, receivers.length);
77  
78          // Start the service.
79          c.start();
80          assertTrue(c.isStarted());
81          assertFalse(c.isStopped());
82          assertFalse(c.isPaused());
83  
84          // The listeners should now be registered and started.
85          assertTrue(connector.isStarted());
86          receivers = connector.getReceivers("*stopped*");
87          assertEquals(1, receivers.length);
88          assertTrue(receivers[0].isConnected());
89      }
90  
91      // MULE-503
92      @Test
93      public void testStoppingComponentStopsEndpoints() throws Exception
94      {
95          Service c = muleContext.getRegistry().lookupService("startedComponent");
96          assertTrue(c.isStarted());
97          assertFalse(c.isStopped());
98          assertFalse(c.isPaused());
99  
100         // The listeners should be registered and started.
101         AbstractConnector connector = (AbstractConnector)muleContext.getRegistry().lookupConnector("connector.test.mule.default");
102         assertNotNull(connector);
103         assertTrue(connector.isStarted());
104         MessageReceiver[] receivers = connector.getReceivers("*started*");
105         assertEquals(1, receivers.length);
106         assertTrue(receivers[0].isConnected());
107 
108         // Stop service
109         c.stop();
110         assertFalse(c.isStarted());
111         assertFalse(c.isPaused());
112         assertTrue(c.isStopped());
113 
114         // Connector is still started, but no more receivers.
115         assertTrue(connector.isStarted());
116         receivers = connector.getReceivers("*started*");
117         assertEquals(0, receivers.length);
118     }
119     
120     @Test
121     public void testSendToStoppedComponent() throws Exception
122     {
123         Service c = muleContext.getRegistry().lookupService("stoppedComponent");
124         // Service initially stopped
125         assertFalse(c.isStarted());
126         assertFalse(c.isPaused());
127         assertTrue(c.isStopped());
128 
129         try
130         {
131             c.dispatchEvent(getTestEvent("hello", c));
132             fail();
133         }
134         catch (LifecycleException e)
135         {
136             // expected
137         }
138 
139         try
140         {
141             c.sendEvent(getTestEvent("hello", c));
142             fail();
143         }
144         catch (LifecycleException e)
145         {
146             // expected
147         }
148     }
149 
150     @Test
151     public void testInitialStatePaused() throws Exception
152     {
153         Service c = muleContext.getRegistry().lookupService("pausedComponent");
154         // Service initially started but paused.
155         assertFalse(c.isStarted());
156         assertTrue(c.isPaused());
157         assertFalse(c.isStopped());
158 
159         // The listeners should be registered and started.
160         AbstractConnector connector = (AbstractConnector)muleContext.getRegistry().lookupConnector("connector.test.mule.default");
161         assertNotNull(connector);
162         assertTrue(connector.isStarted());
163         MessageReceiver[] receivers = connector.getReceivers("*paused*");
164         assertEquals(1, receivers.length);
165         assertTrue(receivers[0].isConnected());
166     }
167 
168     @Test
169     public void testSendToPausedComponent() throws Exception
170     {
171         // TODO MULE-1995
172         final Service c = muleContext.getRegistry().lookupService("startedComponent");
173         assertTrue(c.isStarted());
174         assertFalse(c.isPaused());
175         assertFalse(c.isStopped());
176         
177         c.pause();
178         assertTrue(c.isPaused());
179         assertFalse(c.isStopped());
180         assertFalse(c.isStarted());
181         
182         new Thread(new Runnable()
183         {
184             public void run()
185             {
186                 try
187                 {
188                     Thread.sleep(2000);
189                 }
190                 catch (InterruptedException e)
191                 {
192                     Thread.currentThread().interrupt();
193                     throw new RuntimeException(e);
194                 }
195 
196                 try
197                 {
198                     c.resume();
199                 }
200                 catch (MuleException e)
201                 {
202                     fail(e.getMessage());
203                 }
204             }
205         }).start();
206         long t0 = System.currentTimeMillis();
207         c.sendEvent(getTestEvent("hello"));
208         long t1 = System.currentTimeMillis();
209         assertTrue(t1 - t0 > 1000);
210     }
211 }