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