Source Code Cross Referenced for TestNullCompositeConfiguration.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:
018:        package org.apache.commons.configuration;
019:
020:        import java.io.File;
021:        import java.util.ArrayList;
022:        import java.util.Iterator;
023:        import java.util.List;
024:
025:        import junit.framework.TestCase;
026:
027:        /**
028:         * Test loading multiple configurations.
029:         *
030:         * @version $Id: TestNullCompositeConfiguration.java 439648 2006-09-02 20:42:10Z oheger $
031:         */
032:        public class TestNullCompositeConfiguration extends TestCase {
033:            protected PropertiesConfiguration conf1;
034:            protected PropertiesConfiguration conf2;
035:            protected XMLConfiguration xmlConf;
036:            protected CompositeConfiguration cc;
037:
038:            /** The File that we test with */
039:            private String testProperties = new File("conf/test.properties")
040:                    .getAbsolutePath();
041:            private String testProperties2 = new File("conf/test2.properties")
042:                    .getAbsolutePath();
043:            private String testPropertiesXML = new File("conf/test.xml")
044:                    .getAbsolutePath();
045:
046:            protected void setUp() throws Exception {
047:                cc = new CompositeConfiguration();
048:                conf1 = new PropertiesConfiguration(testProperties);
049:                conf2 = new PropertiesConfiguration(testProperties2);
050:                xmlConf = new XMLConfiguration(new File(testPropertiesXML));
051:
052:                cc.setThrowExceptionOnMissing(false);
053:            }
054:
055:            public void testThrowExceptionOnMissing() {
056:                assertFalse("Throw Exception Property is set!", cc
057:                        .isThrowExceptionOnMissing());
058:            }
059:
060:            public void testAddRemoveConfigurations() throws Exception {
061:                cc.addConfiguration(conf1);
062:                assertEquals(2, cc.getNumberOfConfigurations());
063:                cc.addConfiguration(conf1);
064:                assertEquals(2, cc.getNumberOfConfigurations());
065:                cc.addConfiguration(conf2);
066:                assertEquals(3, cc.getNumberOfConfigurations());
067:                cc.removeConfiguration(conf1);
068:                assertEquals(2, cc.getNumberOfConfigurations());
069:                cc.clear();
070:                assertEquals(1, cc.getNumberOfConfigurations());
071:            }
072:
073:            public void testGetPropertyWIncludes() throws Exception {
074:                cc.addConfiguration(conf1);
075:                cc.addConfiguration(conf2);
076:                List l = cc.getList("packages");
077:                assertTrue(l.contains("packagea"));
078:
079:            }
080:
081:            public void testGetProperty() throws Exception {
082:                cc.addConfiguration(conf1);
083:                cc.addConfiguration(conf2);
084:                assertEquals("Make sure we get the property from conf1 first",
085:                        "test.properties", cc.getString("propertyInOrder"));
086:                cc.clear();
087:
088:                cc.addConfiguration(conf2);
089:                cc.addConfiguration(conf1);
090:                assertEquals("Make sure we get the property from conf2 first",
091:                        "test2.properties", cc.getString("propertyInOrder"));
092:            }
093:
094:            public void testCantRemoveMemoryConfig() throws Exception {
095:                cc.clear();
096:                assertEquals(1, cc.getNumberOfConfigurations());
097:
098:                Configuration internal = cc.getConfiguration(0);
099:                cc.removeConfiguration(internal);
100:
101:                assertEquals(1, cc.getNumberOfConfigurations());
102:
103:            }
104:
105:            public void testGetPropertyMissing() throws Exception {
106:                cc.addConfiguration(conf1);
107:                cc.addConfiguration(conf2);
108:
109:                assertNull("Bogus property is not null!", cc
110:                        .getString("bogus.property"));
111:
112:                assertTrue("Should be false", !cc.getBoolean(
113:                        "test.missing.boolean", false));
114:                assertTrue("Should be true", cc.getBoolean(
115:                        "test.missing.boolean.true", true));
116:
117:            }
118:
119:            /**
120:             * Tests <code>List</code> parsing.
121:             */
122:            public void testMultipleTypesOfConfigs() throws Exception {
123:                cc.addConfiguration(conf1);
124:                cc.addConfiguration(xmlConf);
125:                assertEquals("Make sure we get the property from conf1 first",
126:                        1, cc.getInt("test.short"));
127:                cc.clear();
128:
129:                cc.addConfiguration(xmlConf);
130:                cc.addConfiguration(conf1);
131:                assertEquals("Make sure we get the property from xml", 8, cc
132:                        .getInt("test.short"));
133:            }
134:
135:            /**
136:             * Tests <code>List</code> parsing.
137:             */
138:            public void testPropertyExistsInOnlyOneConfig() throws Exception {
139:                cc.addConfiguration(conf1);
140:                cc.addConfiguration(xmlConf);
141:                assertEquals("value", cc.getString("element"));
142:            }
143:
144:            /**
145:             * Tests getting a default when the key doesn't exist
146:             */
147:            public void testDefaultValueWhenKeyMissing() throws Exception {
148:                cc.addConfiguration(conf1);
149:                cc.addConfiguration(xmlConf);
150:                assertEquals("default", cc.getString("bogus", "default"));
151:                assertTrue(1.4 == cc.getDouble("bogus", 1.4));
152:                assertTrue(1.4 == cc.getDouble("bogus", 1.4));
153:            }
154:
155:            /**
156:             * Tests <code>List</code> parsing.
157:             */
158:            public void testGettingConfiguration() throws Exception {
159:                cc.addConfiguration(conf1);
160:                cc.addConfiguration(xmlConf);
161:                assertEquals(PropertiesConfiguration.class, cc
162:                        .getConfiguration(0).getClass());
163:                assertEquals(XMLConfiguration.class, cc.getConfiguration(1)
164:                        .getClass());
165:            }
166:
167:            /**
168:             * Tests setting values.  These are set in memory mode only!
169:             */
170:            public void testClearingProperty() throws Exception {
171:                cc.addConfiguration(conf1);
172:                cc.addConfiguration(xmlConf);
173:                cc.clearProperty("test.short");
174:                assertTrue("Make sure test.short is gone!", !cc
175:                        .containsKey("test.short"));
176:            }
177:
178:            /**
179:             * Tests adding values.  Make sure they _DON'T_ override any other properties but add to the
180:             * existing properties  and keep sequence
181:             */
182:            public void testAddingProperty() throws Exception {
183:                cc.addConfiguration(conf1);
184:                cc.addConfiguration(xmlConf);
185:
186:                String[] values = cc.getStringArray("test.short");
187:
188:                assertEquals("Number of values before add is wrong!", 1,
189:                        values.length);
190:                assertEquals("First Value before add is wrong", "1", values[0]);
191:
192:                cc.addProperty("test.short", "88");
193:
194:                values = cc.getStringArray("test.short");
195:
196:                assertEquals("Number of values is wrong!", 2, values.length);
197:                assertEquals("First Value is wrong", "1", values[0]);
198:                assertEquals("Third Value is wrong", "88", values[1]);
199:            }
200:
201:            /**
202:             * Tests setting values.  These are set in memory mode only!
203:             */
204:            public void testSettingMissingProperty() throws Exception {
205:                cc.addConfiguration(conf1);
206:                cc.addConfiguration(xmlConf);
207:                cc.setProperty("my.new.property", "supernew");
208:                assertEquals("supernew", cc.getString("my.new.property"));
209:            }
210:
211:            /**
212:             * Tests retrieving subsets of configurations
213:             */
214:            public void testGettingSubset() throws Exception {
215:                cc.addConfiguration(conf1);
216:                cc.addConfiguration(xmlConf);
217:
218:                Configuration subset = null;
219:                subset = cc.subset("test");
220:                assertNotNull(subset);
221:                assertFalse("Shouldn't be empty", subset.isEmpty());
222:                assertEquals(
223:                        "Make sure the initial loaded configs subset overrides any later add configs subset",
224:                        "1", subset.getString("short"));
225:
226:                cc.setProperty("test.short", "43");
227:                subset = cc.subset("test");
228:                assertEquals(
229:                        "Make sure the initial loaded configs subset overrides any later add configs subset",
230:                        "43", subset.getString("short"));
231:            }
232:
233:            /**
234:             * Tests subsets and still can resolve elements
235:             */
236:            public void testSubsetCanResolve() throws Exception {
237:                cc = new CompositeConfiguration();
238:                final BaseConfiguration config = new BaseConfiguration();
239:                config.addProperty("subset.tempfile",
240:                        "${java.io.tmpdir}/file.tmp");
241:                cc.addConfiguration(config);
242:                cc.addConfiguration(ConfigurationConverter
243:                        .getConfiguration(System.getProperties()));
244:
245:                Configuration subset = cc.subset("subset");
246:                assertEquals(
247:                        System.getProperty("java.io.tmpdir") + "/file.tmp",
248:                        subset.getString("tempfile"));
249:            }
250:
251:            /**
252:             * Tests <code>List</code> parsing.
253:             */
254:            public void testList() throws Exception {
255:                cc.addConfiguration(conf1);
256:                cc.addConfiguration(xmlConf);
257:
258:                List packages = cc.getList("packages");
259:                // we should get 3 packages here
260:                assertEquals(3, packages.size());
261:
262:                List defaultList = new ArrayList();
263:                defaultList.add("1");
264:                defaultList.add("2");
265:
266:                packages = cc.getList("packages.which.dont.exist", defaultList);
267:                // we should get 2 packages here
268:                assertEquals(2, packages.size());
269:
270:            }
271:
272:            /**
273:             * Tests <code>String</code> array parsing.
274:             */
275:            public void testStringArray() throws Exception {
276:                cc.addConfiguration(conf1);
277:                cc.addConfiguration(xmlConf);
278:
279:                String[] packages = cc.getStringArray("packages");
280:                // we should get 3 packages here
281:                assertEquals(3, packages.length);
282:
283:                packages = cc.getStringArray("packages.which.dont.exist");
284:                // we should get 0 packages here
285:                assertEquals(0, packages.length);
286:            }
287:
288:            public void testGetList() {
289:                Configuration conf1 = new BaseConfiguration();
290:                conf1.addProperty("array", "value1");
291:                conf1.addProperty("array", "value2");
292:
293:                Configuration conf2 = new BaseConfiguration();
294:                conf2.addProperty("array", "value3");
295:                conf2.addProperty("array", "value4");
296:
297:                cc.addConfiguration(conf1);
298:                cc.addConfiguration(conf2);
299:
300:                // check the composite 'array' property
301:                List list = cc.getList("array");
302:                assertNotNull("null list", list);
303:                assertEquals("list size", 2, list.size());
304:                assertTrue("'value1' not found in the list", list
305:                        .contains("value1"));
306:                assertTrue("'value2' not found in the list", list
307:                        .contains("value2"));
308:
309:                // add an element to the list in the composite configuration
310:                cc.addProperty("array", "value5");
311:
312:                // test the new list
313:                list = cc.getList("array");
314:                assertNotNull("null list", list);
315:                assertEquals("list size", 3, list.size());
316:                assertTrue("'value1' not found in the list", list
317:                        .contains("value1"));
318:                assertTrue("'value2' not found in the list", list
319:                        .contains("value2"));
320:                assertTrue("'value5' not found in the list", list
321:                        .contains("value5"));
322:            }
323:
324:            public void testGetVector() {
325:                Configuration conf1 = new BaseConfiguration();
326:                conf1.addProperty("array", "value1");
327:                conf1.addProperty("array", "value2");
328:
329:                Configuration conf2 = new BaseConfiguration();
330:                conf2.addProperty("array", "value3");
331:                conf2.addProperty("array", "value4");
332:
333:                cc.addConfiguration(conf1);
334:                cc.addConfiguration(conf2);
335:
336:                // add an element to the vector in the composite configuration
337:                cc.addProperty("array", "value5");
338:
339:                List list = cc.getList("array");
340:
341:                for (Iterator it = list.iterator(); it.hasNext();) {
342:                    Object value = it.next();
343:                    System.out.println(value.getClass().getName() + " -> "
344:                            + value);
345:                }
346:
347:            }
348:
349:            /**
350:             * Tests <code>getKeys</code> preserves the order
351:             */
352:            public void testGetKeysPreservesOrder() throws Exception {
353:                cc.addConfiguration(conf1);
354:                List orderedList = new ArrayList();
355:                for (Iterator keys = conf1.getKeys(); keys.hasNext();) {
356:                    orderedList.add(keys.next());
357:                }
358:                List iteratedList = new ArrayList();
359:                for (Iterator keys = cc.getKeys(); keys.hasNext();) {
360:                    iteratedList.add(keys.next());
361:                }
362:                assertEquals(orderedList.size(), iteratedList.size());
363:                for (int i = 0; i < orderedList.size(); i++) {
364:                    assertEquals(orderedList.get(i), iteratedList.get(i));
365:                }
366:            }
367:
368:            /**
369:             * Tests <code>getKeys(String key)</code> preserves the order
370:             */
371:            public void testGetKeys2PreservesOrder() throws Exception {
372:                cc.addConfiguration(conf1);
373:                List orderedList = new ArrayList();
374:                for (Iterator keys = conf1.getKeys("test"); keys.hasNext();) {
375:                    orderedList.add(keys.next());
376:                }
377:                List iteratedList = new ArrayList();
378:                for (Iterator keys = cc.getKeys("test"); keys.hasNext();) {
379:                    iteratedList.add(keys.next());
380:                }
381:                assertEquals(orderedList.size(), iteratedList.size());
382:                for (int i = 0; i < orderedList.size(); i++) {
383:                    assertEquals(orderedList.get(i), iteratedList.get(i));
384:                }
385:            }
386:
387:            public void testGetStringWithDefaults() {
388:                BaseConfiguration defaults = new BaseConfiguration();
389:                defaults.addProperty("default", "default string");
390:
391:                Configuration c = new CompositeConfiguration(defaults);
392:
393:                c.addProperty("string", "test string");
394:
395:                assertEquals("test string", c.getString("string"));
396:
397:                assertNull("XXX should have been null!", c.getString("XXX"));
398:
399:                //test defaults
400:                assertEquals("test string", c.getString("string",
401:                        "some default value"));
402:                assertEquals("default string", c.getString("default"));
403:                assertEquals("default string", c.getString("default",
404:                        "some default value"));
405:                assertEquals("some default value", c.getString("XXX",
406:                        "some default value"));
407:            }
408:
409:            public void testCheckingInMemoryConfiguration() throws Exception {
410:                String TEST_KEY = "testKey";
411:                Configuration defaults = new PropertiesConfiguration();
412:                defaults.setProperty(TEST_KEY, "testValue");
413:                Configuration testConfiguration = new CompositeConfiguration(
414:                        defaults);
415:                assertTrue(testConfiguration.containsKey(TEST_KEY));
416:                assertFalse(testConfiguration.isEmpty());
417:                boolean foundTestKey = false;
418:                Iterator i = testConfiguration.getKeys();
419:                //assertTrue(i instanceof IteratorChain);
420:                //IteratorChain ic = (IteratorChain)i;
421:                //assertEquals(2,i.size());
422:                for (; i.hasNext();) {
423:                    String key = (String) i.next();
424:                    if (key.equals(TEST_KEY)) {
425:                        foundTestKey = true;
426:                    }
427:                }
428:                assertTrue(foundTestKey);
429:                testConfiguration.clearProperty(TEST_KEY);
430:                assertFalse(testConfiguration.containsKey(TEST_KEY));
431:            }
432:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.