You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2009/08/04 22:26:10 UTC

svn commit: r800948 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/ main/java/org/apache/commons/configuration2/flat/ test/java/org/apache/commons/configuration2/ test/java/org/a...

Author: oheger
Date: Tue Aug  4 20:26:09 2009
New Revision: 800948

URL: http://svn.apache.org/viewvc?rev=800948&view=rev
Log:
AbstractFlatConfiguration does not need special methods for manipulating the single values of a property any more. So the class could be simplified.

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertiesConfigurationLayout.java
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/AbstractFlatConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPropertiesConfigurationLayout.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/FlatConfigurationMockImpl.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestAbstractFlatConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestBaseConfiguration.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertiesConfigurationLayout.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertiesConfigurationLayout.java?rev=800948&r1=800947&r2=800948&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertiesConfigurationLayout.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertiesConfigurationLayout.java Tue Aug  4 20:26:09 2009
@@ -26,7 +26,6 @@
 
 import org.apache.commons.configuration2.event.ConfigurationEvent;
 import org.apache.commons.configuration2.event.ConfigurationListener;
-import org.apache.commons.configuration2.flat.AbstractFlatConfiguration;
 import org.apache.commons.lang.StringUtils;
 
 /**
@@ -617,7 +616,6 @@
                 clear();
                 break;
             case AbstractConfiguration.EVENT_SET_PROPERTY:
-            case AbstractFlatConfiguration.EVENT_PROPERTY_CHANGED:
                 fetchLayoutData(event.getPropertyName());
                 break;
             }

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/AbstractFlatConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/AbstractFlatConfiguration.java?rev=800948&r1=800947&r2=800948&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/AbstractFlatConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/AbstractFlatConfiguration.java Tue Aug  4 20:26:09 2009
@@ -18,7 +18,6 @@
 
 import java.util.Collection;
 import java.util.Iterator;
-import java.util.List;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 
@@ -66,13 +65,6 @@
  */
 public abstract class AbstractFlatConfiguration extends AbstractConfiguration
 {
-    /**
-     * Constant for the property change event. This event is triggered by
-     * <code>setPropertyValue()</code> and <code>clearPropertyValue()</code>,
-     * which manipulate a value of a property with multiple values.
-     */
-    public static final int EVENT_PROPERTY_CHANGED = 9;
-
     /** Stores the <code>NodeHandler</code> used by this configuration. */
     private FlatNodeHandler nodeHandler;
 
@@ -104,73 +96,6 @@
     }
 
     /**
-     * Modifies a specific value of a property with multiple values. If a
-     * property has multiple values, this method can be used to alter a specific
-     * value (identified by its 0-based index) without affecting the other
-     * values. If the index is invalid (i.e. less than 0 or greater than the
-     * number of existing values), the value will be added to the existing
-     * values of this property. Note that this method expects a scalar value
-     * rather than an array or a collection. In the latter case the behavior is
-     * undefined and may vary for different derived classes. This method takes
-     * care of firing the appropriate events and delegates to
-     * <code>setPropertyValueDirect()</code>. It generates a
-     * <code>EVENT_PROPERTY_CHANGED</code> event that contains the key of the
-     * affected property.
-     *
-     * @param key the key of the property
-     * @param index the index of the value to change
-     * @param value the new value; this should be a simple object; arrays or
-     *        collections won't be treated specially, but directly added
-     */
-    public void setPropertyValue(String key, int index, Object value)
-    {
-        fireEvent(EVENT_PROPERTY_CHANGED, key, null, true);
-        setDetailEvents(true);
-        try
-        {
-            setPropertyValueDirect(key, index, value);
-        }
-        finally
-        {
-            setDetailEvents(false);
-        }
-        fireEvent(EVENT_PROPERTY_CHANGED, key, null, false);
-    }
-
-    /**
-     * Removes a specific value of a property with multiple values. If a
-     * property has multiple values, this method can be used for removing a
-     * single value (identified by its 0-based index). If the index is out of
-     * range, no action is performed; in this case <b>false</b> is returned.
-     * This method takes care of firing the appropriate events and delegates to
-     * <code>clearPropertyValueDirect()</code>. It generates a
-     * <code>EVENT_PROPERTY_CHANGED</code> event that contains the key of the
-     * affected property.
-     *
-     * @param key the key of the property
-     * @param index the index of the value to delete
-     * @return a flag whether the value could be removed
-     */
-    public boolean clearPropertyValue(String key, int index)
-    {
-        fireEvent(EVENT_PROPERTY_CHANGED, key, null, true);
-
-        boolean result = false;
-        setDetailEvents(true);
-        try
-        {
-            result = clearPropertyValueDirect(key, index);
-        }
-        finally
-        {
-            setDetailEvents(false);
-        }
-
-        fireEvent(EVENT_PROPERTY_CHANGED, key, null, false);
-        return result;
-    }
-
-    /**
      * Returns the root node of this configuration. A node hierarchy for this
      * configuration (consisting of <code>FlatNode</code> objects) is created
      * on demand. This method returns the root node of this hierarchy. It checks
@@ -240,7 +165,7 @@
         {
             return -1;
         }
-        else if (value instanceof Collection)
+        else if (value instanceof Collection<?>)
         {
             return ((Collection<?>) value).size() - 1;
         }
@@ -295,53 +220,6 @@
     }
 
     /**
-     * Performs the actual modification of the specified property value. This
-     * method is called by <code>setPropertyValue()</code>. The base
-     * implementation provided by this class uses {@code getProperty()} for
-     * obtaining the current value(s) of the specified property. If the property
-     * does not exist or the index is invalid, the value is added as if
-     * <code>addProperty()</code> was called. If the property has a single
-     * value, the passed in index determines what happens: if it is 0, the
-     * single value is replaced by the new one; all other indices cause the new
-     * value to be added to the old one. The method delegates to {@code
-     * setProperty()} or {@code addPropertyDirect()} for storing the new
-     * property value. Derived classes should override it if they can provide a
-     * more efficient implementation.
-     *
-     * @param key the key of the property
-     * @param index the index of the value to change
-     * @param value the new value
-     */
-    protected void setPropertyValueDirect(String key, int index, Object value)
-    {
-        Object oldValue = getProperty(key);
-
-        if (oldValue instanceof List)
-        {
-            @SuppressWarnings("unchecked")
-            List<Object> col = (List<Object>) oldValue;
-            if (index >= 0 && index < col.size())
-            {
-                col.set(index, value);
-                setProperty(key, col);
-            }
-            else
-            {
-                addPropertyDirect(key, value);
-            }
-        }
-
-        else if (oldValue == null || index != 0)
-        {
-            addPropertyDirect(key, value);
-        }
-        else
-        {
-            setProperty(key, value);
-        }
-    }
-
-    /**
      * Initializes the node handler of this configuration.
      */
     private void initNodeHandler()
