1   /*
2    * $Id: AbstractServiceTestCase.java 11373 2008-03-15 05:03:10Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.lifecycle.InitialisationException;
15  import org.mule.api.service.Service;
16  import org.mule.tck.AbstractMuleTestCase;
17  
18  public abstract class AbstractServiceTestCase extends AbstractMuleTestCase
19  {
20  
21      protected Service service;
22  
23      public void testStart() throws MuleException
24      {
25          try
26          {
27              service.start();
28              fail("Exception expected: Cannot start an uninitialised service");
29          }
30          catch (Exception e)
31          {
32              // expected
33          }
34  
35          service.initialise();
36          service.start();
37  
38          try
39          {
40              service.initialise();
41              fail("Exception expected: Cannot initialise an already initialised service");
42          }
43          catch (InitialisationException e)
44          {
45              // expected
46          }
47          service.dispose();
48  
49      }
50  
51      public void testPause() throws MuleException
52      {
53          assertFalse(service.isStarted());
54          assertFalse(service.isPaused());
55  
56          service.initialise();
57  
58          // Pausing a service that is not started does not throw an exception
59          assertFalse(service.isStarted());
60          assertFalse(service.isPaused());
61          service.resume();
62          assertFalse(service.isPaused());
63          service.start();
64          assertTrue(service.isStarted());
65          assertFalse(service.isPaused());
66          service.pause();
67          assertTrue(service.isPaused());
68          service.pause();
69          assertTrue(service.isPaused());
70          service.dispose();
71  
72      }
73  
74      public void testResume() throws MuleException
75      {
76          assertFalse(service.isStarted());
77          assertFalse(service.isPaused());
78  
79          service.initialise();
80  
81          assertFalse(service.isStarted());
82          assertFalse(service.isPaused());
83          service.resume();
84          assertFalse(service.isPaused());
85          service.start();
86          assertTrue(service.isStarted());
87          assertFalse(service.isPaused());
88          service.resume();
89          assertFalse(service.isPaused());
90          service.pause();
91          assertTrue(service.isPaused());
92          service.resume();
93          assertFalse(service.isPaused());
94          service.resume();
95          assertFalse(service.isPaused());
96          service.dispose();
97  
98      }
99  
100     public void testStop() throws MuleException
101     {
102         assertFalse(service.isStarted());
103         assertFalse(service.isPaused());
104         service.stop();
105 
106         try
107         {
108             service.resume();
109             fail("Exception expected: Cannot stop an uninitialised service");
110         }
111         catch (MuleException e)
112         {
113             // expected
114         }
115 
116         service.initialise();
117         assertFalse(service.isStarted());
118 
119         service.stop();
120         assertFalse(service.isStarted());
121         service.start();
122         assertTrue(service.isStarted());
123         service.stop();
124         assertFalse(service.isStarted());
125         service.stop();
126         assertFalse(service.isStarted());
127         service.dispose();
128 
129     }
130 
131     public void testDispose() throws MuleException
132     {
133         assertFalse(service.isStarted());
134         assertFalse(service.isPaused());
135         service.dispose();
136 
137         service.initialise();
138         assertFalse(service.isStarted());
139 
140         service.dispose();
141         assertFalse(service.isStarted());
142         service.dispose();
143     }
144 
145 }