commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r705912 - in /commons/proper/configuration/trunk: src/java/org/apache/commons/configuration/CombinedConfiguration.java src/test/org/apache/commons/configuration/TestCombinedConfiguration.java xdocs/changes.xml
Date Sat, 18 Oct 2008 19:55:42 GMT
Author: oheger
Date: Sat Oct 18 12:55:41 2008
New Revision: 705912

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

Modified:
    commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CombinedConfiguration.java
    commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCombinedConfiguration.java
    commons/proper/configuration/trunk/xdocs/changes.xml

Modified: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CombinedConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CombinedConfiguration.java?rev=705912&r1=705911&r2=705912&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CombinedConfiguration.java
(original)
+++ commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CombinedConfiguration.java
Sat Oct 18 12:55:41 2008
@@ -548,40 +548,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 (Iterator it = configurations.iterator(); it.hasNext();)
-            {
-                try
-                {
-                    // simply retrieve a property; this is enough for
-                    // triggering a reload
-                    ((ConfigData) it.next()).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:
@@ -635,6 +601,53 @@
     }
 
     /**
+     * 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
+     */
+    protected List 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)
+     * @since 1.6
+     */
+    protected void performReloadCheck()
+    {
+        for (Iterator it = configurations.iterator(); it.hasNext();)
+        {
+            try
+            {
+                // simply retrieve a property; this is enough for
+                // triggering a reload
+                ((ConfigData) it.next()).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/trunk/src/test/org/apache/commons/configuration/TestCombinedConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCombinedConfiguration.java?rev=705912&r1=705911&r2=705912&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCombinedConfiguration.java
(original)
+++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCombinedConfiguration.java
Sat Oct 18 12:55:41 2008
@@ -20,18 +20,21 @@
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.text.MessageFormat;
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Iterator;
 import java.util.Set;
 
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
 import org.apache.commons.configuration.event.ConfigurationEvent;
 import org.apache.commons.configuration.event.ConfigurationListener;
 import org.apache.commons.configuration.reloading.FileAlwaysReloadingStrategy;
 import org.apache.commons.configuration.tree.NodeCombiner;
 import org.apache.commons.configuration.tree.UnionCombiner;
 
-import junit.framework.Assert;
-import junit.framework.TestCase;
-
 /**
  * Test class for CombinedConfiguration.
  *
@@ -40,22 +43,40 @@
 public class TestCombinedConfiguration extends TestCase
 {
     /** Constant for the name of a sub configuration. */
-    static final String TEST_NAME = "SUBCONFIG";
+    private static final String TEST_NAME = "SUBCONFIG";
 
     /** Constant for a test key. */
-    static final String TEST_KEY = "test.value";
+    private static final String TEST_KEY = "test.value";
 
     /** Constant for the name of the first child configuration.*/
-    static final String CHILD1 = TEST_NAME + "1";
+    private static final String CHILD1 = TEST_NAME + "1";
 
     /** Constant for the name of the second child configuration.*/
-    static final String CHILD2 = TEST_NAME + "2";
+    private static final String CHILD2 = TEST_NAME + "2";
+
+    /** Constant for the name of the XML reload test file.*/
+    private static final String RELOAD_XML_NAME = "reload.xml";
+
+    /** Constant for the content of a XML reload test file.*/
+    private static final String RELOAD_XML_CONTENT = "<xml><xmlReload>{0}</xmlReload></xml>";
+
+    /** Constant for the name of the properties reload test file.*/
+    private static final String RELOAD_PROPS_NAME = "reload.properties";
+
+    /** Constant for the content of a properties reload test file.*/
+    private static final String RELOAD_PROPS_CONTENT = "propsReload = {0}";
+
+    /** 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 testFiles;
 
     /** The configuration to be tested. */
-    CombinedConfiguration config;
+    private CombinedConfiguration config;
 
     /** The test event listener. */
-    CombinedListener listener;
+    private CombinedListener listener;
 
     protected void setUp() throws Exception
     {
@@ -66,6 +87,25 @@
     }
 
     /**
+     * Performs clean-up after a test run. If test files have been created, they
+     * are removed now.
+     */
+    protected void tearDown() throws Exception
+    {
+        if (testFiles != null)
+        {
+            for (Iterator it = testFiles.iterator(); it.hasNext();)
+            {
+                File f = (File) it.next();
+                if (f.exists())
+                {
+                    assertTrue("Cannot delete test file: " + f, f.delete());
+                }
+            }
+        }
+    }
+
+    /**
      * Tests accessing a newly created combined configuration.
      */
     public void testInit()
@@ -422,11 +462,8 @@
     public void testReloading() throws Exception
     {
         config.setForceReloadCheck(true);
-        File testDir = new File("target");
-        File testXmlFile = new File(testDir, "reload.xml");
-        File testPropsFile = new File(testDir, "reload.properties");
-        writeFile(testXmlFile, "<xml><xmlReload>0</xmlReload></xml>");
-        writeFile(testPropsFile, "propsReload = 0");
+        File testXmlFile = writeReloadFile(RELOAD_XML_NAME, RELOAD_XML_CONTENT, 0);
+        File testPropsFile = writeReloadFile(RELOAD_PROPS_NAME, RELOAD_PROPS_CONTENT, 0);
         XMLConfiguration c1 = new XMLConfiguration(testXmlFile);
         c1.setReloadingStrategy(new FileAlwaysReloadingStrategy());
         PropertiesConfiguration c2 = new PropertiesConfiguration(testPropsFile);
@@ -438,15 +475,31 @@
         assertEquals("Wrong props reload value", 0, config
                 .getInt("propsReload"));
 
-        writeFile(testXmlFile, "<xml><xmlReload>1</xmlReload></xml>");
+        writeReloadFile(RELOAD_XML_NAME, RELOAD_XML_CONTENT, 1);
         assertEquals("XML reload not detected", 1, config.getInt("xmlReload"));
         config.setForceReloadCheck(false);
-        writeFile(testPropsFile, "propsReload = 1");
+        writeReloadFile(RELOAD_PROPS_NAME, RELOAD_PROPS_CONTENT, 1);
         assertEquals("Props reload detected though check flag is false", 0, config
                 .getInt("propsReload"));
+    }
 
-        assertTrue("XML file cannot be removed", testXmlFile.delete());
-        assertTrue("Props file cannot be removed", testPropsFile.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_XML_NAME, RELOAD_XML_CONTENT,
+                0);
+        XMLConfiguration c1 = new XMLConfiguration(testXmlFile);
+        c1.setReloadingStrategy(new FileAlwaysReloadingStrategy());
+        final String prefix = "reloadCheck";
+        config.addConfiguration(c1, CHILD1, prefix);
+        SubnodeConfiguration sub = config.configurationAt(prefix, true);
+        writeReloadFile(RELOAD_XML_NAME, RELOAD_XML_CONTENT, 1);
+        assertEquals("Reload not detected", 1, sub.getInt("xmlReload"));
     }
 
     /**
@@ -585,19 +638,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();
+            }
+            testFiles.add(file);
         }
         finally
         {
@@ -609,6 +669,40 @@
     }
 
     /**
+     * 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, String content, int value)
+            throws IOException
+    {
+        return writeFile(name, MessageFormat.format(content, new Object[] {
+            new Integer(value)
+        }));
+    }
+
+    /**
      * Helper method for creating a test configuration to be added to the
      * combined configuration.
      *

Modified: commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/xdocs/changes.xml?rev=705912&r1=705911&r2=705912&view=diff
==============================================================================
--- commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ commons/proper/configuration/trunk/xdocs/changes.xml Sat Oct 18 12:55:41 2008
@@ -23,6 +23,10 @@
 
   <body>
     <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