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