~ubuntu-branches/ubuntu/saucy/commons-configuration/saucy

« back to all changes in this revision

Viewing changes to src/test/org/apache/commons/configuration/reloading/TestVFSFileChangedReloadingStrategy.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2013-07-01 16:29:44 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20130701162944-98waq5gogha5gpn1
Tags: 1.9-1
* New upstream release
* debian/control:
  - Updated Standards-Version to 3.9.4 (no changes)
  - Use canonical URLs for the Vcs-* fields
  - Added new build dependencies (libjavacc-maven-plugin-java, junit4)
  - Upgraded the dependency on the Servlet API (2.5 -> 3.0)
  - Removed the dependency on the Activation Framework (glassfish-activation)
  - Replaced the dependency on glassfish-mail with libgnumail-java
  - Removed the unused dependencies:
    liblog4j1.2-java-doc, libmaven-assembly-plugin-java
  - Replaced the dependency on libcommons-jexl-java by libcommons-jexl2-java
* debian/watch: Changed to point the official Apache distribution server
* Removed the obsolete file debian/ant.properties
* Installed the upstream changelog in the binary packages
* Added the report plugins to maven.ignoreRules
* Added the classpath attribute to the jar manifest

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 junit.framework.TestCase;
24
 
 
25
 
import org.apache.commons.configuration.ConfigurationException;
26
 
import org.apache.commons.configuration.PropertiesConfiguration;
27
 
import org.apache.commons.configuration.FileSystem;
28
 
import org.apache.commons.configuration.VFSFileSystem;
29
 
 
30
 
/**
31
 
 * Test case for the VFSFileMonitorReloadingStrategy class.
32
 
 *
33
 
 * @author Ralph Goers
34
 
 * @version $Revision: 823891 $
35
 
 */
36
 
public class TestVFSFileChangedReloadingStrategy extends TestCase
37
 
{
38
 
    /** Constant for the name of a test properties file.*/
39
 
    private static final String TEST_FILE = "test.properties";
40
 
 
41
 
    protected void setUp() throws Exception
42
 
    {
43
 
        super.setUp();
44
 
        FileSystem.setDefaultFileSystem(new VFSFileSystem());
45
 
    }
46
 
 
47
 
    protected void tearDown() throws Exception
48
 
    {
49
 
        FileSystem.resetDefaultFileSystem();
50
 
        super.tearDown();
51
 
    }
52
 
 
53
 
    public void testAutomaticReloading() throws Exception
54
 
    {
55
 
        // create a new configuration
56
 
        File file = new File("target/testReload.properties");
57
 
 
58
 
        if (file.exists())
59
 
        {
60
 
            file.delete();
61
 
        }
62
 
 
63
 
        // create the configuration file
64
 
        FileWriter out = new FileWriter(file);
65
 
        out.write("string=value1");
66
 
        out.flush();
67
 
        out.close();
68
 
 
69
 
        // load the configuration
70
 
        PropertiesConfiguration config = new PropertiesConfiguration("target/testReload.properties");
71
 
        VFSFileChangedReloadingStrategy strategy = new VFSFileChangedReloadingStrategy();
72
 
        strategy.setRefreshDelay(500);
73
 
        config.setReloadingStrategy(strategy);
74
 
        assertEquals("Initial value", "value1", config.getString("string"));
75
 
 
76
 
        Thread.sleep(2000);
77
 
 
78
 
        // change the file
79
 
        out = new FileWriter(file);
80
 
        out.write("string=value2");
81
 
        out.flush();
82
 
        out.close();
83
 
 
84
 
        // test the automatic reloading
85
 
        assertEquals("Modified value with enabled reloading", "value2", config.getString("string"));
86
 
    }
87
 
 
88
 
    public void testNewFileReloading() throws Exception
89
 
    {
90
 
        // create a new configuration
91
 
        File file = new File("target/testReload.properties");
92
 
 
93
 
        if (file.exists())
94
 
        {
95
 
            file.delete();
96
 
        }
97
 
 
98
 
        PropertiesConfiguration config = new PropertiesConfiguration();
99
 
        config.setFile(file);
100
 
        VFSFileChangedReloadingStrategy strategy = new VFSFileChangedReloadingStrategy();
101
 
        strategy.setRefreshDelay(500);
102
 
        config.setReloadingStrategy(strategy);
103
 
 
104
 
        assertNull("Initial value", config.getString("string"));
105
 
 
106
 
        // change the file
107
 
        FileWriter out = new FileWriter(file);
108
 
        out.write("string=value1");
109
 
        out.flush();
110
 
        out.close();
111
 
 
112
 
        Thread.sleep(2000);
113
 
 
114
 
        // test the automatic reloading
115
 
        assertEquals("Modified value with enabled reloading", "value1", config.getString("string"));
116
 
    }
117
 
 
118
 
    public void testGetRefreshDelay() throws Exception
119
 
    {
120
 
        VFSFileChangedReloadingStrategy strategy = new VFSFileChangedReloadingStrategy();
121
 
        strategy.setRefreshDelay(500);
122
 
        assertEquals("refresh delay", 500, strategy.getRefreshDelay());
123
 
    }
124
 
 
125
 
    /**
126
 
     * Tests calling reloadingRequired() multiple times before a reload actually
127
 
     * happens. This test is related to CONFIGURATION-302.
128
 
     */
129
 
    public void testReloadingRequiredMultipleTimes()
130
 
            throws ConfigurationException
131
 
    {
132
 
        VFSFileChangedReloadingStrategy strategy = new VFSFileChangedReloadingStrategy()
133
 
        {
134
 
            protected boolean hasChanged()
135
 
            {
136
 
                // signal always a change
137
 
                return true;
138
 
            }
139
 
        };
140
 
        strategy.setRefreshDelay(100000);
141
 
        PropertiesConfiguration config = new PropertiesConfiguration(TEST_FILE);
142
 
        config.setReloadingStrategy(strategy);
143
 
        assertTrue("Reloading not required", strategy.reloadingRequired());
144
 
        assertTrue("Reloading no more required", strategy.reloadingRequired());
145
 
        strategy.reloadingPerformed();
146
 
        assertFalse("Reloading still required", strategy.reloadingRequired());
147
 
    }
148
 
}
 
 
b'\\ No newline at end of file'