KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > bootstrap > test > BootStrapperTest


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.bootstrap.test;
19
20 import java.util.Properties JavaDoc;
21
22 import org.sape.carbon.core.bootstrap.BootStrapper;
23 import org.sape.carbon.core.bootstrap.BootStrapperStateEnum;
24 import org.sape.carbon.core.component.ComponentKeeper;
25 import org.sape.carbon.core.config.ConfigurationService;
26 import org.sape.carbon.core.exception.ExceptionUtility;
27
28 import junit.extensions.ActiveTestSuite;
29 import junit.framework.Test;
30 import junit.framework.TestCase;
31 import junit.framework.TestSuite;
32
33
34 /**
35  * This class tests to make sure that, once loaded, the values of the
36  * <code>ConfigurationService</code> and <code>ComponentKeeper</code> remain
37  * invariant for all tests. It also tests to make sure that the appropriate
38  * exceptions are thrown when invalid configurations are provided and that no
39  * exceptions are thrown with correct configuration.
40  *
41  * Copyright 2002 Sapient
42  * @since carbon 1.0
43  * @author Douglas Voet, Febuary 2002
44  * @version $Revision: 1.21 $($Author: dvoet $ / $Date: 2003/05/05 21:21:11 $)
45  */

46 public class BootStrapperTest extends TestCase {
47     public BootStrapperTest(String JavaDoc name) {
48         super(name);
49     }
50
51     /**
52      * Tests the load method to make sure that it runs without exception.
53      * A prerequisite to this test is that there is a valid system configuration
54      */

55     public void testLoad() {
56         TestCase.assertTrue(
57             "System state was loaded before call to load",
58             BootStrapper.getInstance().getState() == BootStrapperStateEnum.NOT_LOADED);
59
60         BootStrapper.getInstance().load();
61
62         TestCase.assertTrue(
63             "System was not loaded after call to load",
64             BootStrapper.getInstance().getState() == BootStrapperStateEnum.LOADED);
65     }
66
67     /**
68      * Tests to make sure that the reference to the keeper returned by the
69      * BootStrapper is the same as the one returned by the first load.
70      * This test must be run twice to be effective.
71      */

72     public void testFetchComponentKeeper() {
73         ComponentKeeper keeper =
74                 BootStrapper.getInstance().fetchComponentKeeper();
75         TestCase.assertTrue("Keeper reference changed when it should not have",
76                 BootStrapperTest.verifyKeeper(keeper));
77     }
78
79     /**
80      * Tests to make sure that the reference to the config provider returned by
81      * the BootStrapper is the same as the one returned by the first load.
82      * This test must be run twice to be effective.
83      */

84     public void testFetchConfigurationService() {
85         ConfigurationService config =
86                 BootStrapper.getInstance().fetchConfigurationService();
87         TestCase.assertTrue("Config reference changed when it should not have",
88                 BootStrapperTest.verifyConfig(config));
89     }
90
91     /**
92      * Calls fetchComponentKeeper multiple times to ensure it remains
93      * consistent. This is used during threaded testing as other threads call
94      * methods on the BootStrapper.
95      */

96     public void testFetchComponentKeeperN() {
97         for(int n=0; n<BootStrapperTest.numberRepetitions; n++) {
98             testFetchComponentKeeper();
99         }
100     }
101
102     /**
103      * Calls fetchConfigurationService multiple times to ensure it remains
104      * consistent. This is used during threaded testing as other threads call
105      * methods on the BootStrapper.
106      */

107     public void testFetchConfigurationServiceN() {
108         for(int n=0; n<BootStrapperTest.numberRepetitions; n++) {
109             testFetchConfigurationService();
110         }
111     }
112
113     /**
114      * Calls load multiple times to ensure it remains consistent.
115      * This is used during threaded testing as other threads call methods
116      * on the BootStrapper.
117      */

118     public void testLoadN() {
119         for(int n=0; n<BootStrapperTest.numberRepetitions; n++) {
120             BootStrapper.getInstance().load();
121         }
122     }
123
124     // static members
125

126     private static ComponentKeeper keeper;
127     private static Object JavaDoc keeperLock = new Object JavaDoc();
128     private static ConfigurationService config;
129     private static Object JavaDoc configLock = new Object JavaDoc();
130     private static final int numberRepetitions = 100;
131
132     /** Method called by jUnit to get all the tests in this test case */
133     public static Test suite() {
134         TestSuite masterSuite = new TestSuite();
135         // add single threaded tests
136
Test singleThreadedTests = getSingleThreadedTests();
137         if (singleThreadedTests != null) {
138             masterSuite.addTest(singleThreadedTests);
139         }
140         // add multi threaded tests
141
Test multiThreadedTests = getMultiThreadedTests();
142         if (multiThreadedTests != null) {
143             masterSuite.addTest(multiThreadedTests);
144         }
145         return masterSuite;
146     }
147
148     /**
149      * The single threaded tests are:
150      * <ol>
151      * <li>testLoadWithBadConfig</li>
152      * <li>testLoad</li>
153      * <li>testFetchComponentKeeper</li>
154      * <li>testFetchConfigurationService</li>
155      * <li>testFetchComponentKeeper</li>
156      * <li>testFetchConfigurationService</li>
157      * </ol>
158      * testFetchComponentKeeper and testFetchConfigurationService are
159      * repeated to test that we get consistent results.
160      */

161     private static Test getSingleThreadedTests() {
162         TestSuite suite = new TestSuite();
163
164         suite.addTest(new BootStrapperTest("testLoad"));
165         suite.addTest(new BootStrapperTest("testFetchComponentKeeper"));
166         suite.addTest(new BootStrapperTest("testFetchConfigurationService"));
167         // do it again to make sure we are getting consistent results
168
suite.addTest(new BootStrapperTest("testFetchComponentKeeper"));
169         suite.addTest(new BootStrapperTest("testFetchConfigurationService"));
170         suite.addTest(new BootStrapperTest("testSetDeploymentProperty"));
171
172         return suite;
173     }
174
175     /**
176      * This test harness' multi-threaded test consists of running each of:
177      * <ul>
178      * <li>testFetchComponentKeeperN</li>
179      * <li>testFetchConfigurationServiceN</li>
180      * <li>testLoadN</li>
181      * </ul>
182      * in 3 threads.
183      */

184     private static Test getMultiThreadedTests() {
185         TestSuite suite = new ActiveTestSuite();
186
187         addTest(suite, "testFetchComponentKeeperN", 3);
188         addTest(suite, "testFetchConfigurationServiceN", 3);
189         addTest(suite, "testLoadN", 3);
190
191         return suite;
192     }
193
194     /**
195      * This method will add the give test to the give suite the specified
196      * number of times. This is best used for multi-threaded tests where
197      * suite is an instance of ActiveTestSuite and you want to run the same
198      * test in multiple threads.
199      */

200     private static void addTest(TestSuite suite, String JavaDoc testName, int number) {
201         for (int count = 0; count < number; count++) {
202             suite.addTest(new BootStrapperTest(testName));
203         }
204     }
205
206     /**
207      * Used by testFetchComponentKeeper to verify that the keeper does
208      * not change from call to call. It does this by saving the first
209      * value it is called with (so long as it is not null) and using it
210      * as a reference for all future calls.
211      *
212      * @return true if keeper != null && BootStrapperTest.keeper == keeper
213      */

214     private static boolean verifyKeeper(ComponentKeeper keeper) {
215         if (keeper == null) {
216             return false;
217         }
218
219         synchronized(BootStrapperTest.keeperLock) {
220             if (BootStrapperTest.keeper == null) {
221                 BootStrapperTest.keeper = keeper;
222             }
223             return(BootStrapperTest.keeper == keeper);
224         }
225     }
226
227     /**
228      * Used by testFetchConfigurationService to verify that config does
229      * not change from call to call. It does this by saving the first
230      * value it is called with (so long as it is not null) and using it
231      * as a reference for all future calls.
232      *
233      * @return true if config != null && BootStrapperTest.config == config
234      */

235     private static boolean verifyConfig(ConfigurationService config) {
236         if (config == null) {
237             return false;
238         }
239
240         synchronized(BootStrapperTest.configLock) {
241             if (BootStrapperTest.config == null) {
242                 BootStrapperTest.config = config;
243             }
244             return(BootStrapperTest.config == config);
245         }
246     }
247
248     private static final String JavaDoc KEY = "test.property";
249     private static final String JavaDoc DEPLOYMENT_VALUE = "testDeployment";
250     private static final String JavaDoc SYSTEM_VALUE = "testSystem";
251
252     /**
253      * Tests the setDeploymentProperty method to make sure that it runs and
254      * updates the system properties in the scenario where the property already exists.
255      * In case a new variable is being set, than it will not write to the system wide
256      * properties because that could affect other applications running in the same JVM.
257      */

258     public void testSetDeploymentProperty() {
259         try {
260             Properties JavaDoc systemProperties = System.getProperties();
261             String JavaDoc valueFromSystem = System.getProperty(KEY);
262             if ((valueFromSystem == null) || (valueFromSystem.equals(""))) {
263                 Object JavaDoc settingProperty = BootStrapper.getInstance().
264                                          setDeploymentProperty(KEY,DEPLOYMENT_VALUE);
265                 String JavaDoc valueFromDeployment = BootStrapper.getInstance().
266                                              getDeploymentProperty(KEY);
267                 TestCase.assertTrue(
268                            "Deployment property not changed where it should have been", valueFromDeployment.equals(DEPLOYMENT_VALUE));
269             }
270
271             String JavaDoc oldValueFromSystem = System.setProperty(KEY,"");
272             Object JavaDoc settingValue = BootStrapper.getInstance().setDeploymentProperty(KEY,SYSTEM_VALUE);
273             String JavaDoc valueReturned = BootStrapper.getInstance().getDeploymentProperty(KEY);
274             TestCase.assertTrue("SystemPropertyChanged",valueReturned.equals(SYSTEM_VALUE));
275
276             System.setProperties(systemProperties);
277         } catch(Exception JavaDoc e) {
278             TestCase.fail("Encountered unexpected " +
279                 "error while executing BootStrapper.setDeploymentProperty: " +
280                 ExceptionUtility.printStackTracesToString(e));
281         }
282     }
283 }
284
Popular Tags