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.net.URL;
021: import java.util.Properties;
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.FileOutputStream;
025: import java.io.FileWriter;
026: import java.io.IOException;
027: import java.io.PrintWriter;
028:
029: import org.apache.commons.configuration.event.ConfigurationErrorEvent;
030: import org.apache.commons.configuration.event.ConfigurationErrorListener;
031: import org.apache.commons.configuration.reloading.FileAlwaysReloadingStrategy;
032: import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
033:
034: import junit.framework.TestCase;
035:
036: /**
037: * @author Emmanuel Bourg
038: * @version $Revision: 497574 $, $Date: 2007-01-18 22:02:55 +0100 (Do, 18 Jan 2007) $
039: */
040: public class TestFileConfiguration extends TestCase {
041: private static final File TARGET_DIR = new File("target");
042:
043: private static final String RESOURCE_NAME = "config/deep/deeptest.properties";
044:
045: public void testSetURL() throws Exception {
046: // http URL
047: FileConfiguration config = new PropertiesConfiguration();
048: config
049: .setURL(new URL(
050: "http://jakarta.apache.org/commons/configuration/index.html"));
051:
052: assertEquals("base path",
053: "http://jakarta.apache.org/commons/configuration/",
054: config.getBasePath());
055: assertEquals("file name", "index.html", config.getFileName());
056:
057: // file URL
058: config.setURL(new URL("file:/temp/test.properties"));
059: assertEquals("base path", "file:/temp/", config.getBasePath());
060: assertEquals("file name", "test.properties", config
061: .getFileName());
062: }
063:
064: public void testSetURLWithParams() throws Exception {
065: FileConfiguration config = new PropertiesConfiguration();
066: URL url = new URL(
067: "http://issues.apache.org/bugzilla/show_bug.cgi?id=37886");
068: config.setURL(url);
069: assertEquals("Base path incorrect",
070: "http://issues.apache.org/bugzilla/", config
071: .getBasePath());
072: assertEquals("File name incorrect", "show_bug.cgi", config
073: .getFileName());
074: assertEquals("URL was not correctly stored", url, config
075: .getURL());
076: }
077:
078: public void testLocations() throws Exception {
079: PropertiesConfiguration config = new PropertiesConfiguration();
080:
081: File directory = new File("conf");
082: File file = new File(directory, "test.properties");
083: config.setFile(file);
084: assertEquals(directory.getAbsolutePath(), config.getBasePath());
085: assertEquals("test.properties", config.getFileName());
086: assertEquals(file.getAbsolutePath(), config.getPath());
087:
088: config.setPath("conf" + File.separator + "test.properties");
089: assertEquals("test.properties", config.getFileName());
090: assertEquals(directory.getAbsolutePath(), config.getBasePath());
091: assertEquals(file.getAbsolutePath(), config.getPath());
092: assertEquals(file.toURL(), config.getURL());
093:
094: config.setBasePath(null);
095: config.setFileName("test.properties");
096: assertNull(config.getBasePath());
097: assertEquals("test.properties", config.getFileName());
098: }
099:
100: public void testCreateFile1() throws Exception {
101: File file = new File(
102: "target/test-resources/foo/bar/test.properties");
103: if (file.exists()) {
104: file.delete();
105: file.getParentFile().delete();
106: }
107:
108: assertFalse("The file should not exist", file.exists());
109:
110: FileConfiguration config = new PropertiesConfiguration(file);
111: config.save();
112:
113: assertTrue("The file doesn't exist", file.exists());
114: }
115:
116: public void testCreateFile2() throws Exception {
117: File file = new File(
118: "target/test-resources/foo/bar/test.properties");
119: if (file.exists()) {
120: file.delete();
121: file.getParentFile().delete();
122: }
123:
124: assertFalse("The file should not exist", file.exists());
125:
126: FileConfiguration config = new PropertiesConfiguration();
127: config.setFile(file);
128: config.save();
129:
130: assertTrue("The file doesn't exist", file.exists());
131: }
132:
133: public void testCreateFile3() throws Exception {
134: File file = new File(
135: "target/test-resources/foo/bar/test.properties");
136: if (file.exists()) {
137: file.delete();
138: file.getParentFile().delete();
139: }
140:
141: assertFalse("The file should not exist", file.exists());
142:
143: FileConfiguration config = new PropertiesConfiguration();
144: config.save(file);
145:
146: assertTrue("The file doesn't exist", file.exists());
147: }
148:
149: /**
150: * Tests collaboration with ConfigurationFactory: Is the base path set on
151: * loading is valid in file based configurations?
152: *
153: * @throws Exception if an error occurs
154: */
155: public void testWithConfigurationFactory() throws Exception {
156: File dir = new File("conf");
157: File file = new File(dir, "testFileConfiguration.properties");
158:
159: if (file.exists()) {
160: assertTrue("File cannot be deleted", file.delete());
161: }
162:
163: try {
164: ConfigurationFactory factory = new ConfigurationFactory();
165: factory.setConfigurationURL(new File(dir,
166: "testDigesterConfiguration2.xml").toURL());
167: CompositeConfiguration cc = (CompositeConfiguration) factory
168: .getConfiguration();
169: PropertiesConfiguration config = null;
170: for (int i = 0; config == null; i++) {
171: if (cc.getConfiguration(i) instanceof PropertiesConfiguration) {
172: config = (PropertiesConfiguration) cc
173: .getConfiguration(i);
174: }
175: }
176:
177: config.setProperty("test", "yes");
178: config.save(file.getName());
179: assertTrue(file.exists());
180: config = new PropertiesConfiguration();
181: config.setFile(file);
182: config.load();
183:
184: assertEquals("yes", config.getProperty("test"));
185: assertEquals("masterOfPost", config
186: .getProperty("mail.account.user"));
187: } finally {
188: if (file.exists()) {
189: assertTrue("File could not be deleted", file.delete());
190: }
191: }
192: }
193:
194: /**
195: * Tests if invalid URLs cause an exception.
196: */
197: public void testSaveInvalidURL() throws Exception {
198: FileConfiguration config = new PropertiesConfiguration();
199: try {
200: config.save(new URL("http://jakarta.apache.org"));
201: fail("Should throw a ConfigurationException!");
202: } catch (ConfigurationException cex) {
203: //fine
204: }
205:
206: try {
207: config.save("http://www.apache.org");
208: fail("Should throw a ConfigurationException!");
209: } catch (ConfigurationException cex) {
210: //fine
211: }
212: }
213:
214: /**
215: * Tests if the URL used by the load() method is also used by save().
216: */
217: public void testFileOverwrite() throws Exception {
218: FileOutputStream out = null;
219: FileInputStream in = null;
220: File tempFile = null;
221: try {
222: String path = System.getProperties().getProperty(
223: "user.home");
224: File homeDir = new File(path);
225: tempFile = File.createTempFile("CONF", null, homeDir);
226: String fileName = tempFile.getName();
227: Properties props = new Properties();
228: props.setProperty("1", "one");
229: out = new FileOutputStream(tempFile);
230: props.store(out, "TestFileOverwrite");
231: out.close();
232: out = null;
233: FileConfiguration config = new PropertiesConfiguration(
234: fileName);
235: config.load();
236: String value = config.getString("1");
237: assertTrue("one".equals(value));
238: config.setProperty("1", "two");
239: config.save();
240: props = new Properties();
241: in = new FileInputStream(tempFile);
242: props.load(in);
243: String value2 = props.getProperty("1");
244: assertTrue("two".equals(value2));
245: } finally {
246: if (out != null) {
247: try {
248: out.close();
249: } catch (IOException ioex) {
250: ioex.printStackTrace();
251: }
252: }
253: if (in != null) {
254: try {
255: in.close();
256: } catch (IOException ioex) {
257: ioex.printStackTrace();
258: }
259: }
260: if (tempFile.exists()) {
261: assertTrue(tempFile.delete());
262: }
263: }
264: }
265:
266: /**
267: * Tests setting a file changed reloading strategy together with the auto
268: * save feature.
269: */
270: public void testReloadingWithAutoSave() throws Exception {
271: File configFile = new File(TARGET_DIR, "test.properties");
272: PrintWriter out = null;
273:
274: try {
275: out = new PrintWriter(new FileWriter(configFile));
276: out.println("a = one");
277: out.close();
278: out = null;
279:
280: PropertiesConfiguration config = new PropertiesConfiguration(
281: configFile);
282: config
283: .setReloadingStrategy(new FileChangedReloadingStrategy());
284: config.setAutoSave(true);
285:
286: assertEquals("one", config.getProperty("a"));
287: config.setProperty("b", "two");
288: assertEquals("one", config.getProperty("a"));
289: } finally {
290: if (out != null) {
291: out.close();
292: }
293: if (configFile.exists()) {
294: assertTrue(configFile.delete());
295: }
296: }
297: }
298:
299: /**
300: * Tests loading and saving a configuration file with a complicated path
301: * name including spaces. (related to issue 35210)
302: */
303: public void testPathWithSpaces() throws Exception {
304: File path = new File(TARGET_DIR, "path with spaces");
305: File confFile = new File(path, "config-test.properties");
306: PrintWriter out = null;
307:
308: try {
309: if (!path.exists()) {
310: assertTrue(path.mkdir());
311: }
312: out = new PrintWriter(new FileWriter(confFile));
313: out.println("saved = false");
314: out.close();
315: out = null;
316:
317: URL url = new URL(TARGET_DIR.toURL()
318: + "path%20with%20spaces/config-test.properties");
319: PropertiesConfiguration config = new PropertiesConfiguration(
320: url);
321: config.load();
322: assertFalse(config.getBoolean("saved"));
323:
324: config.setProperty("saved", Boolean.TRUE);
325: config.save();
326: config = new PropertiesConfiguration();
327: config.setFile(confFile);
328: config.load();
329: assertTrue(config.getBoolean("saved"));
330: } finally {
331: if (out != null) {
332: out.close();
333: }
334: if (confFile.exists()) {
335: assertTrue(confFile.delete());
336: }
337: if (path.exists()) {
338: assertTrue(path.delete());
339: }
340: }
341: }
342:
343: /**
344: * Tests the getFile() method.
345: */
346: public void testGetFile() throws ConfigurationException {
347: FileConfiguration config = new PropertiesConfiguration();
348: assertNull(config.getFile());
349: File file = new File("conf/test.properties").getAbsoluteFile();
350: config.setFile(file);
351: assertEquals(file, config.getFile());
352: config.load();
353: assertEquals(file, config.getFile());
354: }
355:
356: /**
357: * Tests to invoke save() without explicitely setting a file name. This
358: * will cause an exception.
359: */
360: public void testSaveWithoutFileName() throws Exception {
361: FileConfiguration config = new PropertiesConfiguration();
362: File file = new File("conf/test.properties");
363: config.load(file);
364: try {
365: config.save();
366: fail("Could save config without setting a file name!");
367: } catch (ConfigurationException cex) {
368: //ok
369: }
370:
371: config = new PropertiesConfiguration();
372: config.load("conf/test.properties");
373: try {
374: config.save();
375: fail("Could save config without setting a file name!");
376: } catch (ConfigurationException cex) {
377: //ok
378: }
379:
380: config = new PropertiesConfiguration();
381: config.load(file.toURL());
382: try {
383: config.save();
384: fail("Could save config without setting a file name!");
385: } catch (ConfigurationException cex) {
386: //ok
387: }
388: }
389:
390: /**
391: * Checks that loading a directory instead of a file throws an exception.
392: */
393: public void testLoadDirectory() {
394: PropertiesConfiguration config = new PropertiesConfiguration();
395:
396: try {
397: config.load("target");
398: fail("Could load config from a directory!");
399: } catch (ConfigurationException e) {
400: // ok
401: }
402:
403: try {
404: config.load(new File("target"));
405: fail("Could load config from a directory!");
406: } catch (ConfigurationException e) {
407: // ok
408: }
409:
410: try {
411: new PropertiesConfiguration("target");
412: fail("Could load config from a directory!");
413: } catch (ConfigurationException e) {
414: // ok
415: }
416:
417: try {
418: new PropertiesConfiguration(new File("target"));
419: fail("Could load config from a directory!");
420: } catch (ConfigurationException e) {
421: // ok
422: }
423: }
424:
425: /**
426: * Tests whether the constructor behaves the same as setFileName() when the
427: * configuration source is in the classpath.
428: */
429: public void testInitFromClassPath() throws ConfigurationException {
430: PropertiesConfiguration config1 = new PropertiesConfiguration();
431: config1.setFileName(RESOURCE_NAME);
432: config1.load();
433: PropertiesConfiguration config2 = new PropertiesConfiguration(
434: RESOURCE_NAME);
435: compare(config1, config2);
436: }
437:
438: /**
439: * Tests the loading of configuration file in a Combined configuration
440: * when the configuration source is in the classpath.
441: */
442: public void testLoadFromClassPath() throws ConfigurationException {
443: DefaultConfigurationBuilder cf = new DefaultConfigurationBuilder(
444: "conf/config/deep/testFileFromClasspath.xml");
445: CombinedConfiguration config = cf.getConfiguration(true);
446: Configuration config1 = config.getConfiguration("propConf");
447: Configuration config2 = config.getConfiguration("propConfDeep");
448: compare(config1, config2);
449: }
450:
451: /**
452: * Tests cloning a file based configuration.
453: */
454: public void testClone() throws ConfigurationException {
455: PropertiesConfiguration config = new PropertiesConfiguration(
456: RESOURCE_NAME);
457: PropertiesConfiguration copy = (PropertiesConfiguration) config
458: .clone();
459: compare(config, copy);
460: assertNull("URL was not reset", copy.getURL());
461: assertNull("Base path was not reset", copy.getBasePath());
462: assertNull("File name was not reset", copy.getFileName());
463: assertNotSame("Reloading strategy was not reset", config
464: .getReloadingStrategy(), copy.getReloadingStrategy());
465: }
466:
467: /**
468: * Tests whether an error log listener was registered at the configuration.
469: */
470: public void testLogErrorListener() {
471: PropertiesConfiguration config = new PropertiesConfiguration();
472: assertEquals("No error log listener registered", 1, config
473: .getErrorListeners().size());
474: }
475:
476: /**
477: * Tests handling of errors in the reload() method.
478: */
479: public void testReloadError() throws ConfigurationException {
480: class TestConfigurationErrorListener implements
481: ConfigurationErrorListener {
482: ConfigurationErrorEvent event;
483:
484: int errorCount;
485:
486: public void configurationError(ConfigurationErrorEvent event) {
487: this .event = event;
488: errorCount++;
489: }
490: }
491: ;
492: TestConfigurationErrorListener l = new TestConfigurationErrorListener();
493: PropertiesConfiguration config = new PropertiesConfiguration(
494: RESOURCE_NAME);
495: config.clearErrorListeners();
496: config.addErrorListener(l);
497: config.setReloadingStrategy(new FileAlwaysReloadingStrategy());
498: config.getString("test");
499: config.setFileName("Not existing file");
500: config.getString("test");
501: assertEquals("Wrong number of error events", 1, l.errorCount);
502: assertEquals("Wrong error type",
503: AbstractFileConfiguration.EVENT_RELOAD, l.event
504: .getType());
505: assertNull("Wrong property name", l.event.getPropertyName());
506: assertNull("Wrong property value", l.event.getPropertyValue());
507: assertNotNull("Exception is not set", l.event.getCause());
508: }
509:
510: /**
511: * Helper method for comparing the content of two configuration objects.
512: *
513: * @param config1 the first configuration
514: * @param config2 the second configuration
515: */
516: private void compare(Configuration config1, Configuration config2) {
517: StrictConfigurationComparator cc = new StrictConfigurationComparator();
518: assertTrue("Configurations are different", cc.compare(config1,
519: config2));
520: }
521: }
|