KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.Method JavaDoc;
21
22 import org.sape.carbon.core.component.ComponentConfiguration;
23 import org.sape.carbon.core.component.FunctionalInterface;
24 import org.sape.carbon.core.component.lifecycle.DefaultLifecycleInterceptor;
25 import org.sape.carbon.core.component.lifecycle.LifecycleInterceptor;
26 import org.sape.carbon.core.component.lifecycle.LifecycleStateEnum;
27 import org.sape.carbon.core.component.proxy.ComponentProxyInvocationHandler;
28 import org.sape.carbon.core.component.proxy.Decorator;
29 import org.sape.carbon.core.util.thread.ReadWriteLock;
30 import org.sape.carbon.core.util.thread.ReentrantWriterPreferenceReadWriteLock;
31
32 import junit.extensions.ActiveTestSuite;
33 import junit.framework.Test;
34 import junit.framework.TestCase;
35 import junit.framework.TestSuite;
36
37 /**
38  * Whitebox-tests the LifecycleInterceptor's manipulation of the Component by
39  * directly calling it. LifecycleInterceptor would usually be invoked by the
40  * ComponentProxy. Each test prepares a new Component and associates a
41  * new LifecycleInterceptor with it, placing it in the desired entry-state
42  * before testing.
43  *
44  * Copyright 2002 Sapient
45  * @since carbon 1.0
46  * @author Chris Herron, Febuary 2002
47  * @version $Revision: 1.18 $($Author: dvoet $ / $Date: 2003/05/05 21:21:13 $)
48  */

49 public class InternalLifecycleTest extends TestCase implements TestListener {
50
51     private boolean configureCalled = false;
52     private boolean destroyCalled = false;
53     private boolean initializeCalled = false;
54     private boolean resumeCalled = false;
55     private boolean startCalled = false;
56     private boolean stopCalled = false;
57     private boolean suspendCalled = false;
58
59     public InternalLifecycleTest(String JavaDoc name) {
60         super(name);
61     }
62
63     /**
64      * Create a Component F.I. instance to run tests against
65      */

66     private FunctionalInterface prepareComponentFunctionalImpl() {
67         return new TestComponentImpl(this);
68     }
69
70     private ComponentProxyInvocationHandler prepareComponentProxy() {
71         return new ComponentProxyInvocationHandler() {
72
73             public void addDecorator(Decorator decorator) {
74             }
75
76             public void setFunctionalImplementation(
77                     Class JavaDoc[] implementedInterfaces,
78                     FunctionalInterface functionalImplementation) {
79             }
80
81             public Object JavaDoc getDelegate(Class JavaDoc delegateInterface) {
82                 return null;
83             }
84
85             public ReadWriteLock getMonitor() {
86                 return new ReentrantWriterPreferenceReadWriteLock();
87             }
88
89             public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) {
90                 return null;
91             }
92
93             public String JavaDoc getComponentName() {
94                 return null;
95             }
96
97             public void setComponentName(String JavaDoc name) {
98             }
99         };
100     }
101
102     /**
103      * Create a LifecycleInterceptor to run tests against
104      */

105     private LifecycleInterceptor prepareLifecycleInterceptor() {
106         return new DefaultLifecycleInterceptor(
107             prepareComponentFunctionalImpl(),
108             prepareComponentProxy(),
109             null);
110     }
111
112     /**
113      * Test that a LifecycleInterceptor starts in CREATING state when
114      * instantiated, and that it moves to STOPPED state when asked to
115      * initialize the component.
116      */

117     public void testInitialize() {
118         LifecycleInterceptor lifecycleInterceptor = prepareLifecycleInterceptor();
119
120         //check that Component (LifecycleInterceptor) begins in CREATING state
121
TestCase.assertTrue(
122             "Component+Interceptor did not begin in CREATING state",
123             lifecycleInterceptor.getLifecycleState()
124                 == LifecycleStateEnum.CREATING);
125
126         //Initialize the Component
127
lifecycleInterceptor.initializeComponent(null);
128
129         //check that Component ends in STOPPED state
130
TestCase.assertTrue(
131             "Component did not end up in STOPPED state",
132             lifecycleInterceptor.getLifecycleState() == LifecycleStateEnum.STOPPED);
133
134         TestCase.assertTrue(
135             "initialize method on functional implementation was not called",
136             this.initializeCalled);
137
138     }
139
140     /**
141      * Test that the component moves to RUNNING state when LifecycleInterceptor
142      * is asked to start the component.
143      */

