KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > config > classloader > test > InternalClassloaderNodeTest


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.services.config.classloader.test;
19
20 import java.io.File JavaDoc;
21
22 import junit.extensions.ActiveTestSuite;
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26
27 import org.sape.carbon.core.bootstrap.BootStrapper;
28 import org.sape.carbon.core.config.Config;
29 import org.sape.carbon.core.config.ConfigurationException;
30 import org.sape.carbon.core.config.node.ConfigurationDocument;
31 import org.sape.carbon.core.config.node.Folder;
32 import org.sape.carbon.core.config.node.Node;
33 import org.sape.carbon.core.config.node.NodeNotFoundException;
34 import org.sape.carbon.core.config.node.NodeRemovalException;
35 import org.sape.carbon.core.config.test.InternalNodeTest;
36 import org.sape.carbon.core.config.test.TestConfiguration;
37
38 /**
39  * Copyright 2002 Sapient
40  * Template for junit test harness. Change this description to reflect what this class is testing.
41  * @since carbon 1.0
42  * @author Douglas Voet, March 2002
43  * @version $Revision: 1.6 $($Author: dvoet $ / $Date: 2003/11/11 21:19:59 $)
44  */

45 public class InternalClassloaderNodeTest extends InternalNodeTest {
46     private static final String JavaDoc CLASSLOADER_NODE_NAME =
47         "/classloader_config/test/ClassloaderNodeTest";
48
49     private static final String JavaDoc EXISTING_FOLDER_NAME = "testFolder";
50     private static final String JavaDoc EXISTING_DOCUMENT_NAME = "testDocument";
51     private static final String JavaDoc EXISTING_TEST_VALUE = "foobar";
52
53     public InternalClassloaderNodeTest(String JavaDoc name) {
54         super(name);
55     }
56
57     public void testRemoveClassLoaderResource() throws NodeNotFoundException {
58         try {
59             super.root.fetchChild("TestNode1").remove();
60             fail("did not catch NodeRemovalException as expected");
61         } catch (NodeRemovalException nre) {
62             // expected
63
}
64     }
65     
66     public void testReadExistingConfiguration() throws ConfigurationException {
67         Folder subFolder = (Folder) this.root.fetchChild(EXISTING_FOLDER_NAME);
68         ConfigurationDocument doc =
69             (ConfigurationDocument) subFolder.fetchChild(EXISTING_DOCUMENT_NAME);
70
71         TestConfiguration config =
72             (TestConfiguration) doc.readConfiguration();
73
74         if (!EXISTING_TEST_VALUE.equals(config.getTestValue())) {
75             TestCase.fail(
76                 "Configuration value not as expected. "
77                     + "Actual: ["
78                     + config.getTestValue()
79                     + "], "
80                     + "Expected: ["
81                     + EXISTING_TEST_VALUE
82                     + "]");
83         }
84     }
85     
86     public void testFetchRemovedConfigurationDoc() throws NodeNotFoundException {
87         Node configDoc =
88             this.root.fetchChild(FOLDER_NAME).fetchChild(DOCUMENT_NAME);
89
90         TestCase.assertTrue(
91             "Was able to fetch a removed configuration document",
92             !(configDoc instanceof ConfigurationDocument));
93     }
94     
95     public void testRefresh() throws ConfigurationException {
96         String JavaDoc testDataDir =
97             BootStrapper.getInstance().getDeploymentProperty(
98                 "test.config-classloader.TestDataDir");
99                 
100         File JavaDoc originalJar = new File JavaDoc(testDataDir, "config");
101         File JavaDoc tempJar = new File JavaDoc(testDataDir, "temp_config");
102         File JavaDoc modifiedJar = new File JavaDoc(testDataDir, "modified_config");
103         
104         try {
105             // check before
106
Folder subFolder = (Folder) this.root.fetchChild(EXISTING_FOLDER_NAME);
107             ConfigurationDocument doc =
108                 (ConfigurationDocument) subFolder.fetchChild(EXISTING_DOCUMENT_NAME);
109
110             TestConfiguration config =
111                 (TestConfiguration) doc.readConfiguration();
112                 
113             String JavaDoc orginalValue = config.getTestValue();
114
115             // change datastore
116
originalJar.renameTo(tempJar);
117             modifiedJar.renameTo(originalJar);
118             
119             // refresh
120
super.root.refresh();
121             
122             // check after
123
config =(TestConfiguration) doc.readConfiguration();
124             String JavaDoc refreshedValue = config.getTestValue();
125             
126             assertTrue(
127                 "Config value did not change after refresh: orginial value [" +
128                 orginalValue + "] refreshed value [" + refreshedValue + "]",
129                 !orginalValue.equals(refreshedValue));
130             
131         } finally {
132             // revert jars
133
originalJar.renameTo(modifiedJar);
134             tempJar.renameTo(originalJar);
135         }
136     }
137
138     /**
139      * @see TestCase#setUp()
140      */

141     protected void setUp() throws Exception JavaDoc {
142         super.root = (Folder) Config.getInstance().
143             fetchNode(InternalClassloaderNodeTest.CLASSLOADER_NODE_NAME);
144     }
145
146     protected String JavaDoc getTestNodeAttributesAbsoluteName() {
147         return CLASSLOADER_NODE_NAME + super.getTestNodeAttributesAbsoluteName();
148     }
149
150     /**
151      * Method called by jUnit to get all the tests in this test case.
152      * @return Test the suite of tests in this test case
153      */

154     public static Test suite() {
155         TestSuite masterSuite = new TestSuite();
156         // add single threaded tests
157
Test singleThreadedTests = getSingleThreadedTests();
158         if (singleThreadedTests != null) {
159             masterSuite.addTest(singleThreadedTests);
160         }
161         // add multi threaded tests
162
Test multiThreadedTests = getMultiThreadedTests();
163         if (multiThreadedTests != null) {
164             masterSuite.addTest(multiThreadedTests);
165         }
166         return masterSuite;
167     }
168
169     /**
170      * This method is used within the suite method to get all of the single threaded tests.
171      * Add all your single threaded tests in this method with a line like:
172      * suite.addTest(new InternalNodeTest("testFunction1"));
173      * @return Test the suite of single threaded tests in this test case
174      */

175     private static Test getSingleThreadedTests() {
176         TestSuite suite = new TestSuite();
177
178         suite.addTest(new InternalClassloaderNodeTest("testAddSubFolder"));
179         suite.addTest(new InternalClassloaderNodeTest("testAddConfigurationDocument"));
180         suite.addTest(new InternalClassloaderNodeTest("testWriteConfiguration"));
181         suite.addTest(new InternalClassloaderNodeTest("testReadConfiguration"));
182         suite.addTest(new InternalClassloaderNodeTest("testReadExistingConfiguration"));
183         suite.addTest(new InternalClassloaderNodeTest("testNodeAttributes"));
184         suite.addTest(new InternalClassloaderNodeTest("testFetchChildren"));
185         suite.addTest(
186             new InternalClassloaderNodeTest("testAddDuplicateConfigurationDocument"));
187         suite.addTest(new InternalClassloaderNodeTest("testAddDuplicateSubFolder"));
188         suite.addTest(new InternalClassloaderNodeTest("testAddNestedConfiguration"));
189         suite.addTest(new InternalClassloaderNodeTest("testReadNestedConfiguration"));
190         suite.addTest(new InternalClassloaderNodeTest("testRemoveNestedConfiguration"));
191         suite.addTest(new InternalClassloaderNodeTest("testNodeRemoval"));
192         suite.addTest(new InternalClassloaderNodeTest("testFetchRemovedConfigurationDoc"));
193         suite.addTest(new InternalClassloaderNodeTest("testNullParameters"));
194         suite.addTest(new InternalClassloaderNodeTest("testLinkFolder"));
195         suite.addTest(new InternalClassloaderNodeTest("testLinkDoc"));
196         suite.addTest(new InternalClassloaderNodeTest("testRemoveClassLoaderResource"));
197         suite.addTest(new InternalClassloaderNodeTest("testRefresh"));
198
199         return suite;
200     }
201
202     /**
203      * This method is used within the suite method to get all of the multi threaded tests.
204      * Add all your multi threaded tests in this method with a line like: addTest(suite, "testFunction1", 5);
205      * @return Test the suite of multi-threaded tests in this test case
206      */

207     private static Test getMultiThreadedTests() {
208         TestSuite suite = new ActiveTestSuite();
209
210         /*
211          * add your tests here following these examples:
212          *
213          * addTest(suite, "testFunction1", 5);
214          * addTest(suite, "testFunction2", 10);
215          */

216
217         return suite;
218     }
219
220     /**
221      * This method will add the give test to the give suite the specified
222      * number of times. This is best used for multi-threaded tests where
223      * suite is an instance of ActiveTestSuite and you want to run the same test in multiple threads.
224      * @param suite the suite to add the test to.
225      * @param testName the name of the test to add.
226      * @param number the number of times to add the test to the suite
227      */

228     private static void addTest(TestSuite suite, String JavaDoc testName, int number) {
229         for (int count = 0; count < number; count++) {
230             suite.addTest(new InternalClassloaderNodeTest(testName));
231         }
232     }
233 }
234
Popular Tags