KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > component > lifecycle > test > ExternalLifecycleTest


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.component.lifecycle.test;
19
20 import org.sape.carbon.core.component.Component;
21 import org.sape.carbon.core.component.ComponentConfiguration;
22 import org.sape.carbon.core.component.Lookup;
23 import org.sape.carbon.core.component.lifecycle.ComponentUnavailableException;
24 import org.sape.carbon.core.component.lifecycle.LifecycleInterceptor;
25 import org.sape.carbon.core.component.lifecycle.LifecycleStateEnum;
26 import org.sape.carbon.core.component.lifecycle.StateTransitionException;
27 import org.sape.carbon.core.config.Config;
28
29 import junit.extensions.ActiveTestSuite;
30 import junit.framework.Test;
31 import junit.framework.TestCase;
32 import junit.framework.TestSuite;
33
34 /**
35  * Blackbox testing for Lifecycle functionality. Interacts with a test
36  * component through the ComponentProxy. Checks for proper behavior
37  * of the proxy while under non-running lifecycle states.
38  *
39  * Copyright 2002 Sapient
40  * @since carbon 1.0
41  * @author Chris Herron, Febuary 2002
42  * @version $Revision: 1.11 $($Author: dvoet $ / $Date: 2003/05/05 21:21:13 $)
43  */

