Source Code Cross Referenced for TestHierarchicalXMLConfiguration.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:        package org.apache.commons.configuration;
002:
003:        /*
004:         * Licensed to the Apache Software Foundation (ASF) under one or more
005:         * contributor license agreements.  See the NOTICE file distributed with
006:         * this work for additional information regarding copyright ownership.
007:         * The ASF licenses this file to You under the Apache License, Version 2.0
008:         * (the "License"); you may not use this file except in compliance with
009:         * the License.  You may obtain a copy of the License at
010:         *
011:         *     http://www.apache.org/licenses/LICENSE-2.0
012:         *
013:         * Unless required by applicable law or agreed to in writing, software
014:         * distributed under the License is distributed on an "AS IS" BASIS,
015:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016:         * See the License for the specific language governing permissions and
017:         * limitations under the License.
018:         */
019:
020:        import junit.framework.TestCase;
021:
022:        import java.io.File;
023:        import java.util.Collection;
024:        import java.util.Iterator;
025:
026:        /**
027:         * Test class for XMLConfiguration. In addition to TestXMLConfiguration this
028:         * class especially tests the hierarchical nature of this class and structured
029:         * data access.
030:         *
031:         * @author Emmanuel Bourg
032:         * @author Mark Woodman
033:         * @author Oliver Heger
034:         * @version $Id: TestHierarchicalXMLConfiguration.java 439648 2006-09-02 20:42:10Z oheger $
035:         */
036:        public class TestHierarchicalXMLConfiguration extends TestCase {
037:            /** Test resources directory. */
038:            private static final String TEST_DIR = "conf";
039:
040:            /** Test file #1 **/
041:            private static final String TEST_FILENAME = "testHierarchicalXMLConfiguration.xml";
042:
043:            /** Test file #2 **/
044:            private static final String TEST_FILENAME2 = "testHierarchicalXMLConfiguration2.xml";
045:
046:            /** Test file path #1 **/
047:            private static final String TEST_FILE = TEST_DIR + File.separator
048:                    + TEST_FILENAME;
049:
050:            /** Test file path #2 **/
051:            private static final String TEST_FILE2 = TEST_DIR + File.separator
052:                    + TEST_FILENAME2;
053:
054:            /** Test file path #3.*/
055:            private static final String TEST_FILE3 = TEST_DIR + File.separator
056:                    + "test.xml";
057:
058:            /** File name for saving.*/
059:            private static final String TEST_SAVENAME = "testhierarchicalsave.xml";
060:
061:            /** File path for saving.*/
062:            private static final String TEST_SAVE = "target" + File.separator
063:                    + TEST_SAVENAME;
064:
065:            /** Instance config used for tests. */
066:            private XMLConfiguration config;
067:
068:            /** Fixture setup. */
069:            protected void setUp() throws Exception {
070:                config = new XMLConfiguration();
071:            }
072:
073:            private void configTest(XMLConfiguration config) {
074:                assertEquals(1, config.getMaxIndex("tables.table"));
075:                assertEquals("system", config
076:                        .getProperty("tables.table(0)[@tableType]"));
077:                assertEquals("application", config
078:                        .getProperty("tables.table(1)[@tableType]"));
079:
080:                assertEquals("users", config
081:                        .getProperty("tables.table(0).name"));
082:                assertEquals("documents", config
083:                        .getProperty("tables.table(1).name"));
084:
085:                Object prop = config
086:                        .getProperty("tables.table.fields.field.name");
087:                assertTrue(prop instanceof  Collection);
088:                assertEquals(10, ((Collection) prop).size());
089:
090:                prop = config.getProperty("tables.table(0).fields.field.type");
091:                assertTrue(prop instanceof  Collection);
092:                assertEquals(5, ((Collection) prop).size());
093:
094:                prop = config.getProperty("tables.table(1).fields.field.type");
095:                assertTrue(prop instanceof  Collection);
096:                assertEquals(5, ((Collection) prop).size());
097:            }
098:
099:            public void testGetProperty() throws Exception {
100:                config.setFileName(TEST_FILE);
101:                config.load();
102:
103:                configTest(config);
104:            }
105:
106:            public void testLoadURL() throws Exception {
107:                config.load(new File(TEST_FILE).getAbsoluteFile().toURL());
108:                configTest(config);
109:            }
110:
111:            public void testLoadBasePath1() throws Exception {
112:                config.setBasePath(TEST_DIR);
113:                config.setFileName(TEST_FILENAME);
114:                config.load();
115:                configTest(config);
116:            }
117:
118:            public void testLoadBasePath2() throws Exception {
119:                config.setBasePath(new File(TEST_FILE).getAbsoluteFile()
120:                        .toURL().toString());
121:                config.setFileName(TEST_FILENAME);
122:                config.load();
123:                configTest(config);
124:            }
125:
126:            /**
127:             * Ensure various node types are correctly processed in config.
128:             * @throws Exception
129:             */
130:            public void testXmlNodeTypes() throws Exception {
131:                // Number of keys expected from test configuration file
132:                final int KEY_COUNT = 5;
133:
134:                // Load the configuration file
135:                config.load(new File(TEST_FILE2).getAbsoluteFile().toURL());
136:
137:                // Validate comment in element ignored
138:                assertEquals(
139:                        "Comment in element must not change element value.",
140:                        "Case1Text", config.getString("case1"));
141:
142:                // Validate sibling comment ignored
143:                assertEquals(
144:                        "Comment as sibling must not change element value.",
145:                        "Case2Text", config.getString("case2.child"));
146:
147:                // Validate comment ignored, CDATA processed
148:                assertEquals(
149:                        "Comment and use of CDATA must not change element value.",
150:                        "Case3Text", config.getString("case3"));
151:
152:                // Validate comment and processing instruction ignored
153:                assertEquals(
154:                        "Comment and use of PI must not change element value.",
155:                        "Case4Text", config.getString("case4"));
156:
157:                // Validate comment ignored in parent attribute
158:                assertEquals("Comment must not change attribute node value.",
159:                        "Case5Text", config.getString("case5[@attr]"));
160:
161:                // Validate non-text nodes haven't snuck in as keys
162:                Iterator iter = config.getKeys();
163:                int count = 0;
164:                while (iter.hasNext()) {
165:                    iter.next();
166:                    count++;
167:                }
168:                assertEquals(
169:                        "Config must contain only " + KEY_COUNT + " keys.",
170:                        KEY_COUNT, count);
171:            }
172:
173:            public void testSave() throws Exception {
174:                removeTestSaveFile();
175:                try {
176:                    config.setFileName(TEST_FILE3);
177:                    config.load();
178:                    File saveFile = new File(TEST_SAVE);
179:                    config.save(saveFile);
180:
181:                    config = new XMLConfiguration();
182:                    config.load(saveFile.toURL());
183:                    assertEquals("value", config.getProperty("element"));
184:                    assertEquals("I'm complex!", config
185:                            .getProperty("element2.subelement.subsubelement"));
186:                    assertEquals(8, config.getInt("test.short"));
187:                    assertEquals("one", config
188:                            .getString("list(0).item(0)[@name]"));
189:                    assertEquals("two", config.getString("list(0).item(1)"));
190:                    assertEquals("six", config
191:                            .getString("list(1).sublist.item(1)"));
192:
193:                } finally {
194:                    removeTestSaveFile();
195:                }
196:            }
197:
198:            /**
199:             * Tests to save a newly created configuration.
200:             *
201:             */
202:            public void testSaveNew() throws Exception {
203:                config.addProperty("connection.url", "jdbc://mydb:1234");
204:                config.addProperty("connection.user", "scott");
205:                config.addProperty("connection.passwd", "tiger");
206:                config.addProperty("connection[@type]", "system");
207:                config.addProperty("tables.table.name", "tests");
208:                config.addProperty("tables.table(0).fields.field.name",
209:                        "test_id");
210:                config.addProperty("tables.table(0).fields.field(-1).name",
211:                        "test_name");
212:                config.addProperty("tables.table(-1).name", "results");
213:                config.addProperty("tables.table(1).fields.field.name",
214:                        "res_id");
215:                config.addProperty("tables.table(1).fields.field(0).type",
216:                        "int");
217:                config.addProperty("tables.table(1).fields.field(-1).name",
218:                        "value");
219:                config.addProperty("tables.table(1).fields.field(1).type",
220:                        "string");
221:                config.addProperty("tables.table(1).fields.field(1)[@null]",
222:                        "true");
223:
224:                removeTestSaveFile();
225:                try {
226:                    File saveFile = new File(TEST_SAVE);
227:                    config.setFile(saveFile);
228:                    config.setRootElementName("myconfig");
229:                    config.save();
230:
231:                    config = new XMLConfiguration();
232:                    config.load(saveFile);
233:                    assertEquals(1, config.getMaxIndex("tables.table.name"));
234:                    assertEquals("tests", config
235:                            .getString("tables.table(0).name"));
236:                    assertEquals("test_name", config
237:                            .getString("tables.table(0).fields.field(1).name"));
238:                    assertEquals("int", config
239:                            .getString("tables.table(1).fields.field(0).type"));
240:                    assertTrue(config
241:                            .getBoolean("tables.table(1).fields.field(1)[@null]"));
242:                    assertEquals("tiger", config.getString("connection.passwd"));
243:                    assertEquals("system", config
244:                            .getProperty("connection[@type]"));
245:                    assertEquals("myconfig", config.getRootElementName());
246:                } finally {
247:                    removeTestSaveFile();
248:                }
249:            }
250:
251:            /**
252:             * Tests to save a modified configuration.
253:             *
254:             */
255:            public void testSaveModified() throws Exception {
256:                config.setFile(new File(TEST_FILE3));
257:                config.load();
258:
259:                assertTrue(config.getString("mean").startsWith(
260:                        "This is\n A long story..."));
261:                assertTrue(config.getString("mean").indexOf("And even longer") > 0);
262:                config.clearProperty("test.entity[@name]");
263:                config.setProperty("element", "new value");
264:                config.setProperty("test(0)", "A <new> value");
265:                config.addProperty("test(1).int", new Integer(9));
266:                config.addProperty("list(1).sublist.item", "seven");
267:                config.setProperty("clear", "yes");
268:                config.setProperty("mean", "now it's simple");
269:                config.addProperty("[@topattr]", "available");
270:                config.addProperty("[@topattr]", "successfull");
271:
272:                removeTestSaveFile();
273:                try {
274:                    config.save(new File(TEST_SAVE));
275:                    config = new XMLConfiguration();
276:                    config.load(TEST_SAVE);
277:                    assertFalse(config.containsKey("test.entity[@name]"));
278:                    assertEquals("1<2", config.getProperty("test.entity"));
279:                    assertEquals("new value", config.getString("element"));
280:                    assertEquals("A <new> value", config.getProperty("test(0)"));
281:                    assertEquals((short) 8, config.getShort("test(1).short"));
282:                    assertEquals(9, config.getInt("test(1).int"));
283:                    assertEquals("six", config
284:                            .getProperty("list(1).sublist.item(1)"));
285:                    assertEquals("seven", config
286:                            .getProperty("list(1).sublist.item(2)"));
287:                    assertEquals("yes", config.getProperty("clear"));
288:                    assertEquals("now it's simple", config.getString("mean"));
289:                    assertEquals("available", config.getString("[@topattr](0)"));
290:                    assertEquals("successfull", config
291:                            .getString("[@topattr](1)"));
292:                } finally {
293:                    removeTestSaveFile();
294:                }
295:            }
296:
297:            /**
298:             * Tests manipulation of the root element's name.
299:             *
300:             */
301:            public void testRootElement() throws Exception {
302:                assertEquals("configuration", config.getRootElementName());
303:                config.setRootElementName("newRootName");
304:                assertEquals("newRootName", config.getRootElementName());
305:
306:                config.setFile(new File(TEST_FILE3));
307:                config.load();
308:                assertEquals("testconfig", config.getRootElementName());
309:                try {
310:                    config.setRootElementName("anotherRootElement");
311:                    fail("Setting root element name when loaded from file!");
312:                } catch (UnsupportedOperationException uex) {
313:                    //fine
314:                }
315:            }
316:
317:            /**
318:             * Helper method that ensures that the test save file has been removed. 
319:             *
320:             */
321:            private void removeTestSaveFile() {
322:                File saveFile = new File(TEST_SAVE);
323:                if (saveFile.exists()) {
324:                    assertTrue(saveFile.delete());
325:                }
326:            }
327:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.