1
2
3
4
5
6
7
8
9
10
11 package org.mule.model;
12
13 import org.mule.api.MuleException;
14 import org.mule.api.service.Service;
15 import org.mule.tck.junit4.AbstractMuleContextTestCase;
16
17 import org.junit.Test;
18
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22
23 public abstract class AbstractServiceTestCase extends AbstractMuleContextTestCase
24 {
25 protected abstract Service getService();
26
27 @Test
28 public void testStart() throws MuleException
29 {
30 try
31 {
32 getService().start();
33 fail("Exception expected: Cannot start an uninitialised service");
34 }
35 catch (Exception e)
36 {
37
38 }
39
40 getService().initialise();
41 getService().start();
42
43 try
44 {
45 getService().initialise();
46 fail("Exception expected: Cannot initialise an already initialised service");
47 }
48 catch (IllegalStateException e)
49 {
50
51 }
52 getService().dispose();
53
54 }
55
56 @Test
57 public void testPause() throws MuleException
58 {
59 assertFalse(getService().isStarted());
60 assertFalse(getService().isPaused());
61 assertFalse(getService().isStopped());
62
63 getService().initialise();
64
65
66 assertFalse(getService().isStarted());
67 assertFalse(getService().isPaused());
68 assertFalse(getService().isStopped());
69 try
70 {
71 getService().resume();
72 fail("cannot resume a service that is not paused");
73 }
74 catch (IllegalStateException e)
75 {
76
77 }
78 assertFalse(getService().isPaused());
79 getService().start();
80 assertTrue(getService().isStarted());
81 assertFalse(getService().isPaused());
82 assertFalse(getService().isStopped());
83 getService().pause();
84 assertTrue(getService().isPaused());
85 assertFalse(getService().isStarted());
86 assertFalse(getService().isStopped());
87 try
88 {
89 getService().pause();
90 fail("cannot pause a service that is already paused");
91 }
92 catch (IllegalStateException e)
93 {
94
95 }
96 assertTrue(getService().isPaused());
97 getService().dispose();
98
99 }
100
101 @Test
102 public void testResume() throws MuleException
103 {
104 assertFalse(getService().isStarted());
105 assertFalse(getService().isPaused());
106
107 getService().initialise();
108
109 assertFalse(getService().isStarted());
110 assertFalse(getService().isPaused());
111 try
112 {
113 getService().resume();
114 fail("cannot resume a service that is not paused");
115 }
116 catch (IllegalStateException e)
117 {
118
119 }
120 assertFalse(getService().isPaused());
121 getService().start();
122 assertTrue(getService().isStarted());
123 assertFalse(getService().isPaused());
124 try
125 {
126 getService().resume();
127 fail("cannot resume a service that is not paused");
128 }
129 catch (IllegalStateException e)
130 {
131
132 }
133 assertFalse(getService().isPaused());
134 getService().pause();
135 assertTrue(getService().isPaused());
136 getService().resume();
137 assertFalse(getService().isPaused());
138
139 assertTrue(getService().isStarted());
140 try
141 {
142 getService().resume();
143 fail("cannot resume a service that is not paused");
144 }
145 catch (IllegalStateException e)
146 {
147
148 }
149 assertFalse(getService().isPaused());
150 getService().dispose();
151
152 }
153
154 @Test
155 public void testStop() throws MuleException
156 {
157 assertFalse(getService().isStarted());
158 assertFalse(getService().isPaused());
159
160 try
161 {
162 getService().stop();
163 fail("Exception expected: Cannot stop an uninitialised service");
164 }
165 catch (IllegalStateException e)
166 {
167
168 }
169
170 try
171 {
172 getService().resume();
173 fail("Exception expected: Cannot resume an uninitialised service");
174 }
175 catch (IllegalStateException e)
176 {
177
178 }
179
180 getService().initialise();
181 assertFalse(getService().isStarted());
182
183
184 getService().stop();
185
186 assertFalse(getService().isStarted());
187 getService().start();
188 assertTrue(getService().isStarted());
189 getService().stop();
190 assertFalse(getService().isStarted());
191 try
192 {
193 getService().stop();
194 fail("Exception expected: Cannot stop a service that is not started");
195 }
196 catch (IllegalStateException e)
197 {
198
199 }
200 assertFalse(getService().isStarted());
201 getService().dispose();
202
203 }
204
205 @Test
206 public void testDispose() throws MuleException
207 {
208 assertFalse(getService().isStarted());
209 assertFalse(getService().isPaused());
210 getService().dispose();
211
212 try
213 {
214 getService().dispose();
215 fail("Exception expected: Cannot dispose a service that is already disposed");
216 }
217 catch (IllegalStateException e)
218 {
219
220 }
221
222 try
223 {
224 getService().initialise();
225 fail("Exception expected: Cannot invoke initialise (or any lifecycle) on an object once it is disposed");
226 }
227 catch (IllegalStateException e)
228 {
229
230 }
231 }
232
233 }