View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Test case for the VFSFileMonitorReloadingStrategy class.
38   *
39   * @author Ralph Goers
40   * @version $Id: TestVFSFileChangedReloadingStrategy.java 1225909 2011-12-30 20:09:00Z oheger $
41   */
42  public class TestVFSFileChangedReloadingStrategy
43  {
44      /** Constant for the name of a test properties file.*/
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          // create a new configuration
63          File file = new File("target/testReload.properties");
64  
65          if (file.exists())
66          {
67              file.delete();
68          }
69  
70          // create the configuration file
71          FileWriter out = new FileWriter(file);
72          out.write("string=value1");
73          out.flush();
74          out.close();
75  
76          // load the configuration
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          // change the file
86          out = new FileWriter(file);
87          out.write("string=value2");
88          out.flush();
89          out.close();
90  
91          // test the automatic reloading
92          assertEquals("Modified value with enabled reloading", "value2", config.getString("string"));
93      }
94  
95      @Test
96      public void testNewFileReloading() throws Exception
97      {
98          // create a new configuration
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         // change the file
115         FileWriter out = new FileWriter(file);
116         out.write("string=value1");
117         out.flush();
118         out.close();
119 
120         Thread.sleep(2000);
121 
122         // test the automatic reloading
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      * Tests calling reloadingRequired() multiple times before a reload actually
136      * happens. This test is related to CONFIGURATION-302.
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                 // signal always a change
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 }