Source Code Cross Referenced for TestBaseNullConfiguration.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.math.BigDecimal;
021:        import java.math.BigInteger;
022:        import java.util.Iterator;
023:        import java.util.List;
024:        import java.util.NoSuchElementException;
025:        import java.util.Properties;
026:
027:        import junit.framework.TestCase;
028:        import junitx.framework.ObjectAssert;
029:
030:        /**
031:         * Tests some basic functions of the BaseConfiguration class. Missing keys might
032:         * return null.
033:         *
034:         * @version $Id: TestBaseNullConfiguration.java 439648 2006-09-02 20:42:10Z oheger $
035:         */
036:        public class TestBaseNullConfiguration extends TestCase {
037:            protected BaseConfiguration config = null;
038:
039:            protected static Class missingElementException = NoSuchElementException.class;
040:            protected static Class incompatibleElementException = ConversionException.class;
041:
042:            protected void setUp() throws Exception {
043:                config = new BaseConfiguration();
044:                config.setThrowExceptionOnMissing(false);
045:            }
046:
047:            public void testThrowExceptionOnMissing() {
048:                assertFalse("Throw Exception Property is set!", config
049:                        .isThrowExceptionOnMissing());
050:            }
051:
052:            public void testGetProperty() {
053:                /* should be empty and return null */
054:                assertEquals("This returns null", config.getProperty("foo"),
055:                        null);
056:
057:                /* add a real value, and get it two different ways */
058:                config.setProperty("number", "1");
059:                assertEquals("This returns '1'", config.getProperty("number"),
060:                        "1");
061:                assertEquals("This returns '1'", config.getString("number"),
062:                        "1");
063:            }
064:
065:            public void testGetByte() {
066:                config.setProperty("number", "1");
067:                byte oneB = 1;
068:                byte twoB = 2;
069:                assertEquals("This returns 1(byte)", oneB, config
070:                        .getByte("number"));
071:                assertEquals("This returns 1(byte)", oneB, config.getByte(
072:                        "number", twoB));
073:                assertEquals("This returns 2(default byte)", twoB, config
074:                        .getByte("numberNotInConfig", twoB));
075:                assertEquals("This returns 1(Byte)", new Byte(oneB), config
076:                        .getByte("number", new Byte("2")));
077:
078:                // missing key without default value
079:                Throwable t = null;
080:                try {
081:                    config.getByte("numberNotInConfig");
082:                } catch (Throwable T) {
083:                    t = T;
084:                }
085:                assertNotNull("No exception thrown for missing keys", t);
086:                ObjectAssert.assertInstanceOf(
087:                        "Exception thrown for missing keys",
088:                        missingElementException, t);
089:
090:                // existing key with an incompatible value
091:                config.setProperty("test.empty", "");
092:                t = null;
093:                try {
094:                    config.getByte("test.empty");
095:                } catch (Throwable T) {
096:                    t = T;
097:                }
098:                assertNotNull("No exception thrown for incompatible values", t);
099:                ObjectAssert.assertInstanceOf(
100:                        "Exception thrown for incompatible values",
101:                        incompatibleElementException, t);
102:            }
103:
104:            public void testGetShort() {
105:                config.setProperty("numberS", "1");
106:                short oneS = 1;
107:                short twoS = 2;
108:                assertEquals("This returns 1(short)", oneS, config
109:                        .getShort("numberS"));
110:                assertEquals("This returns 1(short)", oneS, config.getShort(
111:                        "numberS", twoS));
112:                assertEquals("This returns 2(default short)", twoS, config
113:                        .getShort("numberNotInConfig", twoS));
114:                assertEquals("This returns 1(Short)", new Short(oneS), config
115:                        .getShort("numberS", new Short("2")));
116:
117:                // missing key without default value
118:                Throwable t = null;
119:                try {
120:                    config.getShort("numberNotInConfig");
121:                } catch (Throwable T) {
122:                    t = T;
123:                }
124:                assertNotNull("No exception thrown for missing keys", t);
125:                ObjectAssert.assertInstanceOf(
126:                        "Exception thrown for missing keys",
127:                        missingElementException, t);
128:
129:                // existing key with an incompatible value
130:                config.setProperty("test.empty", "");
131:                t = null;
132:                try {
133:                    config.getShort("test.empty");
134:                } catch (Throwable T) {
135:                    t = T;
136:                }
137:                assertNotNull("No exception thrown for incompatible values", t);
138:                ObjectAssert.assertInstanceOf(
139:                        "Exception thrown for incompatible values",
140:                        incompatibleElementException, t);
141:            }
142:
143:            public void testGetLong() {
144:                config.setProperty("numberL", "1");
145:                long oneL = 1;
146:                long twoL = 2;
147:                assertEquals("This returns 1(long)", oneL, config
148:                        .getLong("numberL"));
149:                assertEquals("This returns 1(long)", oneL, config.getLong(
150:                        "numberL", twoL));
151:                assertEquals("This returns 2(default long)", twoL, config
152:                        .getLong("numberNotInConfig", twoL));
153:                assertEquals("This returns 1(Long)", new Long(oneL), config
154:                        .getLong("numberL", new Long("2")));
155:
156:                // missing key without default value
157:                Throwable t = null;
158:                try {
159:                    config.getLong("numberNotInConfig");
160:                } catch (Throwable T) {
161:                    t = T;
162:                }
163:                assertNotNull("No exception thrown for missing keys", t);
164:                ObjectAssert.assertInstanceOf(
165:                        "Exception thrown for missing keys",
166:                        missingElementException, t);
167:
168:                // existing key with an incompatible value
169:                config.setProperty("test.empty", "");
170:                t = null;
171:                try {
172:                    config.getLong("test.empty");
173:                } catch (Throwable T) {
174:                    t = T;
175:                }
176:                assertNotNull("No exception thrown for incompatible values", t);
177:                ObjectAssert.assertInstanceOf(
178:                        "Exception thrown for incompatible values",
179:                        incompatibleElementException, t);
180:            }
181:
182:            public void testGetFloat() {
183:                config.setProperty("numberF", "1.0");
184:                float oneF = 1;
185:                float twoF = 2;
186:                assertEquals("This returns 1(float)", oneF, config
187:                        .getFloat("numberF"), 0);
188:                assertEquals("This returns 1(float)", oneF, config.getFloat(
189:                        "numberF", twoF), 0);
190:                assertEquals("This returns 2(default float)", twoF, config
191:                        .getFloat("numberNotInConfig", twoF), 0);
192:                assertEquals("This returns 1(Float)", new Float(oneF), config
193:                        .getFloat("numberF", new Float("2")));
194:
195:                // missing key without default value
196:                Throwable t = null;
197:                try {
198:                    config.getFloat("numberNotInConfig");
199:                } catch (Throwable T) {
200:                    t = T;
201:                }
202:                assertNotNull("No exception thrown for missing keys", t);
203:                ObjectAssert.assertInstanceOf(
204:                        "Exception thrown for missing keys",
205:                        missingElementException, t);
206:
207:                // existing key with an incompatible value
208:                config.setProperty("test.empty", "");
209:                t = null;
210:                try {
211:                    config.getFloat("test.empty");
212:                } catch (Throwable T) {
213:                    t = T;
214:                }
215:                assertNotNull("No exception thrown for incompatible values", t);
216:                ObjectAssert.assertInstanceOf(
217:                        "Exception thrown for incompatible values",
218:                        incompatibleElementException, t);
219:            }
220:
221:            public void testGetDouble() {
222:                config.setProperty("numberD", "1.0");
223:                double oneD = 1;
224:                double twoD = 2;
225:                assertEquals("This returns 1(double)", oneD, config
226:                        .getDouble("numberD"), 0);
227:                assertEquals("This returns 1(double)", oneD, config.getDouble(
228:                        "numberD", twoD), 0);
229:                assertEquals("This returns 2(default double)", twoD, config
230:                        .getDouble("numberNotInConfig", twoD), 0);
231:                assertEquals("This returns 1(Double)", new Double(oneD), config
232:                        .getDouble("numberD", new Double("2")));
233:
234:                // missing key without default value
235:                Throwable t = null;
236:                try {
237:                    config.getDouble("numberNotInConfig");
238:                } catch (Throwable T) {
239:                    t = T;
240:                }
241:                assertNotNull("No exception thrown for missing keys", t);
242:                ObjectAssert.assertInstanceOf(
243:                        "Exception thrown for missing keys",
244:                        missingElementException, t);
245:
246:                // existing key with an incompatible value
247:                config.setProperty("test.empty", "");
248:                t = null;
249:                try {
250:                    config.getDouble("test.empty");
251:                } catch (Throwable T) {
252:                    t = T;
253:                }
254:                assertNotNull("No exception thrown for incompatible values", t);
255:                ObjectAssert.assertInstanceOf(
256:                        "Exception thrown for incompatible values",
257:                        incompatibleElementException, t);
258:            }
259:
260:            public void testGetBigDecimal() {
261:                config.setProperty("numberBigD", "123.456");
262:                BigDecimal number = new BigDecimal("123.456");
263:                BigDecimal defaultValue = new BigDecimal("654.321");
264:
265:                assertEquals("Existing key", number, config
266:                        .getBigDecimal("numberBigD"));
267:                assertEquals("Existing key with default value", number, config
268:                        .getBigDecimal("numberBigD", defaultValue));
269:                assertEquals("Missing key with default value", defaultValue,
270:                        config.getBigDecimal("numberNotInConfig", defaultValue));
271:
272:                // missing key without default value
273:                assertEquals("Missing Key is not null!", null, config
274:                        .getBigDecimal("numberNotInConfig"));
275:
276:                // existing key with an incompatible value
277:                config.setProperty("test.empty", "");
278:                Throwable t = null;
279:                try {
280:                    config.getBigDecimal("test.empty");
281:                } catch (Throwable T) {
282:                    t = T;
283:                }
284:                assertNotNull("No exception thrown for incompatible values", t);
285:                ObjectAssert.assertInstanceOf(
286:                        "Exception thrown for incompatible values",
287:                        incompatibleElementException, t);
288:            }
289:
290:            public void testGetBigInteger() {
291:                config.setProperty("numberBigI", "1234567890");
292:                BigInteger number = new BigInteger("1234567890");
293:                BigInteger defaultValue = new BigInteger("654321");
294:
295:                assertEquals("Existing key", number, config
296:                        .getBigInteger("numberBigI"));
297:                assertEquals("Existing key with default value", number, config
298:                        .getBigInteger("numberBigI", defaultValue));
299:                assertEquals("Missing key with default value", defaultValue,
300:                        config.getBigInteger("numberNotInConfig", defaultValue));
301:
302:                // missing key without default value
303:                assertEquals("Missing Key is not null!", null, config
304:                        .getBigInteger("numberNotInConfig"));
305:
306:                // existing key with an incompatible value
307:                config.setProperty("test.empty", "");
308:                Throwable t = null;
309:                try {
310:                    config.getBigInteger("test.empty");
311:                } catch (Throwable T) {
312:                    t = T;
313:                }
314:                assertNotNull("No exception thrown for incompatible values", t);
315:                ObjectAssert.assertInstanceOf(
316:                        "Exception thrown for incompatible values",
317:                        incompatibleElementException, t);
318:            }
319:
320:            public void testGetString() {
321:                config.setProperty("testString", "The quick brown fox");
322:                String string = new String("The quick brown fox");
323:                String defaultValue = new String("jumps over the lazy dog");
324:
325:                assertEquals("Existing key", string, config
326:                        .getString("testString"));
327:                assertEquals("Existing key with default value", string, config
328:                        .getString("testString", defaultValue));
329:                assertEquals("Missing key with default value", defaultValue,
330:                        config.getString("stringNotInConfig", defaultValue));
331:
332:                // missing key without default value
333:                assertEquals("Missing Key is not null!", null, config
334:                        .getString("stringNotInConfig"));
335:
336:            }
337:
338:            public void testGetBoolean() {
339:                config.setProperty("boolA", Boolean.TRUE);
340:                boolean boolT = true, boolF = false;
341:                assertEquals("This returns true", boolT, config
342:                        .getBoolean("boolA"));
343:                assertEquals("This returns true, not the default", boolT,
344:                        config.getBoolean("boolA", boolF));
345:                assertEquals("This returns false(default)", boolF, config
346:                        .getBoolean("boolNotInConfig", boolF));
347:                assertEquals("This returns true(Boolean)", new Boolean(boolT),
348:                        config.getBoolean("boolA", new Boolean(boolF)));
349:
350:                // missing key without default value
351:                Throwable t = null;
352:                try {
353:                    config.getBoolean("numberNotInConfig");
354:                } catch (Throwable T) {
355:                    t = T;
356:                }
357:                assertNotNull("No exception thrown for missing keys", t);
358:                ObjectAssert.assertInstanceOf(
359:                        "Exception thrown for missing keys",
360:                        missingElementException, t);
361:
362:                // existing key with an incompatible value
363:                config.setProperty("test.empty", "");
364:                t = null;
365:                try {
366:                    config.getBoolean("test.empty");
367:                } catch (Throwable T) {
368:                    t = T;
369:                }
370:                assertNotNull("No exception thrown for incompatible values", t);
371:                ObjectAssert.assertInstanceOf(
372:                        "Exception thrown for incompatible values",
373:                        incompatibleElementException, t);
374:            }
375:
376:            public void testGetList() {
377:                config.addProperty("number", "1");
378:                config.addProperty("number", "2");
379:                List list = config.getList("number");
380:                assertNotNull("The list is null", list);
381:                assertEquals("List size", 2, list.size());
382:                assertTrue("The number 1 is missing from the list", list
383:                        .contains("1"));
384:                assertTrue("The number 2 is missing from the list", list
385:                        .contains("2"));
386:
387:                /*
388:                 *  now test dan's new fix where we get the first scalar
389:                 *  when we access a list valued property
390:                 */
391:                try {
392:                    config.getString("number");
393:                } catch (NoSuchElementException nsse) {
394:                    fail("Should return a string");
395:                }
396:            }
397:
398:            public void testCommaSeparatedString() {
399:                String prop = "hey, that's a test";
400:                config.setProperty("prop.string", prop);
401:                try {
402:                    config.getList("prop.string");
403:                } catch (NoSuchElementException nsse) {
404:                    fail("Should return a list");
405:                }
406:
407:                String prop2 = "hey\\, that's a test";
408:                config.clearProperty("prop.string");
409:                config.setProperty("prop.string", prop2);
410:                try {
411:                    config.getString("prop.string");
412:                } catch (NoSuchElementException nsse) {
413:                    fail("Should return a list");
414:                }
415:
416:            }
417:
418:            public void testPropertyAccess() {
419:                config.clearProperty("prop.properties");
420:                config.setProperty("prop.properties", "");
421:                assertEquals("This returns an empty Properties object", config
422:                        .getProperties("prop.properties"), new Properties());
423:                config.clearProperty("prop.properties");
424:                config.setProperty("prop.properties",
425:                        "foo=bar, baz=moo, seal=clubber");
426:
427:                Properties p = new Properties();
428:                p.setProperty("foo", "bar");
429:                p.setProperty("baz", "moo");
430:                p.setProperty("seal", "clubber");
431:                assertEquals("This returns a filled in Properties object",
432:                        config.getProperties("prop.properties"), p);
433:            }
434:
435:            public void testSubset() {
436:                /*
437:                 * test subset : assure we don't reprocess the data elements
438:                 * when generating the subset
439:                 */
440:
441:                String prop = "hey, that's a test";
442:                String prop2 = "hey\\, that's a test";
443:                config.setProperty("prop.string", prop2);
444:                config.setProperty("property.string", "hello");
445:
446:                Configuration subEprop = config.subset("prop");
447:
448:                assertEquals("Returns the full string", prop, subEprop
449:                        .getString("string"));
450:                try {
451:                    subEprop.getString("string");
452:                } catch (NoSuchElementException nsse) {
453:                    fail("Should return a string");
454:                }
455:                try {
456:                    subEprop.getList("string");
457:                } catch (NoSuchElementException nsse) {
458:                    fail("Should return a list");
459:                }
460:
461:                Iterator it = subEprop.getKeys();
462:                it.next();
463:                assertFalse(it.hasNext());
464:
465:                subEprop = config.subset("prop.");
466:                it = subEprop.getKeys();
467:                assertFalse(it.hasNext());
468:            }
469:
470:            public void testInterpolation() throws Exception {
471:                config.setProperty("applicationRoot", "/home/applicationRoot");
472:                config.setProperty("db", "${applicationRoot}/db/hypersonic");
473:                String unInterpolatedValue = "${applicationRoot2}/db/hypersonic";
474:                config.setProperty("dbFailedInterpolate", unInterpolatedValue);
475:                String dbProp = "/home/applicationRoot/db/hypersonic";
476:
477:                //construct a new config, using config as the defaults config for it.
478:                BaseConfiguration super Prop = config;
479:
480:                assertEquals("Checking interpolated variable", dbProp,
481:                        super Prop.getString("db"));
482:                assertEquals("lookup fails, leave variable as is", super Prop
483:                        .getString("dbFailedInterpolate"), unInterpolatedValue);
484:
485:                super Prop.setProperty("arrayInt", "${applicationRoot}/1");
486:                String[] arrayInt = super Prop.getStringArray("arrayInt");
487:                assertEquals("check first entry was interpolated",
488:                        "/home/applicationRoot/1", arrayInt[0]);
489:            }
490:
491:            public void testMultipleInterpolation() throws Exception {
492:                config.setProperty("test.base-level", "/base-level");
493:                config.setProperty("test.first-level",
494:                        "${test.base-level}/first-level");
495:                config.setProperty("test.second-level",
496:                        "${test.first-level}/second-level");
497:                config.setProperty("test.third-level",
498:                        "${test.second-level}/third-level");
499:
500:                String expectedValue = "/base-level/first-level/second-level/third-level";
501:
502:                assertEquals(config.getString("test.third-level"),
503:                        expectedValue);
504:            }
505:
506:            public void testInterpolationLoop() throws Exception {
507:                config.setProperty("test.a", "${test.b}");
508:                config.setProperty("test.b", "${test.a}");
509:
510:                try {
511:                    config.getString("test.a");
512:                } catch (IllegalStateException e) {
513:                    return;
514:                }
515:
516:                fail("IllegalStateException should have been thrown for looped property references");
517:            }
518:
519:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.