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.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              // expected
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              // expected
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          // Pausing a service that is not started does not throw an exception
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              // expected
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              // expected
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             // expected
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             // expected
128         }
129         assertFalse(getService().isPaused());
130         getService().pause();
131         assertTrue(getService().isPaused());
132         getService().resume();
133         assertFalse(getService().isPaused());
134         // Resume is a meta phase, so after pause, we go back to started
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             // expected
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             // expected
164         }
165 
166         try
167         {
168             getService().resume();
169             fail("Exception expected: Cannot resume an uninitialised service");
170         }
171         catch (IllegalStateException e)
172         {
173             // expected
174         }
175 
176         getService().initialise();
177         assertFalse(getService().isStarted());
178 
179         // Can stop a service that is not started
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             // expected
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             // expected
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             // expected
226         }
227     }
228 
229 }