KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > configuration > TestSubsetConfiguration


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License")
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.configuration;
18
19 import java.io.File JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.NoSuchElementException JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import junit.framework.TestCase;
28
29 /**
30  * Test case for the {@link SubsetConfiguration} class.
31  *
32  * @author Emmanuel Bourg
33  * @version $Revision: 156639 $, $Date: 2005-03-09 13:50:07 +0100 (Mi, 09 Mrz 2005) $
34  */

35 public class TestSubsetConfiguration extends TestCase
36 {
37     static final String JavaDoc TEST_DIR = "conf";
38     static final String JavaDoc TEST_FILE = "testDigesterConfiguration2.xml";
39
40     public void testGetProperty()
41     {
42         Configuration conf = new BaseConfiguration();
43         conf.setProperty("test.key1", "value1");
44         conf.setProperty("testing.key2", "value1");
45
46         Configuration subset = new SubsetConfiguration(conf, "test", ".");
47         assertFalse("the subset is empty", subset.isEmpty());
48         assertTrue("'key1' not found in the subset", subset.containsKey("key1"));
49         assertFalse("'ng.key2' found in the subset", subset.containsKey("ng.key2"));
50     }
51
52     public void testSetProperty()
53     {
54         Configuration conf = new BaseConfiguration();
55         Configuration subset = new SubsetConfiguration(conf, "test", ".");
56
57         // set a property in the subset and check the parent
58
subset.setProperty("key1", "value1");
59         assertEquals("key1 in the subset configuration", "value1", subset.getProperty("key1"));
60         assertEquals("test.key1 in the parent configuration", "value1", conf.getProperty("test.key1"));
61
62         // set a property in the parent and check in the subset
63
conf.setProperty("test.key2", "value2");
64         assertEquals("test.key2 in the parent configuration", "value2", conf.getProperty("test.key2"));
65         assertEquals("key2 in the subset configuration", "value2", subset.getProperty("key2"));
66     }
67
68     public void testGetParentKey()
69     {
70         // subset with delimiter
71
SubsetConfiguration subset = new SubsetConfiguration(null, "prefix", ".");
72         assertEquals("parent key for \"key\"", "prefix.key", subset.getParentKey("key"));
73         assertEquals("parent key for \"\"", "prefix", subset.getParentKey(""));
74
75         // subset without delimiter
76
subset = new SubsetConfiguration(null, "prefix", null);
77         assertEquals("parent key for \"key\"", "prefixkey", subset.getParentKey("key"));
78         assertEquals("parent key for \"\"", "prefix", subset.getParentKey(""));
79     }
80
81     public void testGetChildKey()
82     {
83         // subset with delimiter
84
SubsetConfiguration subset = new SubsetConfiguration(null, "prefix", ".");
85         assertEquals("parent key for \"prefixkey\"", "key", subset.getChildKey("prefix.key"));
86         assertEquals("parent key for \"prefix\"", "", subset.getChildKey("prefix"));
87
88         // subset without delimiter
89
subset = new SubsetConfiguration(null, "prefix", null);
90         assertEquals("parent key for \"prefixkey\"", "key", subset.getChildKey("prefixkey"));
91         assertEquals("parent key for \"prefix\"", "", subset.getChildKey("prefix"));
92     }
93
94     public void testGetKeys()
95     {
96         Configuration conf = new BaseConfiguration();
97         conf.setProperty("test", "value0");
98         conf.setProperty("test.key1", "value1");
99         conf.setProperty("testing.key2", "value1");
100
101         Configuration subset = new SubsetConfiguration(conf, "test", ".");
102
103         Iterator JavaDoc it = subset.getKeys();
104         assertEquals("1st key", "", it.next());
105         assertEquals("2nd key", "key1", it.next());
106         assertFalse("too many elements", it.hasNext());
107     }
108
109     public void testGetKeysWithPrefix()
110     {
111         Configuration conf = new BaseConfiguration();
112         conf.setProperty("test.abc", "value0");
113         conf.setProperty("test.abc.key1", "value1");
114         conf.setProperty("test.abcdef.key2", "value1");
115
116         Configuration subset = new SubsetConfiguration(conf, "test", ".");
117
118         Iterator JavaDoc it = subset.getKeys("abc");
119         assertEquals("1st key", "abc", it.next());
120         assertEquals("2nd key", "abc.key1", it.next());
121         assertFalse("too many elements", it.hasNext());
122     }
123
124     public void testGetList()
125     {
126         Configuration conf = new BaseConfiguration();
127         conf.setProperty("test.abc", "value0,value1");
128         conf.addProperty("test.abc", "value3");
129
130         Configuration subset = new SubsetConfiguration(conf, "test", ".");
131         List JavaDoc list = subset.getList("abc", new ArrayList JavaDoc());
132         assertEquals(3, list.size());
133     }
134
135     public void testGetParent()
136     {
137         Configuration conf = new BaseConfiguration();
138         SubsetConfiguration subset = new SubsetConfiguration(conf, "prefix", ".");
139
140         assertEquals("parent", conf, subset.getParent());
141     }
142
143     public void testGetPrefix()
144     {
145         Configuration conf = new BaseConfiguration();
146         SubsetConfiguration subset = new SubsetConfiguration(conf, "prefix", ".");
147
148         assertEquals("prefix", "prefix", subset.getPrefix());
149     }
150
151     public void testSetPrefix()
152     {
153         Configuration conf = new BaseConfiguration();
154         SubsetConfiguration subset = new SubsetConfiguration(conf, null, ".");
155         subset.setPrefix("prefix");
156
157         assertEquals("prefix", "prefix", subset.getPrefix());
158     }
159
160     public void testThrowtExceptionOnMissing()
161     {
162         BaseConfiguration config = new BaseConfiguration();
163         config.setThrowExceptionOnMissing(true);
164
165         SubsetConfiguration subset = new SubsetConfiguration(config, "prefix");
166
167         try
168         {
169             subset.getString("foo");
170             fail("NoSuchElementException expected");
171         }
172         catch (NoSuchElementException JavaDoc e)
173         {
174             // expected
175
}
176
177         config.setThrowExceptionOnMissing(false);
178         assertNull(subset.getString("foo"));
179
180
181         subset.setThrowExceptionOnMissing(true);
182         try
183         {
184             config.getString("foo");
185             fail("NoSuchElementException expected");
186         }
187         catch (NoSuchElementException JavaDoc e)
188         {
189             // expected
190
}
191     }
192
193     public void testNested() throws Exception JavaDoc
194     {
195         ConfigurationFactory factory = new ConfigurationFactory();
196         File JavaDoc src = new File JavaDoc(new File JavaDoc(TEST_DIR), TEST_FILE);
197         factory.setConfigurationURL(src.toURL());
198         Configuration config = factory.getConfiguration();
199         Configuration subConf = config.subset("tables.table(0)");
200         assertTrue(subConf.getKeys().hasNext());
201         Configuration subSubConf = subConf.subset("fields.field(1)");
202         Iterator JavaDoc itKeys = subSubConf.getKeys();
203         Set JavaDoc keys = new HashSet JavaDoc();
204         keys.add("name");
205         keys.add("type");
206         while(itKeys.hasNext())
207         {
208             String JavaDoc k = (String JavaDoc) itKeys.next();
209             assertTrue(keys.contains(k));
210             keys.remove(k);
211         }
212         assertTrue(keys.isEmpty());
213     }
214
215     public void testClear()
216     {
217         Configuration config = new BaseConfiguration();
218         config.setProperty("test.key1", "value1");
219         config.setProperty("testing.key2", "value1");
220
221         Configuration subset = config.subset("test");
222         subset.clear();
223
224         assertTrue("the subset is not empty", subset.isEmpty());
225         assertFalse("the parent configuration is empty", config.isEmpty());
226     }
227 }
228
Popular Tags