commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r706393 - in /commons/proper/configuration/branches/configuration2_experimental: src/main/java/org/apache/commons/configuration2/combined/ src/test/java/org/apache/commons/configuration2/combined/ xdocs/
Date Mon, 20 Oct 2008 19:55:28 GMT
Author: oheger
Date: Mon Oct 20 12:55:28 2008
New Revision: 706393

URL: http://svn.apache.org/viewvc?rev=706393&view=rev
Log:
CONFIGURATION-341: SubnodeConfigurations of a CombinedConfiguration now also trigger the reload
mechanism. Ported patch to configuration2 branch.

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/CombinedConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestCombinedConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/CombinedConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/CombinedConfiguration.java?rev=706393&r1=706392&r2=706393&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/CombinedConfiguration.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/CombinedConfiguration.java
Mon Oct 20 12:55:28 2008
@@ -521,6 +521,7 @@
     /**
      * Clears this configuration. All contained configurations will be removed.
      */
+    @Override
     public void clear()
     {
         fireEvent(EVENT_CLEAR, null, null, true);
@@ -539,6 +540,7 @@
      *
      * @return the copied object
      */
+    @Override
     public Object clone()
     {
         try
@@ -565,39 +567,6 @@
     }
 
     /**
-     * Returns the value of the specified property. This implementation
-     * evaluates the <em>force reload check</em> flag. If it is set, all
-     * contained configurations will be triggered before the value of the
-     * requested property is retrieved.
-     *
-     * @param key the key of the desired property
-     * @return the value of this property
-     * @since 1.4
-     */
-    public Object getProperty(String key)
-    {
-        if (isForceReloadCheck())
-        {
-            for (ConfigData<?> cd : configurations)
-            {
-                try
-                {
-                    // simply retrieve a property; this is enough for
-                    // triggering a reload
-                    cd.getConfiguration().getProperty(PROP_RELOAD_CHECK);
-                }
-                catch (Exception ex)
-                {
-                    // ignore all exceptions, e.g. missing property exceptions
-                    ;
-                }
-            }
-        }
-
-        return super.getProperty(key);
-    }
-
-    /**
      * Returns the configuration source, in which the specified key is defined.
      * This method will determine the configuration node that is identified by
      * the given key. The following constellations are possible:
@@ -669,6 +638,52 @@
     }
 
     /**
+     * Evaluates the passed in property key and returns a list with the matching
+     * configuration nodes. This implementation also evaluates the
+     * <em>force reload check</em> flag. If it is set,
+     * <code>performReloadCheck()</code> is invoked.
+     *
+     * @param key the property key
+     * @return a list with the matching configuration nodes
+     */
+    @Override
+    protected NodeList<Object> fetchNodeList(String key)
+    {
+        if (isForceReloadCheck())
+        {
+            performReloadCheck();
+        }
+
+        return super.fetchNodeList(key);
+    }
+
+    /**
+     * Triggers the contained configurations to perform a reload check if
+     * necessary. This method is called when a property of this combined
+     * configuration is accessed and the <code>forceReloadCheck</code> property
+     * is set to <b>true</b>.
+     *
+     * @see #setForceReloadCheck(boolean)
+     */
+    protected void performReloadCheck()
+    {
+        for (ConfigData<?> cd : configurations)
+        {
+            try
+            {
+                // simply retrieve a property; this is enough for
+                // triggering a reload
+                cd.getConfiguration().getProperty(PROP_RELOAD_CHECK);
+            }
+            catch (Exception ex)
+            {
+                // ignore all exceptions, e.g. missing property exceptions
+                ;
+            }
+        }
+    }
+
+    /**
      * Creates the root node of this combined configuration.
      *
      * @return the combined root node

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestCombinedConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestCombinedConfiguration.java?rev=706393&r1=706392&r2=706393&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestCombinedConfiguration.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestCombinedConfiguration.java
Mon Oct 20 12:55:28 2008
@@ -20,23 +20,25 @@
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Set;
 
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
 import org.apache.commons.configuration2.AbstractHierarchicalConfiguration;
 import org.apache.commons.configuration2.ConfigurationAssert;
 import org.apache.commons.configuration2.ConfigurationException;
 import org.apache.commons.configuration2.ConfigurationRuntimeException;
 import org.apache.commons.configuration2.InMemoryConfiguration;
 import org.apache.commons.configuration2.StrictConfigurationComparator;
+import org.apache.commons.configuration2.SubConfiguration;
 import org.apache.commons.configuration2.XMLConfiguration;
 import org.apache.commons.configuration2.event.ConfigurationEvent;
 import org.apache.commons.configuration2.event.ConfigurationListener;
 import org.apache.commons.configuration2.reloading.FileAlwaysReloadingStrategy;
 
-import junit.framework.Assert;
-import junit.framework.TestCase;
-
 /**
  * Test class for CombinedConfiguration.
  *
@@ -56,6 +58,21 @@
     /** Constant for the name of the second child configuration.*/
     private static final String CHILD2 = TEST_NAME + "2";
 
+    /** Constant for the name of the XML reload test file.*/
+    private static final String RELOAD_NAME1 = "reload.xml";
+
+    /** Constant for the content of a XML reload test file.*/
+    private static final String RELOAD_CONTENT = "<xml><xmlReload>%d</xmlReload></xml>";
+
+    /** Constant for the name of the properties reload test file.*/
+    private static final String RELOAD_NAME2 = "reload2.xml";
+
+    /** Constant for the directory for writing test files.*/
+    private static final File TEST_DIR = new File("target");
+
+    /** A list with files created during a test.*/
+    private Collection<File> testFiles;
+
     /** The configuration to be tested. */
     private CombinedConfiguration config;
 
@@ -72,6 +89,25 @@
     }
 
     /**
+     * Performs clean-up after a test run. If test files have been created, they
+     * are removed now.
+     */
+    @Override
+    protected void tearDown() throws Exception
+    {
+        if (testFiles != null)
+        {
+            for (File f : testFiles)
+            {
+                if (f.exists())
+                {
+                    assertTrue("Cannot delete test file: " + f, f.delete());
+                }
+            }
+        }
+    }
+
+    /**
      * Tests accessing a newly created combined configuration.
      */
     public void testInit()
