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  
22  import java.io.File;
23  import java.io.FileWriter;
24  
25  import org.apache.commons.configuration.PropertiesConfiguration;
26  import org.junit.Test;
27  
28  /**
29   * Test case for the ManagedReloadingStrategy class.
30   *
31   * @author Nicolas De loof
32   * @version $Id: TestManagedReloadingStrategy.java 1225908 2011-12-30 20:04:48Z oheger $
33   */
34  public class TestManagedReloadingStrategy
35  {
36      @Test
37      public void testManagedRefresh() throws Exception
38      {
39          File file = new File("target/testReload.properties");
40          if (file.exists())
41          {
42              file.delete();
43          }
44          // create the configuration file
45          FileWriter out = new FileWriter(file);
46          out.write("string=value1");
47          out.flush();
48          out.close();
49  
50          // load the configuration
51          PropertiesConfiguration config = new PropertiesConfiguration("target/testReload.properties");
52          ManagedReloadingStrategy strategy = new ManagedReloadingStrategy();
53          config.setReloadingStrategy(strategy);
54          assertEquals("Initial value", "value1", config.getString("string"));
55  
56          // change the file
57          out = new FileWriter(file);
58          out.write("string=value2");
59          out.flush();
60          out.close();
61  
62          // test the automatic reloading
63          assertEquals("No automatic reloading", "value1", config.getString("string"));
64          strategy.refresh();
65          assertEquals("Modified value with enabled reloading", "value2", config.getString("string"));
66      }
67  
68  }