001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.configuration.reloading;
019    
020    import static org.junit.Assert.assertEquals;
021    
022    import java.io.File;
023    import java.io.FileWriter;
024    
025    import org.apache.commons.configuration.PropertiesConfiguration;
026    import org.junit.Test;
027    
028    /**
029     * Test case for the ManagedReloadingStrategy class.
030     *
031     * @author Nicolas De loof
032     * @version $Id: TestManagedReloadingStrategy.java 1225908 2011-12-30 20:04:48Z oheger $
033     */
034    public class TestManagedReloadingStrategy
035    {
036        @Test
037        public void testManagedRefresh() throws Exception
038        {
039            File file = new File("target/testReload.properties");
040            if (file.exists())
041            {
042                file.delete();
043            }
044            // create the configuration file
045            FileWriter out = new FileWriter(file);
046            out.write("string=value1");
047            out.flush();
048            out.close();
049    
050            // load the configuration
051            PropertiesConfiguration config = new PropertiesConfiguration("target/testReload.properties");
052            ManagedReloadingStrategy strategy = new ManagedReloadingStrategy();
053            config.setReloadingStrategy(strategy);
054            assertEquals("Initial value", "value1", config.getString("string"));
055    
056            // change the file
057            out = new FileWriter(file);
058            out.write("string=value2");
059            out.flush();
060            out.close();
061    
062            // test the automatic reloading
063            assertEquals("No automatic reloading", "value1", config.getString("string"));
064            strategy.refresh();
065            assertEquals("Modified value with enabled reloading", "value2", config.getString("string"));
066        }
067    
068    }