KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Calendar JavaDoc;
22 import java.util.Date 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.InvalidConfigurationException;
31 import org.sape.carbon.core.config.PropertyConfiguration;
32 import org.sape.carbon.core.config.format.ConfigurationFormatService;
33 import org.sape.carbon.core.config.format.DefaultConfigurationFormatService;
34 import org.sape.carbon.core.config.node.Node;
35 import org.sape.carbon.core.exception.InvalidParameterException;
36 import org.sape.carbon.core.util.classify.SeverityEnum;
37
38 import junit.extensions.ActiveTestSuite;
39 import junit.framework.Test;
40 import junit.framework.TestCase;
41 import junit.framework.TestSuite;
42
43 /**
44  * This test harness tests the functionality of implementing a configuration
45  * interface over top of a backing datastore.
46  *
47  * Copyright 2002 Sapient
48  * @since carbon 1.0
49  * @author Greg Hinkle, February 2002
50  * @version $Revision: 1.37 $ ($Version: $ / $Date: 2003/07/29 19:50:19 $)
51  */

52 public class ConfigurationFormatServiceTest extends TestCase {
53
54     public static final String JavaDoc TEST_STORE_LOCATION = "/core/test/WriteTest";
55     public static final String JavaDoc TEST_TOKEN_LOCATION = "/core/test/TokenTest";
56     public static final String JavaDoc PROPERTY_TOKEN_LOCATION =
57         "/core/test/PropertyTokens";
58     public static final String JavaDoc CONFIG_TOKEN_LOCATION =
59         "/core/test/ConfigTokens";
60
61
62     public ConfigurationFormatServiceTest(String JavaDoc name) {
63         super(name);
64     }
65
66
67     /**
68      * Method called by jUnit to get all the tests in this test case.
69      * @return Test the suite of tests in this test case
70      */

71     public static Test suite() {
72         TestSuite masterSuite = new TestSuite();
73
74         // add single threaded tests
75
Test singleThreadedTests = getSingleThreadedTests();
76         if (singleThreadedTests != null) {
77             masterSuite.addTest(singleThreadedTests);
78         }
79
80         // add multi threaded tests
81
Test multiThreadedTests = getMultiThreadedTests();
82         if (multiThreadedTests != null) {
83             masterSuite.addTest(multiThreadedTests);
84         }
85
86         return masterSuite;
87     }
88
89     /**
90      * <P>This method is used within the suite method to get all of the single
91      * threaded tests.</P>
92      *
93      * testWritable<BR>
94      * testNewConfiguration<BR>
95      * testChangeBool<BR>
96      * testChangeByte<BR>
97      * testChangeShort<BR>
98      * testChangeInt<BR>
99      * testChangeLong<BR>
100      * testChangeFloat<BR>
101      * testChangeDouble<BR>
102      * testChangeString<BR>
103      * testChangeClass<BR>
104      * testChangeDate<BR>
105      * testEmptyArray<BR>
106      * testNullValue<BR>
107      * testEnumSupport<BR>
108      * testArrayAccess<BR>
109      * testIndexedArrayAccess<BR>
110      * testArrayBounds<BR>
111      * testAddArrayItem<BR>
112      * testDefaultingConfig<BR>
113      * testSuperInterfaceDefaultedConfigurations<BR>
114      * testConfigurationWrite<BR>
115      * testConfigRead<BR>
116      * testClone<BR>
117      * testReferences<BR>
118      * testTokenReplacement<BR>
119      *
120      * @return Test the suite of single threaded tests in this test case
121      */

122     private static Test getSingleThreadedTests() {
123         TestSuite suite = new TestSuite();
124         /*
125          * add your tests here following these examples:
126          *
127          * suite.addTest(new %Name%Test("testFunction1"));
128          * suite.addTest(new %Name%Test("testFunction2"));
129          */

130
131         suite.addTest(new ConfigurationFormatServiceTest("testNewConfiguration"));
132         suite.addTest(new ConfigurationFormatServiceTest("testWritable"));
133         suite.addTest(new ConfigurationFormatServiceTest("testChangeBool"));
134         suite.addTest(new ConfigurationFormatServiceTest("testChangeByte"));
135         suite.addTest(new ConfigurationFormatServiceTest("testChangeShort"));
136         suite.addTest(new ConfigurationFormatServiceTest("testChangeInt"));
137         suite.addTest(new ConfigurationFormatServiceTest("testChangeLong"));
138         suite.addTest(new ConfigurationFormatServiceTest("testChangeFloat"));
139         suite.addTest(new ConfigurationFormatServiceTest("testChangeDouble"));
140         suite.addTest(new ConfigurationFormatServiceTest("testChangeString"));
141         suite.addTest(new ConfigurationFormatServiceTest("testChangeClass"));
142         suite.addTest(new ConfigurationFormatServiceTest("testChangeDate"));
143         suite.addTest(new ConfigurationFormatServiceTest("testEmptyArray"));
144         suite.addTest(new ConfigurationFormatServiceTest("testNullValue"));
145         suite.addTest(new ConfigurationFormatServiceTest("testChangeSubConfiguration"));
146         suite.addTest(new ConfigurationFormatServiceTest("testReferences"));
147
148         suite.addTest(new ConfigurationFormatServiceTest("testEnumSupport"));
149
150         suite.addTest(new ConfigurationFormatServiceTest("testArrayAccess"));
151         suite.addTest(new ConfigurationFormatServiceTest("testIndexedArrayAccess"));
152         suite.addTest(new ConfigurationFormatServiceTest("testArrayBounds"));
153         suite.addTest(new ConfigurationFormatServiceTest("testAddArrayItem"));
154
155         suite.addTest(new ConfigurationFormatServiceTest("testFormatServiceConfigAccess"));
156
157         suite.addTest(new ConfigurationFormatServiceTest("testDefaultingConfig"));
158         suite.addTest(new ConfigurationFormatServiceTest("testSuperInterfaceDefaultedConfigurations"));
159
160         suite.addTest(new ConfigurationFormatServiceTest("testConfigurationWrite"));
161         suite.addTest(new ConfigurationFormatServiceTest("testConfigRead"));
162
163         suite.addTest(new ConfigurationFormatServiceTest("testClone"));
164
165         suite.addTest(new ConfigurationFormatServiceTest("testTokenReplacement"));
166
167         suite.addTest(new ConfigurationFormatServiceTest("testEqualsAndHashCode"));
168         suite.addTest(new ConfigurationFormatServiceTest("testNonCacheableTypes"));
169
170         return suite;
171     }
172
173     /**
174      * This method is used within the suite method to get all of the multi
175      * threaded tests.
176      *
177      * Add all your multi threaded tests in this method with a line like:
178      * addTest(suite, "testFunction1", 5);
179      *
180      * @return Test the suite of multi-threaded tests in this test case
181      */

182     private static Test getMultiThreadedTests() {
183         TestSuite suite = new ActiveTestSuite();
184         /*
185          * add your tests here following these examples:
186          *
187          * addTest(suite, "testFunction1", 5);
188          * addTest(suite, "testFunction2", 10);
189          */

190         addTest(suite, "testConfigRead",20);
191
192         return suite;
193     }
194
195     /**
196      * This method will add the give test to the give suite the specified
197      * number of times. This is best used for multi-threaded tests where
198      * suite is an instance of ActiveTestSuite and you want to run the same
199      * test in multiple threads.
200      *
201      * @param suite the suite to add the test to.
202      * @param testName the name of the test to add.
203      * @param number the number of times to add the test to the suite
204      */

205     private static void addTest(TestSuite suite, String JavaDoc testName, int number) {
206         for(int count=0; count<number; count++) {
207             suite.addTest(new ConfigurationFormatServiceTest(testName));
208         }
209     }
210
211
212
213
214
215
216
217
218     private static ConfigurationFormatService configurationFormatService;
219     private static FormatTestConfiguration config;
220
221
222     private static boolean BOOL = true;
223     private static byte BYTE = Byte.MAX_VALUE - 1;
224     private static short SHORT = Short.MAX_VALUE - 1;
225     private static int INT = Integer.MAX_VALUE - 1;
226     private static long LONG = Long.MAX_VALUE - 1L;
227     private static float FLOAT = Float.MAX_VALUE - 2.2F;
228     private static double DOUBLE = Double.MAX_VALUE - 2.2D;
229     private static String JavaDoc STRING = "Hello, World!";
230     private static Class JavaDoc CLASS = ConfigurationFormatServiceTest.class;
231
232
233     private static Date JavaDoc DATE;
234     static {
235         Calendar JavaDoc cal = Calendar.getInstance();
236         cal.set(2002,8,22,0,0,0);
237         cal.set(Calendar.MILLISECOND,0);
238         DATE = cal.getTime();
239     }
240
241     /**
242      * Tests the creation of a new object implementing a test configuration
243      * interface
244      */

245     public void testNewConfiguration() {
246         ConfigurationFormatServiceTest.configurationFormatService =
247             new DefaultConfigurationFormatService();
248
249         ConfigurationFormatServiceTest.config =
250             (FormatTestConfiguration)
251             ConfigurationFormatServiceTest.
252                 configurationFormatService.
253                 newConfiguration(FormatTestConfiguration.class);
254     }
255
256     /**
257      * Test the read-only and writable states of a config document
258      */

259     public void testWritable() {
260         String JavaDoc testString = "FooBarBazSplat";
261         FormatTestConfiguration testConfig = (FormatTestConfiguration)
262             ConfigurationFormatServiceTest.config.clone();
263
264         if (!testConfig.isConfigurationWritable()) {
265             fail("Configuration clone was not writable");
266         }
267         testConfig.setMyString(testString);
268         if (!testConfig.getMyString().equals(testString)) {
269             fail("Unable to set a string in a document that was writable.");
270         }
271
272         testConfig.setConfigurationReadOnly();
273         try {
274             testConfig.setMyString(testString);
275             fail("Did not receive the expected exception when writing to a read-only document.");
276         } catch (UnsupportedOperationException JavaDoc uoe) {
277             // Expected
278
}
279
280         testConfig = (FormatTestConfiguration) testConfig.clone();
281
282         if (!testConfig.isConfigurationWritable()) {
283             fail("Configuration clone was not writable");
284         }
285
286     }
287
288     /**
289      * Tests the functionality of setting boolean values
290      */

291     public void testChangeBool() {
292         ConfigurationFormatServiceTest.config.setMyBool(BOOL);
293         if (ConfigurationFormatServiceTest.config.getMyBool() != BOOL) {
294             fail("Bool value not the same as value set.");
295         }
296     }
297
298     /**
299      * Tests the functionality of setting byte values
300      */

301     public void testChangeByte() {
302         ConfigurationFormatServiceTest.config.setMyByte(BYTE);
303         if (ConfigurationFormatServiceTest.config.getMyByte() != BYTE) {
304             fail("Byte value not the same as value set.");
305         }
306     }
307
308     /**
309      * Tests the functionality of setting short values
310      */

311     public void testChangeShort() {
312         ConfigurationFormatServiceTest.config.setMyShort(SHORT);
313         if (ConfigurationFormatServiceTest.config.getMyShort() != SHORT) {
314             fail("Short value not the same as value set.");
315         }
316     }
317
318     /**
319      * Tests the functionality of setting int values
320      */

321     public void testChangeInt() {
322         ConfigurationFormatServiceTest.config.setMyInt(INT);
323         if (ConfigurationFormatServiceTest.config.getMyInt() != INT) {
324             fail("Int value not the same as value set.");
325         }
326     }
327
328     /**
329      * Tests the functionality of setting long values
330      */

331     public void testChangeLong() {
332         ConfigurationFormatServiceTest.config.setMyLong(LONG);
333         if (ConfigurationFormatServiceTest.config.getMyLong() != LONG) {
334             fail("Long value not the same as value set.");
335         }
336     }
337
338     /**
339      * Tests the functionality of setting float values
340      */

341     public void testChangeFloat() {
342         ConfigurationFormatServiceTest.config.setMyFloat(FLOAT);
343         if (ConfigurationFormatServiceTest.config.getMyFloat() != FLOAT) {
344             fail("Float value not the same as value set.");
345         }
346     }
347
348     /**
349      * Tests the functionality of setting double values
350      */

351     public void testChangeDouble() {
352         ConfigurationFormatServiceTest.config.setMyDouble(DOUBLE);
353         if (ConfigurationFormatServiceTest.config.getMyDouble() != DOUBLE) {
354             fail("Double value not the same as value set.");
355         }
356     }
357
358     /**
359      * Tests the functionality of setting string values
360      */

361     public void testChangeString() {
362         ConfigurationFormatServiceTest.config.setMyString(STRING);
363         if (ConfigurationFormatServiceTest.config.getMyString() != STRING) {
364             fail("String value not the same as value set.");
365         }
366     }
367
368     /**
369      * Tests the functionality of setting class values
370      */

371     public void testChangeClass() {
372         ConfigurationFormatServiceTest.config.setMyClass(CLASS);
373         if (ConfigurationFormatServiceTest.config.getMyClass() != CLASS) {
374             fail("Class value not the same as value set.");
375         }
376     }
377
378     /**
379      * Tests the functionality of setting class values
380      */

381     public void testChangeDate() {
382         ConfigurationFormatServiceTest.config.setMyDate(DATE);
383         if (!ConfigurationFormatServiceTest.config.getMyDate().equals(DATE)) {
384             fail("Date value [" +
385                 DATE +
386                 "] not the same as value set [" +
387                 ConfigurationFormatServiceTest.config.getMyDate() + "].");
388         }
389     }
390
391     /**
392      * Tests the functionality of setting class values
393      */

394     public void testChangeSubConfiguration() {
395
396         Configuration newConfig =
397             configurationFormatService.newConfiguration(Configuration.class);
398
399         String JavaDoc expectedSubName =
400             ConfigurationFormatServiceTest.config.getConfigurationName()
401                 + Node.DELIMITER
402                 + "SubConfiguration";
403
404         ConfigurationFormatServiceTest
405             .config
406             .setSubConfiguration(newConfig);
407
408         assertTrue(
409             "Reference to original configuration was not maintained",
410             ConfigurationFormatServiceTest
411                 .config
412                 .getSubConfiguration()
413                 .getConfigurationName()
414                 .equals(expectedSubName));
415     }
416
417     /**
418      * This tests the configuration of a configuration reference
419      */

420     public void testReferences() {
421         ConfigurationFormatServiceTest
422             .config
423             .setSubConfigurationRef(
424                 (Config.getInstance().fetchConfiguration(TEST_TOKEN_LOCATION)));
425
426         assertTrue(
427             "Reference to original configuration was not maintained",
428             ConfigurationFormatServiceTest
429                 .config
430                 .getSubConfigurationRef()
431                 .getConfigurationName()
432                 .equals(TEST_TOKEN_LOCATION));
433     }
434
435     public void testEnumSupport() {
436         ConfigurationFormatServiceTest.config.setEnum(SeverityEnum.FATAL);
437         if (ConfigurationFormatServiceTest.config.getEnum() != SeverityEnum.FATAL) {
438             fail("Enum value not the same as value set.");
439         }
440     }
441
442
443     public void testNullValue() {
444         String JavaDoc value = ConfigurationFormatServiceTest.config.getNullValue();
445
446         if (value != null) {
447             fail("Was expecting attribute [NullValue] to be null, got: " + value);
448         }
449
450         boolean failed = false;
451         int intValue = -100;
452         try {
453             intValue = ConfigurationFormatServiceTest.config.getNullPrimitive();
454         } catch (InvalidConfigurationException ice) {
455             failed = true;
456         }
457         if (!failed) {
458             fail("Expected thrown exception when retrieving a primitive that was null, but got [" +
459                 intValue + "].");
460         }
461
462
463         value = ConfigurationFormatServiceTest.config.getDefaultNullValue();
464
465         if (value != null) {
466             fail("Was expecting attribute [DefaultNullValue] to be null, got: " + value);
467         }
468     }
469
470
471     /**
472      * Tests to ensure that arrays that contain no values are not null in this
473      * configuration implementation.
474      */

475     public void testEmptyArray() {
476         String JavaDoc[] empty = ConfigurationFormatServiceTest.config.getEmptyArray();
477
478         if (empty == null) {
479             fail("Empty array was null.");
480         }
481
482         if (empty.length != 0) {
483             fail("Emtpy array had a non-zero length of: " + empty.length);
484         }
485
486     }
487
488     /**
489      * Tests to ensure that gets and sets of entire arrays work for arrays of
490      * primitives, objects (Strings), and sub-configurations.
491      */

492     public void testArrayAccess() {
493         // int value tests
494
int[] testIntArray = new int[] { 1, 2, 3, 4 };
495         ConfigurationFormatServiceTest.config.setIntArray(testIntArray);
496         if (!Arrays.equals(
497             testIntArray, ConfigurationFormatServiceTest.config.getIntArray())) {
498
499             fail("Array returned from configuration did not match original " +
500                 "array, either the set or get of the array failed");
501         }
502
503         // String value tests
504
String JavaDoc[] testStringArray = new String JavaDoc[] { "1", "2", "3", "4" };
505         ConfigurationFormatServiceTest.config.setStringArray(testStringArray);
506         String JavaDoc[] stringArrayFromConfig =
507             ConfigurationFormatServiceTest.config.getStringArray();
508
509         if (testStringArray.length != stringArrayFromConfig.length) {
510             fail("Array returned from configuration did not match original " +
511                 "array, either the set or get of the array failed");
512         }
513         for (int i = 0; i < testStringArray.length; i++) {
514             if (!testStringArray[i].equals(stringArrayFromConfig[i])) {
515                 fail("Array returned from configuration did not match " +
516                     "original array, either the set or get of the array failed");
517             }
518         }
519
520         // configuration value tests
521
FormatTestConfiguration[] testConfigArray =
522             new FormatTestConfiguration[4];
523
524         for (int i = 0; i < testConfigArray.length; i++) {
525             testConfigArray[i] = (FormatTestConfiguration)
526                 ConfigurationFormatServiceTest.configurationFormatService.
527                     newConfiguration(FormatTestConfiguration.class);
528
529             testConfigArray[i].setMyInt(i + 1);
530         }
531
532         ConfigurationFormatServiceTest.config.setMyArray(testConfigArray);
533         FormatTestConfiguration[] arrayFromConfig =
534             ConfigurationFormatServiceTest.config.getMyArray();
535
536         if (testConfigArray.length != arrayFromConfig.length) {
537             fail("Array returned from configuration did not match original " +
538                 "array, either the set or get of the array failed");
539         }
540         for (int i = 0; i < testConfigArray.length; i++) {
541             if (testConfigArray[i].getMyInt() !=
542                 arrayFromConfig[i].getMyInt()) {
543
544                 fail("Array returned from configuration did not match " +
545                     "original array, either the set or get of the array failed");
546             }
547         }
548     }
549
550     /**
551      * Tests to ensure that gets and sets of individual array members work
552      * for arrays of primitives, objects (Strings), and sub-configurations.
553      */

554     public void testIndexedArrayAccess() {
555         // int value tests
556
ConfigurationFormatServiceTest.config.setIntArray(2, 10);
557         if (ConfigurationFormatServiceTest.config.getIntArray(2) != 10) {
558             fail("Array element from configuration did not match set value, " +
559                 "either the set or get of the array failed");
560         }
561
562         // String value tests
563
ConfigurationFormatServiceTest.config.setStringArray(2, "10");
564         if (!ConfigurationFormatServiceTest.config.
565             getStringArray(2).equals("10")) {
566
567             fail("Array element from configuration did not match set value, " +
568                 "either the set or get of the array failed");
569         }
570
571         // configuration value tests
572
FormatTestConfiguration testConfig = (FormatTestConfiguration)
573             ConfigurationFormatServiceTest.configurationFormatService.
574                 newConfiguration(FormatTestConfiguration.class);
575
576         testConfig.setMyInt(10);
577
578         ConfigurationFormatServiceTest.config.setMyArray(2, testConfig);
579         if (ConfigurationFormatServiceTest.config.getMyArray(2).getMyInt() != 10) {
580             fail("Array element from configuration did not match set value, " +
581                 "either the set or get of the array failed");
582         }
583     }
584
585     /**
586      * Tests to ensure that bounds checking is acurate for
587      * gets and sets of array members
588      * for arrays of primitives, objects (Strings), and sub-configurations.
589      */

590     public void testArrayBounds() {
591         // int value tests
592
try {
593             ConfigurationFormatServiceTest.config.setIntArray(-1, 10);
594             fail("InvalidParameterException not thrown for invalid index");
595         } catch(InvalidParameterException ipe) {
596             // expected
597
}
598         try {
599             ConfigurationFormatServiceTest.config.setIntArray(4, 10);
600             fail("InvalidParameterException not thrown for invalid index");
601         } catch(InvalidParameterException ipe) {
602             // expected
603
}
604         try {
605             ConfigurationFormatServiceTest.config.getIntArray(-1);
606             fail("InvalidParameterException not thrown for invalid index");
607         } catch(InvalidParameterException ipe) {
608             // expected
609
}
610         try {
611             ConfigurationFormatServiceTest.config.getIntArray(4);
612             fail("InvalidParameterException not thrown for invalid index");
613         } catch(InvalidParameterException ipe) {
614             // expected
615
}
616
617         // String value tests
618
try {
619             ConfigurationFormatServiceTest.config.setStringArray(-1, "10");
620             fail("InvalidParameterException not thrown for invalid index");
621         } catch(InvalidParameterException ipe) {
622             // expected
623
}
624         try {
625             ConfigurationFormatServiceTest.config.setStringArray(4, "10");
626             fail("InvalidParameterException not thrown for invalid index");
627         } catch(InvalidParameterException ipe) {
628             // expected
629
}
630         try {
631             ConfigurationFormatServiceTest.config.getStringArray(-1);
632             fail("InvalidParameterException not thrown for invalid index");
633         } catch(InvalidParameterException ipe) {
634             // expected
635
}
636         try {
637             ConfigurationFormatServiceTest.config.getStringArray(4);
638             fail("InvalidParameterException not thrown for invalid index");
639         } catch(InvalidParameterException ipe) {
640             // expected
641
}
642
643         // configuration value tests
644
FormatTestConfiguration testConfig = (FormatTestConfiguration)
645             ConfigurationFormatServiceTest.configurationFormatService.
646                 newConfiguration(FormatTestConfiguration.class);
647
648         try {
649             ConfigurationFormatServiceTest.config.setMyArray(-1, testConfig);
650             fail("InvalidParameterException not thrown for invalid index");
651         } catch(InvalidParameterException ipe) {
652             // expected
653
}
654         try {
655             ConfigurationFormatServiceTest.config.setMyArray(4, testConfig);
656             fail("InvalidParameterException not thrown for invalid index");
657         } catch(InvalidParameterException ipe) {
658             // expected
659
}
660         try {
661             ConfigurationFormatServiceTest.config.getMyArray(-1);
662             fail("InvalidParameterException not thrown for invalid index");
663         } catch(InvalidParameterException ipe) {
664             // expected
665
}
666         try {
667             ConfigurationFormatServiceTest.config.getMyArray(4);
668             fail("InvalidParameterException not thrown for invalid index");
669         } catch(InvalidParameterException ipe) {
670             // expected
671
}
672     }
673
674     /**
675      * Tests to ensure that adding members of arrays work
676      * for arrays of primitives, objects (Strings), and sub-configurations.
677      */

678     public void testAddArrayItem() {
679         // int value tests
680
int intOriginalLength =
681             ConfigurationFormatServiceTest.config.getIntArray().length;
682         ConfigurationFormatServiceTest.config.addIntArray(20);
683         int[] intArrayFromConfig =
684             ConfigurationFormatServiceTest.config.getIntArray();
685         if (intArrayFromConfig.length != intOriginalLength + 1 ||
686             intArrayFromConfig[intArrayFromConfig.length - 1] != 20) {
687
688             fail("Array did not change as expected");
689         }
690
691         // int value tests
692
int stringOriginalLength =
693             ConfigurationFormatServiceTest.config.getStringArray().length;
694         ConfigurationFormatServiceTest.config.addStringArray("20");
695         String JavaDoc[] stringArrayFromConfig =
696             ConfigurationFormatServiceTest.config.getStringArray();
697         if (stringArrayFromConfig.length != stringOriginalLength + 1 ||
698             !stringArrayFromConfig[intArrayFromConfig.length - 1].equals("20")) {
699
700             fail("Array did not change as expected");
701         }
702
703         // configuration value tests
704
int configOriginalLength =
705             ConfigurationFormatServiceTest.config.getMyArray().length;
706
707         FormatTestConfiguration testConfig = (FormatTestConfiguration)
708             ConfigurationFormatServiceTest.configurationFormatService.
709                 newConfiguration(FormatTestConfiguration.class);
710
711         testConfig.setMyInt(20);
712         ConfigurationFormatServiceTest.config.addMyArray(testConfig);
713         FormatTestConfiguration[] configArrayFromConfig =
714             ConfigurationFormatServiceTest.config.getMyArray();
715         if (configArrayFromConfig.length != configOriginalLength + 1 ||
716             configArrayFromConfig[configArrayFromConfig.length - 1].getMyInt() != 20) {
717
718             fail("Array did not change as expected");
719         }
720     }
721
722     /**
723      * Tests the writing of configuration objects from their object state to
724      * their serialized version
725      */

726     public void testConfigurationWrite() {
727
728         try {
729             Config.getInstance().storeConfiguration(
730                 TEST_STORE_LOCATION,
731                 ConfigurationFormatServiceTest.config);
732         } catch (Exception JavaDoc e) {
733             e.printStackTrace();
734             fail ("Unable to serialize configuration document" + e);
735         }
736
737     }
738
739     /**
740      * Tests the functionality of reading configuration values from their
741      * persistent store
742      */

743     public void testConfigRead() throws Exception JavaDoc {
744
745         FormatTestConfiguration newC =
746             (FormatTestConfiguration)
747             Config.getInstance().fetchConfiguration(TEST_STORE_LOCATION);
748
749         if (newC.getMyFloat() != FLOAT)
750             fail("The document read in contains the wrong float value.");
751     }
752
753     /**
754      * Tests alterChildConfiguration and getChildConfiguration methods
755      * of the format service
756      */

757     public void testFormatServiceConfigAccess() {
758
759         // test null set to clear it out
760
configurationFormatService.alterChildConfiguration(
761             ConfigurationFormatServiceTest.config,
762             "SubConfiguration",
763             null);
764
765         assertNull(
766             "alterChildConfiguration with null new configuration "
767                 + "did not clear existing configuration",
768             configurationFormatService.getChildConfiguration(
769                 ConfigurationFormatServiceTest.config,
770                 "SubConfiguration"));
771
772         Configuration newConfig =
773             configurationFormatService.newConfiguration(Configuration.class);
774
775         String JavaDoc expectedSubName =
776             ConfigurationFormatServiceTest.config.getConfigurationName()
777                 + Node.DELIMITER
778                 + "SubConfiguration";
779
780         // test null set to clear it out
781
configurationFormatService.alterChildConfiguration(
782             ConfigurationFormatServiceTest.config,
783             "SubConfiguration",
784             newConfig);
785
786         assertTrue(
787             "Reference to original configuration was not maintained",
788             configurationFormatService.getChildConfiguration(
789                 ConfigurationFormatServiceTest.config,
790                 "SubConfiguration")
791                 .getConfigurationName()
792                 .equals(expectedSubName));
793     }
794
795     /**
796      * Tests the functionality of cloning configuration objects
797      */

798     public void testClone() throws Exception JavaDoc {
799
800         FormatTestConfiguration clone =
801             (FormatTestConfiguration)
802             ConfigurationFormatServiceTest.config.clone();
803
804         String JavaDoc newMessage = "Hello, You've been cloned!";
805         clone.setMyString(newMessage);
806
807         /*
808         System.out.println("\n\nORIGINAL:");
809         ConfigurationFormatServiceTest.configurationFormatService.
810                 writeConfigurationStream(
811                     ConfigurationFormatServiceTest.config,
812                     System.out);
813         System.out.println("\n\nCloned and Altered:");
814         ConfigurationFormatServiceTest.configurationFormatService.
815                 writeConfigurationStream(
816                     clone,
817                     System.out);
818          */

819
820         if (!clone.getMyString().equals(newMessage)) {
821             fail("Could not set String property after cloning.");
822         }
823
824         if (ConfigurationFormatServiceTest.config.getMyString().equals(newMessage)) {
825             fail("Setting a String property on the clone altered the origninal.");
826         }
827     }
828
829
830
831
832     public void testEqualsAndHashCode() {
833         Configuration subConfig1 =
834             ConfigurationFormatServiceTest.config.getSubConfiguration();
835
836         Configuration subConfig2 =
837             ConfigurationFormatServiceTest.config.getSubConfiguration();
838
839         assertTrue("subConfig1.equals(subConfig2) was not true",
840             subConfig1.equals(subConfig2));
841
842         assertTrue("subConfig1 had different hashcode from subConfig2",
843             subConfig1.hashCode() == subConfig2.hashCode());
844
845         assertTrue("subConfig1.equals(ConfigurationFormatServiceTest.config) was not false",
846             !subConfig1.equals(ConfigurationFormatServiceTest.config));
847
848         assertTrue("subConfig1 had the same hashcode as ConfigurationFormatServiceTest.config",
849             subConfig1.hashCode() !=
850                 ConfigurationFormatServiceTest.config.hashCode());
851
852     }
853     
854     public void testNonCacheableTypes() {
855         // set the component in config
856
ConfigurationFormatServiceTest.config.setTestComponent(
857             (TestComponent)
858             Lookup.getInstance().fetchComponent("/core/config/test/TestComponent"));
859
860         // get the value again so it gets cached
861
LifecycleInterceptor lifecycleView =
862             (LifecycleInterceptor) ConfigurationFormatServiceTest.config.getTestComponent();
863         lifecycleView.destroyComponent();
864         
865         // get the component again, if it is destroyed, then it is cached and
866
// the test should fail
867
lifecycleView =
868             (LifecycleInterceptor) ConfigurationFormatServiceTest.config.getTestComponent();
869         
870         TestCase.assertTrue(
871             "Config object returned a stale component reference which " +
872             "means it was cached and it should not have been",
873             lifecycleView.getLifecycleState() == LifecycleStateEnum.RUNNING);
874     }
875
876
877     /**
878      * This internal interface is used to test the configuration of the basic
879      * supported types in the configuration subsystem
880      */

881     public static interface FormatTestConfiguration extends Configuration {
882
883         boolean getMyBool();
884         void setMyBool(boolean value);
885
886         byte getMyByte();
887         void setMyByte(byte value);
888
889         short getMyShort();
890         void setMyShort(short value);
891
892         int getMyInt();
893         void setMyInt(int value);
894
895         long getMyLong();
896         void setMyLong(long value);
897
898         float getMyFloat();
899         void setMyFloat(float value);
900
901         double getMyDouble();
902         void setMyDouble(double value);
903
904         String JavaDoc getMyString();
905         void setMyString(String JavaDoc value);
906
907         Object JavaDoc getMyValue();
908         void setMyValue(Object JavaDoc value);
909
910         Class JavaDoc getMyClass();
911         void setMyClass(Class JavaDoc value);
912
913         Date JavaDoc getMyDate();
914         void setMyDate(Date JavaDoc value);
915
916         FormatTestConfiguration[] getMyArray();
917         FormatTestConfiguration getMyArray(int index);
918         void setMyArray(FormatTestConfiguration[] value);
919         void addMyArray(FormatTestConfiguration value);
920         void setMyArray(int index, FormatTestConfiguration value);
921
922         int[] getIntArray();
923         int getIntArray(int index);
924         void setIntArray(int[] value);
925         void addIntArray(int value);
926         void setIntArray(int index, int value);
927
928         String JavaDoc[] getStringArray();
929         String JavaDoc getStringArray(int index);
930         void setStringArray(String JavaDoc[] value);
931         void addStringArray(String JavaDoc value);
932         void setStringArray(int index, String JavaDoc value);
933
934         String JavaDoc[] getEmptyArray();
935         void setEmptyArray(String JavaDoc[] value);
936
937         SeverityEnum getEnum();
938         void setEnum(SeverityEnum value);
939
940         String JavaDoc getNullValue();
941         //void setNullValue(String value);
942

943         int getNullPrimitive();
944
945         String JavaDoc DefaultNullValue = null;
946         String JavaDoc getDefaultNullValue();
947
948         Configuration getSubConfiguration();
949         void setSubConfiguration(Configuration config);
950
951         Configuration getSubConfigurationRef();
952         void setSubConfigurationRef(Configuration config);
953         
954         TestComponent getTestComponent();
955         void setTestComponent(TestComponent component);
956
957     }
958
959     public static interface TokenTestConfiguration extends Configuration {
960         int getOne();
961         String JavaDoc getTwo();
962         String JavaDoc getThree();
963         String JavaDoc getFour();
964         String JavaDoc getFive();
965         String JavaDoc getSix();
966         String JavaDoc getSeven();
967         String JavaDoc getEight();
968         String JavaDoc getNine();
969         String JavaDoc getTen();
970     }
971
972     public static interface ConfigTokenConfiguration extends Configuration {
973         String JavaDoc getOne();
974     }
975
976     public void testTokenReplacement() {
977         TokenTestConfiguration config = (TokenTestConfiguration)
978             Config.getInstance().fetchConfiguration(TEST_TOKEN_LOCATION);
979
980         PropertyConfiguration propTokensConfig = (PropertyConfiguration)
981             Config.getInstance().fetchConfiguration(PROPERTY_TOKEN_LOCATION);
982
983         ConfigTokenConfiguration configTokensConfig = (ConfigTokenConfiguration)
984             Config.getInstance().fetchConfiguration(CONFIG_TOKEN_LOCATION);
985
986         TestCase.assertTrue("Value was incorrect: [" + config.getOne() + "]",
987             config.getOne() == propTokensConfig.getIntProperty("One"));
988
989         TestCase.assertTrue("Value was incorrect: [" + config.getTwo() + "]",
990             config.getTwo().equals(
991             propTokensConfig.getProperty("One") + ":" +
992             propTokensConfig.getProperty("Two")));
993
994         TestCase.assertTrue("Value was incorrect: [" + config.getThree() + "]",
995             config.getThree().equals(
996                 "{" + propTokensConfig.getProperty("One") + "$\\"));
997
998         TestCase.assertTrue("Value was incorrect: [" + config.getFour() + "]",
999             config.getFour().equals("{}$\\"));
1000
1001        TestCase.assertTrue("Value was incorrect: [" + config.getFive() + "]",
1002            config.getFive().equals("{foo"));
1003
1004        try {
1005            config.getSix();
1006            TestCase.fail(
1007                "did not catch expected InvalidConfigurationException");
1008        } catch(InvalidConfigurationException ice) {
1009            // expected
1010
}
1011
1012        try {
1013            config.getSeven();
1014            TestCase.fail(
1015                "did not catch expected InvalidConfigurationException");
1016        } catch(InvalidConfigurationException ice) {
1017            // expected
1018
}
1019
1020// This is now a valid system property (TODO: GH - create a positive test for this case)
1021
// try {
1022
// config.getEight();
1023
// TestCase.fail(
1024
// "did not catch expected InvalidConfigurationException");
1025
// } catch(InvalidConfigurationException ice) {
1026
// // expected
1027
// }
1028
//
1029
try {
1030            config.getNine();
1031            TestCase.fail(
1032                "did not catch expected InvalidConfigurationException");
1033        } catch(InvalidConfigurationException ice) {
1034            // expected
1035
}
1036
1037        TestCase.assertTrue("Value was incorrect: [" + config.getTen() + "]",
1038            config.getTen().equals(configTokensConfig.getOne()));
1039
1040    }
1041
1042
1043    public static interface TestDefaultingConfiguration extends Configuration {
1044
1045        int MyInt = 3;
1046        int getMyInt();
1047        void setMyInt(int value);
1048
1049        double MyDouble = 53.25D;
1050        double getMyDouble();
1051        void setMyDouble(double value);
1052
1053
1054    }
1055
1056
1057    public void testDefaultingConfig() {
1058
1059        TestDefaultingConfiguration config =
1060            (TestDefaultingConfiguration)
1061            Config.getInstance().createConfiguration(TestDefaultingConfiguration.class);
1062
1063        int myInt = config.getMyInt();
1064        if (myInt != 3) {
1065            fail("Did not get the expect default MyInt value of " + 3);
1066        }
1067
1068        config.setMyInt(10);
1069        myInt = config.getMyInt();
1070        if (myInt != 10) {
1071            fail("Did not get the overriden MyInt value of " + 10);
1072        }
1073
1074        double myDouble = config.getMyDouble();
1075        if (myDouble != 53.25D) {
1076            fail("Did not get the expect default MyDouble value of " + 52.25D);
1077        }
1078
1079        config.setMyDouble(12.12D);
1080        myDouble = config.getMyDouble();
1081        if (myDouble != 12.12D) {
1082            fail("Did not get the overriden MyDouble value of " + 12.12D);
1083        }
1084
1085    }
1086
1087
1088    public static interface SuperConf extends Configuration {
1089        int MySuperDefaultedInt = 5;
1090
1091        String JavaDoc MySuperDefaultedString = "Foo";
1092    }
1093
1094    public static interface SubConf extends SuperConf {
1095        int getMySuperDefaultedInt();
1096        String JavaDoc getMySuperDefaultedString();
1097    }
1098
1099
1100    public void testSuperInterfaceDefaultedConfigurations() {
1101
1102        SuperConf superConf =
1103            (SuperConf)
1104            Config.getInstance().createConfiguration(SuperConf.class);
1105
1106        SubConf subConf =
1107            (SubConf)
1108            Config.getInstance().createConfiguration(SubConf.class);
1109
1110        if (subConf.getMySuperDefaultedInt() != 5) {
1111            fail("Did not receive the expected default value from the " +
1112                 "super interface. Got: " + subConf.getMySuperDefaultedInt());
1113        }
1114
1115        if (!subConf.getMySuperDefaultedString().equals("Foo")) {
1116            fail("Did not receive the expected default String value from the " +
1117                 "super interface. Got: " + subConf.getMySuperDefaultedString());
1118        }
1119    }
1120
1121
1122}
1123
Popular Tags