Source Code Cross Referenced for TestDefaultConfigurationBuilder.java in  » Library » Apache-commons-configuration-1.4-src » org » apache » commons » configuration » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache commons configuration 1.4 src » org.apache.commons.configuration 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.commons.configuration;
018:
019:        import java.io.File;
020:        import java.util.Collection;
021:        import java.util.Set;
022:
023:        import org.apache.commons.configuration.beanutils.BeanHelper;
024:        import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
025:        import org.apache.commons.configuration.tree.DefaultConfigurationNode;
026:        import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
027:
028:        import junit.framework.TestCase;
029:
030:        /**
031:         * Test class for DefaultConfigurationBuilder.
032:         *
033:         * @author Oliver Heger
034:         * @version $Id: TestDefaultConfigurationBuilder.java 384601 2006-03-09
035:         * 20:22:58Z oheger $
036:         */
037:        public class TestDefaultConfigurationBuilder extends TestCase {
038:            /** Test configuration definition file. */
039:            private static final File TEST_FILE = new File(
040:                    "conf/testDigesterConfiguration.xml");
041:
042:            private static final File ADDITIONAL_FILE = new File(
043:                    "conf/testDigesterConfiguration2.xml");
044:
045:            private static final File OPTIONAL_FILE = new File(
046:                    "conf/testDigesterOptionalConfiguration.xml");
047:
048:            private static final File OPTIONALEX_FILE = new File(
049:                    "conf/testDigesterOptionalConfigurationEx.xml");
050:
051:            private static final File MULTI_FILE = new File(
052:                    "conf/testDigesterConfiguration3.xml");
053:
054:            private static final File INIT_FILE = new File(
055:                    "conf/testComplexInitialization.xml");
056:
057:            /** Constant for the name of an optional configuration.*/
058:            private static final String OPTIONAL_NAME = "optionalConfig";
059:
060:            /** Stores the object to be tested. */
061:            DefaultConfigurationBuilder factory;
062:
063:            protected void setUp() throws Exception {
064:                super .setUp();
065:                System
066:                        .setProperty("java.naming.factory.initial",
067:                                "org.apache.commons.configuration.MockInitialContextFactory");
068:                System.setProperty("test_file_xml", "test.xml");
069:                System.setProperty("test_file_combine", "testcombine1.xml");
070:                factory = new DefaultConfigurationBuilder();
071:            }
072:
073:            /**
074:             * Tests the isReservedNode() method of ConfigurationDeclaration.
075:             */
076:            public void testConfigurationDeclarationIsReserved() {
077:                DefaultConfigurationBuilder.ConfigurationDeclaration decl = new DefaultConfigurationBuilder.ConfigurationDeclaration(
078:                        factory, factory);
079:                DefaultConfigurationNode parent = new DefaultConfigurationNode();
080:                DefaultConfigurationNode nd = new DefaultConfigurationNode("at");
081:                parent.addAttribute(nd);
082:                assertTrue("Attribute at not recognized", decl
083:                        .isReservedNode(nd));
084:                nd = new DefaultConfigurationNode("optional");
085:                parent.addAttribute(nd);
086:                assertTrue("Attribute optional not recognized", decl
087:                        .isReservedNode(nd));
088:                nd = new DefaultConfigurationNode("config-class");
089:                parent.addAttribute(nd);
090:                assertTrue("Inherited attribute not recognized", decl
091:                        .isReservedNode(nd));
092:                nd = new DefaultConfigurationNode("different");
093:                parent.addAttribute(nd);
094:                assertFalse("Wrong reserved attribute", decl.isReservedNode(nd));
095:                nd = new DefaultConfigurationNode("at");
096:                parent.addChild(nd);
097:                assertFalse("Node type not evaluated", decl.isReservedNode(nd));
098:            }
099:
100:            /**
101:             * Tests if the at attribute is correctly detected as reserved attribute.
102:             */
103:            public void testConfigurationDeclarationIsReservedAt() {
104:                checkOldReservedAttribute("at");
105:            }
106:
107:            /**
108:             * Tests if the optional attribute is correctly detected as reserved
109:             * attribute.
110:             */
111:            public void testConfigurationDeclarationIsReservedOptional() {
112:                checkOldReservedAttribute("optional");
113:            }
114:
115:            /**
116:             * Tests if special reserved attributes are recognized by the
117:             * isReservedNode() method. For compatibility reasons the attributes "at"
118:             * and "optional" are also treated as reserved attributes, but only if there
119:             * are no corresponding attributes with the "config-" prefix.
120:             *
121:             * @param name the attribute name
122:             */
123:            private void checkOldReservedAttribute(String name) {
124:                DefaultConfigurationBuilder.ConfigurationDeclaration decl = new DefaultConfigurationBuilder.ConfigurationDeclaration(
125:                        factory, factory);
126:                DefaultConfigurationNode parent = new DefaultConfigurationNode();
127:                DefaultConfigurationNode nd = new DefaultConfigurationNode(
128:                        "config-" + name);
129:                parent.addAttribute(nd);
130:                assertTrue("config-" + name + " attribute not recognized", decl
131:                        .isReservedNode(nd));
132:                DefaultConfigurationNode nd2 = new DefaultConfigurationNode(
133:                        name);
134:                parent.addAttribute(nd2);
135:                assertFalse(name + " is reserved though config- exists", decl
136:                        .isReservedNode(nd2));
137:                assertTrue("config- attribute not recognized when " + name
138:                        + " exists", decl.isReservedNode(nd));
139:            }
140:
141:            /**
142:             * Tests access to certain reserved attributes of a
143:             * ConfigurationDeclaration.
144:             */
145:            public void testConfigurationDeclarationGetAttributes() {
146:                factory.addProperty("xml.fileName", "test.xml");
147:                DefaultConfigurationBuilder.ConfigurationDeclaration decl = new DefaultConfigurationBuilder.ConfigurationDeclaration(
148:                        factory, factory.configurationAt("xml"));
149:                assertNull("Found an at attribute", decl.getAt());
150:                assertFalse("Found an optional attribute", decl.isOptional());
151:                factory.addProperty("xml[@config-at]", "test1");
152:                assertEquals("Wrong value of at attribute", "test1", decl
153:                        .getAt());
154:                factory.addProperty("xml[@at]", "test2");
155:                assertEquals("Wrong value of config-at attribute", "test1",
156:                        decl.getAt());
157:                factory.clearProperty("xml[@config-at]");
158:                assertEquals("Old at attribute not detected", "test2", decl
159:                        .getAt());
160:                factory.addProperty("xml[@config-optional]", "true");
161:                assertTrue("Wrong value of optional attribute", decl
162:                        .isOptional());
163:                factory.addProperty("xml[@optional]", "false");
164:                assertTrue("Wrong value of config-optional attribute", decl
165:                        .isOptional());
166:                factory.clearProperty("xml[@config-optional]");
167:                factory.setProperty("xml[@optional]", Boolean.TRUE);
168:                assertTrue("Old optional attribute not detected", decl
169:                        .isOptional());
170:                factory.setProperty("xml[@optional]", "invalid value");
171:                try {
172:                    decl.isOptional();
173:                    fail("Invalid optional attribute was not detected!");
174:                } catch (ConfigurationRuntimeException crex) {
175:                    // ok
176:                }
177:            }
178:
179:            /**
180:             * Tests adding a new configuration provider.
181:             */
182:            public void testAddConfigurationProvider() {
183:                DefaultConfigurationBuilder.ConfigurationProvider provider = new DefaultConfigurationBuilder.ConfigurationProvider();
184:                assertNull("Provider already registered", factory
185:                        .providerForTag("test"));
186:                factory.addConfigurationProvider("test", provider);
187:                assertSame("Provider not registered", provider, factory
188:                        .providerForTag("test"));
189:            }
190:
191:            /**
192:             * Tries to register a null configuration provider. This should cause an
193:             * exception.
194:             */
195:            public void testAddConfigurationProviderNull() {
196:                try {
197:                    factory.addConfigurationProvider("test", null);
198:                    fail("Could register null provider");
199:                } catch (IllegalArgumentException iex) {
200:                    // ok
201:                }
202:            }
203:
204:            /**
205:             * Tries to register a configuration provider for a null tag. This should
206:             * cause an exception to be thrown.
207:             */
208:            public void testAddConfigurationProviderNullTag() {
209:                try {
210:                    factory
211:                            .addConfigurationProvider(
212:                                    null,
213:                                    new DefaultConfigurationBuilder.ConfigurationProvider());
214:                    fail("Could register provider for null tag!");
215:                } catch (IllegalArgumentException iex) {
216:                    // ok
217:                }
218:            }
219:
220:            /**
221:             * Tests removing configuration providers.
222:             */
223:            public void testRemoveConfigurationProvider() {
224:                assertNull("Removing unknown provider", factory
225:                        .removeConfigurationProvider("test"));
226:                assertNull("Removing provider for null tag", factory
227:                        .removeConfigurationProvider(null));
228:                DefaultConfigurationBuilder.ConfigurationProvider provider = new DefaultConfigurationBuilder.ConfigurationProvider();
229:                factory.addConfigurationProvider("test", provider);
230:                assertSame("Failed to remove provider", provider, factory
231:                        .removeConfigurationProvider("test"));
232:                assertNull("Provider still registered", factory
233:                        .providerForTag("test"));
234:            }
235:
236:            /**
237:             * Tests creating a configuration object from a configuration declaration.
238:             */
239:            public void testConfigurationBeanFactoryCreateBean() {
240:                factory.addConfigurationProvider("test",
241:                        new DefaultConfigurationBuilder.ConfigurationProvider(
242:                                PropertiesConfiguration.class));
243:                factory.addProperty("test[@throwExceptionOnMissing]", "true");
244:                DefaultConfigurationBuilder.ConfigurationDeclaration decl = new DefaultConfigurationBuilder.ConfigurationDeclaration(
245:                        factory, factory.configurationAt("test"));
246:                PropertiesConfiguration conf = (PropertiesConfiguration) BeanHelper
247:                        .createBean(decl);
248:                assertTrue("Property was not initialized", conf
249:                        .isThrowExceptionOnMissing());
250:            }
251:
252:            /**
253:             * Tests creating a configuration object from an unknown tag. This should
254:             * cause an exception.
255:             */
256:            public void testConfigurationBeanFactoryCreateUnknownTag() {
257:                factory.addProperty("test[@throwExceptionOnMissing]", "true");
258:                DefaultConfigurationBuilder.ConfigurationDeclaration decl = new DefaultConfigurationBuilder.ConfigurationDeclaration(
259:                        factory, factory.configurationAt("test"));
260:                try {
261:                    BeanHelper.createBean(decl);
262:                    fail("Could create configuration from unknown tag!");
263:                } catch (ConfigurationRuntimeException crex) {
264:                    // ok
265:                }
266:            }
267:
268:            /**
269:             * Tests loading a simple configuration definition file.
270:             */
271:            public void testLoadConfiguration() throws ConfigurationException {
272:                factory.setFile(TEST_FILE);
273:                checkConfiguration();
274:            }
275:
276:            /**
277:             * Tests the file constructor.
278:             */
279:            public void testLoadConfigurationFromFile()
280:                    throws ConfigurationException {
281:                factory = new DefaultConfigurationBuilder(TEST_FILE);
282:                checkConfiguration();
283:            }
284:
285:            /**
286:             * Tests the file name constructor.
287:             */
288:            public void testLoadConfigurationFromFileName()
289:                    throws ConfigurationException {
290:                factory = new DefaultConfigurationBuilder(TEST_FILE
291:                        .getAbsolutePath());
292:                checkConfiguration();
293:            }
294:
295:            /**
296:             * Tests the URL constructor.
297:             */
298:            public void testLoadConfigurationFromURL() throws Exception {
299:                factory = new DefaultConfigurationBuilder(TEST_FILE.toURL());
300:                checkConfiguration();
301:            }
302:
303:            /**
304:             * Tests if the configuration was correctly created by the factory.
305:             */
306:            private void checkConfiguration() throws ConfigurationException {
307:                CombinedConfiguration compositeConfiguration = (CombinedConfiguration) factory
308:                        .getConfiguration();
309:
310:                assertEquals("Number of configurations", 3,
311:                        compositeConfiguration.getNumberOfConfigurations());
312:                assertEquals(PropertiesConfiguration.class,
313:                        compositeConfiguration.getConfiguration(0).getClass());
314:                assertEquals(XMLPropertiesConfiguration.class,
315:                        compositeConfiguration.getConfiguration(1).getClass());
316:                assertEquals(XMLConfiguration.class, compositeConfiguration
317:                        .getConfiguration(2).getClass());
318:
319:                // check the first configuration
320:                PropertiesConfiguration pc = (PropertiesConfiguration) compositeConfiguration
321:                        .getConfiguration(0);
322:                assertNotNull("Make sure we have a fileName: "
323:                        + pc.getFileName(), pc.getFileName());
324:
325:                // check some properties
326:                checkProperties(compositeConfiguration);
327:            }
328:
329:            /**
330:             * Checks if the passed in configuration contains the expected properties.
331:             *
332:             * @param compositeConfiguration the configuration to check
333:             */
334:            private void checkProperties(Configuration compositeConfiguration) {
335:                assertTrue("Make sure we have loaded our key",
336:                        compositeConfiguration.getBoolean("test.boolean"));
337:                assertEquals("I'm complex!", compositeConfiguration
338:                        .getProperty("element2.subelement.subsubelement"));
339:                assertEquals("property in the XMLPropertiesConfiguration",
340:                        "value1", compositeConfiguration.getProperty("key1"));
341:            }
342:
343:            /**
344:             * Tests loading a configuration definition file with an additional section.
345:             */
346:            public void testLoadAdditional() throws ConfigurationException {
347:                factory.setFile(ADDITIONAL_FILE);
348:                CombinedConfiguration compositeConfiguration = (CombinedConfiguration) factory
349:                        .getConfiguration();
350:                assertEquals("Verify how many configs", 2,
351:                        compositeConfiguration.getNumberOfConfigurations());
352:
353:                // Test if union was constructed correctly
354:                Object prop = compositeConfiguration
355:                        .getProperty("tables.table.name");
356:                assertTrue(prop instanceof  Collection);
357:                assertEquals(3, ((Collection) prop).size());
358:                assertEquals("users", compositeConfiguration
359:                        .getProperty("tables.table(0).name"));
360:                assertEquals("documents", compositeConfiguration
361:                        .getProperty("tables.table(1).name"));
362:                assertEquals("tasks", compositeConfiguration
363:                        .getProperty("tables.table(2).name"));
364:
365:                prop = compositeConfiguration
366:                        .getProperty("tables.table.fields.field.name");
367:                assertTrue(prop instanceof  Collection);
368:                assertEquals(17, ((Collection) prop).size());
369:
370:                assertEquals("smtp.mydomain.org", compositeConfiguration
371:                        .getString("mail.host.smtp"));
372:                assertEquals("pop3.mydomain.org", compositeConfiguration
373:                        .getString("mail.host.pop"));
374:
375:                // This was overriden
376:                assertEquals("masterOfPost", compositeConfiguration
377:                        .getString("mail.account.user"));
378:                assertEquals("topsecret", compositeConfiguration
379:                        .getString("mail.account.psswd"));
380:
381:                // This was overriden, too, but not in additional section
382:                assertEquals("enhanced factory", compositeConfiguration
383:                        .getString("test.configuration"));
384:            }
385:
386:            /**
387:             * Tests loading a definition file that contains optional configurations.
388:             */
389:            public void testLoadOptional() throws Exception {
390:                factory.setURL(OPTIONAL_FILE.toURL());
391:                Configuration config = factory.getConfiguration();
392:                assertTrue(config.getBoolean("test.boolean"));
393:                assertEquals("value", config.getProperty("element"));
394:            }
395:
396:            /**
397:             * Tests loading a definition file with optional and non optional
398:             * configuration sources. One non optional does not exist, so this should
399:             * cause an exception.
400:             */
401:            public void testLoadOptionalWithException() {
402:                factory.setFile(OPTIONALEX_FILE);
403:                try {
404:                    factory.getConfiguration();
405:                    fail("Non existing source did not cause an exception!");
406:                } catch (ConfigurationException cex) {
407:                    // ok
408:                }
409:            }
410:
411:            /**
412:             * Tries to load a configuration file with an optional, non file-based
413:             * configuration. The optional attribute should work for other configuration
414:             * classes, too.
415:             */
416:            public void testLoadOptionalNonFileBased()
417:                    throws ConfigurationException {
418:                CombinedConfiguration config = prepareOptionalTest(
419:                        "configuration", false);
420:                assertTrue("Configuration not empty", config.isEmpty());
421:                assertEquals("Wrong number of configurations", 0, config
422:                        .getNumberOfConfigurations());
423:            }
424:
425:            /**
426:             * Tests an optional, non existing configuration with the forceCreate
427:             * attribute. This configuration should be added to the resulting
428:             * configuration.
429:             */
430:            public void testLoadOptionalForceCreate()
431:                    throws ConfigurationException {
432:                factory.setBasePath(TEST_FILE.getParent());
433:                CombinedConfiguration config = prepareOptionalTest("xml", true);
434:                assertEquals("Wrong number of configurations", 1, config
435:                        .getNumberOfConfigurations());
436:                FileConfiguration fc = (FileConfiguration) config
437:                        .getConfiguration(OPTIONAL_NAME);
438:                assertNotNull("Optional config not found", fc);
439:                assertEquals("File name was not set", "nonExisting.xml", fc
440:                        .getFileName());
441:                assertNotNull("Base path was not set", fc.getBasePath());
442:            }
443:
444:            /**
445:             * Tests loading an embedded optional configuration builder with the force
446:             * create attribute.
447:             */
448:            public void testLoadOptionalBuilderForceCreate()
449:                    throws ConfigurationException {
450:                CombinedConfiguration config = prepareOptionalTest(
451:                        "configuration", true);
452:                assertEquals("Wrong number of configurations", 1, config
453:                        .getNumberOfConfigurations());
454:                assertTrue(
455:                        "Wrong optional configuration type",
456:                        config.getConfiguration(OPTIONAL_NAME) instanceof  CombinedConfiguration);
457:            }
458:
459:            /**
460:             * Tests loading an optional configuration with the force create attribute
461:             * set. The provider will always throw an exception. In this case the
462:             * configuration will not be added to the resulting combined configuration.
463:             */
464:            public void testLoadOptionalForceCreateWithException()
465:                    throws ConfigurationException {
466:                factory
467:                        .addConfigurationProvider(
468:                                "test",
469:                                new DefaultConfigurationBuilder.ConfigurationBuilderProvider() {
470:                                    // Throw an exception here, too
471:                                    public AbstractConfiguration getEmptyConfiguration(
472:                                            DefaultConfigurationBuilder.ConfigurationDeclaration decl)
473:                                            throws Exception {
474:                                        throw new Exception(
475:                                                "Unable to create configuration!");
476:                                    }
477:                                });
478:                CombinedConfiguration config = prepareOptionalTest("test", true);
479:                assertEquals("Optional configuration could be created", 0,
480:                        config.getNumberOfConfigurations());
481:            }
482:
483:            /**
484:             * Prepares a test for loading a configuration definition file with an
485:             * optional configuration declaration.
486:             *
487:             * @param tag the tag name with the optional configuration
488:             * @param force the forceCreate attribute
489:             * @return the combined configuration obtained from the builder
490:             * @throws ConfigurationException if an error occurs
491:             */
492:            private CombinedConfiguration prepareOptionalTest(String tag,
493:                    boolean force) throws ConfigurationException {
494:                String prefix = "override." + tag;
495:                factory.addProperty(prefix + "[@fileName]", "nonExisting.xml");
496:                factory
497:                        .addProperty(prefix + "[@config-optional]",
498:                                Boolean.TRUE);
499:                factory.addProperty(prefix + "[@config-name]", OPTIONAL_NAME);
500:                if (force) {
501:                    factory.addProperty(prefix + "[@config-forceCreate]",
502:                            Boolean.TRUE);
503:                }
504:                return factory.getConfiguration(false);
505:            }
506:
507:            /**
508:             * Tests loading a definition file with multiple different sources.
509:             */
510:            public void testLoadDifferentSources()
511:                    throws ConfigurationException {
512:                factory.setFile(MULTI_FILE);
513:                Configuration config = factory.getConfiguration();
514:                assertFalse(config.isEmpty());
515:                assertTrue(config instanceof  CombinedConfiguration);
516:                CombinedConfiguration cc = (CombinedConfiguration) config;
517:                assertEquals("Wrong number of configurations", 1, cc
518:                        .getNumberOfConfigurations());
519:
520:                assertNotNull(config
521:                        .getProperty("tables.table(0).fields.field(2).name"));
522:                assertNotNull(config
523:                        .getProperty("element2.subelement.subsubelement"));
524:                assertEquals("value", config.getProperty("element3"));
525:                assertEquals("foo", config.getProperty("element3[@name]"));
526:                assertNotNull(config.getProperty("mail.account.user"));
527:
528:                // test JNDIConfiguration
529:                assertNotNull(config.getProperty("test.onlyinjndi"));
530:                assertTrue(config.getBoolean("test.onlyinjndi"));
531:
532:                Configuration subset = config.subset("test");
533:                assertNotNull(subset.getProperty("onlyinjndi"));
534:                assertTrue(subset.getBoolean("onlyinjndi"));
535:
536:                // test SystemConfiguration
537:                assertNotNull(config.getProperty("java.version"));
538:                assertEquals(System.getProperty("java.version"), config
539:                        .getString("java.version"));
540:            }
541:
542:            /**
543:             * Tests if the base path is correctly evaluated.
544:             */
545:            public void testSetConfigurationBasePath()
546:                    throws ConfigurationException {
547:                factory.addProperty("properties[@fileName]", "test.properties");
548:                File deepDir = new File("conf/config/deep");
549:                factory.setConfigurationBasePath(deepDir.getAbsolutePath());
550:
551:                Configuration config = factory.getConfiguration(false);
552:                assertEquals("Wrong property value", "somevalue", config
553:                        .getString("somekey"));
554:            }
555:
556:            /**
557:             * Tests reading a configuration definition file that contains complex
558:             * initialization of properties of the declared configuration sources.
559:             */
560:            public void testComplexInitialization()
561:                    throws ConfigurationException {
562:                factory.setFile(INIT_FILE);
563:                CombinedConfiguration cc = (CombinedConfiguration) factory
564:                        .getConfiguration();
565:
566:                assertEquals("System property not found", "test.xml", cc
567:                        .getString("test_file_xml"));
568:                PropertiesConfiguration c1 = (PropertiesConfiguration) cc
569:                        .getConfiguration(1);
570:                assertTrue(
571:                        "Reloading strategy was not set",
572:                        c1.getReloadingStrategy() instanceof  FileChangedReloadingStrategy);
573:                assertEquals("Refresh delay was not set", 10000,
574:                        ((FileChangedReloadingStrategy) c1
575:                                .getReloadingStrategy()).getRefreshDelay());
576:
577:                Configuration xmlConf = cc.getConfiguration("xml");
578:                assertEquals("Property not found", "I'm complex!", xmlConf
579:                        .getString("element2/subelement/subsubelement"));
580:                assertEquals("List index not found", "two", xmlConf
581:                        .getString("list[0]/item[1]"));
582:                assertEquals("Property in combiner file not found", "yellow",
583:                        cc.getString("/gui/selcolor"));
584:
585:                assertTrue("Delimiter flag was not set", cc
586:                        .isDelimiterParsingDisabled());
587:                assertTrue("Expression engine was not set", cc
588:                        .getExpressionEngine() instanceof  XPathExpressionEngine);
589:            }
590:
591:            /**
592:             * Tests if the returned combined configuration has the expected structure.
593:             */
594:            public void testCombinedConfiguration()
595:                    throws ConfigurationException {
596:                factory.setFile(INIT_FILE);
597:                CombinedConfiguration cc = (CombinedConfiguration) factory
598:                        .getConfiguration();
599:                assertNotNull("Properties configuration not found", cc
600:                        .getConfiguration("properties"));
601:                assertNotNull("XML configuration not found", cc
602:                        .getConfiguration("xml"));
603:                assertEquals("Wrong number of contained configs", 4, cc
604:                        .getNumberOfConfigurations());
605:
606:                CombinedConfiguration cc2 = (CombinedConfiguration) cc
607:                        .getConfiguration(DefaultConfigurationBuilder.ADDITIONAL_NAME);
608:                assertNotNull("No additional configuration found", cc2);
609:                Set names = cc2.getConfigurationNames();
610:                assertEquals("Wrong number of contained additional configs", 2,
611:                        names.size());
612:                assertTrue("Config 1 not contained", names
613:                        .contains("combiner1"));
614:                assertTrue("Config 2 not contained", names
615:                        .contains("combiner2"));
616:            }
617:
618:            /**
619:             * Tests the structure of the returned combined configuration if there is no
620:             * additional section.
621:             */
622:            public void testCombinedConfigurationNoAdditional()
623:                    throws ConfigurationException {
624:                factory.setFile(TEST_FILE);
625:                CombinedConfiguration cc = factory.getConfiguration(true);
626:                assertNull(
627:                        "Additional configuration was found",
628:                        cc
629:                                .getConfiguration(DefaultConfigurationBuilder.ADDITIONAL_NAME));
630:            }
631:
632:            /**
633:             * Tests whether the list node definition was correctly processed.
634:             */
635:            public void testCombinedConfigurationListNodes()
636:                    throws ConfigurationException {
637:                factory.setFile(INIT_FILE);
638:                CombinedConfiguration cc = factory.getConfiguration(true);
639:                Set listNodes = cc.getNodeCombiner().getListNodes();
640:                assertEquals("Wrong number of list nodes", 2, listNodes.size());
641:                assertTrue("table node not a list node", listNodes
642:                        .contains("table"));
643:                assertTrue("list node not a list node", listNodes
644:                        .contains("list"));
645:
646:                CombinedConfiguration cca = (CombinedConfiguration) cc
647:                        .getConfiguration(DefaultConfigurationBuilder.ADDITIONAL_NAME);
648:                listNodes = cca.getNodeCombiner().getListNodes();
649:                assertTrue("Found list nodes for additional combiner",
650:                        listNodes.isEmpty());
651:            }
652:
653:            /**
654:             * Tests whether a configuration builder can itself be declared in a
655:             * configuration definition file.
656:             */
657:            public void testConfigurationBuilderProvider()
658:                    throws ConfigurationException {
659:                factory.addProperty("override.configuration[@fileName]",
660:                        TEST_FILE.getAbsolutePath());
661:                CombinedConfiguration cc = factory.getConfiguration(false);
662:                assertEquals("Wrong number of configurations", 1, cc
663:                        .getNumberOfConfigurations());
664:                checkProperties(cc);
665:            }
666:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.