Author: oheger
Date: Sun Jun 10 09:10:17 2007
New Revision: 545904

URL: http://svn.apache.org/viewvc?view=rev&rev=545904
Log:
CONFIGURATION-280: Fixed possible data loss for file-based configurations in 
auto-save mode that are associated with a reloading strategy; thanks to Roman 
Kurmanowytsch

Modified:
    
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
    
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java
    jakarta/commons/proper/configuration/trunk/xdocs/changes.xml

Modified: 
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java?view=diff&rev=545904&r1=545903&r2=545904
==============================================================================
--- 
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
 (original)
+++ 
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
 Sun Jun 10 09:10:17 2007
@@ -827,6 +827,8 @@
                         }
                         fireEvent(EVENT_RELOAD, null, getURL(), true);
                         setDetailEvents(false);
+                        boolean autoSaveBak = this.isAutoSave(); // save the 
current state
+                        this.setAutoSave(false); // deactivate autoSave to 
prevent information loss
                         try
                         {
                             clear();
@@ -834,6 +836,7 @@
                         }
                         finally
                         {
+                            this.setAutoSave(autoSaveBak); // set autoSave to 
previous value
                             setDetailEvents(true);
                         }
                         fireEvent(EVENT_RELOAD, null, getURL(), false);

Modified: 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java?view=diff&rev=545904&r1=545903&r2=545904
==============================================================================
--- 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java
 (original)
+++ 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java
 Sun Jun 10 09:10:17 2007
@@ -263,7 +263,7 @@
         // set a new attribute
         conf.setProperty("[EMAIL PROTECTED]", "value");
         assertEquals("[EMAIL PROTECTED]", "value", conf.getProperty("[EMAIL 
PROTECTED]"));
-        
+
         conf.setProperty("name1","value1");
         assertEquals("value1",conf.getProperty("name1"));
     }
@@ -352,11 +352,11 @@
         assertTrue(conf.isEmpty());
         conf.addProperty("test", "yes");
         conf.save();
-        
+
         conf = new XMLConfiguration(testSaveConf);
         assertEquals("yes", conf.getString("test"));
     }
-    
+
     /**
      * Tests loading a configuration from a URL.
      */
@@ -367,7 +367,7 @@
         assertEquals("value", conf.getProperty("element"));
         assertEquals(url, conf.getURL());
     }
-    
+
     /**
      * Tests loading from a stream.
      */
@@ -377,12 +377,12 @@
         conf = new XMLConfiguration();
         conf.load(new ByteArrayInputStream(xml.getBytes()));
         assertEquals(1, conf.getInt("test"));
-        
+
         conf = new XMLConfiguration();
         conf.load(new ByteArrayInputStream(xml.getBytes()), "UTF8");
         assertEquals(1, conf.getInt("test"));
     }
-    
+
     /**
      * Tests loading a non well formed XML from a string.
      */
@@ -439,7 +439,7 @@
         {
            conf.addProperty("[EMAIL PROTECTED]", "value" + i);
         }
-        
+
         // add comma delimited lists with escaped delimiters
         conf.addProperty("split.list5", "a\\,b\\,c");
         conf.setProperty("element3", "value\\,value1\\,value2");
@@ -453,7 +453,7 @@
         checkConfig.setFileName(testSaveConf.getAbsolutePath());
         checkSavedConfig(checkConfig);
     }
-    
+
     /**
      * Tests saving to a URL.
      */
@@ -464,7 +464,7 @@
         checkConfig.setFile(testSaveConf);
         checkSavedConfig(checkConfig);
     }
-    
+
     /**
      * Tests saving to a stream.
      */
@@ -485,11 +485,11 @@
                 out.close();
             }
         }
-        
+
         XMLConfiguration checkConfig = new XMLConfiguration();
         checkConfig.setFile(testSaveConf);
         checkSavedConfig(checkConfig);
-        
+
         try
         {
             out = new FileOutputStream(testSaveConf);
@@ -502,7 +502,7 @@
                 out.close();
             }
         }
-        
+
         checkConfig.clear();
         checkSavedConfig(checkConfig);
     }
@@ -518,13 +518,13 @@
         // reload the configuration
         XMLConfiguration conf2 = new XMLConfiguration(conf.getFile());
         assertEquals("'autosave' property", "ok", conf2.getString("autosave"));
-        
+
         conf.clearTree("clear");
         conf2 = new XMLConfiguration(conf.getFile());
         Configuration sub = conf2.subset("clear");
         assertTrue(sub.isEmpty());
     }
