Source Code Cross Referenced for TestConfigurationUtils.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.net.URL;
022:        import java.util.ArrayList;
023:        import java.util.Iterator;
024:        import java.util.List;
025:
026:        import com.mockobjects.dynamic.Mock;
027:
028:        import junit.framework.TestCase;
029:        import junitx.framework.ListAssert;
030:
031:        /**
032:         * Tests the ConfigurationUtils class
033:         *
034:         * @version $Revision: 503227 $, $Date: 2007-02-03 17:19:15 +0100 (Sa, 03 Feb 2007) $
035:         */
036:        public class TestConfigurationUtils extends TestCase {
037:            protected Configuration config = new BaseConfiguration();
038:
039:            public void testToString() {
040:                String lineSeparator = System.getProperty("line.separator");
041:
042:                assertEquals("String representation of an empty configuration",
043:                        "", ConfigurationUtils.toString(config));
044:
045:                config.setProperty("one", "1");
046:                assertEquals("String representation of a configuration",
047:                        "one=1", ConfigurationUtils.toString(config));
048:
049:                config.setProperty("two", "2");
050:                assertEquals("String representation of a configuration",
051:                        "one=1" + lineSeparator + "two=2", ConfigurationUtils
052:                                .toString(config));
053:
054:                config.clearProperty("one");
055:                assertEquals("String representation of a configuration",
056:                        "two=2", ConfigurationUtils.toString(config));
057:
058:                config.setProperty("one", "1");
059:                assertEquals("String representation of a configuration",
060:                        "two=2" + lineSeparator + "one=1", ConfigurationUtils
061:                                .toString(config));
062:            }
063:
064:            public void testGetURL() throws Exception {
065:                assertEquals(
066:                        "http://localhost:8080/webapp/config/config.xml",
067:                        ConfigurationUtils
068:                                .getURL(
069:                                        "http://localhost:8080/webapp/config/baseConfig.xml",
070:                                        "config.xml").toString());
071:                assertEquals("http://localhost:8080/webapp/config/config.xml",
072:                        ConfigurationUtils.getURL(
073:                                "http://localhost:8080/webapp/baseConfig.xml",
074:                                "config/config.xml").toString());
075:                URL url = ConfigurationUtils.getURL(null, "config.xml");
076:                assertEquals("file", url.getProtocol());
077:                assertEquals("", url.getHost());
078:
079:                assertEquals(
080:                        "http://localhost:8080/webapp/config/config.xml",
081:                        ConfigurationUtils
082:                                .getURL(
083:                                        "ftp://ftp.server.com/downloads/baseConfig.xml",
084:                                        "http://localhost:8080/webapp/config/config.xml")
085:                                .toString());
086:                assertEquals(
087:                        "http://localhost:8080/webapp/config/config.xml",
088:                        ConfigurationUtils
089:                                .getURL(null,
090:                                        "http://localhost:8080/webapp/config/config.xml")
091:                                .toString());
092:                File absFile = new File("config.xml").getAbsoluteFile();
093:                assertEquals(absFile.toURL(), ConfigurationUtils.getURL(
094:                        "http://localhost:8080/webapp/config/baseConfig.xml",
095:                        absFile.getAbsolutePath()));
096:                assertEquals(absFile.toURL(), ConfigurationUtils.getURL(null,
097:                        absFile.getAbsolutePath()));
098:
099:                assertEquals(absFile.toURL(), ConfigurationUtils.getURL(absFile
100:                        .getParent(), "config.xml"));
101:            }
102:
103:            public void testGetBasePath() throws Exception {
104:                URL url = new URL("http://xyz.net/foo/bar.xml");
105:                assertEquals("base path of " + url, "http://xyz.net/foo/",
106:                        ConfigurationUtils.getBasePath(url));
107:
108:                url = new URL("http://xyz.net/foo/");
109:                assertEquals("base path of " + url, "http://xyz.net/foo/",
110:                        ConfigurationUtils.getBasePath(url));
111:
112:                url = new URL("http://xyz.net/foo");
113:                assertEquals("base path of " + url, "http://xyz.net/",
114:                        ConfigurationUtils.getBasePath(url));
115:
116:                url = new URL("http://xyz.net/");
117:                assertEquals("base path of " + url, "http://xyz.net/",
118:                        ConfigurationUtils.getBasePath(url));
119:
120:                url = new URL("http://xyz.net");
121:                assertEquals("base path of " + url, "http://xyz.net",
122:                        ConfigurationUtils.getBasePath(url));
123:            }
124:
125:            public void testGetFileName() throws Exception {
126:                assertEquals("file name for a null URL", null,
127:                        ConfigurationUtils.getFileName(null));
128:
129:                URL url = new URL("http://xyz.net/foo/");
130:                assertEquals("file for a directory URL " + url, null,
131:                        ConfigurationUtils.getFileName(url));
132:
133:                url = new URL("http://xyz.net/foo/bar.xml");
134:                assertEquals("file name for a valid URL " + url, "bar.xml",
135:                        ConfigurationUtils.getFileName(url));
136:            }
137:
138:            public void testCopy() {
139:                // create the source configuration
140:                Configuration conf1 = new BaseConfiguration();
141:                conf1.addProperty("key1", "value1");
142:                conf1.addProperty("key2", "value2");
143:
144:                // create the target configuration
145:                Configuration conf2 = new BaseConfiguration();
146:                conf2.addProperty("key1", "value3");
147:                conf2.addProperty("key2", "value4");
148:
149:                // copy the source configuration into the target configuration
150:                ConfigurationUtils.copy(conf1, conf2);
151:
152:                assertEquals("'key1' property", "value1", conf2
153:                        .getProperty("key1"));
154:                assertEquals("'key2' property", "value2", conf2
155:                        .getProperty("key2"));
156:            }
157:
158:            public void testAppend() {
159:                // create the source configuration
160:                Configuration conf1 = new BaseConfiguration();
161:                conf1.addProperty("key1", "value1");
162:                conf1.addProperty("key2", "value2");
163:
164:                // create the target configuration
165:                Configuration conf2 = new BaseConfiguration();
166:                conf2.addProperty("key1", "value3");
167:                conf2.addProperty("key2", "value4");
168:
169:                // append the source configuration to the target configuration
170:                ConfigurationUtils.append(conf1, conf2);
171:
172:                List expected = new ArrayList();
173:                expected.add("value3");
174:                expected.add("value1");
175:                ListAssert.assertEquals("'key1' property", expected, conf2
176:                        .getList("key1"));
177:
178:                expected = new ArrayList();
179:                expected.add("value4");
180:                expected.add("value2");
181:                ListAssert.assertEquals("'key2' property", expected, conf2
182:                        .getList("key2"));
183:            }
184:
185:            public void testGetFile() throws Exception {
186:                File directory = new File("target");
187:                File reference = new File(directory, "test.txt")
188:                        .getAbsoluteFile();
189:
190:                assertEquals(reference, ConfigurationUtils.getFile(null,
191:                        reference.getAbsolutePath()));
192:                assertEquals(reference, ConfigurationUtils.getFile(directory
193:                        .getAbsolutePath(), reference.getAbsolutePath()));
194:                assertEquals(reference, ConfigurationUtils.getFile(directory
195:                        .getAbsolutePath(), reference.getName()));
196:                assertEquals(reference, ConfigurationUtils.getFile(directory
197:                        .toURL().toString(), reference.getName()));
198:                assertEquals(reference, ConfigurationUtils.getFile("invalid",
199:                        reference.toURL().toString()));
200:                assertEquals(
201:                        reference,
202:                        ConfigurationUtils
203:                                .getFile(
204:                                        "jar:file:/C:/myjar.jar!/my-config.xml/someprops.properties",
205:                                        reference.getAbsolutePath()));
206:            }
207:
208:            public void testLocateWithNullTCCL() throws Exception {
209:                ClassLoader cl = Thread.currentThread().getContextClassLoader();
210:                try {
211:                    Thread.currentThread().setContextClassLoader(null);
212:                    assertNull(ConfigurationUtils.locate("abase", "aname"));
213:                    // This assert fails when maven 2 is used, so commented out
214:                    //assertNotNull(ConfigurationUtils.locate("test.xml"));
215:                } finally {
216:                    Thread.currentThread().setContextClassLoader(cl);
217:                }
218:            }
219:
220:            /**
221:             * Tests converting a configuration into a hierarchical one.
222:             */
223:            public void testConvertToHierarchical() {
224:                Configuration conf = new BaseConfiguration();
225:                for (int i = 0; i < 10; i++) {
226:                    conf.addProperty("test" + i, "value" + i);
227:                    conf.addProperty("test.list", "item" + i);
228:                }
229:
230:                HierarchicalConfiguration hc = ConfigurationUtils
231:                        .convertToHierarchical(conf);
232:                for (Iterator it = conf.getKeys(); it.hasNext();) {
233:                    String key = (String) it.next();
234:                    assertEquals("Wrong value for key " + key, conf
235:                            .getProperty(key), hc.getProperty(key));
236:                }
237:            }
238:
239:            /**
240:             * Tests converting a configuration into a hierarchical one that is already
241:             * hierarchical.
242:             */
243:            public void testConvertHierarchicalToHierarchical() {
244:                Configuration conf = new HierarchicalConfiguration();
245:                conf.addProperty("test", "yes");
246:                assertSame("Wrong configuration returned", conf,
247:                        ConfigurationUtils.convertToHierarchical(conf));
248:            }
249:
250:            /**
251:             * Tests converting a null configuration to a hierarchical one. The result
252:             * should be null, too.
253:             */
254:            public void testConvertNullToHierarchical() {
255:                assertNull("Wrong conversion result for null config",
256:                        ConfigurationUtils.convertToHierarchical(null));
257:            }
258:
259:            /**
260:             * Tests cloning a configuration that supports this operation.
261:             */
262:            public void testCloneConfiguration() {
263:                HierarchicalConfiguration conf = new HierarchicalConfiguration();
264:                conf.addProperty("test", "yes");
265:                HierarchicalConfiguration copy = (HierarchicalConfiguration) ConfigurationUtils
266:                        .cloneConfiguration(conf);
267:                assertNotSame("Same object was returned", conf, copy);
268:                assertEquals("Property was not cloned", "yes", copy
269:                        .getString("test"));
270:            }
271:
272:            /**
273:             * Tests cloning a configuration that does not support this operation. This
274:             * should cause an exception.
275:             */
276:            public void testCloneConfigurationNotSupported() {
277:                Configuration myNonCloneableConfig = new NonCloneableConfiguration();
278:                try {
279:                    ConfigurationUtils.cloneConfiguration(myNonCloneableConfig);
280:                    fail("Could clone non cloneable config!");
281:                } catch (ConfigurationRuntimeException crex) {
282:                    // ok
283:                }
284:            }
285:
286:            /**
287:             * Tests cloning a <b>null</b> configuration.
288:             */
289:            public void testCloneConfigurationNull() {
290:                assertNull("Wrong return value", ConfigurationUtils
291:                        .cloneConfiguration(null));
292:            }
293:
294:            /**
295:             * Tests whether runtime exceptions can be enabled.
296:             */
297:            public void testEnableRuntimeExceptions() {
298:                PropertiesConfiguration config = new PropertiesConfiguration() {
299:                    protected void addPropertyDirect(String key, Object value) {
300:                        // always simulate an exception
301:                        fireError(EVENT_ADD_PROPERTY, key, value,
302:                                new RuntimeException("A faked exception!"));
303:                    }
304:                };
305:                config.clearErrorListeners();
306:                ConfigurationUtils.enableRuntimeExceptions(config);
307:                try {
308:                    config.addProperty("test", "testValue");
309:                    fail("No runtime exception was thrown!");
310:                } catch (ConfigurationRuntimeException crex) {
311:                    // ok
312:                }
313:            }
314:
315:            /**
316:             * Tries to enable runtime exceptions for a configurtion that does not
317:             * inherit from EventSource. This should cause an exception.
318:             */
319:            public void testEnableRuntimeExceptionsInvalid() {
320:                try {
321:                    ConfigurationUtils
322:                            .enableRuntimeExceptions((Configuration) new Mock(
323:                                    Configuration.class).proxy());
324:                    fail("Could enable exceptions for non EventSource configuration!");
325:                } catch (IllegalArgumentException iex) {
326:                    // ok
327:                }
328:            }
329:
330:            /**
331:             * Tries to enable runtime exceptions for a null configuration. This should
332:             * cause an exception.
333:             */
334:            public void testEnableRuntimeExceptionsNull() {
335:                try {
336:                    ConfigurationUtils.enableRuntimeExceptions(null);
337:                    fail("Could enable exceptions for a null configuration!");
338:                } catch (IllegalArgumentException iex) {
339:                    //ok
340:                }
341:            }
342:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.