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.config.cache.test;
019:
020: import org.sape.carbon.core.bootstrap.BootStrapper;
021: import org.sape.carbon.core.config.Config;
022: import org.sape.carbon.core.config.ConfigurationException;
023: import org.sape.carbon.core.config.ConfigurationNotFoundException;
024: import org.sape.carbon.core.config.PropertyConfiguration;
025: import org.sape.carbon.core.config.cache.ConfigurationCache;
026: import org.sape.carbon.core.config.cache.ConfigurationCacheFactory;
027: import org.sape.carbon.core.config.cache.DoubleCheckConfigurationCache;
028: import org.sape.carbon.core.config.cache.SynchronizedConfigurationCache;
029:
030: import junit.extensions.ActiveTestSuite;
031: import junit.framework.Test;
032: import junit.framework.TestCase;
033: import junit.framework.TestSuite;
034:
035: /**
036: *
037: *
038: * Copyright 2002 Sapient
039: * @since carbon 1.1
040: * @author Douglas Voet, Oct 25, 2002
041: * @version $Revision: 1.4 $($Author: dvoet $ / $Date: 2003/05/05 21:21:16 $)
042: */
043: public class ConfigurationCacheTest extends TestCase {
044:
045: private String actualCacheClassName;
046:
047: public ConfigurationCacheTest(String name) {
048: super (name);
049: }
050:
051: public void testSynchronizedCache() throws ConfigurationException {
052: setCacheClass(SynchronizedConfigurationCache.class);
053: testCache();
054: restoreCacheClass();
055: }
056:
057: public void testDoubleCheckCache() throws ConfigurationException {
058: setCacheClass(DoubleCheckConfigurationCache.class);
059: testCache();
060: restoreCacheClass();
061: }
062:
063: private void setCacheClass(Class cacheClass) {
064: this .actualCacheClassName = BootStrapper
065: .getInstance()
066: .getDeploymentProperty(
067: ConfigurationCacheFactory.CACHE_TYPE_PROPERTY_NAME);
068:
069: BootStrapper.getInstance().setDeploymentProperty(
070: ConfigurationCacheFactory.CACHE_TYPE_PROPERTY_NAME,
071: cacheClass.getName());
072: }
073:
074: private void restoreCacheClass() {
075: BootStrapper.getInstance().setDeploymentProperty(
076: ConfigurationCacheFactory.CACHE_TYPE_PROPERTY_NAME,
077: this .actualCacheClassName);
078: }
079:
080: private void testCache() throws ConfigurationException {
081: final String CONFIG_NAME = "/core/test/ConfigurationCacheTest";
082: final String ATTRIBUTE_NAME = "foo";
083:
084: PropertyConfiguration origConfig = (PropertyConfiguration) Config
085: .getInstance().createConfiguration(
086: PropertyConfiguration.class);
087:
088: origConfig.setProperty(ATTRIBUTE_NAME, "bar");
089: Config.getInstance()
090: .storeConfiguration(CONFIG_NAME, origConfig);
091:
092: ConfigurationCache cache = ConfigurationCacheFactory
093: .getInstance(Config.getInstance()
094: .getConfigurationService());
095:
096: PropertyConfiguration cachedConfig = (PropertyConfiguration) cache
097: .getConfiguration(CONFIG_NAME);
098:
099: origConfig.setProperty(ATTRIBUTE_NAME, "splat");
100: Config.getInstance()
101: .storeConfiguration(CONFIG_NAME, origConfig);
102:
103: PropertyConfiguration changedConfig = (PropertyConfiguration) cache
104: .getConfiguration(CONFIG_NAME);
105:
106: TestCase.assertTrue("Cached configuration did not change",
107: !cachedConfig.getProperty(ATTRIBUTE_NAME).equals(
108: changedConfig.getProperty(ATTRIBUTE_NAME)));
109:
110: Config.getInstance().fetchNode(CONFIG_NAME).remove();
111:
112: try {
113: cache.getConfiguration(CONFIG_NAME);
114: TestCase.fail("Cached configuration was not removed");
115: } catch (ConfigurationNotFoundException cnfe) {
116: // expected
117: }
118: }
119:
120: /**
121: * Method called by jUnit to get all the tests in this test case.
122: * @return Test the suite of tests in this test case
123: */
124: public static Test suite() {
125: TestSuite masterSuite = new TestSuite();
126: // add single threaded tests
127: Test singleThreadedTests = getSingleThreadedTests();
128: if (singleThreadedTests != null) {
129: masterSuite.addTest(singleThreadedTests);
130: }
131: // add multi threaded tests
132: Test multiThreadedTests = getMultiThreadedTests();
133: if (multiThreadedTests != null) {
134: masterSuite.addTest(multiThreadedTests);
135: }
136: return masterSuite;
137: }
138:
139: /**
140: * This method is used within the suite method to get all of the single threaded tests.
141: * Add all your single threaded tests in this method with a line like:
142: * suite.addTest(new InternalNodeTest("testFunction1"));
143: * @return Test the suite of single threaded tests in this test case
144: */
145: private static Test getSingleThreadedTests() {
146: TestSuite suite = new TestSuite();
147:
148: suite.addTest(new ConfigurationCacheTest(
149: "testSynchronizedCache"));
150: suite
151: .addTest(new ConfigurationCacheTest(
152: "testDoubleCheckCache"));
153:
154: return suite;
155: }
156:
157: /**
158: * This method is used within the suite method to get all of the multi threaded tests.
159: * Add all your multi threaded tests in this method with a line like: addTest(suite, "testFunction1", 5);
160: * @return Test the suite of multi-threaded tests in this test case
161: */
162: private static Test getMultiThreadedTests() {
163: TestSuite suite = new ActiveTestSuite();
164:
165: /*
166: * add your tests here following these examples:
167: *
168: * addTest(suite, "testFunction1", 5);
169: * addTest(suite, "testFunction2", 10);
170: */
171:
172: return suite;
173: }
174:
175: /**
176: * This method will add the give test to the give suite the specified
177: * number of times. This is best used for multi-threaded tests where
178: * suite is an instance of ActiveTestSuite and you want to run the same test in multiple threads.
179: * @param suite the suite to add the test to.
180: * @param testName the name of the test to add.
181: * @param number the number of times to add the test to the suite
182: */
183: private static void addTest(TestSuite suite, String testName,
184: int number) {
185: for (int count = 0; count < number; count++) {
186: suite.addTest(new ConfigurationCacheTest(testName));
187: }
188: }
189: }
|