144     public void testStart() {
145         LifecycleInterceptor lifecycleInterceptor = prepareLifecycleInterceptor();
146
147         //Initialize the component to bring it to STOPPED
148
lifecycleInterceptor.initializeComponent(null);
149
150         //Start the component to bring it to RUNNING
151
lifecycleInterceptor.startComponent();
152
153         //check that Component moves to RUNNING state
154
TestCase.assertTrue(
155             "Component did not end up in RUNNING state",
156             lifecycleInterceptor.getLifecycleState() == LifecycleStateEnum.RUNNING);
157
158         TestCase.assertTrue(
159             "start method on functional implementation was not called",
160             this.startCalled);
161     }
162
163     /**
164      * Test that the component moves to STOPPED state when LifecycleInterceptor
165      * is asked to stop the component.
166      */

167     public void testStop() {
168         LifecycleInterceptor lifecycleInterceptor = prepareLifecycleInterceptor();
169
170         //Initialize the component to bring it to STOPPED
171
lifecycleInterceptor.initializeComponent(null);
172         //Start the component to bring it to RUNNING
173
lifecycleInterceptor.startComponent();
174
175         //Stop the component to move it to STOPPED
176
lifecycleInterceptor.stopComponent();
177
178         //check that Component moves to STOPPED state
179
TestCase.assertTrue(
180             "Component did not end up in STOPPED state",
181             lifecycleInterceptor.getLifecycleState() == LifecycleStateEnum.STOPPED);
182
183         TestCase.assertTrue(
184             "stop method on functional implementation was not called",
185             this.stopCalled);
186     }
187
188     /**
189      * Test that the component moves to SUSPENDED state when LifecycleInterceptor
190      * is asked to suspend the component.
191      */

192     public void testSuspend() {
193         LifecycleInterceptor lifecycleInterceptor = prepareLifecycleInterceptor();
194
195         //Initialize the component to bring it to STOPPED
196
lifecycleInterceptor.initializeComponent(null);
197         //Start the component to bring it to RUNNING
198
lifecycleInterceptor.startComponent();
199
200         //Suspend the component to move it to SUSPENDED
201
lifecycleInterceptor.suspendComponent();
202
203         //check that Component moves to SUSPENDED state
204
TestCase.assertTrue(
205             "Component did not end up in SUSPENDED state",
206             lifecycleInterceptor.getLifecycleState()
207                 == LifecycleStateEnum.SUSPENDED);
208
209         TestCase.assertTrue(
210             "suspend method on functional implementation was not called",
211             this.suspendCalled);
212     }
213
214     /**
215      * Test that the component moves to RUNNING state when LifecycleInterceptor
216      * is asked to resume the component.
217      */

218     public void testResume() {
219         LifecycleInterceptor lifecycleInterceptor = prepareLifecycleInterceptor();
220
221         //Initialize the component to bring it to STOPPED
222
lifecycleInterceptor.initializeComponent(null);
223         //Start the component to bring it to RUNNING
224
lifecycleInterceptor.startComponent();
225
226         //Suspend the component to move it to SUSPENDED
227
lifecycleInterceptor.suspendComponent();
228
229         //SResume the component to move it to RUNNING
230
lifecycleInterceptor.resumeComponent();
231
232         //check that Component moves to RUNNING state
233
TestCase.assertTrue(
234             "Component did not end up in RUNNING state",
235             lifecycleInterceptor.getLifecycleState() == LifecycleStateEnum.RUNNING);
236
237         TestCase.assertTrue(
238             "resume method on functional implementation was not called",
239             this.resumeCalled);
240     }
241
242     /**
243      * Test that the component returns to RUNNING state
244      * when LifecycleInterceptor is asked to configure the component.
245      */

246     public void testConfigureWhileRunning() {
247         LifecycleInterceptor lifecycleInterceptor = prepareLifecycleInterceptor();
248         //fetch a test config implementation class
249
ComponentConfiguration config = null;
250
251         //Initialize the component to bring it to STOPPED
252
lifecycleInterceptor.initializeComponent(null);
253         //Start the component to bring it to RUNNING
254
lifecycleInterceptor.startComponent();
255
256         //check that Component is in RUNNING state before test
257
TestCase.assertTrue(
258             "Component not in RUNNING state prior to config test",
259             lifecycleInterceptor.getLifecycleState() == LifecycleStateEnum.RUNNING);
260
261         //Configure the Component and expect it to return to RUNNING
262
lifecycleInterceptor.configureComponent(config);
263
264         //check that Component returns to RUNNING state
265
TestCase.assertTrue(
266             "Component did not return RUNNING state. "
267                 + "Was attempting configuration while RUNNING",
268             lifecycleInterceptor.getLifecycleState() == LifecycleStateEnum.RUNNING);
269
270         TestCase.assertTrue(
271             "configure method on functional implementation was not called",
272             this.configureCalled);
273     }
274
275     /**
276      * Test that the component returns to STOPPED state
277      * when LifecycleInterceptor is asked to configure the component.
278      */

