KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > config > format > test > ConfigurationArrayTypeTest


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.format.test;
19
20 import java.awt.*;
21 import java.util.List JavaDoc;
22 import java.util.ArrayList JavaDoc;
23
24 import org.sape.carbon.core.component.Lookup;
25 import org.sape.carbon.core.component.lifecycle.LifecycleInterceptor;
26 import org.sape.carbon.core.component.lifecycle.LifecycleStateEnum;
27 import org.sape.carbon.core.component.proxy.test.TestComponent;
28 import org.sape.carbon.core.config.Config;
29 import org.sape.carbon.core.config.Configuration;
30 import org.sape.carbon.core.config.ConfigurationStoreException;
31 import org.sape.carbon.core.config.node.Node;
32 import org.sape.carbon.core.config.test.TestConfiguration;
33
34 import junit.extensions.ActiveTestSuite;
35 import junit.framework.Test;
36 import junit.framework.TestCase;
37 import junit.framework.TestSuite;
38
39 /**
40  * <p>Tests the capabilities of the ConfigurationFormatService to handle
41  * arrays of data.</p>
42  *
43  * Copyright 2003 Sapient
44  * @since carbon 2.0
45  * @author Greg Hinkle, March 2003
46  * @version $Revision: 1.8 $($Author: jdreed $ / $Date: 2003/08/16 06:20:35 $)
47  */