44 public class ExternalLifecycleTest extends TestCase {
45     public ExternalLifecycleTest(String JavaDoc name) {
46         super(name);
47     }
48
49     private Component fetchTestComponent() {
50         return Lookup.getInstance().fetchComponent(TEST_COMPONENT_NAME);
51     }
52
53
54     /**
55      *
56      */

57     public void testCallWhileSuspended() {
58         TestComponent component = (TestComponent) fetchTestComponent();
59         long endTime = System.currentTimeMillis() + 2000;
60         ((LifecycleInterceptor) component).suspendComponent();
61         try {
62             component.doSomething();
63
64             TestCase.fail(
65                 "Did not catch expected ComponentUnavailableException");
66         } catch (ComponentUnavailableException cue) {
67             if (System.currentTimeMillis() < endTime) {
68                 TestCase.fail(
69                     "Component threw ComponentUnavailableException too soon");
70             }
71         } finally {
72             ((LifecycleInterceptor) component).resumeComponent();
73         }
74     }
75
76     /**
77      *
78      */

79     public void testCallWhileStopped() {
80         TestComponent component = (TestComponent) fetchTestComponent();
81         ((LifecycleInterceptor) component).stopComponent();
82         try {
83             component.doSomething();
84
85             TestCase.fail(
86                 "Did not catch expected ComponentUnavailableException");
87         } catch (ComponentUnavailableException cue) {
88             // expected
89
} finally {
90             ((LifecycleInterceptor) component).startComponent();
91         }
92     }
93
94     public void testCallWhileDestroyed() {
95         TestComponent component = (TestComponent) fetchTestComponent();
96
97         Lookup.getInstance().getComponentKeeper().
98             destroyComponent(TEST_COMPONENT_NAME);
99
100         try {
101             component.doSomething();
102
103             TestCase.fail(
104                 "Did not catch expected ComponentUnavailableException");
105         } catch (ComponentUnavailableException cue) {
106             // expected
107
}
108     }
109
110     public void testDestroyOnException() {
111         DestroyOnExceptionTestComponent component = getTestComponent();
112
113         component.crashOnSuspend();
114         try {
115             ((LifecycleInterceptor) component).suspendComponent();
116         } catch (StateTransitionException ste) {
117             // expected
118
}
119         checkComponent(component, LifecycleStateEnum.DESTROYED);
120
121         component = getTestComponent();
122         component.crashOnResume();
123         ((LifecycleInterceptor) component).suspendComponent();
124         try {
125             ((LifecycleInterceptor) component).resumeComponent();
126         } catch (StateTransitionException ste) {
127             // expected
128
}
129         checkComponent(component, LifecycleStateEnum.DESTROYED);
130
131         component = getTestComponent();
132         component.crashOnStop();
133         try {
134             ((LifecycleInterceptor) component).stopComponent();
135         } catch (StateTransitionException ste) {
136             // expected
137
}
138         checkComponent(component, LifecycleStateEnum.DESTROYED);
139
140         component = getTestComponent();
141         component.crashOnStart();
142         ((LifecycleInterceptor) component).stopComponent();
143         try {
144             ((LifecycleInterceptor) component).startComponent();
145         } catch (StateTransitionException ste) {
146             // expected
147
}
148         checkComponent(component, LifecycleStateEnum.DESTROYED);
149
150         component = getTestComponent();
151         component.crashOnConfigure();
152         try {
153             ((LifecycleInterceptor) component).configureComponent(
154                 (ComponentConfiguration) Config.getInstance().
155                 createConfiguration(ComponentConfiguration.class));
156
157         } catch (StateTransitionException ste) {
158             // expected
159
}
160         checkComponent(component, LifecycleStateEnum.DESTROYED);
161
162     }
163
164     public void testNonFatalExceptionHandling() {
165         DestroyOnExceptionTestComponent component = getNFTestComponent();
166
167         component.crashOnSuspend();
168         try {
169             ((LifecycleInterceptor) component).suspendComponent();
170         } catch (StateTransitionException ste) {
171             // expected
172
}
173         checkComponent(component, LifecycleStateEnum.RUNNING);
174
175         component = getNFTestComponent();
176         component.crashOnResume();
177         ((LifecycleInterceptor) component).suspendComponent();
178         try {
179             ((LifecycleInterceptor) component).resumeComponent();
180         } catch (StateTransitionException ste) {
181             // expected
182
}
183         checkComponent(component, LifecycleStateEnum.SUSPENDED);
184
185         component = getNFTestComponent();
186         component.crashOnStop();
187         try {
188             ((LifecycleInterceptor) component).stopComponent();
189         } catch (StateTransitionException ste) {
190             // expected
191
}
192         checkComponent(component, LifecycleStateEnum.RUNNING);
193
194         component = getNFTestComponent();
195         component.crashOnStart();
196         ((LifecycleInterceptor) component).stopComponent();
197         try {
198             ((LifecycleInterceptor) component).startComponent();
199         } catch (StateTransitionException ste) {
200             // expected
201
}
202         checkComponent(component, LifecycleStateEnum.STOPPED);
203
204         component = getNFTestComponent();
205         component.crashOnConfigure();
206         ((LifecycleInterceptor) component).stopComponent();
207         try {
208             ((LifecycleInterceptor) component).configureComponent(
209                 (ComponentConfiguration) Config.getInstance().
210                 createConfiguration(ComponentConfiguration.class));
211
212         } catch (StateTransitionException ste) {
213             // expected
214
}
215         checkComponent(component, LifecycleStateEnum.STOPPED);
216
217         component = getNFTestComponent();
218         component.crashOnConfigure();
219         try {
220             ((LifecycleInterceptor) component).configureComponent(
221                 (ComponentConfiguration) Config.getInstance().
222                 createConfiguration(ComponentConfiguration.class));
223
224         } catch (StateTransitionException ste) {
225             // expected
226
}
227         checkComponent(component, LifecycleStateEnum.SUSPENDED);
228
229         component = getNFTestComponent();
230         component.crashOnResume();
231         try {
232             ((LifecycleInterceptor) component).configureComponent(
233                 (ComponentConfiguration) Config.getInstance().
234                 createConfiguration(ComponentConfiguration.class));
235
236         } catch (StateTransitionException ste) {
237             // expected
238
}
239         checkComponent(component, LifecycleStateEnum.SUSPENDED);
240
241     }
242
243     public void testInternalComponentReference() {
244         TestComponent testComponent = (TestComponent) fetchTestComponent();
245         TestCase.assertTrue(
246             "Internal component self-reference did not == the external " +
247             "component reference",
248             testComponent == testComponent.getThisComponent());
249     }
250
251     private DestroyOnExceptionTestComponent getTestComponent() {
252         return (DestroyOnExceptionTestComponent) Lookup.getInstance().
253             fetchComponent(TEST_DESTROY_ON_EXCPETION_COMPONENT_NAME);
254     }
255
256     private DestroyOnExceptionTestComponent getNFTestComponent() {
257         Lookup.getInstance().getComponentKeeper().destroyComponent(
258             TEST_NON_FATAL_EXCPETION_COMPONENT_NAME);
259
260         return (DestroyOnExceptionTestComponent) Lookup.getInstance().
261             fetchComponent(TEST_NON_FATAL_EXCPETION_COMPONENT_NAME);
262     }
263
264     private void checkComponent(
265         DestroyOnExceptionTestComponent component,
266         LifecycleStateEnum expectedState) {
267
268         if (((LifecycleInterceptor) component).getLifecycleState() !=
269             expectedState) {
270
271             fail("Component was not in expected state, state was ["
272                 + ((LifecycleInterceptor) component).getLifecycleState()
273                 + "] expected state was ["
274                 + expectedState
275                 + "]");
276         }
277     }
278
279     private static final String JavaDoc TEST_COMPONENT_NAME =
280         "/core/test/LifecycleTestComponent";
281     private static final String JavaDoc TEST_DESTROY_ON_EXCPETION_COMPONENT_NAME =
282         "/core/test/DestroyOnExceptionTestComponent";
283     private static final String JavaDoc TEST_NON_FATAL_EXCPETION_COMPONENT_NAME =
284         "/core/test/NonFatalExceptionTestComponent";
285
286     /** Method called by jUnit to get all the tests in this test case */
287     public static Test suite() {
288         TestSuite masterSuite = new TestSuite();
289         // add single threaded tests
290
Test singleThreadedTests = getSingleThreadedTests();
291         if (singleThreadedTests != null) {
292             masterSuite.addTest(singleThreadedTests);
293         }
294         // add multi threaded tests
295
Test multiThreadedTests = getMultiThreadedTests();
296         if (multiThreadedTests != null) {
297             masterSuite.addTest(multiThreadedTests);
298         }
299         return masterSuite;
300     }
301
302     /**
303      * This method is used within the suite method to get all of the single threaded tests.
304      * Add all your single threaded tests in this method with a line like: suite.addTest(new LifecycleTest("testFunction1"));
305      */

306     private static Test getSingleThreadedTests() {
307         TestSuite suite = new TestSuite();
308
309         suite.addTest(new ExternalLifecycleTest("testInternalComponentReference"));
310         suite.addTest(new ExternalLifecycleTest("testCallWhileSuspended"));
311         suite.addTest(new ExternalLifecycleTest("testCallWhileStopped"));
312         suite.addTest(new ExternalLifecycleTest("testCallWhileDestroyed"));
313         suite.addTest(new ExternalLifecycleTest("testDestroyOnException"));
314         suite.addTest(new ExternalLifecycleTest("testNonFatalExceptionHandling"));
315
316         return suite;
317     }
318
319     /**
320      * This method is used within the suite method to get all of the multi threaded tests.
321      * Add all your multi threaded tests in this method with a line like: addTest(suite, "testFunction1", 5);
322      */

323     private static Test getMultiThreadedTests() {
324         TestSuite suite = new ActiveTestSuite();
325
326         /*
327          * add your tests here following these examples:
328          *
329          * addTest(suite, "testFunction1", 5);
330          * addTest(suite, "testFunction2", 10);
331          */

332
333         return suite;
334     }
335
336     /**
337      * This method will add the give test to the give suite the specified
338      * number of times. This is best used for multi-threaded tests where
339      * suite is an instance of ActiveTestSuite and you want to run the same test in multiple threads.
340      */

341     private static void addTest(TestSuite suite, String JavaDoc testName, int number) {
342         for (int count = 0; count < number; count++) {
343             suite.addTest(new ExternalLifecycleTest(testName));
344         }
345     }
346 }
347
Popular Tags