Author: oheger Date: Mon Feb 7 20:36:11 2011 New Revision: 1068111 URL: http://svn.apache.org/viewvc?rev=1068111&view=rev Log: CONFIGURATION-433: Improved handling of indexed properties in ConfigurationDynaBean. Also some minor Javadocs corrections. Ported fix to configuration2 branch.
Modified: commons/proper/configuration/branches/configuration2_experimental/src/changes/changes.xml commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/ConfigurationDynaBean.java commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/beanutils/TestConfigurationDynaBean.java Modified: commons/proper/configuration/branches/configuration2_experimental/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/changes/changes.xml?rev=1068111&r1=1068110&r2=1068111&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/changes/changes.xml (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/changes/changes.xml Mon Feb 7 20:36:11 2011 @@ -79,6 +79,10 @@ </release> <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/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/ConfigurationDynaBean.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/ConfigurationDynaBean.java?rev=1068111&r1=1068110&r2=1068111&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/ConfigurationDynaBean.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/beanutils/ConfigurationDynaBean.java Mon Feb 7 20:36:11 2011 @@ -25,7 +25,6 @@ import org.apache.commons.beanutils.Dyna import org.apache.commons.beanutils.DynaClass; import org.apache.commons.configuration2.Configuration; import org.apache.commons.configuration2.ConfigurationMap; -import org.apache.commons.configuration2.ConversionException; import org.apache.commons.configuration2.SubsetConfiguration; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -43,10 +42,10 @@ import org.apache.commons.logging.LogFac * method. Similarly, indexed properties reference lists of configuration * properties using the * {@link org.apache.commons.configuration2.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> * @@ -154,20 +153,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.", e); + throw new IllegalArgumentException("Property '" + name + + "' is not indexed."); } + + List<?> list = getConfiguration().getList(name); + return list.get(index); } public Object get(String name, String key) @@ -194,36 +187,28 @@ 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<Object> list = (List<Object>) 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) + { + @SuppressWarnings("rawtypes") + 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.", e); + getConfiguration().setProperty(name, value); } } @@ -231,4 +216,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/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/beanutils/TestConfigurationDynaBean.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/beanutils/TestConfigurationDynaBean.java?rev=1068111&r1=1068110&r2=1068111&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/beanutils/TestConfigurationDynaBean.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/beanutils/TestConfigurationDynaBean.java Mon Feb 7 20:36:11 2011 @@ -722,11 +722,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"); @@ -738,44 +736,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 + } + } + + /** + * Tests whether accessing a non-indexed string property using the index get + * method causes an exception. + */ + public void testGetIndexedString() + { + bean.set("stringProp", "value"); + try + { + bean.get("stringProp", 0); + fail("Could access non-indexed property with indexed get method!"); } - catch (Throwable t) + catch(IllegalArgumentException iex) { - fail("Threw " + t + " instead of IllegalArgumentException"); - return; + //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"); } - - }