48 public class ConfigurationArrayTypeTest extends TestCase {
49
50     public static final String JavaDoc TEST_DOCUMENT = "/core/config/test/ArrayTest";
51
52
53     public static final String JavaDoc NODE_A_PATH = "Path of Node A";
54     public static final String JavaDoc NODE_B_PATH = "Path of Node B";
55
56
57     public ConfigurationArrayTypeTest(String JavaDoc name) {
58         super(name);
59     }
60
61
62
63     public void testStringArray() {
64
65         HolderConfiguration config =
66             (HolderConfiguration)
67             Config.getInstance().createConfiguration(HolderConfiguration.class);
68
69         config.addString("a");
70         config.addString("b");
71         config.addString("c");
72
73         String JavaDoc[] strings = config.getString();
74         for (int i = 0; i < strings.length; i++) {
75             String JavaDoc string = strings[i];
76             // System.out.println("Array entry[" + i + "] = " + string);
77
}
78
79         assertEquals(
80             "String array value not as expected",
81             config.getString(0),"a");
82
83         assertEquals(
84             "String array value not as expected",
85             config.getString(1),"b");
86
87         assertEquals(
88             "String array value not as expected",
89             config.getString(2),"c");
90
91     }
92
93
94     public void testComplexArray() {
95
96         HolderConfiguration config =
97             (HolderConfiguration)
98             Config.getInstance().createConfiguration(HolderConfiguration.class);
99
100         NodeConfiguration nodeA =
101             (NodeConfiguration)
102             Config.getInstance().createConfiguration(NodeConfiguration.class);
103         nodeA.setPath(NODE_A_PATH);
104
105         config.addNode(nodeA);
106
107         NodeConfiguration nodeB =
108             (NodeConfiguration)
109             Config.getInstance().createConfiguration(NodeConfiguration.class);
110         nodeB.setPath(NODE_B_PATH);
111
112         config.addNode(nodeB);
113
114         NodeConfiguration[] nodes = config.getNode();
115         assertEquals("Map did not contain added node by array access.",
116             nodeA.getPath(), nodes[0].getPath());
117
118         assertEquals("Map did not contain added node by array access",
119             nodeB.getPath(), nodes[1].getPath());
120
121         assertEquals("Map did not contain added node by array access.",
122             nodeA.getPath(), config.getNode(0).getPath());
123
124         assertEquals("Map did not contain added node by array access",
125             nodeB.getPath(), config.getNode(1).getPath());
126     }
127
128
129
130     public void testPrimitiveArray() {
131         HolderConfiguration config =
132                     (HolderConfiguration)
133                     Config.getInstance().createConfiguration(HolderConfiguration.class);
134
135         config.addInt(0);
136         config.addInt(1);
137         config.addInt(2);
138         config.addInt(3);
139
140
141         for (int i = 0; i < 4; i++) {
142               assertEquals(
143                   "Array primitive not equal",
144                   config.getInt(i), i);
145         }
146
147     }
148
149     public void testArraySet() {
150         HolderConfiguration config =
151             (HolderConfiguration)
152             Config.getInstance().createConfiguration(HolderConfiguration.class);
153
154         int[] testInts = new int[] { 0,1};
155
156         config.setInt(testInts);
157
158         for (int i = 0; i < testInts.length; i++) {
159             assertEquals(
160                 "Set by Array object did not set array values",
161                 testInts[i],config.getInt(i));
162         }
163     }
164
165     public void testAlterPrimitiveContents() {
166         HolderConfiguration config =
167                     (HolderConfiguration)
168                     Config.getInstance().createConfiguration(HolderConfiguration.class);
169
170         config.addInt(0);
171         config.addInt(1);
172         config.addInt(2);
173         config.addInt(3);
174
175         // Alter a set value in the middle
176
config.setInt(2,42);
177
178         assertEquals(
179             "The altered integer array attribute did not seem to be set properly",
180             config.getInt(2),42);
181
182     }
183
184     public void testArrayChildConfigurations() throws Exception JavaDoc {
185         Node testNode = Config.getInstance().fetchNode(TEST_DOCUMENT);
186         Node[] children = testNode.fetchChildren();
187
188         List JavaDoc validChildren = new ArrayList JavaDoc();
189         validChildren.add("Node[0]");
190         validChildren.add("Node[1]");
191         validChildren.add("NestTest");
192
193         assertTrue(
194             TEST_DOCUMENT +
195             " had wrong number of children, found [" +
196             children.length +
197             "] expected [" + validChildren.size() + "]",
198             children.length == validChildren.size());
199
200         for (int i = 0; i < children.length; i++) {
201             Node child = children[i];
202             assertTrue(
203                 TEST_DOCUMENT +
204                 " contained unexpected or duplicate child name [" +
205                 child.getName() + "]",
206                 validChildren.remove(child.getName()));
207         }
208     }
209
210     public void testNestedArray() {
211
212         NestedArray root =
213             (NestedArray)
214             Config.getInstance().createConfiguration(NestedArray.class);
215         addNest(root,5);
216
217         checkNest(root,5);
218     }
219
220     protected void addNest(NestedArray nestedArray, int nests) {
221         nestedArray.setName(String.valueOf(nests));
222         if (nests > 0) {
223             --nests;
224             NestedArray nest =
225                 (NestedArray)
226                 Config.getInstance().createConfiguration(NestedArray.class);
227
228             addNest(nest, nests);
229
230             nestedArray.addNestedArray(nest);
231         }
232     }
233     protected void checkNest(NestedArray nestedArray, int nests) {
234         assertEquals(
235             "The nested array was not named according to its key.",
236             String.valueOf(nests), nestedArray.getName());
237
238
239         if (nests > 0) {
240             --nests;
241             NestedArray child = nestedArray.getNestedArray(0);
242             assertNotNull(
243                 "Nested child [" + nests + "] was not found.",
244                 child);
245             checkNest(child, nests);
246         }
247     }
248
249
250     public void testPersist() throws ConfigurationStoreException {
251
252         HolderConfiguration config =
253             (HolderConfiguration)
254             Config.getInstance().createConfiguration(HolderConfiguration.class);
255
256
257         NodeConfiguration nodeA =
258             (NodeConfiguration)
259             Config.getInstance().createConfiguration(NodeConfiguration.class);
260         nodeA.setPath("Path of Node A");
261         config.addNode(nodeA);
262
263
264         NodeConfiguration nodeB =
265             (NodeConfiguration)
266             Config.getInstance().createConfiguration(NodeConfiguration.class);
267         nodeB.setPath("Path of Node B");
268         config.addNode(nodeB);
269
270         config.addString("a");
271         config.addString("b");
272         config.addString("c");
273
274
275         config.addInt(0);
276         config.addInt(1);
277         config.addInt(2);
278         config.addInt(3);
279
280
281         NestedArray root =
282             (NestedArray)
283             Config.getInstance().createConfiguration(NestedArray.class);
284
285         addNest(root,5);
286         config.setNestTest(root);
287
288         Config.getInstance().storeConfiguration(TEST_DOCUMENT, config);
289
290     }
291
292     public void testChildAccess() {
293         NodeConfiguration config =
294             (NodeConfiguration)
295             Config.getInstance().fetchWritableConfiguration(
296                 TEST_DOCUMENT + "/Node[0]");
297
298         assertEquals(
299             "The child-accessed configuration did not contain the expected property",
300             config.getPath(), NODE_A_PATH);
301     }
302
303     public void testAlterChild() throws ConfigurationStoreException {
304         String JavaDoc alteredPath = "AlteredPath";
305         String JavaDoc testChild = TEST_DOCUMENT + "/Node[0]";
306
307         NodeConfiguration child =
308             (NodeConfiguration)
309             Config.getInstance().createConfiguration(NodeConfiguration.class);
310         child.setPath(alteredPath);
311         Config.getInstance().storeConfiguration(
312             testChild,
313             child);
314
315         NodeConfiguration readChild =
316             (NodeConfiguration)
317             Config.getInstance().fetchConfiguration(testChild);
318
319         assertEquals(
320             "The child, altered configuration did not contain the expected path",
321             readChild.getPath(), alteredPath);
322     }
323
324     public void testNonCacheableTypes() {
325         // set the component in config
326
HolderConfiguration config =
327             (HolderConfiguration)
328             Config.getInstance().createConfiguration(HolderConfiguration.class);
329             
330         config.addTestComponents(
331             (TestComponent)
332             Lookup.getInstance().fetchComponent("/core/config/test/TestComponent"));
333
334         // get the value again so it gets cached
335
LifecycleInterceptor lifecycleView =
336             (LifecycleInterceptor) config.getTestComponents(0);
337         lifecycleView.destroyComponent();
338         
339         // get the component again, if it is destroyed, then it is cached and
340
// the test should fail
341
lifecycleView =
342             (LifecycleInterceptor) config.getTestComponents(0);
343         
344         TestCase.assertTrue(
345             "Config object returned a stale component reference which " +
346             "means it was cached and it should not have been",
347             lifecycleView.getLifecycleState() == LifecycleStateEnum.RUNNING);
348     }
349
350     public static interface HolderConfiguration extends Configuration {
351
352         String JavaDoc[] getString();
353         String JavaDoc getString(int index);
354         void setString(int index, String JavaDoc value);
355         void setString(String JavaDoc[] values);
356         void addString(String JavaDoc value);
357
358
359         NodeConfiguration[] getNode();
360         NodeConfiguration getNode(int index);
361         void setNode(int index, NodeConfiguration node);
362         void setNode(NodeConfiguration[] nodeArray);
363         void addNode(NodeConfiguration nodeArray);
364
365         int[] getInt();
366         int getInt(int index);
367         void setInt(int index, int value);
368         void setInt(int[] integers);
369         void addInt(int value);
370
371         NestedArray getNestTest();
372         void setNestTest(NestedArray nest);
373         
374         TestComponent[] getTestComponents();
375         TestComponent getTestComponents(int index);
376         void setTestComponents(TestComponent[] components);
377         void setTestComponents(int index, TestComponent component);
378         void addTestComponents(TestComponent component);
379     }
380
381     public static interface NodeConfiguration extends Configuration {
382         String JavaDoc getPath();
383         void setPath(String JavaDoc path);
384
385     }
386
387
388     public static interface NestedArray extends Configuration {
389
390         String JavaDoc getName();
391         void setName(String JavaDoc value);
392
393         NestedArray[] getNestedArray();
394         NestedArray getNestedArray(int index);
395         void setNestedArray(int index, NestedArray value);
396         void setNestedArray(NestedArray[] nestedArray);
397         void addNestedArray(NestedArray nestedArray);
398
399     }
400
401
402
403
404     /**
405      * Method called by jUnit to get all the tests in this test case.
406      * @return Test the suite of tests in this test case
407      */

408     public static Test suite() {
409         TestSuite masterSuite = new TestSuite();
410
411         // add single threaded tests
412
Test singleThreadedTests = getSingleThreadedTests();
413         if (singleThreadedTests != null) {
414             masterSuite.addTest(singleThreadedTests);
415         }
416
417         // add multi threaded tests
418
Test multiThreadedTests = getMultiThreadedTests();
419         if (multiThreadedTests != null) {
420             masterSuite.addTest(multiThreadedTests);
421         }
422
423         return masterSuite;
424     }
425
426     /**
427      * This method is used within the suite method to get all of the single
428      * threaded tests.
429      *
430      * Add all your single threaded tests in this method with a line like:
431      * suite.addTest(new ConfigurationMapTypeTest("testFunction1"));
432      *
433      * @return Test the suite of single threaded tests in this test case
434      */

435     private static Test getSingleThreadedTests() {
436         TestSuite suite = new TestSuite();
437
438         suite.addTest(new ConfigurationArrayTypeTest("testPrimitiveArray"));
439         suite.addTest(new ConfigurationArrayTypeTest("testNestedArray"));
440         suite.addTest(new ConfigurationArrayTypeTest("testComplexArray"));
441         suite.addTest(new ConfigurationArrayTypeTest("testAlterPrimitiveContents"));
442         suite.addTest(new ConfigurationArrayTypeTest("testStringArray"));
443         suite.addTest(new ConfigurationArrayTypeTest("testArraySet"));
444         suite.addTest(new ConfigurationArrayTypeTest("testPersist"));
445         suite.addTest(new ConfigurationArrayTypeTest("testChildAccess"));
446         suite.addTest(new ConfigurationArrayTypeTest("testAlterChild"));
447         suite.addTest(new ConfigurationArrayTypeTest("testArrayChildConfigurations"));
448         suite.addTest(new ConfigurationArrayTypeTest("testNonCacheableTypes"));
449
450         return suite;
451     }
452
453     /**
454      * This method is used within the suite method to get all of the multi
455      * threaded tests.
456      *
457      * Add all your multi threaded tests in this method with a line like:
458      * addTest(suite, "testFunction1", 5);
459      *
460      * @return Test the suite of multi-threaded tests in this test case
461      */

462     private static Test getMultiThreadedTests() {
463         TestSuite suite = new ActiveTestSuite();
464         /*
465          * add your tests here following these examples:
466          *
467          * addTest(suite, "testFunction1", 5);
468          * addTest(suite, "testFunction2", 10);
469          */

470         return suite;
471     }
472
473     /**
474      * This method will add the give test to the give suite the specified
475      * number of times. This is best used for multi-threaded tests where
476      * suite is an instance of ActiveTestSuite and you want to run the same
477      * test in multiple threads.
478      *
479      * @param suite the suite to add the test to.
480      * @param testName the name of the test to add.
481      * @param number the number of times to add the test to the suite
482      */

483     private static void addTest(TestSuite suite, String JavaDoc testName, int number) {
484         for (int count = 0; count < number; count++) {
485             suite.addTest(new ConfigurationArrayTypeTest(testName));
486         }
487     }
488 }
Popular Tags