1 package org.apache.commons.configuration;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Set;
25
26 import junit.framework.TestCase;
27
28 /***
29 * Test class for HierarchicalConfiguration.
30 *
31 * @version $Id: TestHierarchicalConfiguration.java 155408 2005-02-26 12:56:39Z dirkv $
32 */
33 public class TestHierarchicalConfiguration extends TestCase
34 {
35 private static String[] tables = { "users", "documents" };
36
37 private static String[][] fields =
38 {
39 { "uid", "uname", "firstName", "lastName", "email" },
40 { "docid", "name", "creationDate", "authorID", "version" }
41 };
42
43 private HierarchicalConfiguration config;
44
45 protected void setUp() throws Exception
46 {
47 config = new HierarchicalConfiguration();
48 HierarchicalConfiguration.Node nodeTables =
49 new HierarchicalConfiguration.Node("tables");
50 for(int i = 0; i < tables.length; i++)
51 {
52 HierarchicalConfiguration.Node nodeTable =
53 new HierarchicalConfiguration.Node("table");
54 nodeTables.addChild(nodeTable);
55 HierarchicalConfiguration.Node nodeName =
56 new HierarchicalConfiguration.Node("name");
57 nodeName.setValue(tables[i]);
58 nodeTable.addChild(nodeName);
59 HierarchicalConfiguration.Node nodeFields =
60 new HierarchicalConfiguration.Node("fields");
61 nodeTable.addChild(nodeFields);
62 for(int j = 0; j < fields[i].length; j++)
63 {
64 HierarchicalConfiguration.Node nodeField =
65 new HierarchicalConfiguration.Node("field");
66 HierarchicalConfiguration.Node nodeFieldName =
67 new HierarchicalConfiguration.Node("name");
68 nodeFieldName.setValue(fields[i][j]);
69 nodeField.addChild(nodeFieldName);
70 nodeFields.addChild(nodeField);
71 }
72 }
73 config.getRoot().addChild(nodeTables);
74 }
75
76 public void testIsEmpty()
77 {
78 assertFalse(config.isEmpty());
79 HierarchicalConfiguration conf2 = new HierarchicalConfiguration();
80 assertTrue(conf2.isEmpty());
81 HierarchicalConfiguration.Node child1 =
82 new HierarchicalConfiguration.Node("child1");
83 HierarchicalConfiguration.Node child2 =
84 new HierarchicalConfiguration.Node("child2");
85 child1.addChild(child2);
86 conf2.getRoot().addChild(child1);
87 assertTrue(conf2.isEmpty());
88 }
89
90 public void testGetProperty()
91 {
92 assertNull(config.getProperty("tables.table.resultset"));
93 assertNull(config.getProperty("tables.table.fields.field"));
94
95 Object prop = config.getProperty("tables.table(0).fields.field.name");
96 assertNotNull(prop);
97 assertTrue(prop instanceof Collection);
98 assertEquals(5, ((Collection) prop).size());
99
100 prop = config.getProperty("tables.table.fields.field.name");
101 assertNotNull(prop);
102 assertTrue(prop instanceof Collection);
103 assertEquals(10, ((Collection) prop).size());
104
105 prop = config.getProperty("tables.table.fields.field(3).name");
106 assertNotNull(prop);
107 assertTrue(prop instanceof Collection);
108 assertEquals(2, ((Collection) prop).size());
109
110 prop = config.getProperty("tables.table(1).fields.field(2).name");
111 assertNotNull(prop);
112 assertEquals("creationDate", prop.toString());
113 }
114
115 public void testSetProperty()
116 {
117 config.setProperty("tables.table(0).name", "resources");
118 assertEquals("resources", config.getString("tables.table(0).name"));
119 config.setProperty("tables.table.name", "tab1,tab2");
120 assertEquals("tab1", config.getString("tables.table(0).name"));
121 assertEquals("tab2", config.getString("tables.table(1).name"));
122
123 config.setProperty("test.items.item", new int[] { 2, 4, 8, 16 });
124 assertEquals(3, config.getMaxIndex("test.items.item"));
125 assertEquals(8, config.getInt("test.items.item(2)"));
126 config.setProperty("test.items.item(2)", new Integer(6));
127 assertEquals(6, config.getInt("test.items.item(2)"));
128 config.setProperty("test.items.item(2)", new int[] { 7, 9, 11 });
129 assertEquals(5, config.getMaxIndex("test.items.item"));
130
131 config.setProperty("test", Boolean.TRUE);
132 config.setProperty("test.items", "01/01/05");
133 assertEquals(5, config.getMaxIndex("test.items.item"));
134 assertTrue(config.getBoolean("test"));
135 assertEquals("01/01/05", config.getProperty("test.items"));
136 }
137
138 public void testClearProperty()
139 {
140 config.clearProperty("tables.table(0).fields.field(0).name");
141 assertEquals("uname", config.getProperty("tables.table(0).fields.field(0).name"));
142 config.clearProperty("tables.table(0).name");
143 assertFalse(config.containsKey("tables.table(0).name"));
144 assertEquals("firstName", config.getProperty("tables.table(0).fields.field(1).name"));
145 assertEquals("documents", config.getProperty("tables.table.name"));
146 config.clearProperty("tables.table");
147 assertEquals("documents", config.getProperty("tables.table.name"));
148
149 config.addProperty("test", "first");
150 config.addProperty("test.level", "second");
151 config.clearProperty("test");
152 assertEquals("second", config.getString("test.level"));
153 assertFalse(config.containsKey("test"));
154 }
155
156 public void testClearTree()
157 {
158 Object prop = config.getProperty("tables.table(0).fields.field.name");
159 assertNotNull(prop);
160 config.clearTree("tables.table(0).fields.field(3)");
161 prop = config.getProperty("tables.table(0).fields.field.name");
162 assertNotNull(prop);
163 assertTrue(prop instanceof Collection);
164 assertEquals(4, ((Collection) prop).size());
165
166 config.clearTree("tables.table(0).fields");
167 assertNull(config.getProperty("tables.table(0).fields.field.name"));
168 prop = config.getProperty("tables.table.fields.field.name");
169 assertNotNull(prop);
170 assertTrue(prop instanceof Collection);
171 assertEquals(5, ((Collection) prop).size());
172
173 config.clearTree("tables.table(1)");
174 assertNull(config.getProperty("tables.table.fields.field.name"));
175 }
176
177 public void testContainsKey()
178 {
179 assertTrue(config.containsKey("tables.table(0).name"));
180 assertTrue(config.containsKey("tables.table(1).name"));
181 assertFalse(config.containsKey("tables.table(2).name"));
182
183 assertTrue(config.containsKey("tables.table(0).fields.field.name"));
184 assertFalse(config.containsKey("tables.table(0).fields.field"));
185 config.clearTree("tables.table(0).fields");
186 assertFalse(config.containsKey("tables.table(0).fields.field.name"));
187
188 assertTrue(config.containsKey("tables.table.fields.field.name"));
189 }
190
191 public void testGetKeys()
192 {
193 List keys = new ArrayList();
194 for(Iterator it = config.getKeys(); it.hasNext();)
195 {
196 keys.add(it.next());
197 }
198
199 assertEquals(2, keys.size());
200 assertTrue(keys.contains("tables.table.name"));
201 assertTrue(keys.contains("tables.table.fields.field.name"));
202 }
203
204 public void testGetKeysString()
205 {
206
207 config.addProperty("tables.table(0).fields.field(1).type", "VARCHAR");
208 config.addProperty("tables.table(0)[@type]", "system");
209 config.addProperty("tables.table(0).size", "42");
210 config.addProperty("tables.table(0).fields.field(0).size", "128");
211 config.addProperty("connections.connection.param.url", "url1");
212 config.addProperty("connections.connection.param.user", "me");
213 config.addProperty("connections.connection.param.pwd", "secret");
214 config.addProperty("connections.connection(-1).param.url", "url2");
215 config.addProperty("connections.connection(1).param.user", "guest");
216
217 checkKeys("tables.table(1)", new String[] { "name", "fields.field.name" });
218 checkKeys("tables.table(0)",
219 new String[] { "name", "fields.field.name", "tables.table(0)[@type]", "size", "fields.field.type", "fields.field.size" });
220 checkKeys("connections.connection(0).param",
221 new String[] {"url", "user", "pwd" });
222 checkKeys("connections.connection(1).param",
223 new String[] {"url", "user" });
224 }
225
226 public void testAddProperty()
227 {
228 config.addProperty("tables.table(0).fields.field(-1).name", "phone");
229 Object prop = config.getProperty("tables.table(0).fields.field.name");
230 assertNotNull(prop);
231 assertTrue(prop instanceof Collection);
232 assertEquals(6, ((Collection) prop).size());
233
234 config.addProperty("tables.table(0).fields.field.name", "fax");
235 prop = config.getProperty("tables.table.fields.field(5).name");
236 assertNotNull(prop);
237 assertTrue(prop instanceof List);
238 List list = (List) prop;
239 assertEquals("phone", list.get(0));
240 assertEquals("fax", list.get(1));
241
242 config.addProperty("tables.table(-1).name", "config");
243 prop = config.getProperty("tables.table.name");
244 assertNotNull(prop);
245 assertTrue(prop instanceof Collection);
246 assertEquals(3, ((Collection) prop).size());
247 config.addProperty("tables.table(2).fields.field(0).name", "cid");
248 config.addProperty("tables.table(2).fields.field(-1).name",
249 "confName");
250 prop = config.getProperty("tables.table(2).fields.field.name");
251 assertNotNull(prop);
252 assertTrue(prop instanceof Collection);
253 assertEquals(2, ((Collection) prop).size());
254 assertEquals("confName",
255 config.getProperty("tables.table(2).fields.field(1).name"));
256
257 config.addProperty("connection.user", "scott");
258 config.addProperty("connection.passwd", "tiger");
259 assertEquals("tiger", config.getProperty("connection.passwd"));
260
261 ConfigurationKey key = new ConfigurationKey();
262 key.append("tables").append("table").appendIndex(0);
263 key.appendAttribute("tableType");
264 config.addProperty(key.toString(), "system");
265 assertEquals("system", config.getProperty(key.toString()));
266 }
267
268 public void testGetMaxIndex()
269 {
270 assertEquals(4, config.getMaxIndex("tables.table(0).fields.field"));
271 assertEquals(4, config.getMaxIndex("tables.table(1).fields.field"));
272 assertEquals(1, config.getMaxIndex("tables.table"));
273 assertEquals(1, config.getMaxIndex("tables.table.name"));
274 assertEquals(0, config.getMaxIndex("tables.table(0).name"));
275 assertEquals(0, config.getMaxIndex("tables.table(1).fields.field(1)"));
276 assertEquals(-1, config.getMaxIndex("tables.table(2).fields"));
277
278 int maxIdx = config.getMaxIndex("tables.table(0).fields.field.name");
279 for(int i = 0; i <= maxIdx; i++)
280 {
281 ConfigurationKey key = new ConfigurationKey("tables.table(0).fields");
282 key.append("field").appendIndex(i).append("name");
283 assertNotNull(config.getProperty(key.toString()));
284 }
285 }
286
287 public void testSubset()
288 {
289 Configuration conf = config.subset("tables.table(0)");
290 assertEquals("users", conf.getProperty("name"));
291 Object prop = conf.getProperty("fields.field.name");
292 assertNotNull(prop);
293 assertTrue(prop instanceof Collection);
294 assertEquals(5, ((Collection) prop).size());
295
296 for(int i = 0; i < fields[0].length; i++)
297 {
298 ConfigurationKey key = new ConfigurationKey();
299 key.append("fields").append("field").appendIndex(i);
300 key.append("name");
301 assertEquals(fields[0][i], conf.getProperty(key.toString()));
302 }
303
304 assertTrue("subset is not empty", config.subset("tables.table(2)").isEmpty());
305
306 conf = config.subset("tables.table.fields.field");
307 prop = conf.getProperty("name");
308 assertTrue("prop is not a collection", prop instanceof Collection);
309 assertEquals(10, ((Collection) prop).size());
310
311 conf = config.subset("tables.table.fields.field.name");
312 assertTrue("subset is not empty", conf.isEmpty());
313 }
314
315 /***
316 * Helper method for testing the getKeys(String) method.
317 * @param prefix the key to pass into getKeys()
318 * @param expected the expected result
319 */
320 private void checkKeys(String prefix, String[] expected)
321 {
322 Set values = new HashSet();
323 for(int i = 0; i < expected.length; i++)
324 {
325 values.add((expected[i].startsWith(prefix)) ? expected[i] : prefix + "." + expected[i]);
326 }
327
328 Iterator itKeys = config.getKeys(prefix);
329 while(itKeys.hasNext())
330 {
331 String key = (String) itKeys.next();
332 if(!values.contains(key))
333 {
334 fail("Found unexpected key: " + key);
335 }
336 else
337 {
338 values.remove(key);
339 }
340 }
341
342 assertTrue("Remaining keys " + values, values.isEmpty());
343 }
344 }