@@ -426,31 +462,48 @@
      */
     public void testReloading() throws Exception
     {
+        final String prefix1 = "reload1";
+        final String prefix2 = "reload2";
         config.setForceReloadCheck(true);
-        File testDir = new File("target");
-        File testXmlFile = new File(testDir, "reload.xml");
-        File testXmlFile2 = new File(testDir, "reload2.xml");
-        writeFile(testXmlFile, "<xml><xmlReload>0</xmlReload></xml>");
-        writeFile(testXmlFile2, "<xml><xmlReload2>0</xmlReload2></xml>");
+        File testXmlFile = writeReloadFile(RELOAD_NAME1, 0);
+        File testXmlFile2 = writeReloadFile(RELOAD_NAME2, 0);
         XMLConfiguration c1 = new XMLConfiguration(testXmlFile);
         c1.setReloadingStrategy(new FileAlwaysReloadingStrategy());
         XMLConfiguration c2 = new XMLConfiguration(testXmlFile2);
         c2.setThrowExceptionOnMissing(true);
         c2.setReloadingStrategy(new FileAlwaysReloadingStrategy());
-        config.addConfiguration(c1);
-        config.addConfiguration(c2);
-        assertEquals("Wrong xml 1 reload value", 0, config.getInt("xmlReload"));
-        assertEquals("Wrong xml 2 reload value", 0, config.getInt("xmlReload2"));
-
-        writeFile(testXmlFile, "<xml><xmlReload>1</xmlReload></xml>");
-        assertEquals("XML reload 1 not detected", 1, config.getInt("xmlReload"));
+        config.addConfiguration(c1, CHILD1, prefix1);
+        config.addConfiguration(c2, CHILD2, prefix2);
+        assertEquals("Wrong xml 1 reload value", 0, config.getInt(prefix1
+                + ".xmlReload"));
+        assertEquals("Wrong xml 2 reload value", 0, config.getInt(prefix2
+                + ".xmlReload"));
+
+        writeReloadFile(RELOAD_NAME1, 1);
+        assertEquals("XML reload 1 not detected", 1, config.getInt(prefix1
+                + ".xmlReload"));
         config.setForceReloadCheck(false);
-        writeFile(testXmlFile2, "<xml><xmlReload2>1</xmlReload2></xml>");
-        assertEquals("XML 2 reload detected though check flag is false", 0, config
-                .getInt("xmlReload2"));
+        writeReloadFile(RELOAD_NAME2, 1);
+        assertEquals("XML 2 reload detected though check flag is false", 0,
+                config.getInt(prefix2 + ".xmlReload"));
+    }
 
