KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > config > test > DefaultConfigurationServiceTest


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.test;
19
20 import org.sape.carbon.core.config.Config;
21 import org.sape.carbon.core.config.Configuration;
22 import org.sape.carbon.core.config.ConfigurationException;
23 import org.sape.carbon.core.config.ConfigurationNotFoundException;
24 import org.sape.carbon.core.config.format.DefaultConfigurationFormatService;
25 import org.sape.carbon.core.config.node.Node;
26 import org.sape.carbon.core.config.node.event.NodeEventListener;
27 import org.sape.carbon.core.exception.InvalidParameterException;
28
29 import junit.extensions.ActiveTestSuite;
30 import junit.framework.Test;
31 import junit.framework.TestCase;
32 import junit.framework.TestSuite;
33
34 /**
35  * Template for junit test harness. Change this description to reflect what this class is testing.
36  *
37  * Copyright 2002 Sapient
38  * @since carbon 1.0
39  * @author Douglas Voet, March 2002
40  * @version $Revision: 1.17 $($Author: dvoet $ / $Date: 2003/10/15 19:03:21 $)
41  */

42 public class DefaultConfigurationServiceTest
43     extends TestCase
44     implements NodeEventListener {
45
46     private static final String JavaDoc TEST_VALUE = "hello mom";
47     private static final String JavaDoc TEST_CONFIG_NAME = "/fee/fi/fo/fum";
48     private static final String JavaDoc BAD_CONFIG_NAME_1 = "/fee/fi/fo/fum/foo";
49     private static final String JavaDoc BAD_CONFIG_NAME_2 = "/fee/fi/fo/fummmm";
50     private static final String JavaDoc TEST_CONFIG_ROOT = "fee";
51     private static final String JavaDoc BAD_NAME = "fee/fi";
52
53     private int nodeChangedCalled = 0;
54     private int nodeRemovedCalled = 0;
55
56     public DefaultConfigurationServiceTest(String JavaDoc name) {
57         super(name);
58     }
59
60     public void testStoreConfiguration() throws ConfigurationException {
61
62         DefaultConfigurationFormatService formatter =
63             new DefaultConfigurationFormatService();
64
65         TestConfiguration config =
66             (TestConfiguration) formatter.newConfiguration(
67                 TestConfiguration.class);
68
69         config.setTestValue(TEST_VALUE);
70
71         Config.getInstance().storeConfiguration(TEST_CONFIG_NAME, config);
72
73     }
74
75     public void testFetchConfiguration() {
76         TestConfiguration config =
77             (TestConfiguration) Config.getInstance().fetchConfiguration(
78                 TEST_CONFIG_NAME);
79
80         if (!config.getTestValue().equals(TEST_VALUE)) {
81             TestCase.fail(
82                 "Configuration value was not as expected: "
83                     + "Actual - ["
84                     + config.getTestValue()
85                     + "] "
86                     + "Expected - ["
87                     + TEST_VALUE
88                     + "]");
89         }
90     }
91
92     public void testNodeExists() {
93         if (!Config.getInstance().nodeExists(TEST_CONFIG_NAME)) {
94             fail("Configuration " + TEST_CONFIG_NAME + " was not detected " +
95                 "by nodeExists check.");
96         }
97
98         if (Config.getInstance().nodeExists(BAD_CONFIG_NAME_1)) {
99             fail("Configuration " + BAD_CONFIG_NAME_1 + " was detected " +
100                 "as existing by nodeExists check, when it does not exist.");
101         }
102     }
103
104     public void testNodeNotFound() {
105         try {
106             Configuration config =
107                 Config.getInstance().fetchConfiguration(BAD_CONFIG_NAME_1);
108
109             TestCase.fail(
110                 "Did not catch "
111                     + "ConfigurationNotFoundException as expected");
112         } catch (ConfigurationNotFoundException ipe) {
113             // expected
114
}
115
116         try {
117             Configuration config =
118                 Config.getInstance().fetchConfiguration(BAD_CONFIG_NAME_2);
119
120             TestCase.fail(
121                 "Did not catch "
122                     + "ConfigurationNotFoundException as expected");
123         } catch (ConfigurationNotFoundException cnfe) {
124             // expected
125
}
126     }
127
128     /**
129      * also cleans up
130      */

131     public void testFetchRootNode() throws Exception JavaDoc {
132         Node root = Config.getInstance().fetchNode(Config.ROOT_NODE_NAME);
133
134         Node testRoot = root.fetchChild(TEST_CONFIG_ROOT);
135
136         testRoot.remove();
137
138     }
139
140     public void testBadNodeName() {
141         try {
142             Config.getInstance().fetchConfiguration(BAD_NAME);
143             TestCase.fail(
144                 "Did not catch " + "InvalidParameterException as expected");
145         } catch (InvalidParameterException ipe) {
146             // expected
147
}
148
149         try {
150             Config.getInstance().fetchConfiguration(null);
151             TestCase.fail(
152                 "Did not catch " + "NullPointerException as expected");
153         } catch (NullPointerException JavaDoc npe) {
154             // expected
155
}
156     }
157
158     public void testNodeEvents() throws ConfigurationException {
159         TestConfiguration config =
160             (TestConfiguration) Config
161                 .getInstance()
162                 .getConfigurationService()
163                 .fetchWritableConfiguration(TEST_CONFIG_NAME);
164
165         TestConfiguration subConfig =
166             (TestConfiguration) Config
167                 .getInstance()
168                 .getConfigurationService()
169                 .createConfiguration(TestConfiguration.class);
170
171         config.setSubConfig(subConfig);
172         Config.getInstance().storeConfiguration(TEST_CONFIG_NAME, config);
173
174         Config.getInstance().addNodeListener(
175             TEST_CONFIG_NAME,
176             this);
177         Config.getInstance().addNodeListener(
178             TEST_CONFIG_NAME + Node.DELIMITER + "SubConfig",
179             this);
180
181         testStoreConfiguration();
182
183         assertTrue(
184             "Listener was not notified of node change",
185             this.nodeChangedCalled == 2);
186
187         Config.getInstance().fetchNode(TEST_CONFIG_NAME).refresh();
188
189         assertTrue(
190             "Listener was not notified of node change on refresh",
191             this.nodeChangedCalled == 3);
192
193         Config.getInstance().fetchNode(TEST_CONFIG_NAME).remove();
194
195         assertTrue(
196             "Listener was not notified of node removal",
197             this.nodeRemovedCalled == 1);
198
199     }
200
201     public void nodeChanged(Node changedNode) {
202         this.nodeChangedCalled++;
203     }
204
205     /**
206      * @see org.sape.carbon.core.config.node.event.NodeEventListener#nodeRemoved(java.lang.String)
207      */

208     public void nodeRemoved(String JavaDoc removedNodeName) {
209         this.nodeRemovedCalled++;
210     }
211
212     /**
213      * Method called by jUnit to get all the tests in this test case.
214      * @return Test the suite of tests in this test case
215      */

216     public static Test suite() {
217         TestSuite masterSuite = new TestSuite();
218         // add single threaded tests
219
Test singleThreadedTests = getSingleThreadedTests();
220         if (singleThreadedTests != null) {
221             masterSuite.addTest(singleThreadedTests);
222         }
223         // add multi threaded tests
224
Test multiThreadedTests = getMultiThreadedTests();
225         if (multiThreadedTests != null) {
226             masterSuite.addTest(multiThreadedTests);
227         }
228         return masterSuite;
229     }
230
231     /**
232      * This method is used within the suite method to get all of the single threaded tests.
233      * Add all your single threaded tests in this method with a line like:
234      * suite.addTest(new DefaultConfigurationServiceTest("testFunction1"));
235      * @return Test the suite of single threaded tests in this test case
236      */

237     private static Test getSingleThreadedTests() {
238         TestSuite suite = new TestSuite();
239
240         suite.addTest(
241             new DefaultConfigurationServiceTest("testStoreConfiguration"));
242         suite.addTest(
243             new DefaultConfigurationServiceTest("testFetchConfiguration"));
244         suite.addTest(
245             new DefaultConfigurationServiceTest("testNodeExists"));
246         suite.addTest(new DefaultConfigurationServiceTest("testNodeEvents"));
247         suite.addTest(new DefaultConfigurationServiceTest("testNodeNotFound"));
248         suite.addTest(new DefaultConfigurationServiceTest("testFetchRootNode"));
249         suite.addTest(new DefaultConfigurationServiceTest("testBadNodeName"));
250
251         return suite;
252     }
253
254     /**
255      * This method is used within the suite method to get all of the multi threaded tests.
256      * Add all your multi threaded tests in this method with a line like: addTest(suite, "testFunction1", 5);
257      * @return Test the suite of multi-threaded tests in this test case
258      */

259     private static Test getMultiThreadedTests() {
260         TestSuite suite = new ActiveTestSuite();
261
262         /*
263          * add your tests here following these examples:
264          *
265          * addTest(suite, "testFunction1", 5);
266          * addTest(suite, "testFunction2", 10);
267          */

268
269         return suite;
270     }
271
272     /**
273      * This method will add the give test to the give suite the specified
274      * number of times. This is best used for multi-threaded tests where
275      * suite is an instance of ActiveTestSuite and you want to run the same test in multiple threads.
276      * @param suite the suite to add the test to.
277      * @param testName the name of the test to add.
278      * @param number the number of times to add the test to the suite
279      */

280     private static void addTest(TestSuite suite, String JavaDoc testName, int number) {
281         for (int count = 0; count < number; count++) {
282             suite.addTest(new DefaultConfigurationServiceTest(testName));
283         }
284     }
285
286 }
287
Popular Tags