KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > config > jndi > test > InternalJNDINodeTest


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.jndi.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.PropertyConfiguration;
24 import org.sape.carbon.core.config.node.ConfigurationDocument;
25 import org.sape.carbon.core.config.node.Folder;
26 import org.sape.carbon.core.config.node.Node;
27 import org.sape.carbon.core.config.node.event.NodeEventListener;
28 import org.sape.carbon.core.config.test.InternalNodeTest;
29 import org.sape.carbon.core.config.test.TestConfiguration;
30
31 import junit.extensions.ActiveTestSuite;
32 import junit.framework.Test;
33 import junit.framework.TestCase;
34 import junit.framework.TestSuite;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 /**
40  * Test harness for JNDI configuration nodes
41  *
42  * Copyright 2003 Sapient
43  * @since carbon 2.0
44  * @author Douglas Voet, March 2003
45  * @version $Revision: 1.8 $($Author: dvoet $ / $Date: 2003/11/03 19:55:46 $)
46  */

47 public class InternalJNDINodeTest
48     extends InternalNodeTest
49     implements NodeEventListener {
50
51     private static final String JavaDoc JNDI_NODE_NAME =
52         "/jndi_config/test/JNDINodeTest";
53     private static final String JavaDoc THREAD_TEST_NODE_NAME1 = "TestNode1";
54     private static final String JavaDoc THREAD_TEST_NODE_NAME2 = "TestNode2";
55     private static final String JavaDoc THREAD_LINK_TEST_NODE_NAME = "TestNode3";
56     private static final String JavaDoc LINK_TARGET_NAME = "TestNode3Target";
57     private static final String JavaDoc JNDI_EVENT_TEST_NODE_NAME = "JNDIEventTest";
58     private static final String JavaDoc JNDI_EVENT_TEST_LINK_NAME =
59         "/jndi_config/test/JNDINodeEventTest";
60
61     private Log log = LogFactory.getLog(this.getClass());
62
63     private boolean nodeChangedCalled = false;
64     private boolean nodeRemovedCalled = false;
65
66     /**
67      * Constructor for InternalJNDINodeTest.
68      * @param name
69      */

70     public InternalJNDINodeTest(String JavaDoc name) {
71         super(name);
72     }
73
74     /**
75      * @see TestCase#setUp()
76      */

77     protected void setUp() throws Exception JavaDoc {
78         super.root =
79             (Folder) Config.getInstance().fetchNode(
80                 InternalJNDINodeTest.JNDI_NODE_NAME);
81     }
82
83     public void testThreadedRelativeLink() {
84         try {
85             testSetAndGetAttribute(
86                 (ConfigurationDocument) super.root.fetchChild(
87                     THREAD_LINK_TEST_NODE_NAME));
88         } catch (ConfigurationException ce) {
89             fail("caught ConfigurationException: " + ce);
90         }
91     }
92
93     public void testThreadedAccess1() {
94         try {
95             testSetAndGetAttribute(
96                 (ConfigurationDocument) super.root.fetchChild(
97                     THREAD_TEST_NODE_NAME1));
98         } catch (ConfigurationException ce) {
99             fail("caught ConfigurationException: " + ce);
100         }
101     }
102
103     public void testThreadedAccess2() {
104         try {
105             testSetAndGetAttribute(
106                 (ConfigurationDocument) super.root.fetchChild(
107                     THREAD_TEST_NODE_NAME2));
108         } catch (ConfigurationException ce) {
109             fail("caught ConfigurationException: " + ce);
110         }
111     }
112
113     public void testJNDIEvents() throws Exception JavaDoc {
114
115         if ("true".equals(BootStrapper.getInstance().getDeploymentProperty(
116             "test.config-jndi.EventsEnabled"))) {
117
118             try {
119                 TestConfiguration testConfig =
120                     (TestConfiguration) Config
121                         .getInstance()
122                         .createConfiguration(
123                         TestConfiguration.class);
124
125                 ConfigurationDocument eventTestDoc =
126                     this.root.addConfigurationDocument(
127                         JNDI_EVENT_TEST_NODE_NAME,
128                         testConfig);
129                 eventTestDoc.addNodeListener(this);
130
131                 Node parallelRootNode =
132                     Config.getInstance().fetchNode(JNDI_EVENT_TEST_LINK_NAME);
133
134                 ConfigurationDocument parallelEventTestDoc =
135                     (ConfigurationDocument) parallelRootNode.fetchChild(
136                         JNDI_EVENT_TEST_NODE_NAME);
137
138                 testConfig.setTestValue("foo");
139
140                 parallelEventTestDoc.writeConfiguration(testConfig);
141
142                 synchronized (this) {
143                     if (!this.nodeChangedCalled) {
144                         wait(1000);
145                     }
146                     TestCase.assertTrue(
147                         "Node either did not receive a JNDI event or "
148                             + "was not refreshed due to one.",
149                         this.nodeChangedCalled);
150                 }
151
152                 parallelEventTestDoc.remove();
153
154                 synchronized (this) {
155                     if (!this.nodeRemovedCalled) {
156                         wait(1000);
157                     }
158                     TestCase.assertTrue(
159                         "Node either did not receive a JNDI event or "
160                             + "was not refreshed due to one.",
161                         this.nodeRemovedCalled);
162                 }
163             } finally {
164                 if (this.root.containsChild(JNDI_EVENT_TEST_NODE_NAME)) {
165                     this.root.fetchChild(JNDI_EVENT_TEST_NODE_NAME).remove();
166                 }
167             }
168         } else {
169             log.info("JNDI events are disabled so they will not be tested");
170         }
171     }
172
173     private void testSetAndGetAttribute(ConfigurationDocument doc)
174         throws ConfigurationException {
175
176         for (int i = 0; i < 5; i++) {
177             TestJNDIConfiguration config =
178                 (TestJNDIConfiguration) doc.readConfiguration();
179
180             int newValue = config.getValue();
181             config.setValue(newValue);
182             doc.writeConfiguration(config);
183             config = (TestJNDIConfiguration) doc.readConfiguration();
184
185             if (config.getValue() != newValue) {
186                 fail(
187                     "value within config was not the expected value, expected ["
188                         + newValue
189                         + "], actual ["
190                         + config.getValue()
191                         + "]");
192             }
193         }
194     }
195
196     /**
197      * Method called by jUnit to get all the tests in this test case.
198      * @return Test the suite of tests in this test case
199      */

200     public static Test suite() {
201         TestSuite masterSuite = new TestSuite();
202         // add single threaded tests
203
Test singleThreadedTests = getSingleThreadedTests();
204         if (singleThreadedTests != null) {
205             masterSuite.addTest(singleThreadedTests);
206         }
207         // add multi threaded tests
208
Test multiThreadedTests = getMultiThreadedTests();
209         if (multiThreadedTests != null) {
210             masterSuite.addTest(multiThreadedTests);
211         }
212         return masterSuite;
213     }
214
215     /**
216      * This method is used within the suite method to get all of the single threaded tests.
217      * Add all your single threaded tests in this method with a line like:
218      * suite.addTest(new InternalNodeTest("testFunction1"));
219      * @return Test the suite of single threaded tests in this test case
220      */

221     private static Test getSingleThreadedTests() {
222         TestSuite suite = new TestSuite();
223
224         suite.addTest(new InternalJNDINodeTest("testAddSubFolder"));
225         suite.addTest(new InternalJNDINodeTest("testAddConfigurationDocument"));
226         suite.addTest(new InternalJNDINodeTest("testWriteConfiguration"));
227         suite.addTest(new InternalJNDINodeTest("testReadConfiguration"));
228         suite.addTest(new InternalJNDINodeTest("testNodeAttributes"));
229         suite.addTest(new InternalJNDINodeTest("testFetchChildren"));
230         suite.addTest(
231             new InternalJNDINodeTest("testAddDuplicateConfigurationDocument"));
232         suite.addTest(new InternalJNDINodeTest("testAddDuplicateSubFolder"));
233         suite.addTest(new InternalJNDINodeTest("testAddNestedConfiguration"));
234         suite.addTest(new InternalJNDINodeTest("testReadNestedConfiguration"));
235         suite.addTest(
236             new InternalJNDINodeTest("testRemoveNestedConfiguration"));
237         suite.addTest(new InternalJNDINodeTest("testNodeRemoval"));
238         suite.addTest(new InternalJNDINodeTest("testFetchRemovedNode"));
239         suite.addTest(new InternalJNDINodeTest("testNullParameters"));
240         suite.addTest(new InternalJNDINodeTest("testLinkFolder"));
241         suite.addTest(new InternalJNDINodeTest("testLinkDoc"));
242         suite.addTest(new InternalJNDINodeTest("testJNDIEvents"));
243
244         return suite;
245     }
246
247     /**
248      * This method is used within the suite method to get all of the multi threaded tests.
249      * Add all your multi threaded tests in this method with a line like: addTest(suite, "testFunction1", 5);
250      * @return Test the suite of multi-threaded tests in this test case
251      */

252     private static Test getMultiThreadedTests() {
253         TestSuite suite = new ActiveTestSuite();
254
255         /*
256          * add your tests here following these examples:
257          *
258          * addTest(suite, "testFunction1", 5);
259          * addTest(suite, "testFunction2", 10);
260          */

261         addTest(suite, "testThreadedRelativeLink", 1);
262         addTest(suite, "testThreadedAccess1", 1);
263         addTest(suite, "testThreadedAccess2", 1);
264
265         return suite;
266     }
267
268     /**
269      * This method will add the give test to the give suite the specified
270      * number of times. This is best used for multi-threaded tests where
271      * suite is an instance of ActiveTestSuite and you want to run the same test in multiple threads.
272      * @param suite the suite to add the test to.
273      * @param testName the name of the test to add.
274      * @param number the number of times to add the test to the suite
275      */

276     private static void addTest(TestSuite suite, String JavaDoc testName, int number) {
277         for (int count = 0; count < number; count++) {
278             suite.addTest(new InternalJNDINodeTest(testName));
279         }
280     }
281     /**
282      * @see InternalNodeTest#getTestNodeAttributesAbsoluteName()
283      */

284     protected String JavaDoc getTestNodeAttributesAbsoluteName() {
285         return JNDI_NODE_NAME + super.getTestNodeAttributesAbsoluteName();
286     }
287
288     /**
289      * @see org.sape.carbon.core.config.node.event.NodeEventListener#nodeChanged(org.sape.carbon.core.config.node.Node)
290      */

291     public synchronized void nodeChanged(Node changedNode) {
292         this.nodeChangedCalled = true;
293         notify();
294     }
295
296     /**
297      * @see org.sape.carbon.core.config.node.event.NodeEventListener#nodeRemoved(java.lang.String)
298      */

299     public synchronized void nodeRemoved(String JavaDoc removedNodeName) {
300         this.nodeRemovedCalled = true;
301         notify();
302     }
303
304 }
305
Popular Tags