1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration.reloading;
18
19 import java.io.File;
20 import java.io.FileWriter;
21
22 import junit.framework.TestCase;
23 import org.apache.commons.configuration.PropertiesConfiguration;
24
25 /***
26 * Test case for the ReloadableConfiguration class.
27 *
28 * @author Emmanuel Bourg
29 * @version $Revision: 155770 $, $Date: 2005-03-01 13:04:59 +0100 (Di, 01 Mrz 2005) $
30 */
31 public class TestFileChangedReloadingStrategy extends TestCase
32 {
33 public void testAutomaticReloading() throws Exception
34 {
35
36 File file = new File("target/testReload.properties");
37
38 if (file.exists())
39 {
40 file.delete();
41 }
42
43
44 FileWriter out = new FileWriter(file);
45 out.write("string=value1");
46 out.flush();
47 out.close();
48
49
50 PropertiesConfiguration config = new PropertiesConfiguration("target/testReload.properties");
51 FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
52 strategy.setRefreshDelay(500);
53 config.setReloadingStrategy(strategy);
54 assertEquals("Initial value", "value1", config.getString("string"));
55
56 Thread.sleep(2000);
57
58
59 out = new FileWriter(file);
60 out.write("string=value2");
61 out.flush();
62 out.close();
63
64
65 assertEquals("Modified value with enabled reloading", "value2", config.getString("string"));
66 }
67
68 public void testNewFileReloading() throws Exception
69 {
70
71 File file = new File("target/testReload.properties");
72
73 if (file.exists())
74 {
75 file.delete();
76 }
77
78 PropertiesConfiguration config = new PropertiesConfiguration();
79 config.setFile(file);
80 FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
81 strategy.setRefreshDelay(500);
82 config.setReloadingStrategy(strategy);
83
84 assertNull("Initial value", config.getString("string"));
85
86
87 FileWriter out = new FileWriter(file);
88 out.write("string=value1");
89 out.flush();
90 out.close();
91
92 Thread.sleep(2000);
93
94
95 assertEquals("Modified value with enabled reloading", "value1", config.getString("string"));
96 }
97
98 public void testGetRefreshDelay()
99 {
100 FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
101 strategy.setRefreshDelay(500);
102 assertEquals("refresh delay", 500, strategy.getRefreshDelay());
103 }
104
105 }