@@ -350,37 +228,6 @@
     }
 
     /**
-     * Performs the actual remove property value operation. This method is
-     * called by <code>clearPropertyValue()</code>. The base implementation
-     * provided by this class uses {@code getProperty()} for obtaining the
-     * value(s) of the property. If there are multiple values and the index is
-     * in the allowed range, the value with the given index is removed, and the
-     * new values are stored using {@code setProperty()}. Derived classes should
-     * override this method if they can provide a more efficient implementation.
-     *
-     * @param key the key of the property
-     * @param index the index of the value to delete
-     * @return a flag whether the value could be removed
-     */
-    protected boolean clearPropertyValueDirect(String key, int index)
-    {
-        Object value = getProperty(key);
-
-        if (value instanceof List && index >= 0)
-        {
-            List<?> col = (List<?>) value;
-            if (index < col.size())
-            {
-                col.remove(index);
-                setProperty(key, col);
-                return true;
-            }
-        }
-
-        return false;
-    }
-
-    /**
      * Registers a change listener at this configuration that causes the node
      * structure to be invalidated whenever the content is changed.
      */

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPropertiesConfigurationLayout.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPropertiesConfigurationLayout.java?rev=800948&r1=800947&r2=800948&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPropertiesConfigurationLayout.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPropertiesConfigurationLayout.java Tue Aug  4 20:26:09 2009
@@ -23,7 +23,6 @@
 import junit.framework.TestCase;
 
 import org.apache.commons.configuration2.event.ConfigurationEvent;
