KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > config > jar > test > InternalJarNodeTest


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.jar.test;
19
20 import org.sape.carbon.core.config.Config;
21 import org.sape.carbon.core.config.ConfigurationException;
22 import org.sape.carbon.core.config.node.ConfigurationDocument;
23 import org.sape.carbon.core.config.node.Folder;
24 import org.sape.carbon.core.config.test.InternalNodeTest;
25
26 import junit.extensions.ActiveTestSuite;
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29
30 /**
31  *
32  * Copyright 2002 Sapient
33  * @since carbon 1.0
34  * @author Douglas Voet, April 2002
35  * @version $Revision: 1.2 $($Author: dvoet $ / $Date: 2003/05/05 21:21:10 $)
36  */

37 public class InternalJarNodeTest extends InternalNodeTest {
38     private static final String JavaDoc JAR_NODE_NAME =
39         "/jar_config/test/JarNodeTest";
40     private static final String JavaDoc THREAD_TEST_NODE_NAME1 =
41         "TestNode1";
42     private static final String JavaDoc THREAD_TEST_NODE_NAME2 =
43         "TestNode2";
44     private static final String JavaDoc THREAD_LINK_TEST_NODE_NAME =
45         "TestNode3";
46     private static final String JavaDoc LINK_TARGET_NAME =
47         "TestNode3Target";
48
49     /**
50      * Constructor for InternalJarNodeTest.
51      * @param name
52      */

53     public InternalJarNodeTest(String JavaDoc name) {
54         super(name);
55     }
56
57     /**
58      * @see TestCase#setUp()
59      */

60     protected void setUp() throws Exception JavaDoc {
61         super.root = (Folder) Config.getInstance().
62             fetchNode(InternalJarNodeTest.JAR_NODE_NAME);
63     }
64
65     public void testThreadedRelativeLink() {
66         try {
67             testSetAndGetAttribute((ConfigurationDocument)
68                 super.root.fetchChild(THREAD_LINK_TEST_NODE_NAME));
69         } catch (ConfigurationException ce) {
70             fail("caught ConfigurationException: " + ce);
71         }
72     }
73
74     public void testThreadedAccess1() {
75         try {
76             testSetAndGetAttribute((ConfigurationDocument)
77                 super.root.fetchChild(THREAD_TEST_NODE_NAME1));
78         } catch (ConfigurationException ce) {
79             fail("caught ConfigurationException: " + ce);
80         }
81     }
82
83     public void testThreadedAccess2() {
84         try {
85             testSetAndGetAttribute((ConfigurationDocument)
86                 super.root.fetchChild(THREAD_TEST_NODE_NAME2));
87         } catch (ConfigurationException ce) {
88             fail("caught ConfigurationException: " + ce);
89         }
90     }
91
92     private void testSetAndGetAttribute(ConfigurationDocument doc)
93         throws ConfigurationException {
94
95         for (int i=0; i<5; i++) {
96             TestJarConfiguration config = (TestJarConfiguration)
97                 doc.readConfiguration();
98
99             int newValue = config.getValue();
100             config.setValue(newValue);
101             doc.writeConfiguration(config);
102             config = (TestJarConfiguration) doc.readConfiguration();
103
104             if (config.getValue() != newValue) {
105                 fail("value within config was not the expected value, expected ["
106                     + newValue
107                     + "], actual [" + config.getValue() + "]");
108             }
109         }
110     }
111
112     /**
113      * Method called by jUnit to get all the tests in this test case.
114      * @return Test the suite of tests in this test case
115      */

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

137     private static Test getSingleThreadedTests() {
138         TestSuite suite = new TestSuite();
139
140          suite.addTest(new InternalJarNodeTest("testAddSubFolder"));
141          suite.addTest(new InternalJarNodeTest("testAddConfigurationDocument"));
142          suite.addTest(new InternalJarNodeTest("testWriteConfiguration"));
143          suite.addTest(new InternalJarNodeTest("testReadConfiguration"));
144          suite.addTest(new InternalJarNodeTest("testNodeAttributes"));
145          suite.addTest(new InternalJarNodeTest("testFetchChildren"));
146          suite.addTest(
147             new InternalJarNodeTest("testAddDuplicateConfigurationDocument"));
148          suite.addTest(new InternalJarNodeTest("testAddDuplicateSubFolder"));
149         suite.addTest(new InternalJarNodeTest("testAddNestedConfiguration"));
150         suite.addTest(new InternalJarNodeTest("testReadNestedConfiguration"));
151         suite.addTest(new InternalJarNodeTest("testRemoveNestedConfiguration"));
152          suite.addTest(new InternalJarNodeTest("testNodeRemoval"));
153          suite.addTest(new InternalJarNodeTest("testFetchRemovedNode"));
154          suite.addTest(new InternalJarNodeTest("testNullParameters"));
155          suite.addTest(new InternalJarNodeTest("testLinkFolder"));
156          suite.addTest(new InternalJarNodeTest("testLinkDoc"));
157
158         return suite;
159     }
160
161     /**
162      * This method is used within the suite method to get all of the multi threaded tests.
163      * Add all your multi threaded tests in this method with a line like: addTest(suite, "testFunction1", 5);
164      * @return Test the suite of multi-threaded tests in this test case
165      */

166     private static Test getMultiThreadedTests() {
167         TestSuite suite = new ActiveTestSuite();
168
169         /*
170          * add your tests here following these examples:
171          *
172          * addTest(suite, "testFunction1", 5);
173          * addTest(suite, "testFunction2", 10);
174          */

175          addTest(suite, "testThreadedRelativeLink", 1);
176          addTest(suite, "testThreadedAccess1", 1);
177          addTest(suite, "testThreadedAccess2", 1);
178
179         return suite;
180     }
181
182     /**
183      * This method will add the give test to the give suite the specified
184      * number of times. This is best used for multi-threaded tests where
185      * suite is an instance of ActiveTestSuite and you want to run the same test in multiple threads.
186      * @param suite the suite to add the test to.
187      * @param testName the name of the test to add.
188      * @param number the number of times to add the test to the suite
189      */

190     private static void addTest(TestSuite suite, String JavaDoc testName, int number) {
191         for (int count = 0; count < number; count++) {
192             suite.addTest(new InternalJarNodeTest(testName));
193         }
194     }
195     /**
196      * @see InternalNodeTest#getTestNodeAttributesAbsoluteName()
197      */

198     protected String JavaDoc getTestNodeAttributesAbsoluteName() {
199         return JAR_NODE_NAME + super.getTestNodeAttributesAbsoluteName();
200     }
201
202 }
203
Popular Tags