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