1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.configuration.reloading;
19
20 import java.io.File;
21 import java.io.FileWriter;
22 import java.net.URL;
23
24 import junit.framework.TestCase;
25 import org.apache.commons.configuration.PropertiesConfiguration;
26 import org.apache.commons.configuration.XMLConfiguration;
27
28 /***
29 * Test case for the ReloadableConfiguration class.
30 *
31 * @author Emmanuel Bourg
32 * @version $Revision: 439648 $, $Date: 2006-09-02 22:42:10 +0200 (Sa, 02 Sep 2006) $
33 */
34 public class TestFileChangedReloadingStrategy extends TestCase
35 {
36 public void testAutomaticReloading() throws Exception
37 {
38
39 File file = new File("target/testReload.properties");
40
41 if (file.exists())
42 {
43 file.delete();
44 }
45
46
47 FileWriter out = new FileWriter(file);
48 out.write("string=value1");
49 out.flush();
50 out.close();
51
52
53 PropertiesConfiguration config = new PropertiesConfiguration("target/testReload.properties");
54 FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
55 strategy.setRefreshDelay(500);
56 config.setReloadingStrategy(strategy);
57 assertEquals("Initial value", "value1", config.getString("string"));
58
59 Thread.sleep(2000);
60
61
62 out = new FileWriter(file);
63 out.write("string=value2");
64 out.flush();
65 out.close();
66
67
68 assertEquals("Modified value with enabled reloading", "value2", config.getString("string"));
69 }
70
71 public void testNewFileReloading() throws Exception
72 {
73
74 File file = new File("target/testReload.properties");
75
76 if (file.exists())
77 {
78 file.delete();
79 }
80
81 PropertiesConfiguration config = new PropertiesConfiguration();
82 config.setFile(file);
83 FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
84 strategy.setRefreshDelay(500);
85 config.setReloadingStrategy(strategy);
86
87 assertNull("Initial value", config.getString("string"));
88
89
90 FileWriter out = new FileWriter(file);
91 out.write("string=value1");
92 out.flush();
93 out.close();
94
95 Thread.sleep(2000);
96
97
98 assertEquals("Modified value with enabled reloading", "value1", config.getString("string"));
99 }
100
101 public void testGetRefreshDelay()
102 {
103 FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
104 strategy.setRefreshDelay(500);
105 assertEquals("refresh delay", 500, strategy.getRefreshDelay());
106 }
107
108 /***
109 * Tests if a file from the classpath can be monitored.
110 */
111 public void testFromClassPath() throws Exception
112 {
113 PropertiesConfiguration config = new PropertiesConfiguration();
114 config.setFileName("test.properties");
115 config.load();
116 assertTrue(config.getBoolean("configuration.loaded"));
117 FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
118 config.setReloadingStrategy(strategy);
119 assertEquals(config.getURL(), strategy.getFile().toURL());
120 }
121
122 /***
123 * Tests to watch a configuration file in a jar. In this case the jar file
124 * itself should be monitored.
125 */
126 public void testFromJar() throws Exception
127 {
128 XMLConfiguration config = new XMLConfiguration();
129
130 config.setURL(new URL("jar:" + new File("conf/resources.jar").getAbsoluteFile().toURL() + "!/test-jar.xml"));
131 FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
132 config.setReloadingStrategy(strategy);
133 File file = strategy.getFile();
134 assertNotNull("Strategy's file is null", file);
135 assertEquals("Strategy does not monitor the jar file", "resources.jar", file.getName());
136 }
137 }