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