-import org.apache.commons.configuration2.flat.AbstractFlatConfiguration;
 
 /**
  * Test class for PropertiesConfigurationLayout.
@@ -321,7 +320,7 @@
     public void testEventChangeNonExisting()
     {
         ConfigurationEvent event = new ConfigurationEvent(this,
-                AbstractFlatConfiguration.EVENT_PROPERTY_CHANGED, TEST_KEY,
+                AbstractConfiguration.EVENT_SET_PROPERTY, TEST_KEY,
                 TEST_VALUE, false);
         layout.configurationChanged(event);
         assertTrue("New property was not found", layout.getKeys().contains(

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/FlatConfigurationMockImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/FlatConfigurationMockImpl.java?rev=800948&r1=800947&r2=800948&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/FlatConfigurationMockImpl.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/FlatConfigurationMockImpl.java Tue Aug  4 20:26:09 2009
@@ -48,9 +48,6 @@
     /** Stores the value of the test property. */
     Object property;
 
-    /** Stores the expected index. */
-    int expectedIndex;
-
     /** A flag whether an add property operation is expected. */
     boolean expectAdd;
 
@@ -66,36 +63,21 @@
     }
 
     @Override
-    public boolean clearPropertyValueDirect(String key, int index)
+    public void clearPropertyDirect(String key)
     {
         TestFlatNodes.assertEquals("Wrong property key", NAME, key);
         clearProperty = true;
-        expectedIndex = index;
-        return true;
-    }
-
-    @Override
-    public void clearPropertyDirect(String key)
-    {
-        clearPropertyValue(key, FlatNode.INDEX_UNDEFINED);
     }
 
     @Override
-    public void setPropertyValueDirect(String key, int index, Object value)
+    public void setProperty(String key, Object value)
     {
         TestFlatNodes.assertFalse("Add operation expected", expectAdd);
         TestFlatNodes.assertEquals("Wrong property key", NAME, key);
-        TestFlatNodes.assertEquals("Wrong index", expectedIndex, index);
         property = value;
     }
 
     @Override
-    public void setProperty(String key, Object value)
-    {
-        setPropertyValue(key, FlatNode.INDEX_UNDEFINED, value);
-    }
-
-    @Override
     protected void addPropertyDirect(String key, Object value)
     {
         TestFlatNodes.assertTrue("Set operation expected", expectAdd);

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestAbstractFlatConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestAbstractFlatConfiguration.java?rev=800948&r1=800947&r2=800948&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestAbstractFlatConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestAbstractFlatConfiguration.java Tue Aug  4 20:26:09 2009
@@ -20,9 +20,6 @@
 import java.util.Iterator;
 import java.util.List;
 
-import org.apache.commons.configuration2.event.ConfigurationEvent;
-import org.apache.commons.configuration2.event.ConfigurationListener;
-
 import junit.framework.TestCase;
 
 /**
@@ -204,28 +201,6 @@
     }
 
     /**
-     * Tests whether setPropertyValue() sends the expected events.
-     */
-    public void testSetPropertyValueEvents()
-    {
-        ConfigPropertyChangeListener l = new ConfigPropertyChangeListener();
-        config.addConfigurationListener(l);
-        config.setPropertyValue(FlatConfigurationMockImpl.NAME, 0, "newValue");
-        l.verify();
-    }
-
-    /**
-     * Tests whether clearPropertyValue() sends the expected events.
-     */
-    public void testClearPropertyValueEvents()
-    {
-        ConfigPropertyChangeListener l = new ConfigPropertyChangeListener();
-        config.addConfigurationListener(l);
-        config.clearPropertyValue(FlatConfigurationMockImpl.NAME, 0);
-        l.verify();
-    }
-
-    /**
      * Tests cloning a flat configuration. We have to check whether the event
      * listeners are correctly registered.
      */
@@ -240,48 +215,7 @@
         prepareGetRootNode(copy, 2);
         FlatNode root = copy.getRootNode();
         checkNodeStructure(root);
-        copy.clearPropertyValue(FlatConfigurationMockImpl.NAME, 0);
+        copy.clearProperty(FlatConfigurationMockImpl.NAME);
         assertNotSame("Structure was not re-created", root, copy.getRootNode());
     }
