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

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


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.commons.configuration;
018:
019:        import java.math.BigDecimal;
020:        import java.util.List;
021:        import java.util.Iterator;
022:
023:        import junit.framework.TestCase;
024:
025:        /**
026:         * Test class for PropertyConverter.
027:         *
028:         * @author Emmanuel Bourg
029:         * @version $Revision: 439648 $, $Date: 2006-09-02 22:42:10 +0200 (Sa, 02 Sep 2006) $
030:         */
031:        public class TestPropertyConverter extends TestCase {
032:            public void testSplit() {
033:                String s = "abc, xyz , 123";
034:                List list = PropertyConverter.split(s, ',');
035:
036:                assertEquals("size", 3, list.size());
037:                assertEquals("1st token for '" + s + "'", "abc", list.get(0));
038:                assertEquals("2nd token for '" + s + "'", "xyz", list.get(1));
039:                assertEquals("3rd token for '" + s + "'", "123", list.get(2));
040:            }
041:
042:            public void testSplitWithEscapedSeparator() {
043:                String s = "abc\\,xyz, 123";
044:                List list = PropertyConverter.split(s, ',');
045:
046:                assertEquals("size", 2, list.size());
047:                assertEquals("1st token for '" + s + "'", "abc,xyz", list
048:                        .get(0));
049:                assertEquals("2nd token for '" + s + "'", "123", list.get(1));
050:            }
051:
052:            public void testSplitEmptyValues() {
053:                String s = ",,";
054:                List list = PropertyConverter.split(s, ',');
055:
056:                assertEquals("size", 3, list.size());
057:                assertEquals("1st token for '" + s + "'", "", list.get(0));
058:                assertEquals("2nd token for '" + s + "'", "", list.get(1));
059:                assertEquals("3rd token for '" + s + "'", "", list.get(2));
060:            }
061:
062:            public void testSplitWithEndingSlash() {
063:                String s = "abc, xyz\\";
064:                List list = PropertyConverter.split(s, ',');
065:
066:                assertEquals("size", 2, list.size());
067:                assertEquals("1st token for '" + s + "'", "abc", list.get(0));
068:                assertEquals("2nd token for '" + s + "'", "xyz\\", list.get(1));
069:            }
070:
071:            public void testSplitNull() {
072:                List list = PropertyConverter.split(null, ',');
073:                assertNotNull(list);
074:                assertTrue(list.isEmpty());
075:            }
076:
077:            public void testToIterator() {
078:                int[] array = new int[] { 1, 2, 3 };
079:
080:                Iterator it = PropertyConverter.toIterator(array, ',');
081:
082:                assertEquals("1st element", new Integer(1), it.next());
083:                assertEquals("2nd element", new Integer(2), it.next());
084:                assertEquals("3rd element", new Integer(3), it.next());
085:            }
086:
087:            /**
088:             * Tests the interpolation features.
089:             */
090:            public void testInterpolateString() {
091:                PropertiesConfiguration config = new PropertiesConfiguration();
092:                config.addProperty("animal", "quick brown fox");
093:                config.addProperty("target", "lazy dog");
094:                assertEquals("Wrong interpolation",
095:                        "The quick brown fox jumps over the lazy dog.",
096:                        PropertyConverter.interpolate(
097:                                "The ${animal} jumps over the ${target}.",
098:                                config));
099:            }
100:
101:            /**
102:             * Tests interpolation of an object. Here nothing should be substituted.
103:             */
104:            public void testInterpolateObject() {
105:                assertEquals("Object was not correctly interpolated",
106:                        new Integer(42), PropertyConverter.interpolate(
107:                                new Integer(42), new PropertiesConfiguration()));
108:            }
109:
110:            /**
111:             * Tests complex interpolation where the variables' values contain in turn
112:             * other variables.
113:             */
114:            public void testInterpolateRecursive() {
115:                PropertiesConfiguration config = new PropertiesConfiguration();
116:                config.addProperty("animal", "${animal_attr} fox");
117:                config.addProperty("target", "${target_attr} dog");
118:                config.addProperty("animal_attr", "quick brown");
119:                config.addProperty("target_attr", "lazy");
120:                assertEquals("Wrong complex interpolation",
121:                        "The quick brown fox jumps over the lazy dog.",
122:                        PropertyConverter.interpolate(
123:                                "The ${animal} jumps over the ${target}.",
124:                                config));
125:            }
126:
127:            /**
128:             * Tests an interpolation that leads to a cycle. This should throw an
129:             * exception.
130:             */
131:            public void testCyclicInterpolation() {
132:                PropertiesConfiguration config = new PropertiesConfiguration();
133:                config.addProperty("animal", "${animal_attr} ${species}");
134:                config.addProperty("animal_attr", "quick brown");
135:                config.addProperty("species", "${animal}");
136:                try {
137:                    PropertyConverter
138:                            .interpolate("This is a ${animal}", config);
139:                    fail("Cyclic interpolation was not detected!");
140:                } catch (IllegalStateException iex) {
141:                    // ok
142:                }
143:            }
144:
145:            /**
146:             * Tests interpolation if a variable is unknown. Then the variable won't be
147:             * substituted.
148:             */
149:            public void testInterpolationUnknownVariable() {
150:                PropertiesConfiguration config = new PropertiesConfiguration();
151:                config.addProperty("animal", "quick brown fox");
152:                assertEquals("Wrong interpolation",
153:                        "The quick brown fox jumps over ${target}.",
154:                        PropertyConverter.interpolate(
155:                                "The ${animal} jumps over ${target}.", config));
156:            }
157:
158:            /**
159:             * Tests conversion to numbers when the passed in objects are already
160:             * numbers.
161:             */
162:            public void testToNumberDirect() {
163:                Integer i = new Integer(42);
164:                assertSame("Wrong integer", i, PropertyConverter.toNumber(i,
165:                        Integer.class));
166:                BigDecimal d = new BigDecimal("3.1415");
167:                assertSame("Wrong BigDecimal", d, PropertyConverter.toNumber(d,
168:                        Integer.class));
169:            }
170:
171:            /**
172:             * Tests conversion to numbers when the passed in objects have a compatible
173:             * string representation.
174:             */
175:            public void testToNumberFromString() {
176:                assertEquals("Incorrect Integer value", new Integer(42),
177:                        PropertyConverter.toNumber("42", Integer.class));
178:                assertEquals("Incorrect Short value", new Short((short) 10),
179:                        PropertyConverter.toNumber(new StringBuffer("10"),
180:                                Short.class));
181:            }
182:
183:            /**
184:             * Tests conversion to numbers when the passed in objects are strings with
185:             * prefixes for special radices.
186:             */
187:            public void testToNumberFromHexString() {
188:                Number n = PropertyConverter.toNumber("0x10", Integer.class);
189:                assertEquals("Incorrect Integer value", 16, n.intValue());
190:            }
191:
192:            /**
193:             * Tests conversion to numbers when an invalid Hex value is passed in. This
194:             * should cause an exception.
195:             */
196:            public void testToNumberFromInvalidHexString() {
197:                try {
198:                    PropertyConverter.toNumber("0xNotAHexValue", Integer.class);
199:                    fail("Could convert invalid hex value!");
200:                } catch (ConversionException cex) {
201:                    // ok
202:                }
203:            }
204:
205:            /**
206:             * Tests conversion to numbers when the passed in objects have no numeric
207:             * String representation. This should cause an exception.
208:             */
209:            public void testToNumberFromInvalidString() {
210:                try {
211:                    PropertyConverter.toNumber("Not a number", Byte.class);
212:                    fail("Could convert invalid String!");
213:                } catch (ConversionException cex) {
214:                    // ok
215:                }
216:            }
217:
218:            /**
219:             * Tests conversion to numbers when the passed in target class is invalid.
220:             * This should cause an exception.
221:             */
222:            public void testToNumberWithInvalidClass() {
223:                try {
224:                    PropertyConverter.toNumber("42", Object.class);
225:                    fail("Could convert to invalid target class!");
226:                } catch (ConversionException cex) {
227:                    //ok
228:                }
229:            }
230:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.