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