279     public void testConfigureWhileStopped() {
280         LifecycleInterceptor lifecycleInterceptor = prepareLifecycleInterceptor();
281
282         //fetch a test config implementation class
283
ComponentConfiguration config = null;
284
285         //Initialize the component to bring it to STOPPED
286
lifecycleInterceptor.initializeComponent(null);
287         //Start the component to bring it to RUNNING
288
lifecycleInterceptor.startComponent();
289
290         //Stop the component to bring it to STOPPED
291
lifecycleInterceptor.stopComponent();
292
293         //check that Component is in STOPPED state before test
294
TestCase.assertTrue(
295             "Component not in STOPPED state prior to config test",
296             lifecycleInterceptor.getLifecycleState() == LifecycleStateEnum.STOPPED);
297
298         //Configure the Component and expect it to return to STOPPED
299
lifecycleInterceptor.configureComponent(config);
300
301         //check that Component returns to STOPPED state
302
TestCase.assertTrue(
303             "Component did not return STOPPED state. "
304                 + "Was attempting configuration while STOPPED",
305             lifecycleInterceptor.getLifecycleState() == LifecycleStateEnum.STOPPED);
306
307         TestCase.assertTrue(
308             "configure method on functional implementation was not called",
309             this.configureCalled);
310     }
311
312     /**
313      * Test that the component returns to SUSPENDED state
314      * when LifecycleInterceptor is asked to configure the component.
315      */

316     public void testConfigureWhileSuspended() {
317
318         LifecycleInterceptor lifecycleInterceptor = prepareLifecycleInterceptor();
319
320         //fetch a test config implementation class
321
ComponentConfiguration config = null;
322
323         //Initialize the component to bring it to STOPPED
324
lifecycleInterceptor.initializeComponent(null);
325         //Start the component to bring it to RUNNING
326
lifecycleInterceptor.startComponent();
327
328         //Suspend the component to bring it to SUSPENDED
329
lifecycleInterceptor.suspendComponent();
330
331         //check that Component is in SUSPENDED state before test
332
TestCase.assertTrue(
333             "Component not in SUSPENDED state prior to config test",
334             lifecycleInterceptor.getLifecycleState()
335                 == LifecycleStateEnum.SUSPENDED);
336
337         //Configure the Component and expect it to return to SUSPENDED
338
lifecycleInterceptor.configureComponent(config);
339
340         //check that Component returns to SUSPENDED state
341
TestCase.assertTrue(
342             "Component did not return to SUSPENDED state. "
343                 + "Was attempting configuration while SUSPENDED",
344             lifecycleInterceptor.getLifecycleState()
345                 == LifecycleStateEnum.SUSPENDED);
346
347         TestCase.assertTrue(
348             "configure method on functional implementation was not called",
349             this.configureCalled);
350     }
351
352     /**
353      * Test that the component moves to DESTROYED state
354      * when LifecycleInterceptor is asked to destroy the component.
355      */

356     public void testDestroyWhileRunning() {
357
358         LifecycleInterceptor lifecycleInterceptor = prepareLifecycleInterceptor();
359
360         //Initialize the component to bring it to STOPPED
361
lifecycleInterceptor.initializeComponent(null);
362         //Start the component to bring it to RUNNING
363
lifecycleInterceptor.startComponent();
364
365         //check that Component is in STOPPED state before test
366
TestCase.assertTrue(
367             "Component not in STOPPED state prior to destroy test",
368             lifecycleInterceptor.getLifecycleState() == LifecycleStateEnum.RUNNING);
369
370         //Destroy the Component and expect it to move to DESTROYED
371
lifecycleInterceptor.destroyComponent();
372
373         //check that Component moved to DESTROYED state
374
TestCase.assertTrue(
375             "Component did not move to DESTROYED state.",
376             lifecycleInterceptor.getLifecycleState()
377                 == LifecycleStateEnum.DESTROYED);
378
379         TestCase.assertTrue(
380             "stop method on functional implementation was not called",
381             this.stopCalled);
382
383         TestCase.assertTrue(
384             "destroy method on functional implementation was not called",
385             this.destroyCalled);
386     }
387
388     /**
389      * Test that the component moves to DESTROYED state
390      * when LifecycleInterceptor is asked to destroy the component.
391      */

