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