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 static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertNull;
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.File;
26 import java.io.FileWriter;
27
28 import org.apache.commons.configuration.ConfigurationException;
29 import org.apache.commons.configuration.FileSystem;
30 import org.apache.commons.configuration.PropertiesConfiguration;
31 import org.apache.commons.configuration.VFSFileSystem;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35
36
37
38
39
40
41
42 public class TestVFSFileChangedReloadingStrategy
43 {
44
45 private static final String TEST_FILE = "test.properties";
46
47 @Before
48 public void setUp() throws Exception
49 {
50 FileSystem.setDefaultFileSystem(new VFSFileSystem());
51 }
52
53 @After
54 public void tearDown() throws Exception
55 {
56 FileSystem.resetDefaultFileSystem();
57 }
58
59 @Test
60 public void testAutomaticReloading() throws Exception
61 {
62
63 File file = new File("target/testReload.properties");
64
65 if (file.exists())
66 {
67 file.delete();
68 }
69
70
71 FileWriter out = new FileWriter(file);
72 out.write("string=value1");
73 out.flush();
74 out.close();
75
76
77 PropertiesConfiguration config = new PropertiesConfiguration("target/testReload.properties");
78 VFSFileChangedReloadingStrategy strategy = new VFSFileChangedReloadingStrategy();
79 strategy.setRefreshDelay(500);
80 config.setReloadingStrategy(strategy);
81 assertEquals("Initial value", "value1", config.getString("string"));
82
83 Thread.sleep(2000);
84
85
86 out = new FileWriter(file);
87 out.write("string=value2");
88 out.flush();
89 out.close();
90
91
92 assertEquals("Modified value with enabled reloading", "value2", config.getString("string"));
93 }
94
95 @Test
96 public void testNewFileReloading() throws Exception
97 {
98
99 File file = new File("target/testReload.properties");
100
101 if (file.exists())
102 {
103 file.delete();
104 }
105
106 PropertiesConfiguration config = new PropertiesConfiguration();
107 config.setFile(file);
108 VFSFileChangedReloadingStrategy strategy = new VFSFileChangedReloadingStrategy();
109 strategy.setRefreshDelay(500);
110 config.setReloadingStrategy(strategy);
111
112 assertNull("Initial value", config.getString("string"));
113
114
115 FileWriter out = new FileWriter(file);
116 out.write("string=value1");
117 out.flush();
118 out.close();
119
120 Thread.sleep(2000);
121
122
123 assertEquals("Modified value with enabled reloading", "value1", config.getString("string"));
124 }
125
126 @Test
127 public void testGetRefreshDelay() throws Exception
128 {
129 VFSFileChangedReloadingStrategy strategy = new VFSFileChangedReloadingStrategy();
130 strategy.setRefreshDelay(500);
131 assertEquals("refresh delay", 500, strategy.getRefreshDelay());
132 }
133
134
135
136
137
138 @Test
139 public void testReloadingRequiredMultipleTimes()
140 throws ConfigurationException
141 {
142 VFSFileChangedReloadingStrategy strategy = new VFSFileChangedReloadingStrategy()
143 {
144 @Override
145 protected boolean hasChanged()
146 {
147
148 return true;
149 }
150 };
151 strategy.setRefreshDelay(100000);
152 PropertiesConfiguration config = new PropertiesConfiguration(TEST_FILE);
153 config.setReloadingStrategy(strategy);
154 assertTrue("Reloading not required", strategy.reloadingRequired());
155 assertTrue("Reloading no more required", strategy.reloadingRequired());
156 strategy.reloadingPerformed();
157 assertFalse("Reloading still required", strategy.reloadingRequired());
158 }
159 }