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: }
|