KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > configuration > reloading > TestFileChangedReloadingStrategy


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License")
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.configuration.reloading;
18
19 import java.io.File JavaDoc;
20 import java.io.FileWriter JavaDoc;
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 JavaDoc
34     {
35         // create a new configuration
36
File JavaDoc file = new File JavaDoc("target/testReload.properties");
37
38         if (file.exists())
39         {
40             file.delete();
41         }
42
43         // create the configuration file
44
FileWriter JavaDoc out = new FileWriter JavaDoc(file);
45         out.write("string=value1");
46         out.flush();
47         out.close();
48
49         // load the configuration
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         // change the file
59
out = new FileWriter JavaDoc(file);
60         out.write("string=value2");
61         out.flush();
62         out.close();
63
64         // test the automatic reloading
65
assertEquals("Modified value with enabled reloading", "value2", config.getString("string"));
66     }
67
68     public void testNewFileReloading() throws Exception JavaDoc
69     {
70         // create a new configuration
71
File JavaDoc file = new File JavaDoc("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         // change the file
87
FileWriter JavaDoc out = new FileWriter JavaDoc(file);
88         out.write("string=value1");
89         out.flush();
90         out.close();
91
92         Thread.sleep(2000);
93
94         // test the automatic reloading
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 }
106
Popular Tags