Source Code Cross Referenced for TestBaseConfiguration.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.awt.event.KeyEvent;
021:        import java.math.BigDecimal;
022:        import java.math.BigInteger;
023:        import java.util.ArrayList;
024:        import java.util.Collection;
025:        import java.util.Iterator;
026:        import java.util.List;
027:        import java.util.NoSuchElementException;
028:        import java.util.Properties;
029:        import java.util.StringTokenizer;
030:
031:        import org.apache.commons.configuration.event.ConfigurationEvent;
032:        import org.apache.commons.configuration.event.ConfigurationListener;
033:        import org.apache.commons.configuration.interpol.ConfigurationInterpolator;
034:        import org.apache.commons.lang.text.StrLookup;
035:
036:        import junit.framework.TestCase;
037:        import junitx.framework.ListAssert;
038:        import junitx.framework.ObjectAssert;
039:
040:        /**
041:         * Tests some basic functions of the BaseConfiguration class. Missing keys will
042:         * throw Exceptions
043:         *
044:         * @version $Id: TestBaseConfiguration.java 491251 2006-12-30 16:36:46Z oheger $
045:         */
046:        public class TestBaseConfiguration extends TestCase {
047:            /** Constant for the number key.*/
048:            static final String KEY_NUMBER = "number";
049:
050:            protected BaseConfiguration config = null;
051:
052:            protected static Class missingElementException = NoSuchElementException.class;
053:            protected static Class incompatibleElementException = ConversionException.class;
054:
055:            protected void setUp() throws Exception {
056:                config = new BaseConfiguration();
057:                config.setThrowExceptionOnMissing(true);
058:            }
059:
060:            public void testThrowExceptionOnMissing() {
061:                assertTrue("Throw Exception Property is not set!", config
062:                        .isThrowExceptionOnMissing());
063:            }
064:
065:            public void testGetProperty() {
066:                /* should be empty and return null */
067:                assertEquals("This returns null", config.getProperty("foo"),
068:                        null);
069:
070:                /* add a real value, and get it two different ways */
071:                config.setProperty("number", "1");
072:                assertEquals("This returns '1'", config.getProperty("number"),
073:                        "1");
074:                assertEquals("This returns '1'", config.getString("number"),
075:                        "1");
076:            }
077:
078:            public void testGetByte() {
079:                config.setProperty("number", "1");
080:                byte oneB = 1;
081:                byte twoB = 2;
082:                assertEquals("This returns 1(byte)", oneB, config
083:                        .getByte("number"));
084:                assertEquals("This returns 1(byte)", oneB, config.getByte(
085:                        "number", twoB));
086:                assertEquals("This returns 2(default byte)", twoB, config
087:                        .getByte("numberNotInConfig", twoB));
088:                assertEquals("This returns 1(Byte)", new Byte(oneB), config
089:                        .getByte("number", new Byte("2")));
090:
091:                // missing key without default value
092:                Throwable t = null;
093:                try {
094:                    config.getByte("numberNotInConfig");
095:                } catch (Throwable T) {
096:                    t = T;
097:                }
098:                assertNotNull("No exception thrown for missing keys", t);
099:                ObjectAssert.assertInstanceOf(
100:                        "Exception thrown for missing keys",
101:                        missingElementException, t);
102:
103:                // existing key with an incompatible value
104:                config.setProperty("test.empty", "");
105:                t = null;
106:                try {
107:                    config.getByte("test.empty");
108:                } catch (Throwable T) {
109:                    t = T;
110:                }
111:                assertNotNull("No exception thrown for incompatible values", t);
112:                ObjectAssert.assertInstanceOf(
113:                        "Exception thrown for incompatible values",
114:                        incompatibleElementException, t);
115:            }
116:
117:            public void testGetShort() {
118:                config.setProperty("numberS", "1");
119:                short oneS = 1;
120:                short twoS = 2;
121:                assertEquals("This returns 1(short)", oneS, config
122:                        .getShort("numberS"));
123:                assertEquals("This returns 1(short)", oneS, config.getShort(
124:                        "numberS", twoS));
125:                assertEquals("This returns 2(default short)", twoS, config
126:                        .getShort("numberNotInConfig", twoS));
127:                assertEquals("This returns 1(Short)", new Short(oneS), config
128:                        .getShort("numberS", new Short("2")));
129:
130:                // missing key without default value
131:                Throwable t = null;
132:                try {
133:                    config.getShort("numberNotInConfig");
134:                } catch (Throwable T) {
135:                    t = T;
136:                }
137:                assertNotNull("No exception thrown for missing keys", t);
138:                ObjectAssert.assertInstanceOf(
139:                        "Exception thrown for missing keys",
140:                        missingElementException, t);
141:
142:                // existing key with an incompatible value
143:                config.setProperty("test.empty", "");
144:                t = null;
145:                try {
146:                    config.getShort("test.empty");
147:                } catch (Throwable T) {
148:                    t = T;
149:                }
150:                assertNotNull("No exception thrown for incompatible values", t);
151:                ObjectAssert.assertInstanceOf(
152:                        "Exception thrown for incompatible values",
153:                        incompatibleElementException, t);
154:            }
155:
156:            public void testGetLong() {
157:                config.setProperty("numberL", "1");
158:                long oneL = 1;
159:                long twoL = 2;
160:                assertEquals("This returns 1(long)", oneL, config
161:                        .getLong("numberL"));
162:                assertEquals("This returns 1(long)", oneL, config.getLong(
163:                        "numberL", twoL));
164:                assertEquals("This returns 2(default long)", twoL, config
165:                        .getLong("numberNotInConfig", twoL));
166:                assertEquals("This returns 1(Long)", new Long(oneL), config
167:                        .getLong("numberL", new Long("2")));
168:
169:                // missing key without default value
170:                Throwable t = null;
171:                try {
172:                    config.getLong("numberNotInConfig");
173:                } catch (Throwable T) {
174:                    t = T;
175:                }
176:                assertNotNull("No exception thrown for missing keys", t);
177:                ObjectAssert.assertInstanceOf(
178:                        "Exception thrown for missing keys",
179:                        missingElementException, t);
180:
181:                // existing key with an incompatible value
182:                config.setProperty("test.empty", "");
183:                t = null;
184:                try {
185:                    config.getLong("test.empty");
186:                } catch (Throwable T) {
187:                    t = T;
188:                }
189:                assertNotNull("No exception thrown for incompatible values", t);
190:                ObjectAssert.assertInstanceOf(
191:                        "Exception thrown for incompatible values",
192:                        incompatibleElementException, t);
193:            }
194:
195:            public void testGetFloat() {
196:                config.setProperty("numberF", "1.0");
197:                float oneF = 1;
198:                float twoF = 2;
199:                assertEquals("This returns 1(float)", oneF, config
200:                        .getFloat("numberF"), 0);
201:                assertEquals("This returns 1(float)", oneF, config.getFloat(
202:                        "numberF", twoF), 0);
203:                assertEquals("This returns 2(default float)", twoF, config
204:                        .getFloat("numberNotInConfig", twoF), 0);
205:                assertEquals("This returns 1(Float)", new Float(oneF), config
206:                        .getFloat("numberF", new Float("2")));
207:
208:                // missing key without default value
209:                Throwable t = null;
210:                try {
211:                    config.getFloat("numberNotInConfig");
212:                } catch (Throwable T) {
213:                    t = T;
214:                }
215:                assertNotNull("No exception thrown for missing keys", t);
216:                ObjectAssert.assertInstanceOf(
217:                        "Exception thrown for missing keys",
218:                        missingElementException, t);
219:
220:                // existing key with an incompatible value
221:                config.setProperty("test.empty", "");
222:                t = null;
223:                try {
224:                    config.getFloat("test.empty");
225:                } catch (Throwable T) {
226:                    t = T;
227:                }
228:                assertNotNull("No exception thrown for incompatible values", t);
229:                ObjectAssert.assertInstanceOf(
230:                        "Exception thrown for incompatible values",
231:                        incompatibleElementException, t);
232:            }
233:
234:            public void testGetDouble() {
235:                config.setProperty("numberD", "1.0");
236:                double oneD = 1;
237:                double twoD = 2;
238:                assertEquals("This returns 1(double)", oneD, config
239:                        .getDouble("numberD"), 0);
240:                assertEquals("This returns 1(double)", oneD, config.getDouble(
241:                        "numberD", twoD), 0);
242:                assertEquals("This returns 2(default double)", twoD, config
243:                        .getDouble("numberNotInConfig", twoD), 0);
244:                assertEquals("This returns 1(Double)", new Double(oneD), config
245:                        .getDouble("numberD", new Double("2")));
246:
247:                // missing key without default value
248:                Throwable t = null;
249:                try {
250:                    config.getDouble("numberNotInConfig");
251:                } catch (Throwable T) {
252:                    t = T;
253:                }
254:                assertNotNull("No exception thrown for missing keys", t);
255:                ObjectAssert.assertInstanceOf(
256:                        "Exception thrown for missing keys",
257:                        missingElementException, t);
258:
259:                // existing key with an incompatible value
260:                config.setProperty("test.empty", "");
261:                t = null;
262:                try {
263:                    config.getDouble("test.empty");
264:                } catch (Throwable T) {
265:                    t = T;
266:                }
267:                assertNotNull("No exception thrown for incompatible values", t);
268:                ObjectAssert.assertInstanceOf(
269:                        "Exception thrown for incompatible values",
270:                        incompatibleElementException, t);
271:            }
272:
273:            public void testGetBigDecimal() {
274:                config.setProperty("numberBigD", "123.456");
275:                BigDecimal number = new BigDecimal("123.456");
276:                BigDecimal defaultValue = new BigDecimal("654.321");
277:
278:                assertEquals("Existing key", number, config
279:                        .getBigDecimal("numberBigD"));
280:                assertEquals("Existing key with default value", number, config
281:                        .getBigDecimal("numberBigD", defaultValue));
282:                assertEquals("Missing key with default value", defaultValue,
283:                        config.getBigDecimal("numberNotInConfig", defaultValue));
284:
285:                // missing key without default value
286:                Throwable t = null;
287:                try {
288:                    config.getBigDecimal("numberNotInConfig");
289:                } catch (Throwable T) {
290:                    t = T;
291:                }
292:                assertNotNull("No exception thrown for missing keys", t);
293:                ObjectAssert.assertInstanceOf(
294:                        "Exception thrown for missing keys",
295:                        missingElementException, t);
296:
297:                // existing key with an incompatible value
298:                config.setProperty("test.empty", "");
299:                t = null;
300:                try {
301:                    config.getBigDecimal("test.empty");
302:                } catch (Throwable T) {
303:                    t = T;
304:                }
305:                assertNotNull("No exception thrown for incompatible values", t);
306:                ObjectAssert.assertInstanceOf(
307:                        "Exception thrown for incompatible values",
308:                        incompatibleElementException, t);
309:            }
310:
311:            public void testGetBigInteger() {
312:                config.setProperty("numberBigI", "1234567890");
313:                BigInteger number = new BigInteger("1234567890");
314:                BigInteger defaultValue = new BigInteger("654321");
315:
316:                assertEquals("Existing key", number, config
317:                        .getBigInteger("numberBigI"));
318:                assertEquals("Existing key with default value", number, config
319:                        .getBigInteger("numberBigI", defaultValue));
320:                assertEquals("Missing key with default value", defaultValue,
321:                        config.getBigInteger("numberNotInConfig", defaultValue));
322:
323:                // missing key without default value
324:                Throwable t = null;
325:                try {
326:                    config.getBigInteger("numberNotInConfig");
327:                } catch (Throwable T) {
328:                    t = T;
329:                }
330:                assertNotNull("No exception thrown for missing keys", t);
331:                ObjectAssert.assertInstanceOf(
332:                        "Exception thrown for missing keys",
333:                        missingElementException, t);
334:
335:                // existing key with an incompatible value
336:                config.setProperty("test.empty", "");
337:                t = null;
338:                try {
339:                    config.getBigInteger("test.empty");
340:                } catch (Throwable T) {
341:                    t = T;
342:                }
343:                assertNotNull("No exception thrown for incompatible values", t);
344:                ObjectAssert.assertInstanceOf(
345:                        "Exception thrown for incompatible values",
346:                        incompatibleElementException, t);
347:            }
348:
349:            public void testGetString() {
350:                config.setProperty("testString", "The quick brown fox");
351:                String string = "The quick brown fox";
352:                String defaultValue = "jumps over the lazy dog";
353:
354:                assertEquals("Existing key", string, config
355:                        .getString("testString"));
356:                assertEquals("Existing key with default value", string, config
357:                        .getString("testString", defaultValue));
358:                assertEquals("Missing key with default value", defaultValue,
359:                        config.getString("stringNotInConfig", defaultValue));
360:
361:                // missing key without default value
362:                Throwable t = null;
363:                try {
364:                    config.getString("stringNotInConfig");
365:                } catch (Throwable T) {
366:                    t = T;
367:                }
368:                assertNotNull("No exception thrown for missing keys", t);
369:                ObjectAssert.assertInstanceOf(
370:                        "Exception thrown for missing keys",
371:                        missingElementException, t);
372:            }
373:
374:            public void testGetBoolean() {
375:                config.setProperty("boolA", Boolean.TRUE);
376:                boolean boolT = true, boolF = false;
377:                assertEquals("This returns true", boolT, config
378:                        .getBoolean("boolA"));
379:                assertEquals("This returns true, not the default", boolT,
380:                        config.getBoolean("boolA", boolF));
381:                assertEquals("This returns false(default)", boolF, config
382:                        .getBoolean("boolNotInConfig", boolF));
383:                assertEquals("This returns true(Boolean)", new Boolean(boolT),
384:                        config.getBoolean("boolA", new Boolean(boolF)));
385:
386:                // missing key without default value
387:                Throwable t = null;
388:                try {
389:                    config.getBoolean("numberNotInConfig");
390:                } catch (Throwable T) {
391:                    t = T;
392:                }
393:                assertNotNull("No exception thrown for missing keys", t);
394:                ObjectAssert.assertInstanceOf(
395:                        "Exception thrown for missing keys",
396:                        missingElementException, t);
397:
398:                // existing key with an incompatible value
399:                config.setProperty("test.empty", "");
400:                t = null;
401:                try {
402:                    config.getBoolean("test.empty");
403:                } catch (Throwable T) {
404:                    t = T;
405:                }
406:                assertNotNull("No exception thrown for incompatible values", t);
407:                ObjectAssert.assertInstanceOf(
408:                        "Exception thrown for incompatible values",
409:                        incompatibleElementException, t);
410:            }
411:
412:            public void testGetList() {
413:                config.addProperty("number", "1");
414:                config.addProperty("number", "2");
415:                List list = config.getList("number");
416:                assertNotNull("The list is null", list);
417:                assertEquals("List size", 2, list.size());
418:                assertTrue("The number 1 is missing from the list", list
419:                        .contains("1"));
420:                assertTrue("The number 2 is missing from the list", list
421:                        .contains("2"));
422:
423:                /*
424:                 *  now test dan's new fix where we get the first scalar
425:                 *  when we access a list valued property
426:                 */
427:                try {
428:                    config.getString("number");
429:                } catch (NoSuchElementException nsse) {
430:                    fail("Should return a string");
431:                }
432:            }
433:
434:            public void testGetInterpolatedList() {
435:                config.addProperty("number", "1");
436:                config.addProperty("array", "${number}");
437:                config.addProperty("array", "${number}");
438:
439:                List list = new ArrayList();
440:                list.add("1");
441:                list.add("1");
442:
443:                ListAssert.assertEquals("'array' property", list, config
444:                        .getList("array"));
445:            }
446:
447:            public void testGetInterpolatedPrimitives() {
448:                config.addProperty("number", "1");
449:                config.addProperty("value", "${number}");
450:
451:                config.addProperty("boolean", "true");
452:                config.addProperty("booleanValue", "${boolean}");
453:
454:                // primitive types
455:                assertEquals("boolean interpolation", true, config
456:                        .getBoolean("booleanValue"));
457:                assertEquals("byte interpolation", 1, config.getByte("value"));
458:                assertEquals("short interpolation", 1, config.getShort("value"));
459:                assertEquals("int interpolation", 1, config.getInt("value"));
460:                assertEquals("long interpolation", 1, config.getLong("value"));
461:                assertEquals("float interpolation", 1,
462:                        config.getFloat("value"), 0);
463:                assertEquals("double interpolation", 1, config
464:                        .getDouble("value"), 0);
465:
466:                // primitive wrappers
467:                assertEquals("Boolean interpolation", Boolean.TRUE, config
468:                        .getBoolean("booleanValue", null));
469:                assertEquals("Byte interpolation", new Byte("1"), config
470:                        .getByte("value", null));
471:                assertEquals("Short interpolation", new Short("1"), config
472:                        .getShort("value", null));
473:                assertEquals("Integer interpolation", new Integer("1"), config
474:                        .getInteger("value", null));
475:                assertEquals("Long interpolation", new Long("1"), config
476:                        .getLong("value", null));
477:                assertEquals("Float interpolation", new Float("1"), config
478:                        .getFloat("value", null));
479:                assertEquals("Double interpolation", new Double("1"), config
480:                        .getDouble("value", null));
481:
482:                assertEquals("BigInteger interpolation", new BigInteger("1"),
483:                        config.getBigInteger("value", null));
484:                assertEquals("BigDecimal interpolation", new BigDecimal("1"),
485:                        config.getBigDecimal("value", null));
486:            }
487:
488:            public void testCommaSeparatedString() {
489:                String prop = "hey, that's a test";
490:                config.setProperty("prop.string", prop);
491:                try {
492:                    config.getList("prop.string");
493:                } catch (NoSuchElementException nsse) {
494:                    fail("Should return a list");
495:                }
496:
497:                String prop2 = "hey\\, that's a test";
498:                config.clearProperty("prop.string");
499:                config.setProperty("prop.string", prop2);
500:                try {
501:                    config.getString("prop.string");
502:                } catch (NoSuchElementException nsse) {
503:                    fail("Should return a list");
504:                }
505:
506:            }
507:
508:            public void testAddProperty() throws Exception {
509:                Collection props = new ArrayList();
510:                props.add("one");
511:                props.add("two,three,four");
512:                props.add(new String[] { "5.1", "5.2", "5.3,5.4", "5.5" });
513:                props.add("six");
514:                config.addProperty("complex.property", props);
515:
516:                Object val = config.getProperty("complex.property");
517:                assertTrue(val instanceof  Collection);
518:                Collection col = (Collection) val;
519:                assertEquals(10, col.size());
520:
521:                props = new ArrayList();
522:                props.add("quick");
523:                props.add("brown");
524:                props.add("fox,jumps");
525:                Object[] data = new Object[] { "The", props, "over,the",
526:                        "lazy", "dog." };
527:                config.setProperty("complex.property", data);
528:                val = config.getProperty("complex.property");
529:                assertTrue(val instanceof  Collection);
530:                col = (Collection) val;
531:                Iterator it = col.iterator();
532:                StringTokenizer tok = new StringTokenizer(
533:                        "The quick brown fox jumps over the lazy dog.", " ");
534:                while (tok.hasMoreTokens()) {
535:                    assertTrue(it.hasNext());
536:                    assertEquals(tok.nextToken(), it.next());
537:                }
538:                assertFalse(it.hasNext());
539:
540:                config.setProperty("complex.property", null);
541:                assertFalse(config.containsKey("complex.property"));
542:            }
543:
544:            public void testPropertyAccess() {
545:                config.clearProperty("prop.properties");
546:                config.setProperty("prop.properties", "");
547:                assertEquals("This returns an empty Properties object", config
548:                        .getProperties("prop.properties"), new Properties());
549:                config.clearProperty("prop.properties");
550:                config.setProperty("prop.properties",
551:                        "foo=bar, baz=moo, seal=clubber");
552:
553:                Properties p = new Properties();
554:                p.setProperty("foo", "bar");
555:                p.setProperty("baz", "moo");
556:                p.setProperty("seal", "clubber");
557:                assertEquals("This returns a filled in Properties object",
558:                        config.getProperties("prop.properties"), p);
559:            }
560:
561:            public void testSubset() {
562:                /*
563:                 * test subset : assure we don't reprocess the data elements
564:                 * when generating the subset
565:                 */
566:
567:                String prop = "hey, that's a test";
568:                String prop2 = "hey\\, that's a test";
569:                config.setProperty("prop.string", prop2);
570:                config.setProperty("property.string", "hello");
571:
572:                Configuration subEprop = config.subset("prop");
573:
574:                assertEquals("Returns the full string", prop, subEprop
575:                        .getString("string"));
576:                try {
577:                    subEprop.getString("string");
578:                } catch (NoSuchElementException nsse) {
579:                    fail("Should return a string");
580:                }
581:                try {
582:                    subEprop.getList("string");
583:                } catch (NoSuchElementException nsse) {
584:                    fail("Should return a list");
585:                }
586:
587:                Iterator it = subEprop.getKeys();
588:                it.next();
589:                assertFalse(it.hasNext());
590:
591:                subEprop = config.subset("prop.");
592:                it = subEprop.getKeys();
593:                assertFalse(it.hasNext());
594:            }
595:
596:            public void testInterpolation() throws Exception {
597:                config.setProperty("applicationRoot", "/home/applicationRoot");
598:                config.setProperty("db", "${applicationRoot}/db/hypersonic");
599:                String unInterpolatedValue = "${applicationRoot2}/db/hypersonic";
600:                config.setProperty("dbFailedInterpolate", unInterpolatedValue);
601:                String dbProp = "/home/applicationRoot/db/hypersonic";
602:
603:                //construct a new config, using config as the defaults config for it.
604:                BaseConfiguration super Prop = config;
605:
606:                assertEquals("Checking interpolated variable", dbProp,
607:                        super Prop.getString("db"));
608:                assertEquals("lookup fails, leave variable as is", super Prop
609:                        .getString("dbFailedInterpolate"), unInterpolatedValue);
610:
611:                super Prop.setProperty("arrayInt", "${applicationRoot}/1");
612:                String[] arrayInt = super Prop.getStringArray("arrayInt");
613:                assertEquals("check first entry was interpolated",
614:                        "/home/applicationRoot/1", arrayInt[0]);
615:
616:                config.addProperty("path", "/temp,C:\\Temp,/usr/local/tmp");
617:                config.setProperty("path.current", "${path}");
618:                assertEquals("Interpolation with multi-valued property",
619:                        "/temp", super Prop.getString("path.current"));
620:            }
621:
622:            public void testMultipleInterpolation() throws Exception {
623:                config.setProperty("test.base-level", "/base-level");
624:                config.setProperty("test.first-level",
625:                        "${test.base-level}/first-level");
626:                config.setProperty("test.second-level",
627:                        "${test.first-level}/second-level");
628:                config.setProperty("test.third-level",
629:                        "${test.second-level}/third-level");
630:
631:                String expectedValue = "/base-level/first-level/second-level/third-level";
632:
633:                assertEquals(config.getString("test.third-level"),
634:                        expectedValue);
635:            }
636:
637:            public void testInterpolationLoop() throws Exception {
638:                config.setProperty("test.a", "${test.b}");
639:                config.setProperty("test.b", "${test.a}");
640:
641:                try {
642:                    config.getString("test.a");
643:                } catch (IllegalStateException e) {
644:                    return;
645:                }
646:
647:                fail("IllegalStateException should have been thrown for looped property references");
648:            }
649:
650:            /**
651:             * Tests interpolation when a subset configuration is involved.
652:             */
653:            public void testInterpolationSubset() {
654:                config.addProperty("test.a", new Integer(42));
655:                config.addProperty("test.b", "${test.a}");
656:                assertEquals("Wrong interpolated value", 42, config
657:                        .getInt("test.b"));
658:                Configuration subset = config.subset("test");
659:                assertEquals("Wrong string property", "42", subset
660:                        .getString("b"));
661:                assertEquals("Wrong int property", 42, subset.getInt("b"));
662:            }
663:
664:            /**
665:             * Tests interpolation when the referred property is not found.
666:             */
667:            public void testInterpolationUnknownProperty() {
668:                config.addProperty("test.interpol", "${unknown.property}");
669:                assertEquals("Wrong interpolated unknown property",
670:                        "${unknown.property}", config
671:                                .getString("test.interpol"));
672:            }
673:
674:            /**
675:             * Tests interpolation of system properties.
676:             */
677:            public void testInterpolationSystemProperties() {
678:                String[] sysProperties = { "java.version", "java.vendor",
679:                        "os.name", "java.class.path" };
680:                for (int i = 0; i < sysProperties.length; i++) {
681:                    config.addProperty("prop" + i, "${sys:" + sysProperties[i]
682:                            + "}");
683:                }
684:
685:                for (int i = 0; i < sysProperties.length; i++) {
686:                    assertEquals("Wrong value for system property "
687:                            + sysProperties[i], System
688:                            .getProperty(sysProperties[i]), config
689:                            .getString("prop" + i));
690:                }
691:            }
692:
693:            /**
694:             * Tests interpolation of constant values.
695:             */
696:            public void testInterpolationConstants() {
697:                config.addProperty("key.code",
698:                        "${const:java.awt.event.KeyEvent.VK_CANCEL}");
699:                assertEquals("Wrong value of constant variable",
700:                        KeyEvent.VK_CANCEL, config.getInt("key.code"));
701:            }
702:
703:            /**
704:             * Tests whether a variable can be escaped, so that it won't be
705:             * interpolated.
706:             */
707:            public void testInterpolationEscaped() {
708:                config.addProperty("var", "x");
709:                config.addProperty("escVar", "Use the variable $${${var}}.");
710:                assertEquals("Wrong escaped variable",
711:                        "Use the variable ${x}.", config.getString("escVar"));
712:            }
713:
714:            /**
715:             * Tests accessing and manipulating the interpolator object.
716:             */
717:            public void testGetInterpolator() {
718:                config.addProperty("var", "${echo:testVar}");
719:                ConfigurationInterpolator interpol = config.getInterpolator();
720:                interpol.registerLookup("echo", new StrLookup() {
721:                    public String lookup(String varName) {
722:                        return "Value of variable " + varName;
723:                    }
724:                });
725:                assertEquals("Wrong value of echo variable",
726:                        "Value of variable testVar", config.getString("var"));
727:            }
728:
729:            public void testGetHexadecimalValue() {
730:                config.setProperty("number", "0xFF");
731:                assertEquals("byte value", (byte) 0xFF, config
732:                        .getByte("number"));
733:
734:                config.setProperty("number", "0xFFFF");
735:                assertEquals("short value", (short) 0xFFFF, config
736:                        .getShort("number"));
737:
738:                config.setProperty("number", "0xFFFFFFFF");
739:                assertEquals("int value", 0xFFFFFFFF, config.getInt("number"));
740:
741:                config.setProperty("number", "0xFFFFFFFFFFFFFFFF");
742:                assertEquals("long value", 0xFFFFFFFFFFFFFFFFL, config
743:                        .getLong("number"));
744:
745:                assertEquals("long value", 0xFFFFFFFFFFFFFFFFL, config
746:                        .getBigInteger("number").longValue());
747:            }
748:
749:            public void testResolveContainerStore() {
750:                AbstractConfiguration config = new BaseConfiguration();
751:
752:                // array of objects
753:                config
754:                        .addPropertyDirect("array",
755:                                new String[] { "foo", "bar" });
756:
757:                assertEquals("first element of the 'array' property", "foo",
758:                        config.resolveContainerStore("array"));
759:
760:                // list of objects
761:                List list = new ArrayList();
762:                list.add("foo");
763:                list.add("bar");
764:                config.addPropertyDirect("list", list);
765:
766:                assertEquals("first element of the 'list' property", "foo",
767:                        config.resolveContainerStore("list"));
768:
769:                // arrays of primitives
770:                config.addPropertyDirect("array.boolean", new boolean[] { true,
771:                        false });
772:                assertEquals("first element of the 'array.boolean' property",
773:                        true, config.getBoolean("array.boolean"));
774:
775:                config.addPropertyDirect("array.byte", new byte[] { 1, 2 });
776:                assertEquals("first element of the 'array.byte' property", 1,
777:                        config.getByte("array.byte"));
778:
779:                config.addPropertyDirect("array.short", new short[] { 1, 2 });
780:                assertEquals("first element of the 'array.short' property", 1,
781:                        config.getShort("array.short"));
782:
783:                config.addPropertyDirect("array.int", new int[] { 1, 2 });
784:                assertEquals("first element of the 'array.int' property", 1,
785:                        config.getInt("array.int"));
786:
787:                config.addPropertyDirect("array.long", new long[] { 1, 2 });
788:                assertEquals("first element of the 'array.long' property", 1,
789:                        config.getLong("array.long"));
790:
791:                config.addPropertyDirect("array.float", new float[] { 1, 2 });
792:                assertEquals("first element of the 'array.float' property", 1,
793:                        config.getFloat("array.float"), 0);
794:
795:                config.addPropertyDirect("array.double", new double[] { 1, 2 });
796:                assertEquals("first element of the 'array.double' property", 1,
797:                        config.getDouble("array.double"), 0);
798:            }
799:
800:            /**
801:             * Tests if conversion between number types is possible.
802:             */
803:            public void testNumberConversions() {
804:                config.setProperty(KEY_NUMBER, new Integer(42));
805:                assertEquals("Wrong int returned", 42, config
806:                        .getInt(KEY_NUMBER));
807:                assertEquals("Wrong long returned", 42L, config
808:                        .getLong(KEY_NUMBER));
809:                assertEquals("Wrong byte returned", (byte) 42, config
810:                        .getByte(KEY_NUMBER));
811:                assertEquals("Wrong float returned", 42.0f, config
812:                        .getFloat(KEY_NUMBER), 0.01f);
813:                assertEquals("Wrong double returned", 42.0, config
814:                        .getDouble(KEY_NUMBER), 0.001);
815:
816:                assertEquals("Wrong Long returned", new Long(42L), config
817:                        .getLong(KEY_NUMBER, null));
818:                assertEquals("Wrong BigInt returned", new BigInteger("42"),
819:                        config.getBigInteger(KEY_NUMBER));
820:                assertEquals("Wrong DigDecimal returned", new BigDecimal("42"),
821:                        config.getBigDecimal(KEY_NUMBER));
822:            }
823:
824:            /**
825:             * Tests cloning a BaseConfiguration.
826:             */
827:            public void testClone() {
828:                for (int i = 0; i < 10; i++) {
829:                    config.addProperty("key" + i, new Integer(i));
830:                }
831:                BaseConfiguration config2 = (BaseConfiguration) config.clone();
832:
833:                for (Iterator it = config.getKeys(); it.hasNext();) {
834:                    String key = (String) it.next();
835:                    assertTrue("Key not found: " + key, config2
836:                            .containsKey(key));
837:                    assertEquals("Wrong value for key " + key, config
838:                            .getProperty(key), config2.getProperty(key));
839:                }
840:            }
841:
842:            /**
843:             * Tests whether a cloned configuration is decoupled from its original.
844:             */
845:            public void testCloneModify() {
846:                ConfigurationListener l = new ConfigurationListener() {
847:                    public void configurationChanged(ConfigurationEvent event) {
848:                        // just a dummy
849:                    }
850:                };
851:                config.addConfigurationListener(l);
852:                config.addProperty("original", Boolean.TRUE);
853:                BaseConfiguration config2 = (BaseConfiguration) config.clone();
854:
855:                config2.addProperty("clone", Boolean.TRUE);
856:                assertFalse("New key appears in original", config
857:                        .containsKey("clone"));
858:                config2.setProperty("original", Boolean.FALSE);
859:                assertTrue("Wrong value of original property", config
860:                        .getBoolean("original"));
861:
862:                assertEquals("Event listener was copied", 0, config2
863:                        .getConfigurationListeners().size());
864:            }
865:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.