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/03 22:19:25 UTC
svn commit: r800525 - in
/commons/proper/configuration/branches/configuration2_experimental/src:
main/java/org/apache/commons/configuration2/flat/
test/java/org/apache/commons/configuration2/flat/
Author: oheger
Date: Mon Aug 3 20:19:25 2009
New Revision: 800525
URL: http://svn.apache.org/viewvc?rev=800525&view=rev
Log:
The flat nodes now operate on a plain Configuration rather than an AbstractFlatConfiguration.
Modified:
commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatLeafNode.java
commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatNode.java
commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatRootNode.java
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestFlatNodes.java
Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatLeafNode.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatLeafNode.java?rev=800525&r1=800524&r2=800525&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatLeafNode.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatLeafNode.java Mon Aug 3 20:19:25 2009
@@ -20,6 +20,7 @@
import java.util.Collections;
import java.util.List;
+import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.ConfigurationRuntimeException;
/**
@@ -165,10 +166,10 @@
* @return the value of the represented property
*/
@Override
- public Object getValue(AbstractFlatConfiguration config)
+ public Object getValue(Configuration config)
{
Object value = config.getProperty(getName());
- if (value instanceof Collection)
+ if (value instanceof Collection<?>)
{
int valueIndex = getValueIndex();
if (valueIndex != INDEX_UNDEFINED)
@@ -213,7 +214,7 @@
* @throws ConfigurationRuntimeException if the child cannot be removed
*/
@Override
- public void removeChild(AbstractFlatConfiguration config, FlatNode child)
+ public void removeChild(Configuration config, FlatNode child)
{
throw new ConfigurationRuntimeException(
"Cannot remove a child from a leaf node!");
@@ -229,14 +230,14 @@
* @param value the new value
*/
@Override
- public void setValue(AbstractFlatConfiguration config, Object value)
+ public void setValue(Configuration config, Object value)
{
if (hasValue)
{
int index = getValueIndex();
if (index != INDEX_UNDEFINED)
{
- config.setPropertyValue(getName(), index, value);
+ parent.setMultiProperty(config, this, index, value);
}
else
{
Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatNode.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatNode.java?rev=800525&r1=800524&r2=800525&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatNode.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatNode.java Mon Aug 3 20:19:25 2009
@@ -18,6 +18,7 @@
import java.util.List;
+import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.ConfigurationRuntimeException;
/**
@@ -77,7 +78,7 @@
* @param config the owning configuration
* @return the value of this node
*/
- public abstract Object getValue(AbstractFlatConfiguration config);
+ public abstract Object getValue(Configuration config);
/**
* Sets the value of this node. An implementation can access the passed in
@@ -87,7 +88,7 @@
* @param value the new value
* @throws ConfigurationRuntimeException if the value cannot be set
*/
- public abstract void setValue(AbstractFlatConfiguration config, Object value);
+ public abstract void setValue(Configuration config, Object value);
/**
* Returns the index of the value represented by this node. This is needed
@@ -160,6 +161,5 @@
* @param child the child to be removed
* @throws ConfigurationRuntimeException if the child cannot be removed
*/
- public abstract void removeChild(AbstractFlatConfiguration config,
- FlatNode child);
+ public abstract void removeChild(Configuration config, FlatNode child);
}
Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatRootNode.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatRootNode.java?rev=800525&r1=800524&r2=800525&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatRootNode.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/FlatRootNode.java Mon Aug 3 20:19:25 2009
@@ -17,9 +17,11 @@
package org.apache.commons.configuration2.flat;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Collections;
import java.util.List;
+import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.ConfigurationRuntimeException;
/**
@@ -187,7 +189,7 @@
* @return the value of this node
*/
@Override
- public Object getValue(AbstractFlatConfiguration config)
+ public Object getValue(Configuration config)
{
return null;
}
@@ -214,7 +216,7 @@
* node
*/
@Override
- public void removeChild(AbstractFlatConfiguration config, FlatNode child)
+ public void removeChild(Configuration config, FlatNode child)
{
for (FlatNode c : children)
{
@@ -223,7 +225,7 @@
int index = c.getValueIndex();
if (index != INDEX_UNDEFINED)
{
- config.clearPropertyValue(c.getName(), index);
+ changeMultiProperty(config, c, index, null, true);
}
else
{
@@ -248,7 +250,7 @@
* @throws ConfigurationRuntimeException if the value cannot be set
*/
@Override
- public void setValue(AbstractFlatConfiguration config, Object value)
+ public void setValue(Configuration config, Object value)
{
throw new ConfigurationRuntimeException(
"Cannot set the value of the root node of a flat configuration!");
@@ -291,4 +293,55 @@
return INDEX_UNDEFINED;
}
+
+ /**
+ * Changes the value of a property with multiple values. This method is
+ * called when the value of a child node was changed that occurs multiple
+ * times. It obtains the list with all values for this property, changes the
+ * value with the given index, and sets the new value.
+ *
+ * @param config the current configuration
+ * @param child the child node that was changed
+ * @param index the value index of this child node
+ * @param value the new value
+ */
+ void setMultiProperty(Configuration config, FlatNode child, int index,
+ Object value)
+ {
+ changeMultiProperty(config, child, index, value, false);
+ }
+
+ /**
+ * Helper method for manipulating a property with multiple values. A value
+ * at a given index can either be changed or removed. If the index is
+ * invalid, no change is performed.
+ *
+ * @param config the current configuration
+ * @param child the child node that was changed
+ * @param index the value index of this child node
+ * @param value the new value
+ * @param remove a flag whether the value at the index is to be removed
+ */
+ private static void changeMultiProperty(Configuration config,
+ FlatNode child, int index, Object value, boolean remove)
+ {
+ Object val = config.getProperty(child.getName());
+ if (val instanceof Collection<?>)
+ {
+ Collection<?> col = (Collection<?>) val;
+ if (col.size() > index)
+ {
+ List<Object> newValues = new ArrayList<Object>(col);
+ if (remove)
+ {
+ newValues.remove(index);
+ }
+ else
+ {
+ newValues.set(index, value);
+ }
+ config.setProperty(child.getName(), newValues);
+ }
+ }
+ }
}
Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestFlatNodes.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestFlatNodes.java?rev=800525&r1=800524&r2=800525&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestFlatNodes.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestFlatNodes.java Mon Aug 3 20:19:25 2009
@@ -20,13 +20,13 @@
import java.util.Collection;
import java.util.List;
-import org.apache.commons.configuration2.ConfigurationRuntimeException;
-
import junit.framework.TestCase;
+import org.apache.commons.configuration2.ConfigurationRuntimeException;
+
/**
* Test class for the FlatNode classes.
- *
+ *
* @author <a href="http://commons.apache.org/configuration/team-list.html">Commons
* Configuration team</a>
* @version $Id$
@@ -34,7 +34,7 @@
public class TestFlatNodes extends TestCase
{
/** Constant for the name of the test node. */
- private static final String NAME = FlatConfigurationMockImpl.NAME;
+ private static final String NAME = "testFlatNode";
/** Constant for a test value. */
static final Object VALUE = 42;
@@ -148,8 +148,8 @@
*/
public void testGetValueSimple()
{
- FlatConfigurationMockImpl conf = new FlatConfigurationMockImpl();
- conf.property = VALUE;
+ BaseConfiguration conf = new BaseConfiguration();
+ conf.setProperty(NAME, VALUE);
assertEquals("Wrong property value", VALUE, node.getValue(conf));
}
@@ -159,10 +159,10 @@
*/
public void testGetValueCollectionNoIndex()
{
- FlatConfigurationMockImpl config = new FlatConfigurationMockImpl();
+ BaseConfiguration config = new BaseConfiguration();
Collection<Object> values = new ArrayList<Object>();
values.add(VALUE);
- config.property = values;
+ config.addPropertyDirect(NAME, values);
assertSame("Wrong value collection", values, node.getValue(config));
}
@@ -171,11 +171,11 @@
*/
public void testGetValueCollection()
{
- FlatConfigurationMockImpl config = new FlatConfigurationMockImpl();
+ BaseConfiguration config = new BaseConfiguration();
Collection<Object> values = new ArrayList<Object>();
values.add(VALUE);
values.add(2);
- config.property = values;
+ config.setProperty(NAME, values);
FlatNode c2 = parent.addChild(NAME);
assertEquals("Wrong value index 1", VALUE, node.getValue(config));
assertEquals("Wrong value index 2", 2, c2.getValue(config));
@@ -187,10 +187,10 @@
*/
public void testGetValueCollectionInvalidIndex()
{
- FlatConfigurationMockImpl config = new FlatConfigurationMockImpl();
- Collection<Object> values = new ArrayList<Object>();
- values.add(VALUE);
- config.property = values;
+ BaseConfiguration config = new BaseConfiguration();
+ config.addProperty(NAME, VALUE);
+ config.addProperty(NAME, 2);
+ parent.addChild(NAME);
FlatNode c2 = parent.addChild(NAME);
assertNull("Found value for invalid index", c2.getValue(config));
}
@@ -200,10 +200,9 @@
*/
public void testSetValueNew()
{
- FlatConfigurationMockImpl config = new FlatConfigurationMockImpl();
- config.expectAdd = true;
+ BaseConfiguration config = new BaseConfiguration();
node.setValue(config, VALUE);
- assertEquals("Value was not set", VALUE, config.property);
+ assertEquals("Value was not set", VALUE, config.getProperty(NAME));
}
/**
@@ -211,13 +210,12 @@
*/
public void testSetValueExisting()
{
- FlatConfigurationMockImpl config = new FlatConfigurationMockImpl();
+ BaseConfiguration config = new BaseConfiguration();
// remove node, so that there is only a single child with this name
parent.removeChild(config, node);
- config.expectedIndex = FlatNode.INDEX_UNDEFINED;
FlatNode child = parent.addChild(NAME, true);
child.setValue(config, VALUE);
- assertEquals("Value was not set", VALUE, config.property);
+ assertEquals("Value was not set", VALUE, config.getProperty(NAME));
}
/**
@@ -225,11 +223,48 @@
*/
public void testSetValueCollection()
{
- FlatConfigurationMockImpl config = new FlatConfigurationMockImpl();
+ BaseConfiguration config = new BaseConfiguration();
+ config.addProperty(NAME, 1);
+ config.addProperty(NAME, 2);
FlatNode child = parent.addChild(NAME, true);
- config.expectedIndex = 1;
child.setValue(config, VALUE);
- assertEquals("Value was not set", VALUE, config.property);
+ List<?> values = config.getList(NAME);
+ assertEquals("Wrong number of values", 2, values.size());
+ assertEquals("Wrong value 1", 1, values.get(0));
+ assertEquals("Wrong value 2", VALUE, values.get(1));
+ }
+
+ /**
+ * Tests the modification of a property with multiple values if an invalid
+ * value index is involved. This case should not happen normally. No
+ * modification should be performed.
+ */
+ public void testSetValueCollectionInvalidIndex()
+ {
+ BaseConfiguration config = new BaseConfiguration();
+ config.addProperty(NAME, VALUE);
+ config.addProperty(NAME, 2);
+ parent.addChild(NAME, true);
+ FlatNode child = parent.addChild(NAME, true);
+ child.setValue(config, "new");
+ List<?> values = config.getList(NAME);
+ assertEquals("Wrong number of values", 2, values.size());
+ assertEquals("Wrong value 0", VALUE, values.get(0));
+ assertEquals("Wrong value 1", 2, values.get(1));
+ }
+
+ /**
+ * Tests the modification of a property with multiple values if the configuration
+ * does not return a collection. This should normally not happen. In this
+ * case no modification should be performed.
+ */
+ public void testSetValueCollectionInvalidValue()
+ {
+ BaseConfiguration config = new BaseConfiguration();
+ config.addProperty(NAME, VALUE);
+ FlatNode child = parent.addChild(NAME, true);
+ child.setValue(config, "new");
+ assertEquals("Value was changed", VALUE, config.getProperty(NAME));
}
/**
@@ -238,14 +273,14 @@
*/
public void testSetValueNewAndExisting()
{
- FlatConfigurationMockImpl config = new FlatConfigurationMockImpl();
- config.expectAdd = true;
+ BaseConfiguration config = new BaseConfiguration();
+ config.setProperty(NAME, 1);
node.setValue(config, VALUE);
- assertEquals("Value was not set", VALUE, config.property);
- config.expectAdd = false;
- config.expectedIndex = FlatNode.INDEX_UNDEFINED;
+ List<?> values = config.getList(NAME);
+ assertEquals("Value was not added", 2, values.size());
+ assertEquals("Wrong value", VALUE, values.get(1));
node.setValue(config, "new");
- assertEquals("Value was not changed", "new", config.property);
+ assertEquals("Value was not changed", "new", config.getProperty(NAME));
}
/**
@@ -254,12 +289,10 @@
*/
public void testRemoveChild()
{
- FlatConfigurationMockImpl config = new FlatConfigurationMockImpl();
+ BaseConfiguration config = new BaseConfiguration();
+ config.addProperty(NAME, VALUE);
parent.removeChild(config, node);
- assertTrue("Clear was not called", config.clearProperty);
- assertEquals("Wrong index", FlatNode.INDEX_UNDEFINED,
- config.expectedIndex);
- assertTrue("Child was not removed", parent.getChildren().isEmpty());
+ assertFalse("Property not removed", config.containsKey(NAME));
}
/**
@@ -267,13 +300,61 @@
*/
public void testRemoveChildCollection()
{
- FlatConfigurationMockImpl config = new FlatConfigurationMockImpl();
+ BaseConfiguration config = new BaseConfiguration();
+ config.addProperty(NAME, new Object[] { 1, 2, 3 });
+ FlatNode n2 = parent.addChild(NAME, true);
parent.addChild(NAME, true);
- parent.removeChild(config, node);
- assertTrue("Clear was not called", config.clearProperty);
- assertEquals("Wrong index", 0, config.expectedIndex);
- assertEquals("Child was not removed", 1, parent.getChildren(NAME)
- .size());
+ parent.removeChild(config, n2);
+ List<?> values = config.getList(NAME);
+ assertEquals("Wrong number of values", 2, values.size());
+ assertEquals("Wrong value 1", 1, values.get(0));
+ assertEquals("Wrong value 2", 3, values.get(1));
+ }
+
+ /**
+ * Tests the behavior of removeChild() if after the operation only a single
+ * collection element remains.
+ */
+ public void testRemoveChildCollectionSingleElement()
+ {
+ BaseConfiguration config = new BaseConfiguration();
+ config.addProperty(NAME, VALUE);
+ config.addProperty(NAME, 2);
+ FlatNode n2 = parent.addChild(NAME, true);
+ parent.removeChild(config, n2);
+ assertEquals("Wrong value", VALUE, config.getProperty(NAME));
+ }
+
+ /**
+ * Tests removeChild() if the child has an invalid index. This should
+ * normally not happen. In this case no modification should be performed.
+ */
+ public void testRemoveChildCollectionInvalidIndex()
+ {
+ BaseConfiguration config = new BaseConfiguration();
+ config.addProperty(NAME, VALUE);
+ config.addProperty(NAME, 2);
+ parent.addChild(NAME, true);
+ FlatNode n2 = parent.addChild(NAME, true);
+ parent.removeChild(config, n2);
+ List<?> values = config.getList(NAME);
+ assertEquals("Wrong number of values", 2, values.size());
+ assertEquals("Wrong value 1", VALUE, values.get(0));
+ assertEquals("Wrong value 2", 2, values.get(1));
+ }
+
+ /**
+ * Tests removeChild() for a property with multiple values if the
+ * configuration does not return a collection. This should normally not
+ * happen. In this case no modification should be performed.
+ */
+ public void testRemoveChildeCollectionInvalidValue()
+ {
+ BaseConfiguration config = new BaseConfiguration();
+ config.addProperty(NAME, VALUE);
+ FlatNode n2 = parent.addChild(NAME, true);
+ parent.removeChild(config, n2);
+ assertEquals("Wrong value", VALUE, config.getProperty(NAME));
}
/**
@@ -283,7 +364,7 @@
public void testRemoveChildWrongParent()
{
FlatLeafNode child = new FlatLeafNode(null, "test", true);
- FlatConfigurationMockImpl config = new FlatConfigurationMockImpl();
+ BaseConfiguration config = new BaseConfiguration();
try
{
parent.removeChild(config, child);
@@ -316,7 +397,7 @@
*/
public void testRemoveChildLeaf()
{
- FlatConfigurationMockImpl config = new FlatConfigurationMockImpl();
+ BaseConfiguration config = new BaseConfiguration();
try
{
node.removeChild(config, parent);
@@ -444,7 +525,7 @@
*/
public void testGetValueRoot()
{
- FlatConfigurationMockImpl config = new FlatConfigurationMockImpl();
+ BaseConfiguration config = new BaseConfiguration();
assertNull("Wrong value of root node", parent.getValue(config));
}
@@ -454,7 +535,7 @@
*/
public void testSetValueRoot()
{
- FlatConfigurationMockImpl config = new FlatConfigurationMockImpl();
+ BaseConfiguration config = new BaseConfiguration();
try
{
parent.setValue(config, VALUE);