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 static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.io.File;
28 import java.io.FileWriter;
29 import java.net.URL;
30 import java.util.Collection;
31 import java.util.List;
32
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.xml.sax.SAXException;
36
37
38
39
40
41
42 @SuppressWarnings("deprecation")
43 public class TestConfigurationFactory
44 {
45
46 private URL digesterRules = getClass().getResource("/digesterRules.xml");
47 private File testDigesterFile = ConfigurationAssert.getTestFile("testDigesterConfiguration.xml");
48 private File testDigesterFileReverseOrder =
49 ConfigurationAssert.getTestFile("testDigesterConfigurationReverseOrder.xml");
50 private File testDigesterFileNamespaceAware =
51 ConfigurationAssert.getTestFile("testDigesterConfigurationNamespaceAware.xml");
52 private File testDigesterFileBasePath =
53 ConfigurationAssert.getTestFile("testDigesterConfigurationBasePath.xml");
54 private File testDigesterFileEnhanced =
55 ConfigurationAssert.getTestFile("testDigesterConfiguration2.xml");
56 private File testDigesterFileComplete =
57 ConfigurationAssert.getTestFile("testDigesterConfiguration3.xml");
58 private File testDigesterFileOptional =
59 ConfigurationAssert.getTestFile("testDigesterOptionalConfiguration.xml");
60 private File testDigesterFileOptionalEx =
61 ConfigurationAssert.getTestFile("testDigesterOptionalConfigurationEx.xml");
62 private File testDigesterFileSysProps =
63 ConfigurationAssert.getTestFile("testDigesterConfigurationSysProps.xml");
64 private File testDigesterFileInitProps =
65 ConfigurationAssert.getTestFile("testDigesterConfigurationWithProps.xml");
66
67 private File testDigesterBadXML = ConfigurationAssert.getTestFile("testDigesterBadXML.xml");
68
69 private String testBasePath = new File("conf").getAbsolutePath();
70
71 private File testProperties = ConfigurationAssert.getTestFile("test.properties");
72 private File testAbsConfig = ConfigurationAssert.getOutFile("testAbsConfig.xml");
73
74 private Configuration configuration;
75 private CompositeConfiguration compositeConfiguration;
76 private ConfigurationFactory factory;
77
78 @Before
79 public void setUp() throws Exception
80 {
81 System.setProperty("java.naming.factory.initial", "org.apache.commons.configuration.MockInitialContextFactory");
82 factory = new ConfigurationFactory();
83 }
84
85 @Test
86 public void testJNDI() throws Exception
87 {
88 JNDIConfiguration jndiConfiguration = new JNDIConfiguration();
89 Object o = jndiConfiguration.getProperty("test.boolean");
90 assertNotNull(o);
91 assertEquals("true", o.toString());
92 }
93
94 @Test
95 public void testLoadingConfiguration() throws Exception
96 {
97 factory.setConfigurationFileName(testDigesterFile.toString());
98
99 compositeConfiguration = (CompositeConfiguration) factory.getConfiguration();
100
101 assertEquals("Number of configurations", 4, compositeConfiguration.getNumberOfConfigurations());
102 assertEquals(PropertiesConfiguration.class, compositeConfiguration.getConfiguration(0).getClass());
103 assertEquals(XMLPropertiesConfiguration.class, compositeConfiguration.getConfiguration(1).getClass());
104 assertEquals(XMLConfiguration.class, compositeConfiguration.getConfiguration(2).getClass());
105
106
107 PropertiesConfiguration pc = (PropertiesConfiguration) compositeConfiguration.getConfiguration(0);
108 assertNotNull("Make sure we have a fileName: " + pc.getFileName(), pc.getFileName());
109
110
111 assertTrue("Make sure we have loaded our key", compositeConfiguration.getBoolean("test.boolean"));
112 assertEquals("I'm complex!", compositeConfiguration.getProperty("element2.subelement.subsubelement"));
113 assertEquals("property in the XMLPropertiesConfiguration", "value1", compositeConfiguration.getProperty("key1"));
114 }
115
116 @Test
117 public void testLoadingConfigurationWithRulesXML() throws Exception
118 {
119 factory.setConfigurationFileName(testDigesterFile.toString());
120 factory.setDigesterRules(digesterRules);
121
122 compositeConfiguration = (CompositeConfiguration) factory.getConfiguration();
123
124 assertEquals("Number of configurations", 4, compositeConfiguration.getNumberOfConfigurations());
125 assertEquals(PropertiesConfiguration.class, compositeConfiguration.getConfiguration(0).getClass());
126
127 assertEquals(XMLConfiguration.class, compositeConfiguration.getConfiguration(2).getClass());
128
129
130 PropertiesConfiguration pc = (PropertiesConfiguration) compositeConfiguration.getConfiguration(0);
131 assertNotNull("Make sure we have a fileName: " + pc.getFileName(), pc.getFileName());
132
133
134 assertTrue("Make sure we have loaded our key", pc.getBoolean("test.boolean"));
135 assertTrue("Make sure we have loaded our key", compositeConfiguration.getBoolean("test.boolean"));
136
137 assertEquals("I'm complex!", compositeConfiguration.getProperty("element2.subelement.subsubelement"));
138 }
139
140 @Test
141 public void testLoadingConfigurationReverseOrder() throws Exception
142 {
143 factory.setConfigurationFileName(testDigesterFileReverseOrder.toString());
144
145 configuration = factory.getConfiguration();
146
147 assertEquals("8", configuration.getProperty("test.short"));
148
149 factory.setConfigurationFileName(testDigesterFile.toString());
150
151 configuration = factory.getConfiguration();
152 assertEquals("1", configuration.getProperty("test.short"));
153 }
154
155 @Test
156 public void testLoadingConfigurationNamespaceAware() throws Exception
157 {
158 factory.setConfigurationFileName(testDigesterFileNamespaceAware.toString());
159 factory.setDigesterRuleNamespaceURI("namespace-one");
160
161 checkCompositeConfiguration();
162 }
163
164 @Test
165 public void testLoadingConfigurationBasePath() throws Exception
166 {
167 factory.setConfigurationFileName(testDigesterFileBasePath.toString());
168
169 factory.setBasePath(testBasePath);
170
171
172
173 checkCompositeConfiguration();
174 }
175
176 @Test
177 public void testLoadingAdditional() throws Exception
178 {
179 factory.setConfigurationFileName(testDigesterFileEnhanced.toString());
180 factory.setBasePath(null);
181 checkUnionConfig();
182 }
183
184 @Test
185 public void testLoadingURL() throws Exception
186 {
187 factory.setConfigurationURL(testDigesterFileEnhanced.toURL());
188 checkUnionConfig();
189 }
190
191 @Test(expected = ConfigurationException.class)
192 public void testLoadingURLNonExisting() throws Exception
193 {
194 factory = new ConfigurationFactory();
195 File nonExistingFile = new File("conf/nonexisting.xml");
196 factory.setConfigurationURL(nonExistingFile.toURL());
197 factory.getConfiguration();
198 }
199
200 @Test
201 public void testThrowingConfigurationInitializationException() throws Exception
202 {
203 factory.setConfigurationFileName(testDigesterBadXML.toString());
204 try
205 {
206 factory.getConfiguration();
207 fail("Should have throw an Exception");
208 }
209 catch (ConfigurationException cle)
210 {
211 assertTrue("Unexpected cause: " + cle.getCause(),
212 cle.getCause() instanceof SAXException);
213 }
214 }
215
216
217 @Test
218 public void testAllConfiguration() throws Exception
219 {
220 factory.setConfigurationURL(testDigesterFileComplete.toURL());
221 Configuration config = factory.getConfiguration();
222 assertFalse(config.isEmpty());
223 assertTrue(config instanceof CompositeConfiguration);
224 CompositeConfiguration cc = (CompositeConfiguration) config;
225 assertTrue(cc.getNumberOfConfigurations() > 1);
226
227
228
229 assertNotNull(config.getProperty("tables.table(0).fields.field(2).name"));
230 assertNotNull(config.getProperty("element2.subelement.subsubelement"));
231 assertEquals("value", config.getProperty("element3"));
232 assertEquals("foo", config.getProperty("element3[@name]"));
233 assertNotNull(config.getProperty("mail.account.user"));
234
235
236 assertNotNull(config.getProperty("test.onlyinjndi"));
237 assertTrue(config.getBoolean("test.onlyinjndi"));
238
239 Configuration subset = config.subset("test");
240 assertNotNull(subset.getProperty("onlyinjndi"));
241 assertTrue(subset.getBoolean("onlyinjndi"));
242
243
244 assertNotNull(config.getProperty("java.version"));
245 assertEquals(System.getProperty("java.version"), config.getString("java.version"));
246 }
247
248
249 @Test
250 public void testOptionalConfigurations() throws Exception
251 {
252 factory.setConfigurationURL(testDigesterFileOptional.toURL());
253 Configuration config = factory.getConfiguration();
254 assertTrue(config.getBoolean("test.boolean"));
255 assertEquals("value", config.getProperty("element"));
256
257 factory.setConfigurationURL(testDigesterFileOptionalEx.toURL());
258 try
259 {
260 config = factory.getConfiguration();
261 fail("Unexisting properties loaded!");
262 }
263 catch(ConfigurationException cex)
264 {
265
266 }
267 }
268
269
270 @Test
271 public void testLoadAbsolutePath() throws Exception
272 {
273 try
274 {
275 FileWriter out = null;
276 try
277 {
278 out = new FileWriter(testAbsConfig);
279 out.write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>");
280 out.write("<configuration>");
281 out.write("<properties fileName=\"");
282 out.write(testProperties.getAbsolutePath());
283 out.write("\"/>");
284 out.write("</configuration>");
285 }
286 finally
287 {
288 if (out != null)
289 {
290 out.close();
291 }
292 }
293
294 factory.setConfigurationFileName(testAbsConfig.toString());
295 Configuration config = factory.getConfiguration();
296 assertTrue(config.getBoolean("configuration.loaded"));
297 }
298 finally
299 {
300 if (testAbsConfig.exists())
301 {
302 testAbsConfig.delete();
303 }
304 }
305 }
306
307 @Test
308 public void testBasePath() throws Exception
309 {
310 assertEquals(".", factory.getBasePath());
311 factory.setConfigurationFileName(testDigesterFile.getAbsolutePath());
312
313
314 assertEquals(testDigesterFile.getParentFile().getAbsolutePath(),
315 factory.getBasePath());
316
317 String homeDir = System.getProperty("user.home");
318 factory = new ConfigurationFactory();
319 factory.setBasePath(homeDir);
320 factory.setConfigurationFileName(testDigesterFile.getAbsolutePath());
321
322 assertEquals(homeDir, factory.getBasePath());
323
324 factory = new ConfigurationFactory(testDigesterFile.getAbsolutePath());
325 assertEquals(testDigesterFile.getParentFile().getAbsolutePath(),
326 factory.getBasePath());
327 factory.setBasePath(homeDir);
328 assertEquals(homeDir, factory.getBasePath());
329
330 factory = new ConfigurationFactory();
331 factory.setConfigurationURL(testDigesterFile.toURL());
332 assertEquals(testDigesterFile.toURL().toString(), factory.getBasePath());
333 }
334
335
336
337 @Test
338 public void testLoadingWithSystemProperties() throws ConfigurationException
339 {
340 System.setProperty("config.file", "test.properties");
341 factory.setConfigurationFileName(testDigesterFileSysProps
342 .getAbsolutePath());
343 Configuration config = factory.getConfiguration();
344 assertTrue("Configuration not loaded", config
345 .getBoolean("configuration.loaded"));
346 }
347
348
349
350 @Test
351 public void testLoadInitProperties() throws ConfigurationException
352 {
353 factory.setConfigurationFileName(testDigesterFileInitProps
354 .getAbsolutePath());
355 Configuration config = factory.getConfiguration();
356 PropertiesConfiguration c = (PropertiesConfiguration) ((CompositeConfiguration) config)
357 .getConfiguration(0);
358 assertEquals("List delimiter was not set", ';', c.getListDelimiter());
359 List<Object> l = c.getList("test.mixed.array");
360 assertEquals("Wrong number of list elements", 2, l.size());
361 assertEquals("List delimiter was not applied", "b, c, d", l.get(1));
362 }
363
364 private void checkUnionConfig() throws Exception
365 {
366 compositeConfiguration = (CompositeConfiguration) factory.getConfiguration();
367 assertEquals("Verify how many configs", 3, compositeConfiguration.getNumberOfConfigurations());
368
369
370 Object prop = compositeConfiguration.getProperty("tables.table.name");
371 assertTrue(prop instanceof Collection);
372 assertEquals(3, ((Collection<?>) prop).size());
373 assertEquals("users", compositeConfiguration.getProperty("tables.table(0).name"));
374 assertEquals("documents", compositeConfiguration.getProperty("tables.table(1).name"));
375 assertEquals("tasks", compositeConfiguration.getProperty("tables.table(2).name"));
376
377 prop = compositeConfiguration.getProperty("tables.table.fields.field.name");
378 assertTrue(prop instanceof Collection);
379 assertEquals(17, ((Collection<?>) prop).size());
380
381 assertEquals("smtp.mydomain.org", compositeConfiguration.getString("mail.host.smtp"));
382 assertEquals("pop3.mydomain.org", compositeConfiguration.getString("mail.host.pop"));
383
384
385 assertEquals("masterOfPost", compositeConfiguration.getString("mail.account.user"));
386 assertEquals("topsecret", compositeConfiguration.getString("mail.account.psswd"));
387
388
389 assertEquals("enhanced factory", compositeConfiguration.getString("test.configuration"));
390 }
391
392 private void checkCompositeConfiguration() throws Exception
393 {
394 compositeConfiguration = (CompositeConfiguration) factory.getConfiguration();
395
396 assertEquals("Verify how many configs", 2, compositeConfiguration.getNumberOfConfigurations());
397 assertEquals(PropertiesConfiguration.class, compositeConfiguration.getConfiguration(0).getClass());
398
399 PropertiesConfiguration pc = (PropertiesConfiguration) compositeConfiguration.getConfiguration(0);
400 assertNotNull("Make sure we have a fileName:" + pc.getFileName(), pc.getFileName());
401 assertTrue("Make sure we have loaded our key", pc.getBoolean("test.boolean"));
402 assertTrue("Make sure we have loaded our key", compositeConfiguration.getBoolean("test.boolean"));
403
404 Object property = compositeConfiguration.getProperty("element2.subelement.subsubelement");
405 assertNull("Should have returned a null", property);
406 }
407 }