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.io.FileWriter;
022: import java.util.Collection;
023: import java.util.List;
024:
025: import junit.framework.TestCase;
026:
027: import org.xml.sax.SAXParseException;
028:
029: /**
030: * Test the ConfigurationFactory.
031: *
032: * @version $Id: TestConfigurationFactory.java 506128 2007-02-11 20:43:08Z oheger $
033: */
034: public class TestConfigurationFactory extends TestCase {
035: /** The Files that we test with */
036: private File digesterRules = new File("conf/digesterRules.xml");
037: private File testDigesterFile = new File(
038: "conf/testDigesterConfiguration.xml");
039: private File testDigesterFileReverseOrder = new File(
040: "conf/testDigesterConfigurationReverseOrder.xml");
041: private File testDigesterFileNamespaceAware = new File(
042: "conf/testDigesterConfigurationNamespaceAware.xml");
043: private File testDigesterFileBasePath = new File(
044: "conf/testDigesterConfigurationBasePath.xml");
045: private File testDigesterFileEnhanced = new File(
046: "conf/testDigesterConfiguration2.xml");
047: private File testDigesterFileComplete = new File(
048: "conf/testDigesterConfiguration3.xml");
049: private File testDigesterFileOptional = new File(
050: "conf/testDigesterOptionalConfiguration.xml");
051: private File testDigesterFileOptionalEx = new File(
052: "conf/testDigesterOptionalConfigurationEx.xml");
053: private File testDigesterFileSysProps = new File(
054: "conf/testDigesterConfigurationSysProps.xml");
055: private File testDigesterFileInitProps = new File(
056: "conf/testDigesterConfigurationWithProps.xml");
057:
058: private File testDigesterBadXML = new File(
059: "conf/testDigesterBadXML.xml");
060:
061: private String testBasePath = new File("conf").getAbsolutePath();
062:
063: private File testProperties = new File("conf/test.properties");
064: private File testAbsConfig = new File("target/testAbsConfig.xml");
065:
066: private Configuration configuration;
067: private CompositeConfiguration compositeConfiguration;
068: private ConfigurationFactory factory;
069:
070: public void setUp() throws Exception {
071: System
072: .setProperty("java.naming.factory.initial",
073: "org.apache.commons.configuration.MockInitialContextFactory");
074: factory = new ConfigurationFactory();
075: }
076:
077: public void testJNDI() throws Exception {
078: JNDIConfiguration jndiConfiguration = new JNDIConfiguration();
079: Object o = jndiConfiguration.getProperty("test.boolean");
080: assertNotNull(o);
081: assertEquals("true", o.toString());
082: }
083:
084: public void testLoadingConfiguration() throws Exception {
085: factory.setConfigurationFileName(testDigesterFile.toString());
086:
087: compositeConfiguration = (CompositeConfiguration) factory
088: .getConfiguration();
089:
090: assertEquals("Number of configurations", 4,
091: compositeConfiguration.getNumberOfConfigurations());
092: assertEquals(PropertiesConfiguration.class,
093: compositeConfiguration.getConfiguration(0).getClass());
094: assertEquals(XMLPropertiesConfiguration.class,
095: compositeConfiguration.getConfiguration(1).getClass());
096: assertEquals(XMLConfiguration.class, compositeConfiguration
097: .getConfiguration(2).getClass());
098:
099: // check the first configuration
100: PropertiesConfiguration pc = (PropertiesConfiguration) compositeConfiguration
101: .getConfiguration(0);
102: assertNotNull("Make sure we have a fileName: "
103: + pc.getFileName(), pc.getFileName());
104:
105: // check some properties
106: assertTrue("Make sure we have loaded our key",
107: compositeConfiguration.getBoolean("test.boolean"));
108: assertEquals("I'm complex!", compositeConfiguration
109: .getProperty("element2.subelement.subsubelement"));
110: assertEquals("property in the XMLPropertiesConfiguration",
111: "value1", compositeConfiguration.getProperty("key1"));
112: }
113:
114: public void testLoadingConfigurationWithRulesXML() throws Exception {
115: factory.setConfigurationFileName(testDigesterFile.toString());
116: factory.setDigesterRules(digesterRules.toURL());
117:
118: compositeConfiguration = (CompositeConfiguration) factory
119: .getConfiguration();
120:
121: assertEquals("Number of configurations", 4,
122: compositeConfiguration.getNumberOfConfigurations());
123: assertEquals(PropertiesConfiguration.class,
124: compositeConfiguration.getConfiguration(0).getClass());
125: //assertEquals(XMLPropertiesConfiguration.class, compositeConfiguration.getConfiguration(1).getClass()); // doesn't work
126: assertEquals(XMLConfiguration.class, compositeConfiguration
127: .getConfiguration(2).getClass());
128:
129: // check the first configuration
130: PropertiesConfiguration pc = (PropertiesConfiguration) compositeConfiguration
131: .getConfiguration(0);
132: assertNotNull("Make sure we have a fileName: "
133: + pc.getFileName(), pc.getFileName());
134:
135: // check some properties
136: assertTrue("Make sure we have loaded our key", pc
137: .getBoolean("test.boolean"));
138: assertTrue("Make sure we have loaded our key",
139: compositeConfiguration.getBoolean("test.boolean"));
140:
141: assertEquals("I'm complex!", compositeConfiguration
142: .getProperty("element2.subelement.subsubelement"));
143: }
144:
145: public void testLoadingConfigurationReverseOrder() throws Exception {
146: factory.setConfigurationFileName(testDigesterFileReverseOrder
147: .toString());
148:
149: configuration = factory.getConfiguration();
150:
151: assertEquals("8", configuration.getProperty("test.short"));
152:
153: factory.setConfigurationFileName(testDigesterFile.toString());
154:
155: configuration = factory.getConfiguration();
156: assertEquals("1", configuration.getProperty("test.short"));
157: }
158:
159: public void testLoadingConfigurationNamespaceAware()
160: throws Exception {
161: factory.setConfigurationFileName(testDigesterFileNamespaceAware
162: .toString());
163: //factory.setDigesterRules(digesterRules.toURL());
164: factory.setDigesterRuleNamespaceURI("namespace-one");
165:
166: checkCompositeConfiguration();
167: }
168:
169: public void testLoadingConfigurationBasePath() throws Exception {
170: factory.setConfigurationFileName(testDigesterFileBasePath
171: .toString());
172:
173: factory.setBasePath(testBasePath);
174:
175: //factory.setDigesterRules(digesterRules.toURL());
176: //factory.setDigesterRuleNamespaceURI("namespace-one");
177:
178: checkCompositeConfiguration();
179: }
180:
181: public void testLoadingAdditional() throws Exception {
182: factory.setConfigurationFileName(testDigesterFileEnhanced
183: .toString());
184: factory.setBasePath(null);
185: checkUnionConfig();
186: }
187:
188: public void testLoadingURL() throws Exception {
189: factory.setConfigurationURL(testDigesterFileEnhanced.toURL());
190: checkUnionConfig();
191:
192: factory = new ConfigurationFactory();
193: File nonExistingFile = new File("conf/nonexisting.xml");
194: factory.setConfigurationURL(nonExistingFile.toURL());
195: try {
196: factory.getConfiguration();
197: fail("Could load non existing file!");
198: } catch (ConfigurationException cex) {
199: //ok
200: }
201: }
202:
203: public void testThrowingConfigurationInitializationException()
204: throws Exception {
205: factory.setConfigurationFileName(testDigesterBadXML.toString());
206: try {
207: factory.getConfiguration();
208: fail("Should have throw an Exception");
209: } catch (ConfigurationException cle) {
210: assertTrue(cle.getCause() instanceof SAXParseException);
211: }
212: }
213:
214: // Tests if properties from all sources can be loaded
215: public void testAllConfiguration() throws Exception {
216: factory.setConfigurationURL(testDigesterFileComplete.toURL());
217: Configuration config = factory.getConfiguration();
218: assertFalse(config.isEmpty());
219: assertTrue(config instanceof CompositeConfiguration);
220: CompositeConfiguration cc = (CompositeConfiguration) config;
221: assertTrue(cc.getNumberOfConfigurations() > 1);
222: // Currently fails, should be 4? Only 2?
223: //assertEquals(4, cc.getNumberOfConfigurations());
224:
225: assertNotNull(config
226: .getProperty("tables.table(0).fields.field(2).name"));
227: assertNotNull(config
228: .getProperty("element2.subelement.subsubelement"));
229: assertEquals("value", config.getProperty("element3"));
230: assertEquals("foo", config.getProperty("element3[@name]"));
231: assertNotNull(config.getProperty("mail.account.user"));
232:
233: // test JNDIConfiguration
234: assertNotNull(config.getProperty("test.onlyinjndi"));
235: assertTrue(config.getBoolean("test.onlyinjndi"));
236:
237: Configuration subset = config.subset("test");
238: assertNotNull(subset.getProperty("onlyinjndi"));
239: assertTrue(subset.getBoolean("onlyinjndi"));
240:
241: // test SystemConfiguration
242: assertNotNull(config.getProperty("java.version"));
243: assertEquals(System.getProperty("java.version"), config
244: .getString("java.version"));
245: }
246:
247: // Checks if optional configurations work
248: public void testOptionalConfigurations() throws Exception {
249: factory.setConfigurationURL(testDigesterFileOptional.toURL());
250: Configuration config = factory.getConfiguration();
251: assertTrue(config.getBoolean("test.boolean"));
252: assertEquals("value", config.getProperty("element"));
253:
254: factory.setConfigurationURL(testDigesterFileOptionalEx.toURL());
255: try {
256: config = factory.getConfiguration();
257: fail("Unexisting properties loaded!");
258: } catch (ConfigurationException cex) {
259: // fine
260: }
261: }
262:
263: // Checks if a file with an absolute path can be loaded
264: public void testLoadAbsolutePath() throws Exception {
265: try {
266: FileWriter out = null;
267: try {
268: out = new FileWriter(testAbsConfig);
269: out
270: .write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>");
271: out.write("<configuration>");
272: out.write("<properties fileName=\"");
273: out.write(testProperties.getAbsolutePath());
274: out.write("\"/>");
275: out.write("</configuration>");
276: } finally {
277: if (out != null) {
278: out.close();
279: }
280: }
281:
282: factory.setConfigurationFileName(testAbsConfig.toString());
283: Configuration config = factory.getConfiguration();
284: assertTrue(config.getBoolean("configuration.loaded"));
285: } finally {
286: if (testAbsConfig.exists()) {
287: testAbsConfig.delete();
288: }
289: }
290: }
291:
292: public void testBasePath() throws Exception {
293: assertEquals(".", factory.getBasePath());
294: factory.setConfigurationFileName(testDigesterFile
295: .getAbsolutePath());
296: // if no specific base path has been set, the base is determined
297: // from the file name
298: assertEquals(
299: testDigesterFile.getParentFile().getAbsolutePath(),
300: factory.getBasePath());
301:
302: String homeDir = System.getProperty("user.home");
303: factory = new ConfigurationFactory();
304: factory.setBasePath(homeDir);
305: factory.setConfigurationFileName(testDigesterFile
306: .getAbsolutePath());
307: // if a base path was set, the file name does not play a role
308: assertEquals(homeDir, factory.getBasePath());
309:
310: factory = new ConfigurationFactory(testDigesterFile
311: .getAbsolutePath());
312: assertEquals(
313: testDigesterFile.getParentFile().getAbsolutePath(),
314: factory.getBasePath());
315: factory.setBasePath(homeDir);
316: assertEquals(homeDir, factory.getBasePath());
317:
318: factory = new ConfigurationFactory();
319: factory.setConfigurationURL(testDigesterFile.toURL());
320: assertEquals(testDigesterFile.toURL().toString(), factory
321: .getBasePath());
322: }
323:
324: // Tests if system properties can be resolved in the configuration
325: // definition
326: public void testLoadingWithSystemProperties()
327: throws ConfigurationException {
328: System.setProperty("config.file", "test.properties");
329: factory.setConfigurationFileName(testDigesterFileSysProps
330: .getAbsolutePath());
331: Configuration config = factory.getConfiguration();
332: assertTrue("Configuration not loaded", config
333: .getBoolean("configuration.loaded"));
334: }
335:
336: // Tests if the properties of a configuration object are correctly set
337: // before it is loaded.
338: public void testLoadInitProperties() throws ConfigurationException {
339: factory.setConfigurationFileName(testDigesterFileInitProps
340: .getAbsolutePath());
341: Configuration config = factory.getConfiguration();
342: PropertiesConfiguration c = (PropertiesConfiguration) ((CompositeConfiguration) config)
343: .getConfiguration(0);
344: assertEquals("List delimiter was not set", ';', c
345: .getListDelimiter());
346: List l = c.getList("test.mixed.array");
347: assertEquals("Wrong number of list elements", 2, l.size());
348: assertEquals("List delimiter was not applied", "b, c, d", l
349: .get(1));
350: }
351:
352: private void checkUnionConfig() throws Exception {
353: compositeConfiguration = (CompositeConfiguration) factory
354: .getConfiguration();
355: assertEquals("Verify how many configs", 3,
356: compositeConfiguration.getNumberOfConfigurations());
357:
358: // Test if union was constructed correctly
359: Object prop = compositeConfiguration
360: .getProperty("tables.table.name");
361: assertTrue(prop instanceof Collection);
362: assertEquals(3, ((Collection) prop).size());
363: assertEquals("users", compositeConfiguration
364: .getProperty("tables.table(0).name"));
365: assertEquals("documents", compositeConfiguration
366: .getProperty("tables.table(1).name"));
367: assertEquals("tasks", compositeConfiguration
368: .getProperty("tables.table(2).name"));
369:
370: prop = compositeConfiguration
371: .getProperty("tables.table.fields.field.name");
372: assertTrue(prop instanceof Collection);
373: assertEquals(17, ((Collection) prop).size());
374:
375: assertEquals("smtp.mydomain.org", compositeConfiguration
376: .getString("mail.host.smtp"));
377: assertEquals("pop3.mydomain.org", compositeConfiguration
378: .getString("mail.host.pop"));
379:
380: // This was overriden
381: assertEquals("masterOfPost", compositeConfiguration
382: .getString("mail.account.user"));
383: assertEquals("topsecret", compositeConfiguration
384: .getString("mail.account.psswd"));
385:
386: // This was overriden, too, but not in additional section
387: assertEquals("enhanced factory", compositeConfiguration
388: .getString("test.configuration"));
389: }
390:
391: private void checkCompositeConfiguration() throws Exception {
392: compositeConfiguration = (CompositeConfiguration) factory
393: .getConfiguration();
394:
395: assertEquals("Verify how many configs", 2,
396: compositeConfiguration.getNumberOfConfigurations());
397: assertEquals(PropertiesConfiguration.class,
398: compositeConfiguration.getConfiguration(0).getClass());
399:
400: PropertiesConfiguration pc = (PropertiesConfiguration) compositeConfiguration
401: .getConfiguration(0);
402: assertNotNull("Make sure we have a fileName:"
403: + pc.getFileName(), pc.getFileName());
404: assertTrue("Make sure we have loaded our key", pc
405: .getBoolean("test.boolean"));
406: assertTrue("Make sure we have loaded our key",
407: compositeConfiguration.getBoolean("test.boolean"));
408:
409: Object property = compositeConfiguration
410: .getProperty("element2.subelement.subsubelement");
411: assertNull("Should have returned a null", property);
412: }
413: }
|