Source Code Cross Referenced for TestSubsetConfiguration.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.util.ArrayList;
022:        import java.util.HashSet;
023:        import java.util.Iterator;
024:        import java.util.List;
025:        import java.util.NoSuchElementException;
026:        import java.util.Set;
027:
028:        import junit.framework.TestCase;
029:
030:        /**
031:         * Test case for the {@link SubsetConfiguration} class.
032:         *
033:         * @author Emmanuel Bourg
034:         * @version $Revision: 494597 $, $Date: 2007-01-09 22:32:44 +0100 (Di, 09 Jan 2007) $
035:         */
036:        public class TestSubsetConfiguration extends TestCase {
037:            static final String TEST_DIR = "conf";
038:            static final String TEST_FILE = "testDigesterConfiguration2.xml";
039:
040:            public void testGetProperty() {
041:                Configuration conf = new BaseConfiguration();
042:                conf.setProperty("test.key1", "value1");
043:                conf.setProperty("testing.key2", "value1");
044:
045:                Configuration subset = new SubsetConfiguration(conf, "test",
046:                        ".");
047:                assertFalse("the subset is empty", subset.isEmpty());
048:                assertTrue("'key1' not found in the subset", subset
049:                        .containsKey("key1"));
050:                assertFalse("'ng.key2' found in the subset", subset
051:                        .containsKey("ng.key2"));
052:            }
053:
054:            public void testSetProperty() {
055:                Configuration conf = new BaseConfiguration();
056:                Configuration subset = new SubsetConfiguration(conf, "test",
057:                        ".");
058:
059:                // set a property in the subset and check the parent
060:                subset.setProperty("key1", "value1");
061:                assertEquals("key1 in the subset configuration", "value1",
062:                        subset.getProperty("key1"));
063:                assertEquals("test.key1 in the parent configuration", "value1",
064:                        conf.getProperty("test.key1"));
065:
066:                // set a property in the parent and check in the subset
067:                conf.setProperty("test.key2", "value2");
068:                assertEquals("test.key2 in the parent configuration", "value2",
069:                        conf.getProperty("test.key2"));
070:                assertEquals("key2 in the subset configuration", "value2",
071:                        subset.getProperty("key2"));
072:            }
073:
074:            public void testGetParentKey() {
075:                // subset with delimiter
076:                SubsetConfiguration subset = new SubsetConfiguration(null,
077:                        "prefix", ".");
078:                assertEquals("parent key for \"key\"", "prefix.key", subset
079:                        .getParentKey("key"));
080:                assertEquals("parent key for \"\"", "prefix", subset
081:                        .getParentKey(""));
082:
083:                // subset without delimiter
084:                subset = new SubsetConfiguration(null, "prefix", null);
085:                assertEquals("parent key for \"key\"", "prefixkey", subset
086:                        .getParentKey("key"));
087:                assertEquals("parent key for \"\"", "prefix", subset
088:                        .getParentKey(""));
089:            }
090:
091:            public void testGetChildKey() {
092:                // subset with delimiter
093:                SubsetConfiguration subset = new SubsetConfiguration(null,
094:                        "prefix", ".");
095:                assertEquals("parent key for \"prefixkey\"", "key", subset
096:                        .getChildKey("prefix.key"));
097:                assertEquals("parent key for \"prefix\"", "", subset
098:                        .getChildKey("prefix"));
099:
100:                // subset without delimiter
101:                subset = new SubsetConfiguration(null, "prefix", null);
102:                assertEquals("parent key for \"prefixkey\"", "key", subset
103:                        .getChildKey("prefixkey"));
104:                assertEquals("parent key for \"prefix\"", "", subset
105:                        .getChildKey("prefix"));
106:            }
107:
108:            public void testGetKeys() {
109:                Configuration conf = new BaseConfiguration();
110:                conf.setProperty("test", "value0");
111:                conf.setProperty("test.key1", "value1");
112:                conf.setProperty("testing.key2", "value1");
113:
114:                Configuration subset = new SubsetConfiguration(conf, "test",
115:                        ".");
116:
117:                Iterator it = subset.getKeys();
118:                assertEquals("1st key", "", it.next());
119:                assertEquals("2nd key", "key1", it.next());
120:                assertFalse("too many elements", it.hasNext());
121:            }
122:
123:            public void testGetKeysWithPrefix() {
124:                Configuration conf = new BaseConfiguration();
125:                conf.setProperty("test.abc", "value0");
126:                conf.setProperty("test.abc.key1", "value1");
127:                conf.setProperty("test.abcdef.key2", "value1");
128:
129:                Configuration subset = new SubsetConfiguration(conf, "test",
130:                        ".");
131:
132:                Iterator it = subset.getKeys("abc");
133:                assertEquals("1st key", "abc", it.next());
134:                assertEquals("2nd key", "abc.key1", it.next());
135:                assertFalse("too many elements", it.hasNext());
136:            }
137:
138:            public void testGetList() {
139:                Configuration conf = new BaseConfiguration();
140:                conf.setProperty("test.abc", "value0,value1");
141:                conf.addProperty("test.abc", "value3");
142:
143:                Configuration subset = new SubsetConfiguration(conf, "test",
144:                        ".");
145:                List list = subset.getList("abc", new ArrayList());
146:                assertEquals(3, list.size());
147:            }
148:
149:            public void testGetParent() {
150:                Configuration conf = new BaseConfiguration();
151:                SubsetConfiguration subset = new SubsetConfiguration(conf,
152:                        "prefix", ".");
153:
154:                assertEquals("parent", conf, subset.getParent());
155:            }
156:
157:            public void testGetPrefix() {
158:                Configuration conf = new BaseConfiguration();
159:                SubsetConfiguration subset = new SubsetConfiguration(conf,
160:                        "prefix", ".");
161:
162:                assertEquals("prefix", "prefix", subset.getPrefix());
163:            }
164:
165:            public void testSetPrefix() {
166:                Configuration conf = new BaseConfiguration();
167:                SubsetConfiguration subset = new SubsetConfiguration(conf,
168:                        null, ".");
169:                subset.setPrefix("prefix");
170:
171:                assertEquals("prefix", "prefix", subset.getPrefix());
172:            }
173:
174:            public void testThrowtExceptionOnMissing() {
175:                BaseConfiguration config = new BaseConfiguration();
176:                config.setThrowExceptionOnMissing(true);
177:
178:                SubsetConfiguration subset = new SubsetConfiguration(config,
179:                        "prefix");
180:
181:                try {
182:                    subset.getString("foo");
183:                    fail("NoSuchElementException expected");
184:                } catch (NoSuchElementException e) {
185:                    // expected
186:                }
187:
188:                config.setThrowExceptionOnMissing(false);
189:                assertNull(subset.getString("foo"));
190:
191:                subset.setThrowExceptionOnMissing(true);
192:                try {
193:                    config.getString("foo");
194:                    fail("NoSuchElementException expected");
195:                } catch (NoSuchElementException e) {
196:                    // expected
197:                }
198:            }
199:
200:            public void testNested() throws Exception {
201:                ConfigurationFactory factory = new ConfigurationFactory();
202:                File src = new File(new File(TEST_DIR), TEST_FILE);
203:                factory.setConfigurationURL(src.toURL());
204:                Configuration config = factory.getConfiguration();
205:                Configuration subConf = config.subset("tables.table(0)");
206:                assertTrue(subConf.getKeys().hasNext());
207:                Configuration subSubConf = subConf.subset("fields.field(1)");
208:                Iterator itKeys = subSubConf.getKeys();
209:                Set keys = new HashSet();
210:                keys.add("name");
211:                keys.add("type");
212:                while (itKeys.hasNext()) {
213:                    String k = (String) itKeys.next();
214:                    assertTrue(keys.contains(k));
215:                    keys.remove(k);
216:                }
217:                assertTrue(keys.isEmpty());
218:            }
219:
220:            public void testClear() {
221:                Configuration config = new BaseConfiguration();
222:                config.setProperty("test.key1", "value1");
223:                config.setProperty("testing.key2", "value1");
224:
225:                Configuration subset = config.subset("test");
226:                subset.clear();
227:
228:                assertTrue("the subset is not empty", subset.isEmpty());
229:                assertFalse("the parent configuration is empty", config
230:                        .isEmpty());
231:            }
232:
233:            public void testSetListDelimiter() {
234:                BaseConfiguration config = new BaseConfiguration();
235:                Configuration subset = config.subset("prefix");
236:                config.setListDelimiter('/');
237:                subset.addProperty("list", "a/b/c");
238:                assertEquals("Wrong size of list", 3, config.getList(
239:                        "prefix.list").size());
240:
241:                ((AbstractConfiguration) subset).setListDelimiter(';');
242:                subset.addProperty("list2", "a;b;c");
243:                assertEquals("Wrong size of list2", 3, config.getList(
244:                        "prefix.list2").size());
245:            }
246:
247:            public void testGetListDelimiter() {
248:                BaseConfiguration config = new BaseConfiguration();
249:                AbstractConfiguration subset = (AbstractConfiguration) config
250:                        .subset("prefix");
251:                config.setListDelimiter('/');
252:                assertEquals("Wrong list delimiter in subset", '/', subset
253:                        .getListDelimiter());
254:                subset.setListDelimiter(';');
255:                assertEquals("Wrong list delimiter in parent", ';', config
256:                        .getListDelimiter());
257:            }
258:
259:            public void testSetDelimiterParsingDisabled() {
260:                BaseConfiguration config = new BaseConfiguration();
261:                Configuration subset = config.subset("prefix");
262:                config.setDelimiterParsingDisabled(true);
263:                subset.addProperty("list", "a,b,c");
264:                assertEquals("Wrong value of property", "a,b,c", config
265:                        .getString("prefix.list"));
266:
267:                ((AbstractConfiguration) subset)
268:                        .setDelimiterParsingDisabled(false);
269:                subset.addProperty("list2", "a,b,c");
270:                assertEquals("Wrong size of list2", 3, config.getList(
271:                        "prefix.list2").size());
272:            }
273:
274:            public void testIsDelimiterParsingDisabled() {
275:                BaseConfiguration config = new BaseConfiguration();
276:                AbstractConfiguration subset = (AbstractConfiguration) config
277:                        .subset("prefix");
278:                config.setDelimiterParsingDisabled(true);
279:                assertTrue("Wrong value of list parsing flag in subset", subset
280:                        .isDelimiterParsingDisabled());
281:                subset.setDelimiterParsingDisabled(false);
282:                assertFalse("Wrong value of list parsing flag in parent",
283:                        config.isDelimiterParsingDisabled());
284:            }
285:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.