KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > config > cache > test > ConfigurationCacheTest


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.config.cache.test;
19
20 import org.sape.carbon.core.bootstrap.BootStrapper;
21 import org.sape.carbon.core.config.Config;
22 import org.sape.carbon.core.config.ConfigurationException;
23 import org.sape.carbon.core.config.ConfigurationNotFoundException;
24 import org.sape.carbon.core.config.PropertyConfiguration;
25 import org.sape.carbon.core.config.cache.ConfigurationCache;
26 import org.sape.carbon.core.config.cache.ConfigurationCacheFactory;
27 import org.sape.carbon.core.config.cache.DoubleCheckConfigurationCache;
28 import org.sape.carbon.core.config.cache.SynchronizedConfigurationCache;
29
30 import junit.extensions.ActiveTestSuite;
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import junit.framework.TestSuite;
34
35 /**
36  *
37  *
38  * Copyright 2002 Sapient
39  * @since carbon 1.1
40  * @author Douglas Voet, Oct 25, 2002
41  * @version $Revision: 1.4 $($Author: dvoet $ / $Date: 2003/05/05 21:21:16 $)
42  */

43 public class ConfigurationCacheTest extends TestCase {
44
45     private String JavaDoc actualCacheClassName;
46
47     public ConfigurationCacheTest(String JavaDoc name) {
48         super(name);
49     }
50
51     public void testSynchronizedCache() throws ConfigurationException {
52         setCacheClass(SynchronizedConfigurationCache.class);
53         testCache();
54         restoreCacheClass();
55     }
56
57     public void testDoubleCheckCache() throws ConfigurationException {
58         setCacheClass(DoubleCheckConfigurationCache.class);
59         testCache();
60         restoreCacheClass();
61     }
62
63     private void setCacheClass(Class JavaDoc cacheClass) {
64         this.actualCacheClassName =
65             BootStrapper.getInstance().getDeploymentProperty(
66                 ConfigurationCacheFactory.CACHE_TYPE_PROPERTY_NAME);
67
68         BootStrapper.getInstance().setDeploymentProperty(
69             ConfigurationCacheFactory.CACHE_TYPE_PROPERTY_NAME,
70             cacheClass.getName());
71     }
72
73     private void restoreCacheClass() {
74         BootStrapper.getInstance().setDeploymentProperty(
75             ConfigurationCacheFactory.CACHE_TYPE_PROPERTY_NAME,
76             this.actualCacheClassName);
77     }
78
79     private void testCache() throws ConfigurationException {
80         final String JavaDoc CONFIG_NAME = "/core/test/ConfigurationCacheTest";
81         final String JavaDoc ATTRIBUTE_NAME = "foo";
82
83         PropertyConfiguration origConfig =
84             (PropertyConfiguration) Config.getInstance().createConfiguration(
85                 PropertyConfiguration.class);
86
87         origConfig.setProperty(ATTRIBUTE_NAME, "bar");
88         Config.getInstance().storeConfiguration(CONFIG_NAME, origConfig);
89
90         ConfigurationCache cache =
91             ConfigurationCacheFactory.getInstance(
92                 Config.getInstance().getConfigurationService());
93
94         PropertyConfiguration cachedConfig =
95             (PropertyConfiguration) cache.getConfiguration(CONFIG_NAME);
96
97         origConfig.setProperty(ATTRIBUTE_NAME, "splat");
98         Config.getInstance().storeConfiguration(CONFIG_NAME, origConfig);
99
100         PropertyConfiguration changedConfig =
101             (PropertyConfiguration) cache.getConfiguration(CONFIG_NAME);
102
103         TestCase.assertTrue(
104             "Cached configuration did not change",
105             !cachedConfig.getProperty(ATTRIBUTE_NAME).equals(
106                 changedConfig.getProperty(ATTRIBUTE_NAME)));
107
108         Config.getInstance().fetchNode(CONFIG_NAME).remove();
109
110         try {
111             cache.getConfiguration(CONFIG_NAME);
112             TestCase.fail("Cached configuration was not removed");
113         } catch (ConfigurationNotFoundException cnfe) {
114             // expected
115
}
116     }
117
118     /**
119      * Method called by jUnit to get all the tests in this test case.
120      * @return Test the suite of tests in this test case
121      */

122     public static Test suite() {
123         TestSuite masterSuite = new TestSuite();
124         // add single threaded tests
125
Test singleThreadedTests = getSingleThreadedTests();
126         if (singleThreadedTests != null) {
127             masterSuite.addTest(singleThreadedTests);
128         }
129         // add multi threaded tests
130
Test multiThreadedTests = getMultiThreadedTests();
131         if (multiThreadedTests != null) {
132             masterSuite.addTest(multiThreadedTests);
133         }
134         return masterSuite;
135     }
136
137     /**
138      * This method is used within the suite method to get all of the single threaded tests.
139      * Add all your single threaded tests in this method with a line like:
140      * suite.addTest(new InternalNodeTest("testFunction1"));
141      * @return Test the suite of single threaded tests in this test case
142      */

143     private static Test getSingleThreadedTests() {
144         TestSuite suite = new TestSuite();
145
146         suite.addTest(new ConfigurationCacheTest("testSynchronizedCache"));
147         suite.addTest(new ConfigurationCacheTest("testDoubleCheckCache"));
148
149         return suite;
150     }
151
152     /**
153      * This method is used within the suite method to get all of the multi threaded tests.
154      * Add all your multi threaded tests in this method with a line like: addTest(suite, "testFunction1", 5);
155      * @return Test the suite of multi-threaded tests in this test case
156      */

157     private static Test getMultiThreadedTests() {
158         TestSuite suite = new ActiveTestSuite();
159
160         /*
161          * add your tests here following these examples:
162          *
163          * addTest(suite, "testFunction1", 5);
164          * addTest(suite, "testFunction2", 10);
165          */

166
167         return suite;
168     }
169
170     /**
171      * This method will add the give test to the give suite the specified
172      * number of times. This is best used for multi-threaded tests where
173      * suite is an instance of ActiveTestSuite and you want to run the same test in multiple threads.
174      * @param suite the suite to add the test to.
175      * @param testName the name of the test to add.
176      * @param number the number of times to add the test to the suite
177      */

178     private static void addTest(TestSuite suite, String JavaDoc testName, int number) {
179         for (int count = 0; count < number; count++) {
180             suite.addTest(new ConfigurationCacheTest(testName));
181         }
182     }
183 }
184
Popular Tags