Author: oheger Date: Mon Mar 25 19:15:08 2013 New Revision: 1460855 URL: http://svn.apache.org/r1460855 Log: Reworked ManagedReloadingStrategy.
It now implements the ReloadingDetector interface. Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/reloading/ManagedReloadingStrategy.java commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/reloading/TestManagedReloadingStrategy.java Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/reloading/ManagedReloadingStrategy.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/reloading/ManagedReloadingStrategy.java?rev=1460855&r1=1460854&r2=1460855&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/reloading/ManagedReloadingStrategy.java (original) +++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/reloading/ManagedReloadingStrategy.java Mon Mar 25 19:15:08 2013 @@ -17,7 +17,6 @@ package org.apache.commons.configuration.reloading; -import org.apache.commons.configuration.FileConfiguration; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -28,24 +27,14 @@ import org.apache.commons.logging.LogFac * @author Nicolas De loof * @version $Id$ */ -public class ManagedReloadingStrategy implements ReloadingStrategy, +public class ManagedReloadingStrategy implements ReloadingDetector, ManagedReloadingStrategyMBean { /** The logger. */ private Log log = LogFactory.getLog(ManagedReloadingStrategy.class); - /** Stores a reference to the associated configuration. */ - private FileConfiguration configuration; - /** A flag whether a reload is required. */ - private boolean reloadingRequired; - - /** - * @see org.apache.commons.configuration.reloading.ReloadingStrategy#init() - */ - public void init() - { - } + private volatile boolean reloadingRequired; /** * @see org.apache.commons.configuration.reloading.ReloadingStrategy#reloadingPerformed() @@ -62,22 +51,12 @@ public class ManagedReloadingStrategy im * @return a flag whether reloading is required * @see org.apache.commons.configuration.reloading.ReloadingStrategy#reloadingRequired() */ - public boolean reloadingRequired() + public boolean isReloadingRequired() { return reloadingRequired; } /** - * Sets the associated configuration. - * - * @param configuration the associated configuration - */ - public void setConfiguration(FileConfiguration configuration) - { - this.configuration = configuration; - } - - /** * Tells this strategy that the monitored configuration file should be * refreshed. This method will typically be called from outside (through an * exposed MBean) on behalf of an administrator. @@ -87,8 +66,6 @@ public class ManagedReloadingStrategy im public void refresh() { log.info("Reloading configuration."); - this.reloadingRequired = true; - // force reloading - configuration.isEmpty(); + reloadingRequired = true; } } Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/reloading/TestManagedReloadingStrategy.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/reloading/TestManagedReloadingStrategy.java?rev=1460855&r1=1460854&r2=1460855&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/reloading/TestManagedReloadingStrategy.java (original) +++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/reloading/TestManagedReloadingStrategy.java Mon Mar 25 19:15:08 2013 @@ -17,15 +17,11 @@ package org.apache.commons.configuration.reloading; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; - -import org.apache.commons.configuration.XMLConfiguration; +import org.junit.Before; import org.junit.Test; -import org.junit.rules.TemporaryFolder; /** * Test case for the ManagedReloadingStrategy class. @@ -35,53 +31,45 @@ import org.junit.rules.TemporaryFolder; */ public class TestManagedReloadingStrategy { - /** Constant for the name of the test property. */ - private static final String PROPERTY = "string"; - - /** Constant for the XML fragment to be written. */ - private static final String FMT_XML = "<configuration><" + PROPERTY - + ">%s</" + PROPERTY + "></configuration>"; + /** The instance to be tested. */ + private ManagedReloadingStrategy strategy; - /** A helper object for creating temporary files. */ - public TemporaryFolder folder = new TemporaryFolder(); + @Before + public void setUp() throws Exception + { + strategy = new ManagedReloadingStrategy(); + } /** - * Writes a test configuration file containing a single property with the - * given value. - * - * @param file the file to be written - * @param value the value of the test property - * @throws IOException if an error occurs + * Tests the result of isReloadingRequired() for a newly created instance. */ - private void writeTestFile(File file, String value) throws IOException + @Test + public void testReloadingRequiredInitial() { - FileWriter out = new FileWriter(file); - out.write(String.format(FMT_XML, value)); - out.close(); + assertFalse("Wrong result", strategy.isReloadingRequired()); } + /** + * Tests the refresh() method. + */ @Test - public void testManagedRefresh() throws Exception + public void testRefresh() { - File file = folder.newFile(); - // create the configuration file - writeTestFile(file, "value1"); - - // load the configuration - XMLConfiguration config = new XMLConfiguration(); - config.setFile(file); - config.load(); - ManagedReloadingStrategy strategy = new ManagedReloadingStrategy(); - config.setReloadingStrategy(strategy); - assertEquals("Initial value", "value1", config.getString(PROPERTY)); - - // change the file - writeTestFile(file, "value2"); - - // test the automatic reloading - assertEquals("No automatic reloading", "value1", config.getString(PROPERTY)); strategy.refresh(); - assertEquals("Modified value with enabled reloading", "value2", config.getString(PROPERTY)); + assertTrue("Reloading request not detected", + strategy.isReloadingRequired()); + assertTrue("Reloading state not permanent", + strategy.isReloadingRequired()); } + /** + * Tests whether the reloading state can be reset again. + */ + @Test + public void testReloadingPerformed() + { + strategy.refresh(); + strategy.reloadingPerformed(); + assertFalse("Reloading state not reset", strategy.isReloadingRequired()); + } }