Author: oheger
Date: Sun Feb 6 21:24:09 2011
New Revision: 1067771
URL: http://svn.apache.org/viewvc?rev=1067771&view=rev
Log:
CONFIGURATION-433: Improved handling of indexed properties in
ConfigurationDynaBean. Also some minor Javadocs corrections.
Modified:
commons/proper/configuration/trunk/src/changes/changes.xml
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/beanutils/ConfigurationDynaBean.java
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/beanutils/TestConfigurationDynaBean.java
Modified: commons/proper/configuration/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/changes/changes.xml?rev=1067771&r1=1067770&r2=1067771&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/changes/changes.xml (original)
+++ commons/proper/configuration/trunk/src/changes/changes.xml Sun Feb 6
21:24:09 2011
@@ -23,6 +23,10 @@
<body>
<release version="1.7" date="in SVN" description="">
+ <action dev="oheger" type="fix" issue="CONFIGURATION-433">
+ Minor improvements of the support for indexed properties in
+ ConfigurationDynaBean.
+ </action>
<action dev="oheger" type="fix" issue="CONFIGURATION-428">
XMLConfiguration no longer escapes backslashs in the values of
XML elements.
Modified:
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/beanutils/ConfigurationDynaBean.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/beanutils/ConfigurationDynaBean.java?rev=1067771&r1=1067770&r2=1067771&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/beanutils/ConfigurationDynaBean.java
(original)
+++
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/beanutils/ConfigurationDynaBean.java
Sun Feb 6 21:24:09 2011
@@ -26,7 +26,6 @@ import org.apache.commons.beanutils.Dyna
import org.apache.commons.beanutils.DynaClass;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationMap;
-import org.apache.commons.configuration.ConversionException;
import org.apache.commons.configuration.SubsetConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -44,15 +43,15 @@ import org.apache.commons.logging.LogFac
* method. Similarly, indexed properties reference lists of configuration
* properties using the
* {@link org.apache.commons.configuration.Configuration#getList(String)}
- * method. Setting an indexed property always throws an exception.</p>
+ * method. Setting an indexed property is supported, too.</p>
*
* <p>Note: Some of the methods expect that a dot (".") is used as
- * property delimitor for the wrapped configuration. This is true for most of
+ * property delimiter for the wrapped configuration. This is true for most of
* the default configurations. Hierarchical configurations, for which a
specific
* expression engine is set, may cause problems.</p>
*
* @author <a href="mailto:[email protected]">Ricardo
Gladwell</a>
- * @version $Revision$, $Date$
+ * @version $Id$
* @since 1.0-rc1
*/
public class ConfigurationDynaBean extends ConfigurationMap implements DynaBean
@@ -157,20 +156,14 @@ public class ConfigurationDynaBean exten
public Object get(String name, int index)
{
- try
+ if (!checkIndexedProperty(name))
{
- List list = getConfiguration().getList(name);
- if (list.isEmpty())
- {
- throw new IllegalArgumentException("Indexed property '" + name
+ "' does not exist.");
- }
-
- return list.get(index);
- }
- catch (ConversionException e)
- {
- throw new IllegalArgumentException("Property '" + name + "' is not
indexed.");
+ throw new IllegalArgumentException("Property '" + name
+ + "' is not indexed.");
}
+
+ List list = getConfiguration().getList(name);
+ return list.get(index);
}
public Object get(String name, String key)
@@ -197,36 +190,27 @@ public class ConfigurationDynaBean exten
public void set(String name, int index, Object value)
{
- try
+ if (!checkIndexedProperty(name) && index > 0)
{
- Object property = getConfiguration().getProperty(name);
+ throw new IllegalArgumentException("Property '" + name
+ + "' is not indexed.");
+ }
- if (property == null)
- {
- throw new IllegalArgumentException("Property '" + name + "'
does not exist.");
- }
- else if (property instanceof List)
- {
- List list = (List) property;
- list.set(index, value);
- getConfiguration().setProperty(name, list);
- }
- else if (property.getClass().isArray())
- {
- Array.set(property, index, value);
- }
- else if (index == 0)
- {
- getConfiguration().setProperty(name, value);
- }
- else
- {
- throw new IllegalArgumentException("Property '" + name + "' is
not indexed.");
- }
+ Object property = getConfiguration().getProperty(name);
+
+ if (property instanceof List)
+ {
+ List list = (List) property;
+ list.set(index, value);
+ getConfiguration().setProperty(name, list);
+ }
+ else if (property.getClass().isArray())
+ {
+ Array.set(property, index, value);
}
- catch (ConversionException e)
+ else if (index == 0)
{
- throw new IllegalArgumentException("Property '" + name + "' is not
indexed.");
+ getConfiguration().setProperty(name, value);
}
}
@@ -234,4 +218,26 @@ public class ConfigurationDynaBean exten
{
getConfiguration().setProperty(name + "." + key, value);
}
+
+ /**
+ * Tests whether the given name references an indexed property. This
+ * implementation tests for properties of type list or array. If the
+ * property does not exist, an exception is thrown.
+ *
+ * @param name the name of the property to check
+ * @return a flag whether this is an indexed property
+ * @throws IllegalArgumentException if the property does not exist
+ */
+ private boolean checkIndexedProperty(String name)
+ {
+ Object property = getConfiguration().getProperty(name);
+
+ if (property == null)
+ {
+ throw new IllegalArgumentException("Property '" + name
+ + "' does not exist.");
+ }
+
+ return (property instanceof List) || property.getClass().isArray();
+ }
}
Modified:
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/beanutils/TestConfigurationDynaBean.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/beanutils/TestConfigurationDynaBean.java?rev=1067771&r1=1067770&r2=1067771&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/beanutils/TestConfigurationDynaBean.java
(original)
+++
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/beanutils/TestConfigurationDynaBean.java
Sun Feb 6 21:24:09 2011
@@ -21,15 +21,15 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
+import junit.framework.AssertionFailedError;
+import junit.framework.TestCase;
+import junitx.framework.ObjectAssert;
+
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.configuration.BaseConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.MapConfiguration;
-import junit.framework.AssertionFailedError;
-import junit.framework.TestCase;
-import junitx.framework.ObjectAssert;
-
/**
* <p>Test Case for the <code>ConfigurationDynaBean</code> implementation
class.
* These tests were based on the ones in <code>BasicDynaBeanTestCase</code>
@@ -716,11 +716,9 @@ public class TestConfigurationDynaBean e
}
/**
- * Tests if accessing a non-indexed property using the index
- * get method throws an IllegalArgumentException as it
- * should.
+ * Tests whether nested properties can be accessed.
*/
- public void testNonIndexedPropeties()
+ public void testNestedPropeties()
{
ConfigurationDynaBean nested = (ConfigurationDynaBean)
bean.get("mappedProperty");
@@ -732,44 +730,73 @@ public class TestConfigurationDynaBean e
}
/**
- * Tests if accessing a non-indexed property using the index
+ * Tests if reading a non-indexed property using the index
* get method throws an IllegalArgumentException as it
* should.
*/
- public void testNestedPropeties()
+ public void testGetNonIndexedProperties()
{
try
{
bean.get("booleanProperty", 0);
+ fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException e)
{
- return;
+ //ok
}
- catch (Throwable t)
+ }
+
+ /**
+ * Tests whether accessing a non-indexed string property using the index
get
+ * method causes an exception.
+ */
+ public void testGetIndexedString()
+ {
+ bean.set("stringProp", "value");
+ try
{
- fail("Threw " + t + " instead of IllegalArgumentException");
- return;
+ bean.get("stringProp", 0);
+ fail("Could access non-indexed property with indexed get method!");
}
+ catch(IllegalArgumentException iex)
+ {
+ //ok
+ }
+ }
- fail("Should have thrown IllegalArgumentException");
-
+ /**
+ * Tests whether an indexed access to a non-existing property causes an
+ * exception.
+ */
+ public void testGetIndexedNonExisting()
+ {
try
{
- bean.set("booleanProperty", 0, Boolean.TRUE);
+ bean.get("Non existing property", 0);
+ fail("Non existing property not detected!");
}
- catch (IllegalArgumentException e)
+ catch (IllegalArgumentException iex)
{
- return;
+ // ok
}
- catch (Throwable t)
+ }
+
+ /**
+ * Tests if writing a non-indexed property using the index
+ * set method with an index > 0 throws an IllegalArgumentException as it
+ * should.
+ */
+ public void testSetNonIndexedProperties()
+ {
+ try
{
- fail("Threw " + t + " instead of IllegalArgumentException");
- return;
+ bean.set("booleanProperty", 1, Boolean.TRUE);
+ fail("Could write indexed property!");
+ }
+ catch (IllegalArgumentException e)
+ {
+ //ok
}
-
- fail("Should have thrown IllegalArgumentException");
}
-
-
}