Author: oheger
Date: Tue Jun 12 13:04:45 2007
New Revision: 546620

URL: http://svn.apache.org/viewvc?view=rev&rev=546620
Log:
CONFIGURATION-277: AbstractConfiguration.clear() now catches potential 
UnsupportedOperationExceptions during the iteration over the existing properties

Added:
    
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java
   (with props)
Modified:
    
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractConfiguration.java
    jakarta/commons/proper/configuration/trunk/xdocs/changes.xml

Modified: 
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractConfiguration.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractConfiguration.java?view=diff&rev=546620&r1=546619&r2=546620
==============================================================================
--- 
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractConfiguration.java
 (original)
+++ 
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractConfiguration.java
 Tue Jun 12 13:04:45 2007
@@ -504,17 +504,34 @@
     {
         fireEvent(EVENT_CLEAR, null, null, true);
         setDetailEvents(false);
+        boolean useIterator = true;
         try
         {
             Iterator it = getKeys();
             while (it.hasNext())
             {
                 String key = (String) it.next();
-                it.remove();
+                if (useIterator)
+                {
+                    try
+                    {
+                        it.remove();
+                    }
+                    catch (UnsupportedOperationException usoex)
+                    {
+                        useIterator = false;
+                    }
+                }
+
+                if (useIterator && containsKey(key))
+                {
+                    useIterator = false;
+                }
 
-                if (containsKey(key))
+                if (!useIterator)
                 {
-                    // workaround for Iterators that do not remove the 
property on calling remove()
+                    // workaround for Iterators that do not remove the property
+                    // on calling remove() or do not support remove() at all
                     clearProperty(key);
                 }
             }

Added: 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java?view=auto&rev=546620
==============================================================================
--- 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java
 (added)
+++ 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java
 Tue Jun 12 13:04:45 2007
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Iterator;
+
+import org.apache.commons.collections.CollectionUtils;
+
+import junit.framework.TestCase;
+
+/**
+ * A test class for some of the basic functionality implemented by
+ * AbstractConfiguration.
+ *
+ * @version $Id$
+ */
+public class TestAbstractConfigurationBasicFeatures extends TestCase
+{
+    /**
+     * Tests the clear() implementation of AbstractConfiguration if the 
iterator
+     * returned by getKeys() does not support the remove() operation.
+     */
+    public void testClearIteratorNoRemove()
+    {
+        AbstractConfiguration config = new TestConfigurationImpl(
+                new BaseConfiguration())
+        {
+            // return an iterator that does not support remove operations
+            public Iterator getKeys()
+            {
+                Collection keyCol = new ArrayList();
+                CollectionUtils.addAll(keyCol, getUnderlyingConfiguration()
+                        .getKeys());
+                Object[] keys = keyCol.toArray();
+                return Arrays.asList(keys).iterator();
+            }
+        };
+        for (int i = 0; i < 20; i++)
+        {
+            config.addProperty("key" + i, "value" + i);
+        }
+        config.clear();
+        assertTrue("Configuration not empty", config.isEmpty());
+    }
+
+    /**
+     * A test configuration implementation. This implementation inherits
+     * directly from AbstractConfiguration. For implementing the required
+     * functionality another implementation of AbstractConfiguration is used;
+     * all methods that need to be implemented delegate to this wrapped
+     * configuration.
+     */
+    static class TestConfigurationImpl extends AbstractConfiguration
+    {
+        /** Stores the underlying configuration. */
+        private AbstractConfiguration config;
+
+        public AbstractConfiguration getUnderlyingConfiguration()
+        {
+            return config;
+        }
+
+        public TestConfigurationImpl(AbstractConfiguration wrappedConfig)
+        {
+            config = wrappedConfig;
+        }
+
+        protected void addPropertyDirect(String key, Object value)
+        {
+            config.addPropertyDirect(key, value);
+        }
+
+        public boolean containsKey(String key)
+        {
+            return config.containsKey(key);
+        }
+
+        public Iterator getKeys()
+        {
+            return config.getKeys();
+        }
+
+        public Object getProperty(String key)
+        {
+            return config.getProperty(key);
+        }
+
+        public boolean isEmpty()
+        {
+            return config.isEmpty();
+        }
+
+        protected void clearPropertyDirect(String key)
+        {
+            config.clearPropertyDirect(key);
+        }
+    }
+}

Propchange: 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

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=546620&r1=546619&r2=546620
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Tue Jun 12 
13:04:45 2007
@@ -23,6 +23,11 @@
 
   <body>
     <release version="1.5-SNAPSHOT" date="in SVN" description="">
+      <action dev="oheger" type="add" issue="CONFIGURATION-277">
+        The base implementation of clear() in AbstractConfiguration now checks
+        for a potential UnsupportedOperationException when iterating over the
+        existing properties.
+      </action>
       <action dev="oheger" type="fix" issue="CONFIGURATION-280"
         due-to="Roman Kurmanowytsch">
         Using file-based configurations in auto-save mode together with a



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

Reply via email to