-        assertTrue("XML file cannot be removed", testXmlFile.delete());
-        assertTrue("Props file cannot be removed", testXmlFile2.delete());
+    /**
+     * Tests whether the reload check works with a subnode configuration. This
+     * test is related to CONFIGURATION-341.
+     */
+    public void testReloadingSubnodeConfig() throws IOException,
+            ConfigurationException
+    {
+        config.setForceReloadCheck(true);
+        File testXmlFile = writeReloadFile(RELOAD_NAME1, 0);
+        XMLConfiguration c1 = new XMLConfiguration(testXmlFile);
+        c1.setReloadingStrategy(new FileAlwaysReloadingStrategy());
+        final String prefix = "reloadCheck";
+        config.addConfiguration(c1, CHILD1, prefix);
+        SubConfiguration<?> sub = config.configurationAt(prefix, true);
+        writeReloadFile(RELOAD_NAME1, 1);
+        assertEquals("Reload not detected", 1, sub.getInt("xmlReload"));
     }
 
     /**
@@ -596,19 +649,26 @@
     }
 
     /**
-     * Helper method for writing a file.
+     * Helper method for writing a file. The file is also added to a list and
+     * will be deleted in teadDown() automatically.
      *
      * @param file the file to be written
      * @param content the file's content
      * @throws IOException if an error occurs
      */
-    private static void writeFile(File file, String content) throws IOException
+    private void writeFile(File file, String content) throws IOException
     {
         PrintWriter out = null;
         try
         {
             out = new PrintWriter(new FileWriter(file));
             out.print(content);
+
+            if (testFiles == null)
+            {
+                testFiles = new ArrayList<File>();
+            }
+            testFiles.add(file);
         }
         finally
         {
@@ -620,6 +680,38 @@
     }
 
     /**
+     * Helper method for writing a test file. The file will be created in the
+     * test directory. It is also scheduled for automatic deletion after the
+     * test.
+     *
+     * @param fileName the name of the test file
+     * @param content the content of the file
+     * @return the <code>File</code> object for the test file
+     * @throws IOException if an error occurs
+     */
+    private File writeFile(String fileName, String content) throws IOException
+    {
+        File file = new File(TEST_DIR, fileName);
+        writeFile(file, content);
+        return file;
+    }
+
+    /**
+     * Writes a file for testing reload operations.
+     *
+     * @param name the name of the reload test file
+     * @param content the content of the file
+     * @param value the value of the reload test property
+     * @return the file that was written
+     * @throws IOException if an error occurs
+     */
+    private File writeReloadFile(String name, int value)
+            throws IOException
+    {
+        return writeFile(name, String.format(RELOAD_CONTENT, value));
+    }
+
+    /**
      * Helper method for creating a test configuration to be added to the
      * combined configuration.
      *

Modified: commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml?rev=706393&r1=706392&r2=706393&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml (original)
+++ commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml Mon
Oct 20 12:55:28 2008
@@ -85,6 +85,10 @@
     </release>
 
     <release version="1.6" date="in SVN" description="">
+      <action dev="oheger" type="fix" issue="CONFIGURATION-341">
+        The "force reload check" mechanism of CombinedConfiguration now also
+        works with sub configurations created by configurationAt().
+      </action>
       <action dev="oheger" type="fix" issue="CONFIGURATION-339">
         When performing interpolation the methods getList() and getStringArray()
         of CompositeConfiguration did not take the order of child configurations



Mime
View raw message