1
2
3
4
5
6
7
8
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.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
36 assertTrue(c.isStarted());
37 assertFalse(c.isPaused());
38
39
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
49 public void testInitialStateStopped() throws Exception
50 {
51 Service c = muleContext.getRegistry().lookupService("stoppedComponent");
52 assertEquals("stopped", c.getInitialState());
53
54 assertFalse(c.isStarted());
55
56
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
64 c.start();
65 assertTrue(c.isStarted());
66
67
68 assertTrue(connector.isStarted());
69 receivers = connector.getReceivers("*stopped*");
70 assertEquals(1, receivers.length);
71 assertTrue(receivers[0].isConnected());
72 }
73
74
75 public void testStoppingComponentStopsEndpoints() throws Exception
76 {
77 Service c = muleContext.getRegistry().lookupService("startedComponent");
78 assertTrue(c.isStarted());
79
80
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
89 c.stop();
90 assertFalse(c.isStarted());
91
92
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
102 assertFalse(c.isStarted());
103
104 try
105 {
106 c.dispatchEvent(getTestEvent("hello", c));
107 fail();
108 }
109 catch (LifecycleException e)
110 {
111
112 }
113
114 try
115 {
116 c.sendEvent(getTestEvent("hello", c));
117 fail();
118 }
119 catch (LifecycleException e)
120 {
121
122 }
123 }
124
125 public void testInitialStatePaused() throws Exception
126 {
127 Service c = muleContext.getRegistry().lookupService("pausedComponent");
128
129 assertTrue(c.isStarted());
130 assertTrue(c.isPaused());
131
132
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
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 }