1   /*
2    * $Id: AbstractComponentTestCase.java 10296 2008-01-14 13:41:46Z holger $
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  
12  package org.mule.impl.model;
13  
14  import org.mule.tck.AbstractMuleTestCase;
15  import org.mule.umo.UMOComponent;
16  import org.mule.umo.UMOException;
17  import org.mule.umo.lifecycle.InitialisationException;
18  
19  public abstract class AbstractComponentTestCase extends AbstractMuleTestCase
20  {
21  
22      protected UMOComponent component;
23  
24      public void testStart() throws UMOException
25      {
26          try
27          {
28              component.start();
29              fail("Exception expected: Cannot start an uninitialised component");
30          }
31          catch (UMOException e)
32          {
33              // expected 
34          }
35          catch (NullPointerException npe)
36          {
37              // TODO MULE-2843
38          }
39  
40          component.initialise();
41          component.start();
42  
43          try
44          {
45              component.initialise();
46              fail("Exception expected: Cannot initialise an already initialised component");
47          }
48          catch (InitialisationException e)
49          {
50              // expected
51          }
52  
53      }
54  
55      public void testPause() throws UMOException
56      {
57          assertFalse(component.isStarted());
58          assertFalse(component.isPaused());
59  
60          try
61          {
62              component.pause();
63              fail("Exception expected: Cannot pause an uninitialised component");
64          }
65          catch (UMOException e)
66          {
67              // expected 
68          }
69          catch (NullPointerException npe)
70          {
71              // TODO MULE-2843
72          }
73  
74          component.initialise();
75  
76          // Pausing a component that is not started does not throw an exception
77          assertFalse(component.isStarted());
78          assertFalse(component.isPaused());
79          component.resume();
80          assertFalse(component.isPaused());
81          component.start();
82          assertTrue(component.isStarted());
83          assertFalse(component.isPaused());
84          component.pause();
85          assertTrue(component.isPaused());
86          component.pause();
87          assertTrue(component.isPaused());
88      }
89  
90      public void testResume() throws UMOException
91      {
92          assertFalse(component.isStarted());
93          assertFalse(component.isPaused());
94  
95          try
96          {
97              component.resume();
98              fail("Exception expected: Cannot resume an uninitialised component");
99          }
100         catch (UMOException e)
101         {
102             // expected 
103         }
104         catch (NullPointerException npe)
105         {
106             // TODO MULE-2843
107         }
108 
109         component.initialise();
110 
111         assertFalse(component.isStarted());
112         assertFalse(component.isPaused());
113         component.resume();
114         assertFalse(component.isPaused());
115         component.start();
116         assertTrue(component.isStarted());
117         assertFalse(component.isPaused());
118         component.resume();
119         assertFalse(component.isPaused());
120         component.pause();
121         assertTrue(component.isPaused());
122         component.resume();
123         assertFalse(component.isPaused());
124         component.resume();
125         assertFalse(component.isPaused());
126     }
127 
128     public void testStop() throws UMOException
129     {
130         assertFalse(component.isStarted());
131         assertFalse(component.isPaused());
132 
133         try
134         {
135             component.resume();
136             fail("Exception expected: Cannot stop an uninitialised component");
137         }
138         catch (UMOException e)
139         {
140             // expected 
141         }
142         catch (NullPointerException npe)
143         {
144             // TODO MULE-2843
145         }
146 
147         component.initialise();
148         assertFalse(component.isStarted());
149 
150         component.stop();
151         assertFalse(component.isStarted());
152         component.start();
153         assertTrue(component.isStarted());
154         component.stop();
155         assertFalse(component.isStarted());
156         component.stop();
157         assertFalse(component.isStarted());
158 
159     }
160 
161     public void testDispose() throws UMOException
162     {
163         assertFalse(component.isStarted());
164         assertFalse(component.isPaused());
165 
166         try
167         {
168             component.dispose();
169             fail("Exception expected: Cannot dispose an uninitialised component");
170         }
171         catch (Exception e)
172         {
173             // expected 
174         }
175 
176         component.initialise();
177         assertFalse(component.isStarted());
178 
179         component.dispose();
180         assertFalse(component.isStarted());
181         try
182         {
183             component.dispose();
184             fail("Exception expected: Component has already been disposed");
185         }
186         catch (Exception e)
187         {
188             // expected 
189         }
190     }
191 
192 }