KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > component > startup > test > StartupServiceTest


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.startup.test;
19
20 import org.sape.carbon.core.component.ComponentKeeper;
21 import org.sape.carbon.core.component.Lookup;
22 import org.sape.carbon.core.component.startup.StartupService;
23 import org.sape.carbon.core.component.startup.StartupServiceConfiguration;
24 import org.sape.carbon.core.component.startup.StartupServiceConfiguration.StartupComponentConfiguration;
25 import org.sape.carbon.core.config.Config;
26
27 import junit.extensions.ActiveTestSuite;
28 import junit.framework.Test;
29 import junit.framework.TestCase;
30 import junit.framework.TestSuite;
31
32 /**
33  * Tests to make sure that all components specifed in the Startup Service's
34  * configuration are actually loaded in the system.
35  *
36  * Copyright 2002 Sapient
37  * @since carbon 1.0
38  * @author Douglas Voet, Febuary 2002
39  * @version $Revision: 1.17 $($Author: ghinkl $ / $Date: 2003/10/16 13:21:38 $)
40  */

41 public class StartupServiceTest extends TestCase {
42     public StartupServiceTest(String JavaDoc name) {
43         super(name);
44     }
45
46     /**
47      * Tests to see if the expected startup classes have been loaded.
48      * <p>
49      * This test assumes that the startup component has been called by the
50      * BootStrapper and that there is at least one component configured
51      * to be loaded at startup.
52      * <p>
53      * The test is accomplished by attempting to destroy each component listed
54      * in the Startup components configuration. If the destroy method
55      * throws a ComponentNotFoundException, the test fails.
56      */

57     public void testLoadedStartupComponents() {
58         StartupServiceConfiguration startupConfig =
59             (StartupServiceConfiguration) Config
60                 .getInstance()
61                 .fetchConfiguration(
62                 STARTUP_SERVICE_NAME);
63
64         StartupComponentConfiguration[] startupComponents =
65             startupConfig.getStartupComponent();
66
67         ComponentKeeper keeper = Lookup.getInstance().getComponentKeeper();
68
69         StartupService startup =
70             (StartupService) keeper.fetchComponent(STARTUP_SERVICE_NAME);
71
72         // destroy the test startup components if they are already running
73
for (int i = 0; i < startupComponents.length; i++) {
74             if (keeper
75                 .getComponentNames()
76                 .contains(startupComponents[i].getComponentName())) {
77
78                 keeper.destroyComponent(startupComponents[i].getComponentName());
79             }
80         }
81
82         // start the startup service
83
startup.startComponents();
84
85         // check to make sure the expected components are running
86
for (int i = 0; i < startupComponents.length; i++) {
87             if (startupComponents[i].isEnabled()) {
88                 TestCase.assertTrue(
89                     "Startup component was not loaded: ["
90                         + startupComponents[i].getComponentName() + "]",
91                     keeper.getComponentNames().contains(
92                         startupComponents[i].getComponentName()));
93             }
94         }
95     }
96
97     // static members
98
private static final String JavaDoc STARTUP_SERVICE_NAME =
99         "/core/test/TestStartupService";
100
101     /**
102      * Method called by jUnit to get all the tests in this test case.
103      * @return Test the suite of tests in this test case
104      */

105     public static Test suite() {
106         TestSuite masterSuite = new TestSuite("StartupServiceTest");
107         // add single threaded tests
108
Test singleThreadedTests = getSingleThreadedTests();
109         if (singleThreadedTests != null) {
110             masterSuite.addTest(singleThreadedTests);
111         }
112         // add multi threaded tests
113
Test multiThreadedTests = getMultiThreadedTests();
114         if (multiThreadedTests != null) {
115             masterSuite.addTest(multiThreadedTests);
116         }
117         return masterSuite;
118     }
119
120     /**
121      * This method is used within the suite method to get all of the single threaded tests.
122      * Add all your single threaded tests in this method with a line like: suite.addTest(new StartupServiceTest("testFunction1"));
123      * @return Test the suite of single threaded tests in this test case
124      */

125     private static Test getSingleThreadedTests() {
126         TestSuite suite = new TestSuite();
127
128         /*
129          * add your tests here following these examples:
130          *
131          * suite.addTest(new StartupServiceTest("testFunction1"));
132          * suite.addTest(new StartupServiceTest("testFunction2"));
133          */

134         suite.addTest(new StartupServiceTest("testLoadedStartupComponents"));
135
136         return suite;
137     }
138
139     /**
140      * This method is used within the suite method to get all of the multi threaded tests.
141      * Add all your multi threaded tests in this method with a line like: addTest(suite, "testFunction1", 5);
142      * @return Test the suite of multi-threaded tests in this test case
143      */

144     private static Test getMultiThreadedTests() {
145         TestSuite suite = new ActiveTestSuite();
146
147         /*
148          * add your tests here following these examples:
149          *
150          * addTest(suite, "testFunction1", 5);
151          * addTest(suite, "testFunction2", 10);
152          */

153
154         return suite;
155     }
156
157     /**
158      * This method will add the give test to the give suite the specified
159      * number of times. This is best used for multi-threaded tests where
160      * suite is an instance of ActiveTestSuite and you want to run the same test in multiple threads.
161      * @param suite the suite to add the test to.
162      * @param testName the name of the test to add.
163      * @param number the number of times to add the test to the suite
164      */

165     private static void addTest(
166         TestSuite suite,
167         String JavaDoc testName,
168         int number) {
169         for (int count = 0; count < number; count++) {
170             suite.addTest(new StartupServiceTest(testName));
171         }
172     }
173 }
Popular Tags