-
-    /**
-     * A test event listener implementation for testing the events received for
-     * a manipulation of a property value.
-     */
-    private class ConfigPropertyChangeListener implements ConfigurationListener
-    {
-        /** The number of received before events. */
-        private int beforeCount;
-
-        /** The number of received after events. */
-        private int afterCount;
-
-        public void configurationChanged(ConfigurationEvent event)
-        {
-            assertEquals("Wrong event type",
-                    AbstractFlatConfiguration.EVENT_PROPERTY_CHANGED, event
-                            .getType());
-            assertEquals("Wrong event source", config, event.getSource());
-            assertEquals("Wrong property name", FlatConfigurationMockImpl.NAME,
-                    event.getPropertyName());
-            assertNull("Value not null", event.getPropertyValue());
-            if (event.isBeforeUpdate())
-            {
-                beforeCount++;
-            }
-            else
-            {
-                afterCount++;
-            }
-        }
-
-        /**
-         * Tests whether the expected events were received.
-         */
-        public void verify()
-        {
-            assertEquals("Wrong number of before events", 1, beforeCount);
-            assertEquals("Wrong number of after events", 1, afterCount);
-        }
-    }
 }

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestBaseConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestBaseConfiguration.java?rev=800948&r1=800947&r2=800948&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestBaseConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestBaseConfiguration.java Tue Aug  4 20:26:09 2009
@@ -510,7 +510,7 @@
         config.addProperty("complex.property", props);
 
         Object val = config.getProperty("complex.property");
-        assertTrue(val instanceof Collection);
+        assertTrue(val instanceof Collection<?>);
         Collection<?> col = (Collection<?>) val;
         assertEquals(10, col.size());
 
@@ -523,7 +523,7 @@
         };
         config.setProperty("complex.property", data);
         val = config.getProperty("complex.property");
-        assertTrue(val instanceof Collection);
+        assertTrue(val instanceof Collection<?>);
         col = (Collection<?>) val;
         Iterator<?> it = col.iterator();
         StringTokenizer tok = new StringTokenizer("The quick brown fox jumps over the lazy dog.", " ");
@@ -795,128 +795,4 @@
         assertEquals("Wrong max index for multiple values", count - 1, config
                 .getMaxIndex(TEST_KEY));
     }
