View Javadoc

1   /*
2    * $Id: AbstractServiceTestCase.java 19292 2010-09-02 01:35:19Z ddossot $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.model;
12  
13  import org.mule.api.MuleException;
14  import org.mule.api.service.Service;
15  import org.mule.tck.AbstractMuleTestCase;
16  
17  public abstract class AbstractServiceTestCase extends AbstractMuleTestCase
18  {
19      protected abstract Service getService();
20  
21      public void testStart() throws MuleException
22      {
23          try
24          {
25              getService().start();
26              fail("Exception expected: Cannot start an uninitialised service");
27          }
28          catch (Exception e)
29          {
30              // expected
31          }
32  
33          getService().initialise();
34          getService().start();
35  
36          try
37          {
38              getService().initialise();
39              fail("Exception expected: Cannot initialise an already initialised service");
40          }
41          catch (IllegalStateException e)
42          {
43              // expected
44          }
45          getService().dispose();
46  
47      }
48  
49      public void testPause() throws MuleException
50      {
51          assertFalse(getService().isStarted());
52          assertFalse(getService().isPaused());
53  
54          getService().initialise();
55  
56          // Pausing a service that is not started does not throw an exception
57          assertFalse(getService().isStarted());
58          assertFalse(getService().isPaused());
59          try
60          {
61              getService().resume();
62              fail("cannot resume a service that is not paused");
63          }
64          catch (IllegalStateException e)
65          {
66              // expected
67          }
68          assertFalse(getService().isPaused());
69          getService().start();
70          assertTrue(getService().isStarted());
71          assertFalse(getService().isPaused());
72          getService().pause();
73          assertTrue(getService().isPaused());
74          try
75          {
76              getService().pause();
77              fail("cannot pause a service that is already paused");
78          }
79          catch (IllegalStateException e)
80          {
81              // expected
82          }
83          assertTrue(getService().isPaused());
84          getService().dispose();
85  
86      }
87  
88      public void testResume() throws MuleException
89      {
90          assertFalse(getService().isStarted());
91          assertFalse(getService().isPaused());
92  
93          getService().initialise();
94  
95          assertFalse(getService().isStarted());
96          assertFalse(getService().isPaused());
97          try
98          {
99              getService().resume();
100             fail("cannot resume a service that is not paused");
101         }
102         catch (IllegalStateException e)
103         {
104             // expected
105         }
106         assertFalse(getService().isPaused());
107         getService().start();
108         assertTrue(getService().isStarted());
109         assertFalse(getService().isPaused());
110         try
111         {
112             getService().resume();
113             fail("cannot resume a service that is not paused");
114         }
115         catch (IllegalStateException e)
116         {
117             // expected
118         }
119         assertFalse(getService().isPaused());
120         getService().pause();
121         assertTrue(getService().isPaused());
122         getService().resume();
123         assertFalse(getService().isPaused());
124         // Resume is a meta phase, so after pause, we go back to started
125         assertTrue(getService().isStarted());
126         try
127         {
128             getService().resume();
129             fail("cannot resume a service that is not paused");
130         }
131         catch (IllegalStateException e)
132         {
133             // expected
134         }
135         assertFalse(getService().isPaused());
136         getService().dispose();
137 
138     }
139 
140     public void testStop() throws MuleException
141     {
142         assertFalse(getService().isStarted());
143         assertFalse(getService().isPaused());
144 
145         try
146         {
147             getService().stop();
148             fail("Exception expected: Cannot stop an uninitialised service");
149         }
150         catch (IllegalStateException e)
151         {
152             // expected
153         }
154 
155         try
156         {
157             getService().resume();
158             fail("Exception expected: Cannot resume an uninitialised service");
159         }
160         catch (IllegalStateException e)
161         {
162             // expected
163         }
164 
165         getService().initialise();
166         assertFalse(getService().isStarted());
167 
168         // Can stop a service that is not started
169         getService().stop();
170 
171         assertFalse(getService().isStarted());
172         getService().start();
173         assertTrue(getService().isStarted());
174         getService().stop();
175         assertFalse(getService().isStarted());
176         try
177         {
178             getService().stop();
179             fail("Exception expected: Cannot stop a service that is not started");
180         }
181         catch (IllegalStateException e)
182         {
183             // expected
184         }
185         assertFalse(getService().isStarted());
186         getService().dispose();
187 
188     }
189 
190     public void testDispose() throws MuleException
191     {
192         assertFalse(getService().isStarted());
193         assertFalse(getService().isPaused());
194         getService().dispose();
195 
196         try
197         {
198             getService().dispose();
199             fail("Exception expected: Cannot dispose a service that is already disposed");
200         }
201         catch (IllegalStateException e)
202         {
203             // expected
204         }
205 
206         try
207         {
208             getService().initialise();
209             fail("Exception expected: Cannot invoke initialise (or any lifecycle) on an object once it is disposed");
210         }
211         catch (IllegalStateException e)
212         {
213             // expected
214         }
215     }
216 
217 }