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.reloading;
019:
020: import java.io.File;
021: import java.io.FileWriter;
022: import java.net.URL;
023:
024: import junit.framework.TestCase;
025: import org.apache.commons.configuration.PropertiesConfiguration;
026: import org.apache.commons.configuration.XMLConfiguration;
027:
028: /**
029: * Test case for the ReloadableConfiguration class.
030: *
031: * @author Emmanuel Bourg
032: * @version $Revision: 439648 $, $Date: 2006-09-02 22:42:10 +0200 (Sa, 02 Sep 2006) $
033: */
034: public class TestFileChangedReloadingStrategy extends TestCase {
035: public void testAutomaticReloading() throws Exception {
036: // create a new configuration
037: File file = new File("target/testReload.properties");
038:
039: if (file.exists()) {
040: file.delete();
041: }
042:
043: // create the configuration file
044: FileWriter out = new FileWriter(file);
045: out.write("string=value1");
046: out.flush();
047: out.close();
048:
049: // load the configuration
050: PropertiesConfiguration config = new PropertiesConfiguration(
051: "target/testReload.properties");
052: FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
053: strategy.setRefreshDelay(500);
054: config.setReloadingStrategy(strategy);
055: assertEquals("Initial value", "value1", config
056: .getString("string"));
057:
058: Thread.sleep(2000);
059:
060: // change the file
061: out = new FileWriter(file);
062: out.write("string=value2");
063: out.flush();
064: out.close();
065:
066: // test the automatic reloading
067: assertEquals("Modified value with enabled reloading", "value2",
068: config.getString("string"));
069: }
070:
071: public void testNewFileReloading() throws Exception {
072: // create a new configuration
073: File file = new File("target/testReload.properties");
074:
075: if (file.exists()) {
076: file.delete();
077: }
078:
079: PropertiesConfiguration config = new PropertiesConfiguration();
080: config.setFile(file);
081: FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
082: strategy.setRefreshDelay(500);
083: config.setReloadingStrategy(strategy);
084:
085: assertNull("Initial value", config.getString("string"));
086:
087: // change the file
088: FileWriter out = new FileWriter(file);
089: out.write("string=value1");
090: out.flush();
091: out.close();
092:
093: Thread.sleep(2000);
094:
095: // test the automatic reloading
096: assertEquals("Modified value with enabled reloading", "value1",
097: config.getString("string"));
098: }
099:
100: public void testGetRefreshDelay() {
101: FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
102: strategy.setRefreshDelay(500);
103: assertEquals("refresh delay", 500, strategy.getRefreshDelay());
104: }
105:
106: /**
107: * Tests if a file from the classpath can be monitored.
108: */
109: public void testFromClassPath() throws Exception {
110: PropertiesConfiguration config = new PropertiesConfiguration();
111: config.setFileName("test.properties");
112: config.load();
113: assertTrue(config.getBoolean("configuration.loaded"));
114: FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
115: config.setReloadingStrategy(strategy);
116: assertEquals(config.getURL(), strategy.getFile().toURL());
117: }
118:
119: /**
120: * Tests to watch a configuration file in a jar. In this case the jar file
121: * itself should be monitored.
122: */
123: public void testFromJar() throws Exception {
124: XMLConfiguration config = new XMLConfiguration();
125: // use some jar: URL
126: config.setURL(new URL("jar:"
127: + new File("conf/resources.jar").getAbsoluteFile()
128: .toURL() + "!/test-jar.xml"));
129: FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
130: config.setReloadingStrategy(strategy);
131: File file = strategy.getFile();
132: assertNotNull("Strategy's file is null", file);
133: assertEquals("Strategy does not monitor the jar file",
134: "resources.jar", file.getName());
135: }
136: }
|