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