-    
+
     /**
      * Tests if a second file can be appended to a first.
      */
@@ -536,14 +536,14 @@
         conf.load(testProperties2);
         assertEquals("value", conf.getString("element"));
         assertEquals("tasks", conf.getString("table.name"));
-        
+
         conf.save(testSaveConf);
         conf = new XMLConfiguration(testSaveConf);
         assertEquals("value", conf.getString("element"));
         assertEquals("tasks", conf.getString("table.name"));
         assertEquals("application", conf.getString("[EMAIL PROTECTED]"));
     }
-    
+
     /**
      * Tests saving attributes (related to issue 34442).
      */
@@ -749,12 +749,12 @@
         File nonValidFile = new File("conf/testValidateInvalid.xml");
         conf = new XMLConfiguration();
         assertFalse(conf.isValidating());
-        
+
         // Load a non valid XML document. Should work for isValidating() == 
false
         conf.load(nonValidFile);
         assertEquals("customers", conf.getString("table.name"));
         assertFalse(conf.containsKey("table.fields.field(1).type"));
-        
+
         // Now set the validating flag to true
         conf.setValidating(true);
         try
@@ -767,7 +767,7 @@
             //ok
         }
     }
-    
+
     /**
      * Tests handling of empty elements.
      */
@@ -778,12 +778,12 @@
         conf.addProperty("empty2", "");
         conf.setProperty("empty", "no more empty");
         conf.save(testSaveConf);
-        
+
         conf = new XMLConfiguration(testSaveConf);
         assertEquals("no more empty", conf.getString("empty"));
         assertEquals("", conf.getProperty("empty2"));
     }
-    
+
     /**
      * Tests whether the encoding is correctly detected by the XML parser. This
      * is done by loading an XML file with the encoding "UTF-16". If this
@@ -798,7 +798,7 @@
         conf.load(file);
         assertEquals("test3_yoge", conf.getString("yoge"));
     }
-    
+
     /**
      * Tests whether the encoding is written to the generated XML file.
      */
@@ -813,7 +813,7 @@
         assertTrue("Encoding was not written to file", out.toString().indexOf(
                 "encoding=\"" + ENCODING + "\"") >= 0);
     }
-    
+
     /**
      * Tests whether a default encoding is used if no specific encoding is set.
      * According to the XSLT specification (http://www.w3.org/TR/xslt#output)
@@ -938,7 +938,7 @@
         conf.clear();
         assertNull(conf.getString("test[1]/entity/@name"));
     }
-    
+
     /**
      * Tests the copy constructor.
      */
@@ -1132,6 +1132,19 @@
     }
 
     /**
+     * Tests a combination of auto save = true and an associated reloading
+     * strategy.
+     */
+    public void testAutoSaveWithReloadingStrategy() throws 
ConfigurationException
+    {
+        conf.setFile(testSaveConf);
+        conf.save();
+        conf.setReloadingStrategy(new FileAlwaysReloadingStrategy());
+        conf.setAutoSave(true);
+        assertEquals("Value not found", "value", conf.getProperty("element"));
+    }
+
+    /**
      * Prepares a configuration object for testing a reload operation.
      *
      * @return the initialized configuration
@@ -1168,12 +1181,6 @@
     private void checkSavedConfig(FileConfiguration checkConfig) throws 
ConfigurationException
     {
         checkConfig.load();
-
-        for (Iterator i = conf.getKeys(); i.hasNext();)
-        {
-            String key = (String) i.next();
-            assertTrue("The saved configuration doesn't contain the key '" + 
key + "'", checkConfig.containsKey(key));
-            assertEquals("Value of the '" + key + "' property", 
conf.getProperty(key), checkConfig.getProperty(key));
-        }
+        ConfigurationAssert.assertEquals(conf, checkConfig);
     }
 }

Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?view=diff&rev=545904&r1=545903&r2=545904
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Sun Jun 10 
09:10:17 2007
@@ -23,6 +23,11 @@
 
   <body>
     <release version="1.5-SNAPSHOT" date="in SVN" description="">
+      <action dev="oheger" type="fix" issue="CONFIGURATION-280"
+        due-to="Roman Kurmanowytsch">
+        Using file-based configurations in auto-save mode together with a
+        reloading strategy could cause data loss. This has been fixed.
+      </action>
       <action dev="oheger" type="fix" issue="CONFIGURATION-279">
         A PropertiesConfiguration that was created from a non existing file
         lost its content when it was saved. This problem has been solved.



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to