001: /*
002: * The contents of this file are subject to the Sapient Public License
003: * Version 1.0 (the "License"); you may not use this file except in compliance
004: * with the License. You may obtain a copy of the License at
005: * http://carbon.sf.net/License.html.
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is The Carbon Component Framework.
012: *
013: * The Initial Developer of the Original Code is Sapient Corporation
014: *
015: * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
016: */
017:
018: package org.sape.carbon.core.bootstrap.test;
019:
020: import java.util.Properties;
021:
022: import org.sape.carbon.core.bootstrap.BootStrapper;
023: import org.sape.carbon.core.bootstrap.BootStrapperStateEnum;
024: import org.sape.carbon.core.component.ComponentKeeper;
025: import org.sape.carbon.core.config.ConfigurationService;
026: import org.sape.carbon.core.exception.ExceptionUtility;
027:
028: import junit.extensions.ActiveTestSuite;
029: import junit.framework.Test;
030: import junit.framework.TestCase;
031: import junit.framework.TestSuite;
032:
033: /**
034: * This class tests to make sure that, once loaded, the values of the
035: * <code>ConfigurationService</code> and <code>ComponentKeeper</code> remain
036: * invariant for all tests. It also tests to make sure that the appropriate
037: * exceptions are thrown when invalid configurations are provided and that no
038: * exceptions are thrown with correct configuration.
039: *
040: * Copyright 2002 Sapient
041: * @since carbon 1.0
042: * @author Douglas Voet, Febuary 2002
043: * @version $Revision: 1.21 $($Author: dvoet $ / $Date: 2003/05/05 21:21:11 $)
044: */
045: public class BootStrapperTest extends TestCase {
046: public BootStrapperTest(String name) {
047: super (name);
048: }
049:
050: /**
051: * Tests the load method to make sure that it runs without exception.
052: * A prerequisite to this test is that there is a valid system configuration
053: */
054: public void testLoad() {
055: TestCase
056: .assertTrue(
057: "System state was loaded before call to load",
058: BootStrapper.getInstance().getState() == BootStrapperStateEnum.NOT_LOADED);
059:
060: BootStrapper.getInstance().load();
061:
062: TestCase
063: .assertTrue(
064: "System was not loaded after call to load",
065: BootStrapper.getInstance().getState() == BootStrapperStateEnum.LOADED);
066: }
067:
068: /**
069: * Tests to make sure that the reference to the keeper returned by the
070: * BootStrapper is the same as the one returned by the first load.
071: * This test must be run twice to be effective.
072: */
073: public void testFetchComponentKeeper() {
074: ComponentKeeper keeper = BootStrapper.getInstance()
075: .fetchComponentKeeper();
076: TestCase.assertTrue(
077: "Keeper reference changed when it should not have",
078: BootStrapperTest.verifyKeeper(keeper));
079: }
080:
081: /**
082: * Tests to make sure that the reference to the config provider returned by
083: * the BootStrapper is the same as the one returned by the first load.
084: * This test must be run twice to be effective.
085: */
086: public void testFetchConfigurationService() {
087: ConfigurationService config = BootStrapper.getInstance()
088: .fetchConfigurationService();
089: TestCase.assertTrue(
090: "Config reference changed when it should not have",
091: BootStrapperTest.verifyConfig(config));
092: }
093:
094: /**
095: * Calls fetchComponentKeeper multiple times to ensure it remains
096: * consistent. This is used during threaded testing as other threads call
097: * methods on the BootStrapper.
098: */
099: public void testFetchComponentKeeperN() {
100: for (int n = 0; n < BootStrapperTest.numberRepetitions; n++) {
101: testFetchComponentKeeper();
102: }
103: }
104:
105: /**
106: * Calls fetchConfigurationService multiple times to ensure it remains
107: * consistent. This is used during threaded testing as other threads call
108: * methods on the BootStrapper.
109: */
110: public void testFetchConfigurationServiceN() {
111: for (int n = 0; n < BootStrapperTest.numberRepetitions; n++) {
112: testFetchConfigurationService();
113: }
114: }
115:
116: /**
117: * Calls load multiple times to ensure it remains consistent.
118: * This is used during threaded testing as other threads call methods
119: * on the BootStrapper.
120: */
121: public void testLoadN() {
122: for (int n = 0; n < BootStrapperTest.numberRepetitions; n++) {
123: BootStrapper.getInstance().load();
124: }
125: }
126:
127: // static members
128:
129: private static ComponentKeeper keeper;
130: private static Object keeperLock = new Object();
131: private static ConfigurationService config;
132: private static Object configLock = new Object();
133: private static final int numberRepetitions = 100;
134:
135: /** Method called by jUnit to get all the tests in this test case */
136: public static Test suite() {
137: TestSuite masterSuite = new TestSuite();
138: // add single threaded tests
139: Test singleThreadedTests = getSingleThreadedTests();
140: if (singleThreadedTests != null) {
141: masterSuite.addTest(singleThreadedTests);
142: }
143: // add multi threaded tests
144: Test multiThreadedTests = getMultiThreadedTests();
145: if (multiThreadedTests != null) {
146: masterSuite.addTest(multiThreadedTests);
147: }
148: return masterSuite;
149: }
150:
151: /**
152: * The single threaded tests are:
153: * <ol>
154: * <li>testLoadWithBadConfig</li>
155: * <li>testLoad</li>
156: * <li>testFetchComponentKeeper</li>
157: * <li>testFetchConfigurationService</li>
158: * <li>testFetchComponentKeeper</li>
159: * <li>testFetchConfigurationService</li>
160: * </ol>
161: * testFetchComponentKeeper and testFetchConfigurationService are
162: * repeated to test that we get consistent results.
163: */
164: private static Test getSingleThreadedTests() {
165: TestSuite suite = new TestSuite();
166:
167: suite.addTest(new BootStrapperTest("testLoad"));
168: suite.addTest(new BootStrapperTest("testFetchComponentKeeper"));
169: suite.addTest(new BootStrapperTest(
170: "testFetchConfigurationService"));
171: // do it again to make sure we are getting consistent results
172: suite.addTest(new BootStrapperTest("testFetchComponentKeeper"));
173: suite.addTest(new BootStrapperTest(
174: "testFetchConfigurationService"));
175: suite
176: .addTest(new BootStrapperTest(
177: "testSetDeploymentProperty"));
178:
179: return suite;
180: }
181:
182: /**
183: * This test harness' multi-threaded test consists of running each of:
184: * <ul>
185: * <li>testFetchComponentKeeperN</li>
186: * <li>testFetchConfigurationServiceN</li>
187: * <li>testLoadN</li>
188: * </ul>
189: * in 3 threads.
190: */
191: private static Test getMultiThreadedTests() {
192: TestSuite suite = new ActiveTestSuite();
193:
194: addTest(suite, "testFetchComponentKeeperN", 3);
195: addTest(suite, "testFetchConfigurationServiceN", 3);
196: addTest(suite, "testLoadN", 3);
197:
198: return suite;
199: }
200:
201: /**
202: * This method will add the give test to the give suite the specified
203: * number of times. This is best used for multi-threaded tests where
204: * suite is an instance of ActiveTestSuite and you want to run the same
205: * test in multiple threads.
206: */
207: private static void addTest(TestSuite suite, String testName,
208: int number) {
209: for (int count = 0; count < number; count++) {
210: suite.addTest(new BootStrapperTest(testName));
211: }
212: }
213:
214: /**
215: * Used by testFetchComponentKeeper to verify that the keeper does
216: * not change from call to call. It does this by saving the first
217: * value it is called with (so long as it is not null) and using it
218: * as a reference for all future calls.
219: *
220: * @return true if keeper != null && BootStrapperTest.keeper == keeper
221: */
222: private static boolean verifyKeeper(ComponentKeeper keeper) {
223: if (keeper == null) {
224: return false;
225: }
226:
227: synchronized (BootStrapperTest.keeperLock) {
228: if (BootStrapperTest.keeper == null) {
229: BootStrapperTest.keeper = keeper;
230: }
231: return (BootStrapperTest.keeper == keeper);
232: }
233: }
234:
235: /**
236: * Used by testFetchConfigurationService to verify that config does
237: * not change from call to call. It does this by saving the first
238: * value it is called with (so long as it is not null) and using it
239: * as a reference for all future calls.
240: *
241: * @return true if config != null && BootStrapperTest.config == config
242: */
243: private static boolean verifyConfig(ConfigurationService config) {
244: if (config == null) {
245: return false;
246: }
247:
248: synchronized (BootStrapperTest.configLock) {
249: if (BootStrapperTest.config == null) {
250: BootStrapperTest.config = config;
251: }
252: return (BootStrapperTest.config == config);
253: }
254: }
255:
256: private static final String KEY = "test.property";
257: private static final String DEPLOYMENT_VALUE = "testDeployment";
258: private static final String SYSTEM_VALUE = "testSystem";
259:
260: /**
261: * Tests the setDeploymentProperty method to make sure that it runs and
262: * updates the system properties in the scenario where the property already exists.
263: * In case a new variable is being set, than it will not write to the system wide
264: * properties because that could affect other applications running in the same JVM.
265: */
266: public void testSetDeploymentProperty() {
267: try {
268: Properties systemProperties = System.getProperties();
269: String valueFromSystem = System.getProperty(KEY);
270: if ((valueFromSystem == null)
271: || (valueFromSystem.equals(""))) {
272: Object settingProperty = BootStrapper.getInstance()
273: .setDeploymentProperty(KEY, DEPLOYMENT_VALUE);
274: String valueFromDeployment = BootStrapper.getInstance()
275: .getDeploymentProperty(KEY);
276: TestCase
277: .assertTrue(
278: "Deployment property not changed where it should have been",
279: valueFromDeployment
280: .equals(DEPLOYMENT_VALUE));
281: }
282:
283: String oldValueFromSystem = System.setProperty(KEY, "");
284: Object settingValue = BootStrapper.getInstance()
285: .setDeploymentProperty(KEY, SYSTEM_VALUE);
286: String valueReturned = BootStrapper.getInstance()
287: .getDeploymentProperty(KEY);
288: TestCase.assertTrue("SystemPropertyChanged", valueReturned
289: .equals(SYSTEM_VALUE));
290:
291: System.setProperties(systemProperties);
292: } catch (Exception e) {
293: TestCase
294: .fail("Encountered unexpected "
295: + "error while executing BootStrapper.setDeploymentProperty: "
296: + ExceptionUtility
297: .printStackTracesToString(e));
298: }
299: }
300: }
|