2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.commons.configuration.reloading;
21
import java.io.FileWriter;
23
import junit.framework.TestCase;
25
import org.apache.commons.configuration.ConfigurationException;
26
import org.apache.commons.configuration.PropertiesConfiguration;
27
import org.apache.commons.configuration.FileSystem;
28
import org.apache.commons.configuration.VFSFileSystem;
31
* Test case for the VFSFileMonitorReloadingStrategy class.
34
* @version $Revision: 823891 $
36
public class TestVFSFileChangedReloadingStrategy extends TestCase
38
/** Constant for the name of a test properties file.*/
39
private static final String TEST_FILE = "test.properties";
41
protected void setUp() throws Exception
44
FileSystem.setDefaultFileSystem(new VFSFileSystem());
47
protected void tearDown() throws Exception
49
FileSystem.resetDefaultFileSystem();
53
public void testAutomaticReloading() throws Exception
55
// create a new configuration
56
File file = new File("target/testReload.properties");
63
// create the configuration file
64
FileWriter out = new FileWriter(file);
65
out.write("string=value1");
69
// load the configuration
70
PropertiesConfiguration config = new PropertiesConfiguration("target/testReload.properties");
71
VFSFileChangedReloadingStrategy strategy = new VFSFileChangedReloadingStrategy();
72
strategy.setRefreshDelay(500);
73
config.setReloadingStrategy(strategy);
74
assertEquals("Initial value", "value1", config.getString("string"));
79
out = new FileWriter(file);
80
out.write("string=value2");
84
// test the automatic reloading
85
assertEquals("Modified value with enabled reloading", "value2", config.getString("string"));
88
public void testNewFileReloading() throws Exception
90
// create a new configuration
91
File file = new File("target/testReload.properties");
98
PropertiesConfiguration config = new PropertiesConfiguration();
100
VFSFileChangedReloadingStrategy strategy = new VFSFileChangedReloadingStrategy();
101
strategy.setRefreshDelay(500);
102
config.setReloadingStrategy(strategy);
104
assertNull("Initial value", config.getString("string"));
107
FileWriter out = new FileWriter(file);
108
out.write("string=value1");
114
// test the automatic reloading
115
assertEquals("Modified value with enabled reloading", "value1", config.getString("string"));
118
public void testGetRefreshDelay() throws Exception
120
VFSFileChangedReloadingStrategy strategy = new VFSFileChangedReloadingStrategy();
121
strategy.setRefreshDelay(500);
122
assertEquals("refresh delay", 500, strategy.getRefreshDelay());
126
* Tests calling reloadingRequired() multiple times before a reload actually
127
* happens. This test is related to CONFIGURATION-302.
129
public void testReloadingRequiredMultipleTimes()
130
throws ConfigurationException
132
VFSFileChangedReloadingStrategy strategy = new VFSFileChangedReloadingStrategy()
134
protected boolean hasChanged()
136
// signal always a change
140
strategy.setRefreshDelay(100000);
141
PropertiesConfiguration config = new PropertiesConfiguration(TEST_FILE);
142
config.setReloadingStrategy(strategy);
143
assertTrue("Reloading not required", strategy.reloadingRequired());
144
assertTrue("Reloading no more required", strategy.reloadingRequired());
145
strategy.reloadingPerformed();
146
assertFalse("Reloading still required", strategy.reloadingRequired());
b'\\ No newline at end of file'