-
-    /**
-     * Tests removing a value of a property with multiple values.
-     */
-    public void testClearPropertyValue()
-    {
-        config.addProperty(TEST_KEY, new Integer[] {
-                1, 2, 3
-        });
-        assertTrue("Value not removed", config.clearPropertyValue(TEST_KEY, 1));
-        List<?> lst = config.getList(TEST_KEY);
-        assertEquals("Wrong number of values", 2, lst.size());
-        assertEquals("Wrong value 1", Integer.valueOf(1), lst.get(0));
-        assertEquals("Wrong value 2", Integer.valueOf(3), lst.get(1));
-    }
-
-    /**
-     * Tries to remove a property value with an invalid index. This should be a
-     * no-op.
-     */
-    public void testClearPropertyValueInvalidIndex()
-    {
-        config.addProperty(TEST_KEY, new Integer[] {
-                1, 2, 3
-        });
-        assertFalse("Can remove value with negative index", config
-                .clearPropertyValue(TEST_KEY, -1));
-        assertFalse("Can remove value with index too big", config
-                .clearPropertyValue(TEST_KEY, 1000));
-        assertEquals("Wrong number of values", 3, config.getList(TEST_KEY)
-                .size());
-    }
-
-    /**
-     * Tries to remove a single property value with an index. This should not
-     * work.
-     */
-    public void testClearPropertyValueSingle()
-    {
-        config.addProperty(TEST_KEY, "A value");
-        assertFalse("Can removing single value", config.clearPropertyValue(
-                TEST_KEY, 0));
-        assertEquals("Wrong value", "A value", config.getString(TEST_KEY));
-    }
-
-    /**
-     * Tries to remove a value of a non existing property.
-     */
-    public void testClearPropertyValueNonExisting()
-    {
-        assertFalse("Can remove non existing value", config.clearPropertyValue(
-                TEST_KEY, 0));
-    }
-
-    /**
-     * Tests setting the value of a property.
-     */
-    public void testSetPropertyValue()
-    {
-        config.addProperty(TEST_KEY, new Integer[] {
-                1, 2, 3
-        });
-        config.setPropertyValue(TEST_KEY, 1, 4);
-        List<?> lst = config.getList(TEST_KEY);
-        assertEquals("Wrong number of values", 3, lst.size());
-        assertEquals("Wrong value 1", Integer.valueOf(1), lst.get(0));
-        assertEquals("Wrong value 2", Integer.valueOf(4), lst.get(1));
-        assertEquals("Wrong value 3", Integer.valueOf(3), lst.get(2));
-    }
-
-    /**
-     * Tests setting the value of a non existing property. This should be a
-     * normal add operation.
-     */
-    public void testSetPropertyValueNonExisting()
-    {
-        config.setPropertyValue(TEST_KEY, 0, "test");
-        assertEquals("Value was not set", "test", config.getString(TEST_KEY));
-    }
-
-    /**
-     * Tests setting the value of a property using an invalid index. Then the
-     * value should simply be added.
-     */
-    public void testSetPropertyValueInvalid()
-    {
-        config.addProperty(TEST_KEY, new Integer[] {
-                1, 2, 3
-        });
-        config.setPropertyValue(TEST_KEY, -1, 4);
-        config.setPropertyValue(TEST_KEY, 1111, 5);
-        List<?> lst = config.getList(TEST_KEY);
-        assertEquals("Wrong number of values", 5, lst.size());
-        for (int i = 1; i <= 5; i++)
-        {
-            assertEquals("Wrong value at " + i, Integer.valueOf(i), lst
-                    .get(i - 1));
-        }
-    }
-
-    /**
-     * Tests setting the value of a property with a single value. If the index
-     * is 0, the value must be replaced.
-     */
-    public void testSetPropertyValueSingle0()
-    {
-        config.addProperty(TEST_KEY, 1);
-        config.setPropertyValue(TEST_KEY, 0, 2);
-        assertEquals("Wrong value", 2, config.getInt(TEST_KEY));
-    }
-
-    /**
-     * Tests setting the value of a property with a single value using an
-     * invalid index. In this case, the new value must be added to the property.
-     */
-    public void testSetPropertyValueSingle1()
-    {
-        config.addProperty(TEST_KEY, 1);
-        config.setPropertyValue(TEST_KEY, 1, 2);
-        List<?> lst = config.getList(TEST_KEY);
-        assertEquals("Wrong number of values", 2, lst.size());
-        assertEquals("Wrong value 1", Integer.valueOf(1), lst.get(0));
-        assertEquals("Wrong value 2", Integer.valueOf(2), lst.get(1));
-    }
 }