392     public void testDestroyWhileStopped() {
393
394         LifecycleInterceptor lifecycleInterceptor = prepareLifecycleInterceptor();
395
396         //Initialize the component to bring it to STOPPED
397
lifecycleInterceptor.initializeComponent(null);
398         //Start the component to bring it to RUNNING
399
lifecycleInterceptor.startComponent();
400         //Stop the component to bring it to SUSPENDED
401
lifecycleInterceptor.stopComponent();
402
403         //check that Component is in STOPPED state before test
404
TestCase.assertTrue(
405             "Component not in STOPPED state prior to destroy test",
406             lifecycleInterceptor.getLifecycleState() == LifecycleStateEnum.STOPPED);
407
408         //Destroy the Component and expect it to move to DESTROYED
409
lifecycleInterceptor.destroyComponent();
410
411         //check that Component moved to DESTROYED state
412
TestCase.assertTrue(
413             "Component did not move to DESTROYED state.",
414             lifecycleInterceptor.getLifecycleState()
415                 == LifecycleStateEnum.DESTROYED);
416
417         TestCase.assertTrue(
418             "destroy method on functional implementation was not called",
419             this.destroyCalled);
420     }
421
422     /** Method called by jUnit to get all the tests in this test case */
423     public static Test suite() {
424         TestSuite masterSuite = new TestSuite();
425         // add single threaded tests
426
Test singleThreadedTests = getSingleThreadedTests();
427         if (singleThreadedTests != null) {
428             masterSuite.addTest(singleThreadedTests);
429         }
430         // add multi threaded tests
431
Test multiThreadedTests = getMultiThreadedTests();
432         if (multiThreadedTests != null) {
433             masterSuite.addTest(multiThreadedTests);
434         }
435         return masterSuite;
436     }
437
438     /**
439      * This method is used within the suite method to get all of the single threaded tests.
440      * Add all your single threaded tests in this method with a line like:
441      * suite.addTest(new InternalLifecycleTestTest("testFunction1"));
442      */

443     private static Test getSingleThreadedTests() {
444         TestSuite suite = new TestSuite();
445
446         suite.addTest(new InternalLifecycleTest("testInitialize"));
447         suite.addTest(new InternalLifecycleTest("testStart"));
448         suite.addTest(new InternalLifecycleTest("testStop"));
449         suite.addTest(new InternalLifecycleTest("testSuspend"));
450         suite.addTest(new InternalLifecycleTest("testResume"));
451         suite.addTest(new InternalLifecycleTest("testConfigureWhileRunning"));
452         suite.addTest(
453             new InternalLifecycleTest("testConfigureWhileSuspended"));
454         suite.addTest(new InternalLifecycleTest("testConfigureWhileStopped"));
455         suite.addTest(new InternalLifecycleTest("testDestroyWhileRunning"));
456         suite.addTest(new InternalLifecycleTest("testDestroyWhileStopped"));
457         return suite;
458     }
459
460     /**
461      * This method is used within the suite method to get all of the multi threaded tests.
462      * Add all your multi threaded tests in this method with a line like: addTest(suite, "testFunction1", 5);
463      */

464     private static Test getMultiThreadedTests() {
465         TestSuite suite = new ActiveTestSuite();
466
467         /*
468          * add your tests here following these examples:
469          *
470          * addTest(suite, "testFunction1", 5);
471          * addTest(suite, "testFunction2", 10);
472          */

473
474         return suite;
475     }
476
477     /**
478      * This method will add the give test to the give suite the specified
479      * number of times. This is best used for multi-threaded tests where
480      * suite is an instance of ActiveTestSuite and you want to run the same test in multiple threads.
481      */

482     private static void addTest(
483         TestSuite suite,
484         String JavaDoc testName,
485         int number) {
486         for (int count = 0; count < number; count++) {
487             suite.addTest(new InternalLifecycleTest(testName));
488         }
489     }
490
491     /**
492      * @see TestListener#configureCalled()
493      */

494     public void configureCalled() {
495         this.configureCalled = true;
496     }
497
498     /**
499      * @see TestListener#destroyCalled()
500      */

501     public void destroyCalled() {
502         this.destroyCalled = true;
503     }
504
505     /**
506      * @see TestListener#initializeCalled()
507      */

508     public void initializeCalled() {
509         this.initializeCalled = true;
510     }
511
512     /**
513      * @see TestListener#resumeCalled()
514      */

515     public void resumeCalled() {
516         this.resumeCalled = true;
517     }
518
519     /**
520      * @see TestListener#startCalled()
521      */

522     public void startCalled() {
523         this.startCalled = true;
524     }
525
526     /**
527      * @see TestListener#stopCalled()
528      */

529     public void stopCalled() {
530         this.stopCalled = true;
531     }
532
533     /**
534      * @see TestListener#suspendCalled()
535      */

536     public void suspendCalled() {
537         this.suspendCalled = true;
538     }
539
540 }
Popular Tags