KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Arrays JavaDoc;
21
22 import org.sape.carbon.core.config.Config;
23 import org.sape.carbon.core.config.InvalidConfigurationException;
24 import org.sape.carbon.core.config.PropertyConfiguration;
25
26 import junit.extensions.ActiveTestSuite;
27 import junit.framework.Test;
28 import junit.framework.TestCase;
29 import junit.framework.TestSuite;
30
31
32 /**
33  * Template for junit test harness. Change this description to reflect what
34  * this class is testing.
35  *
36  * Copyright 2002 Sapient
37  * @since carbon 1.0
38  * @author Douglas Voet, April 2002
39  * @version $Revision: 1.11 $($Author: dvoet $ / $Date: 2003/05/05 21:21:17 $)
40  */

41 public class PropertyConfigurationTest extends TestCase {
42
43     private static final String JavaDoc TEST_CHILD_DOCUMENT =
44         "/core/test/ExtendablePropertyConfigTestChild";
45     private static final String JavaDoc TEST_PARENT_DOCUMENT =
46         "/core/test/ExtendablePropertyConfigTestParent";
47
48     private PropertyConfiguration config;
49
50     public PropertyConfigurationTest(String JavaDoc name) {
51         super(name);
52     }
53
54     protected void setUp() throws Exception JavaDoc {
55         super.setUp();
56         config = (PropertyConfiguration)
57             Config.getInstance().createConfiguration(PropertyConfiguration.class);
58     }
59
60     /*
61      * write your test methods here following these examples:
62      *
63      * public void testFunction1() {
64      * test something
65      * }
66      *
67      * public void testFunction2() {
68      * test something else
69      * }
70      */

71
72     /**
73      * Test the read-only and writable states of a config document
74      */

75     public void testWritable() {
76         String JavaDoc testStringName = "WriteTestProperty";
77         String JavaDoc testString = "FooBarBazSplat";
78
79         if (!this.config.isConfigurationWritable()) {
80             fail("Configuration was not created writable.");
81         }
82
83         this.config.setConfigurationReadOnly();
84         try {
85             this.config.setProperty(testStringName, testString);
86             fail("Did not receive the expected exception when writing to a read-only document.");
87         } catch (UnsupportedOperationException JavaDoc uoe) {
88             // Expected
89
}
90
91         this.config = (PropertyConfiguration) this.config.clone();
92         if (!this.config.isConfigurationWritable()) {
93             fail("Configuration clone was not writable");
94         }
95     }
96
97
98     public void testStringProperty() {
99         final String JavaDoc propertyName = "MyString";
100         final String JavaDoc expected = "Hello, World!";
101
102         this.config.setProperty(propertyName, expected);
103         String JavaDoc value = this.config.getProperty(propertyName);
104         if (!expected.equals(value)) {
105             fail("String property failed, got [" +
106                 value + "] expected [" +
107                 expected + "].");
108         }
109     }
110
111     public void testShortProperty() {
112         final String JavaDoc propertyName = "MyShort";
113         final short expected = 3;
114
115         this.config.setShortProperty(propertyName, expected);
116         short value = this.config.getShortProperty(propertyName);
117         if (expected != value) {
118             fail("Short property failed, got [" +
119                 value + "] expected [" +
120                 expected + "].");
121         }
122     }
123
124     public void testIntProperty() {
125         final String JavaDoc propertyName = "MyInt";
126         final int expected = 4;
127
128         this.config.setIntProperty(propertyName, expected);
129         int value = this.config.getIntProperty(propertyName);
130         if (expected != value) {
131             fail("Int property failed, got [" +
132                 value + "] expected [" +
133                 expected + "].");
134         }
135     }
136
137     public void testLongProperty() {
138         final String JavaDoc propertyName = "MyLong";
139         final long expected = 5;
140
141         this.config.setLongProperty(propertyName, expected);
142         long value = this.config.getLongProperty(propertyName);
143         if (expected != value) {
144             fail("Long property failed, got [" +
145                 value + "] expected [" +
146                 expected + "].");
147         }
148     }
149
150     public void testWSVProperty() {
151         final String JavaDoc propertyName = "MyWhiteSpacedValue";
152         final String JavaDoc[] expected = {"A","B","C"};
153
154         this.config.setWSVProperty(propertyName, expected);
155         String JavaDoc[] value = this.config.getWSVProperty(propertyName);
156
157         if ( ! Arrays.equals(expected, value) ) {
158             fail("The retrieved array " + Arrays.asList(value)+
159                 " doesnt match with the expected values "+Arrays.asList(expected));
160         }
161     }
162
163     public void testCSVProperty() {
164         final String JavaDoc propertyName = "MyCommaSpacedValue";
165         final String JavaDoc[] expected = {"D","E","F"};
166
167         this.config.setCSVProperty(propertyName, expected);
168         String JavaDoc[] value = this.config.getCSVProperty(propertyName);
169
170         if ( ! Arrays.equals(expected, value) ) {
171             fail("The retrieved array " + Arrays.asList(value)+
172                 " doesnt match with the expected values "+Arrays.asList(expected));
173         }
174     }
175
176     public void testDeepProperty() {
177         final String JavaDoc propertyName = "MyArray.MyFloat";
178         final float expected = 1.1F;
179
180         this.config.setFloatProperty(propertyName, expected);
181         float value = this.config.getFloatProperty(propertyName);
182         if (expected != value) {
183             fail("Deep float property failed, got [" +
184                 value + "] expected [" +
185                 expected + "].");
186         }
187     }
188
189     public void testDeeperProperty() {
190         final String JavaDoc propertyName = "A.B.C.D.E.F.G";
191         final String JavaDoc expected = "foobar";
192
193         this.config.setProperty(propertyName, expected);
194         String JavaDoc value = this.config.getProperty(propertyName);
195         if (!expected.equals(value)) {
196             fail("Deep string property failed, got [" +
197                 value + "] expected [" +
198                 expected + "].");
199         }
200     }
201
202     public void testDefaultValues() {
203         final String JavaDoc propertyName = "BogusName";
204         final String JavaDoc defaultValue = "foobar";
205
206         try {
207             this.config.getProperty(propertyName);
208             fail("Did not catch expected InvalidConfigurationException");
209         } catch (InvalidConfigurationException ice) {
210             // expected
211
}
212
213         String JavaDoc value = this.config.getProperty(propertyName, defaultValue);
214
215         if (!defaultValue.equals(value)) {
216             fail("Property default failed, got [" +
217                 value + "] expected [" +
218                 defaultValue + "].");
219         }
220     }
221
222     public void testChildStringProperty() {
223         PropertyConfiguration configParent =
224             (PropertyConfiguration)
225             Config.getInstance().fetchConfiguration(TEST_PARENT_DOCUMENT);
226
227         PropertyConfiguration configChild =
228             (PropertyConfiguration)
229             Config.getInstance().fetchConfiguration(TEST_CHILD_DOCUMENT);
230
231         String JavaDoc value = configChild.getProperty("MyString");
232         String JavaDoc expected = "Hello, World!";
233         if (!expected.equals(value)) {
234             fail("String property failed, got [" +
235                 value + "] expected [" +
236                 expected + "].");
237         }
238
239         if (!configChild.getProperty("MyStringP").equals(
240            configParent.getProperty("MyStringP"))) {
241
242             fail("String property inheritance failed, child value did not " +
243             "match parent, child: [" + configChild.getProperty("MyStringP") +
244             "], parent: [" + configParent.getProperty("MyStringP") + "]");
245         }
246     }
247
248     public void testChildShortProperty() {
249         PropertyConfiguration configParent =
250             (PropertyConfiguration)
251             Config.getInstance().fetchConfiguration(TEST_PARENT_DOCUMENT);
252
253         PropertyConfiguration configChild =
254             (PropertyConfiguration)
255             Config.getInstance().fetchConfiguration(TEST_CHILD_DOCUMENT);
256
257         short value = configChild.getShortProperty("MyShort");
258         short expected = 3;
259         if (expected != value) {
260             fail("Short property failed, got [" +
261                 value + "] expected [" +
262                 expected + "].");
263         }
264
265         if (configChild.getShortProperty("MyShortP") !=
266            configParent.getShortProperty("MyShortP")) {
267
268             fail("Short property inheritance failed, child value did not " +
269             "match parent, child: [" + configChild.getShortProperty("MyShortP") +
270             "], parent: [" + configParent.getShortProperty("MyShortP") + "]");
271         }
272     }
273
274     public void testChildIntProperty() {
275         PropertyConfiguration configParent =
276             (PropertyConfiguration)
277             Config.getInstance().fetchConfiguration(TEST_PARENT_DOCUMENT);
278
279         PropertyConfiguration configChild =
280             (PropertyConfiguration)
281             Config.getInstance().fetchConfiguration(TEST_CHILD_DOCUMENT);
282
283         int value = configChild.getIntProperty("MyInt");
284         int expected = 4;
285         if (expected != value) {
286             fail("Int property failed, got [" +
287                 value + "] expected [" +
288                 expected + "].");
289         }
290
291         if (configChild.getIntProperty("MyIntP") !=
292            configParent.getIntProperty("MyIntP")) {
293
294             fail("Int property inheritance failed, child value did not " +
295             "match parent, child: [" + configChild.getIntProperty("MyIntP") +
296             "], parent: [" + configParent.getIntProperty("MyIntP") + "]");
297         }
298     }
299
300     public void testChildLongProperty() {
301         PropertyConfiguration configParent =
302             (PropertyConfiguration)
303             Config.getInstance().fetchConfiguration(TEST_PARENT_DOCUMENT);
304
305         PropertyConfiguration configChild =
306             (PropertyConfiguration)
307             Config.getInstance().fetchConfiguration(TEST_CHILD_DOCUMENT);
308
309         long value = configChild.getLongProperty("MyLong");
310         long expected = 5;
311         if (expected != value) {
312             fail("Long property failed, got [" +
313                 value + "] expected [" +
314                 expected + "].");
315         }
316
317         if (configChild.getLongProperty("MyLongP") !=
318            configParent.getLongProperty("MyLongP")) {
319
320             fail("Long property inheritance failed, child value did not " +
321             "match parent, child: [" + configChild.getLongProperty("MyLongP") +
322             "], parent: [" + configParent.getLongProperty("MyLongP") + "]");
323         }
324     }
325
326     public void testChildDeepProperty() {
327         PropertyConfiguration configParent =
328             (PropertyConfiguration)
329             Config.getInstance().fetchConfiguration(TEST_PARENT_DOCUMENT);
330
331         PropertyConfiguration configChild =
332             (PropertyConfiguration)
333             Config.getInstance().fetchConfiguration(TEST_CHILD_DOCUMENT);
334
335         float value = configChild.getFloatProperty("MyArray.MyFloat");
336         float expected = 1.1F;
337         if (expected != value) {
338             fail("Deep Float property failed, got [" +
339                 value + "] expected [" +
340                 expected + "].");
341         }
342
343         if (configChild.getFloatProperty("MyArrayP.MyFloat") !=
344            configParent.getFloatProperty("MyArrayP.MyFloat")) {
345
346             fail("Long property inheritance failed, child value did not " +
347             "match parent, child: [" + configChild.getFloatProperty("MyArrayP.MyFloat") +
348             "], parent: [" + configParent.getFloatProperty("MyArrayP.MyFloat") + "]");
349         }
350     }
351
352     public void testChildDeeperProperty() {
353         PropertyConfiguration configParent =
354             (PropertyConfiguration)
355             Config.getInstance().fetchConfiguration(TEST_PARENT_DOCUMENT);
356
357         PropertyConfiguration configChild =
358             (PropertyConfiguration)
359             Config.getInstance().fetchConfiguration(TEST_CHILD_DOCUMENT);
360
361         String JavaDoc value = configChild.getProperty("A.B.C.D.E.F.G");
362         String JavaDoc expected = "foobar";
363         if (!expected.equals(value)) {
364             fail("Deep string property failed, got [" +
365                 value + "] expected [" +
366                 expected + "].");
367         }
368
369         if (configChild.getProperty("AP.B.C.D.E.F.G") !=
370            configParent.getProperty("AP.B.C.D.E.F.G")) {
371
372             fail("Deep string property inheritance failed, child value did not " +
373             "match parent, child: [" + configChild.getProperty("AP.B.C.D.E.F.G") +
374             "], parent: [" + configParent.getProperty("AP.B.C.D.E.F.G") + "]");
375         }
376     }
377
378     public void testOverriddenProperty() {
379         PropertyConfiguration configChild =
380             (PropertyConfiguration)
381             Config.getInstance().fetchConfiguration(TEST_CHILD_DOCUMENT);
382
383         String JavaDoc value = configChild.getProperty("Overriden");
384         String JavaDoc expected = "bar";
385         if (!expected.equals(value)) {
386             fail("Deep string property failed, got [" +
387                 value + "] expected [" +
388                 expected + "].");
389         }
390     }
391
392     /**
393      * Method called by jUnit to get all the tests in this test case.
394      * @return Test the suite of tests in this test case
395      */

396     public static Test suite() {
397         TestSuite masterSuite = new TestSuite();
398
399         // add single threaded tests
400
Test singleThreadedTests = getSingleThreadedTests();
401         if (singleThreadedTests != null) {
402             masterSuite.addTest(singleThreadedTests);
403         }
404
405         // add multi threaded tests
406
Test multiThreadedTests = getMultiThreadedTests();
407         if (multiThreadedTests != null) {
408             masterSuite.addTest(multiThreadedTests);
409         }
410
411         return masterSuite;
412     }
413
414     /**
415      * This method is used within the suite method to get all of the single
416      * threaded tests.
417      *
418      * Add all your single threaded tests in this method with a line like:
419      * suite.addTest(new PropertyConfigurationTest("testFunction1"));
420      *
421      * @return Test the suite of single threaded tests in this test case
422      */

423     private static Test getSingleThreadedTests() {
424         TestSuite suite = new TestSuite();
425
426         suite.addTest(new PropertyConfigurationTest("testWritable"));
427         suite.addTest(new PropertyConfigurationTest("testStringProperty"));
428         suite.addTest(new PropertyConfigurationTest("testShortProperty"));
429         suite.addTest(new PropertyConfigurationTest("testIntProperty"));
430         suite.addTest(new PropertyConfigurationTest("testLongProperty"));
431         suite.addTest(new PropertyConfigurationTest("testDeepProperty"));
432         suite.addTest(new PropertyConfigurationTest("testDeeperProperty"));
433
434         suite.addTest(new PropertyConfigurationTest("testChildStringProperty"));
435         suite.addTest(new PropertyConfigurationTest("testChildShortProperty"));
436         suite.addTest(new PropertyConfigurationTest("testChildIntProperty"));
437         suite.addTest(new PropertyConfigurationTest("testChildLongProperty"));
438         suite.addTest(new PropertyConfigurationTest("testChildDeepProperty"));
439         suite.addTest(new PropertyConfigurationTest("testChildDeeperProperty"));
440
441         suite.addTest(new PropertyConfigurationTest("testOverriddenProperty"));
442
443         suite.addTest(new PropertyConfigurationTest("testWSVProperty"));
444         suite.addTest(new PropertyConfigurationTest("testCSVProperty"));
445         suite.addTest(new PropertyConfigurationTest("testDefaultValues"));
446
447         return suite;
448     }
449
450     /**
451      * This method is used within the suite method to get all of the multi
452      * threaded tests.
453      *
454      * Add all your multi threaded tests in this method with a line like:
455      * addTest(suite, "testFunction1", 5);
456      *
457      * @return Test the suite of multi-threaded tests in this test case
458      */

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

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

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