    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
    26  import org.apache.commons.configuration.ConfigurationException;
    27  import org.apache.commons.configuration.PropertiesConfiguration;
    28  import org.apache.commons.configuration.XMLConfiguration;
    29
    30  /***
    31   * Test case for the ReloadableConfiguration class.
    32   *
    33   * @author Emmanuel Bourg
    34   * @version $Revision: 606798 $, $Date: 2007-12-25 20:05:58 +0100 (Di, 25 Dez 2007) $
    35   */
    36  public class TestFileChangedReloadingStrategy extends TestCase
    37  {
    38      /*** Constant for the name of a test properties file.*/
    39      private static final String TEST_FILE = "test.properties";
    40
    41      public void testAutomaticReloading() throws Exception
    42      {
    43          // create a new configuration
    44          File file = new File("target/testReload.properties");
    45
    46          if (file.exists())
    47          {
    48              file.delete();
    49          }
    50
    51          // create the configuration file
    52          FileWriter out = new FileWriter(file);
    53          out.write("string=value1");
    54          out.flush();
    55          out.close();
    56
    57          // load the configuration
    58          PropertiesConfiguration config = new PropertiesConfiguration("target/testReload.properties");
    59          FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
    60          strategy.setRefreshDelay(500);
    61          config.setReloadingStrategy(strategy);
    62          assertEquals("Initial value", "value1", config.getString("string"));
    63
    64          Thread.sleep(2000);
    65
    66          // change the file
    67          out = new FileWriter(file);
    68          out.write("string=value2");
    69          out.flush();
    70          out.close();
    71
    72          // test the automatic reloading
    73          assertEquals("Modified value with enabled reloading", "value2", config.getString("string"));
    74      }
    75
    76      public void testNewFileReloading() throws Exception
    77      {
    78          // create a new configuration
    79          File file = new File("target/testReload.properties");
    80
    81          if (file.exists())
    82          {
    83              file.delete();
    84          }
    85
    86          PropertiesConfiguration config = new PropertiesConfiguration();
    87          config.setFile(file);
    88          FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
    89          strategy.setRefreshDelay(500);
    90          config.setReloadingStrategy(strategy);
    91
    92          assertNull("Initial value", config.getString("string"));
    93
    94          // change the file
    95          FileWriter out = new FileWriter(file);
    96          out.write("string=value1");
    97          out.flush();
    98          out.close();
    99
    100         Thread.sleep(2000);
    101
    102         // test the automatic reloading
    103         assertEquals("Modified value with enabled reloading", "value1", config.getString("string"));
    104     }
    105
    106     public void testGetRefreshDelay()
    107     {
    108         FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
    109         strategy.setRefreshDelay(500);
    110         assertEquals("refresh delay", 500, strategy.getRefreshDelay());
    111     }
    112
    113     /***
    114      * Tests if a file from the classpath can be monitored.
    115      */
    116     public void testFromClassPath() throws Exception
    117     {
    118         PropertiesConfiguration config = new PropertiesConfiguration();
    119         config.setFileName(TEST_FILE);
    120         config.load();
    121         assertTrue(config.getBoolean("configuration.loaded"));
    122         FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
    123         config.setReloadingStrategy(strategy);
    124         assertEquals(config.getURL(), strategy.getFile().toURL());
    125     }
    126
    127     /***
    128      * Tests to watch a configuration file in a jar. In this case the jar file
    129      * itself should be monitored.
    130      */
    131     public void testFromJar() throws Exception
    132     {
    133         XMLConfiguration config = new XMLConfiguration();
    134         // use some jar: URL
    135         config.setURL(new URL("jar:" + new File("conf/resources.jar").getAbsoluteFile().toURL() + "!/test-jar.xml"));
    136         FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
    137         config.setReloadingStrategy(strategy);
    138         File file = strategy.getFile();
    139         assertNotNull("Strategy's file is null", file);
    140         assertEquals("Strategy does not monitor the jar file", "resources.jar", file.getName());
    141     }
    142
    143     /***
    144      * Tests calling reloadingRequired() multiple times before a reload actually
    145      * happens. This test is related to CONFIGURATION-302.
    146      */
    147     public void testReloadingRequiredMultipleTimes()
    148             throws ConfigurationException
    149     {
    150         FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy()
    151         {
    152             protected boolean hasChanged()
    153             {
    154                 // signal always a change
    155                 return true;
    156             }
    157         };
    158         strategy.setRefreshDelay(100000);
    159         PropertiesConfiguration config = new PropertiesConfiguration(TEST_FILE);
    160         config.setReloadingStrategy(strategy);
    161         assertTrue("Reloading not required", strategy.reloadingRequired());
    162         assertTrue("Reloading no more required", strategy.reloadingRequired());
    163         strategy.reloadingPerformed();
    164         assertFalse("Reloading still required", strategy.reloadingRequired());
    165     }
    166 }