commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1068122 - in /commons/proper/configuration/trunk/src: changes/ java/org/apache/commons/configuration/ test/org/apache/commons/configuration/
Date Mon, 07 Feb 2011 20:48:54 GMT
Author: oheger
Date: Mon Feb  7 20:48:54 2011
New Revision: 1068122

URL: http://svn.apache.org/viewvc?rev=1068122&view=rev
Log:
CONFIGURATION-432: Improved getList() and getStringArray() of AbstractConfiguration to support
single-valued properties of primitive types.

Modified:
    commons/proper/configuration/trunk/src/changes/changes.xml
    commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractConfiguration.java
    commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java
    commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationConverter.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=1068122&r1=1068121&r2=1068122&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/changes/changes.xml (original)
+++ commons/proper/configuration/trunk/src/changes/changes.xml Mon Feb  7 20:48:54 2011
@@ -27,6 +27,10 @@
         Minor improvements of the support for indexed properties in
         ConfigurationDynaBean.
       </action>
+      <action dev="oheger" type="fix" issue="CONFIGURATION-432">
+        The methods getList() and getStringArray() of AbstractConfiguration can
+        now handle single-valued properties of primitive types.
+      </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/AbstractConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractConfiguration.java?rev=1068122&r1=1068121&r2=1068122&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractConfiguration.java
(original)
+++ commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractConfiguration.java
Mon Feb  7 20:48:54 2011
@@ -23,6 +23,7 @@ import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 import java.util.NoSuchElementException;
@@ -35,6 +36,7 @@ import org.apache.commons.configuration.
 import org.apache.commons.configuration.event.EventSource;
 import org.apache.commons.configuration.interpol.ConfigurationInterpolator;
 import org.apache.commons.lang.BooleanUtils;
+import org.apache.commons.lang.ClassUtils;
 import org.apache.commons.lang.text.StrLookup;
 import org.apache.commons.lang.text.StrSubstitutor;
 import org.apache.commons.logging.Log;
@@ -1093,6 +1095,11 @@ public abstract class AbstractConfigurat
         {
             array = new String[0];
         }
+        else if (isScalarValue(value))
+        {
+            array = new String[1];
+            array[0] = value.toString();
+        }
         else
         {
             throw new ConversionException('\'' + key + "' doesn't map to a String/List object");
@@ -1139,6 +1146,10 @@ public abstract class AbstractConfigurat
         {
             return Arrays.asList((Object[]) value);
         }
+        else if (isScalarValue(value))
+        {
+            return Collections.singletonList(value.toString());
+        }
         else
         {
             throw new ConversionException('\'' + key + "' doesn't map to a List object: "
+ value + ", a "
@@ -1175,6 +1186,24 @@ public abstract class AbstractConfigurat
     }
 
     /**
+     * Checks whether the specified object is a scalar value. This method is
+     * called by <code>getList()</code> and <code>getStringArray()</code>
if the
+     * property requested is not a string, a list, or an array. If it returns
+     * <b>true</b>, the calling method transforms the value to a string and
+     * returns a list or an array with this single element. This implementation
+     * returns <b>true</b> if the value is of a wrapper type for a primitive
+     * type.
+     *
+     * @param value the value to be checked
+     * @return a flag whether the value is a scalar
+     * @since 1.7
+     */
+    protected boolean isScalarValue(Object value)
+    {
+        return ClassUtils.wrapperToPrimitive(value.getClass()) != null;
+    }
+
+    /**
      * Copies the content of the specified configuration into this
      * configuration. If the specified configuration contains a key that is also
      * present in this configuration, the value of this key will be replaced by

Modified: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java?rev=1068122&r1=1068121&r2=1068122&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java
(original)
+++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java
Mon Feb  7 20:48:54 2011
@@ -280,6 +280,62 @@ public class TestAbstractConfigurationBa
     }
 
     /**
+     * Tests getList() for single non-string values.
+     */
+    public void testGetListNonString()
+    {
+        checkGetListScalar(new Integer(42));
+        checkGetListScalar(new Long(42));
+        checkGetListScalar(new Short((short) 42));
+        checkGetListScalar(new Byte((byte) 42));
+        checkGetListScalar(new Float(42));
+        checkGetListScalar(new Double(42));
+        checkGetListScalar(Boolean.TRUE);
+}
+
+    /**
+     * Tests getStringArray() for single son-string values.
+     */
+    public void testGetStringArrayNonString()
+    {
+        checkGetStringArrayScalar(new Integer(42));
+        checkGetStringArrayScalar(new Long(42));
+        checkGetStringArrayScalar(new Short((short) 42));
+        checkGetStringArrayScalar(new Byte((byte) 42));
+        checkGetStringArrayScalar(new Float(42));
+        checkGetStringArrayScalar(new Double(42));
+        checkGetStringArrayScalar(Boolean.TRUE);
+    }
+
+    /**
+     * Helper method for checking getList() if the property value is a scalar.
+     * @param value the value of the property
+     */
+    private void checkGetListScalar(Object value)
+    {
+        BaseConfiguration config = new BaseConfiguration();
+        config.addProperty(KEY_PREFIX, value);
+        List lst = config.getList(KEY_PREFIX);
+        assertEquals("Wrong number of values", 1, lst.size());
+        assertEquals("Wrong value", value.toString(), lst.get(0));
+    }
+
+    /**
+     * Helper method for checking getStringArray() if the property value is a
+     * scalar.
+     *
+     * @param value the value of the property
+     */
+    private void checkGetStringArrayScalar(Object value)
+    {
+        BaseConfiguration config = new BaseConfiguration();
+        config.addProperty(KEY_PREFIX, value);
+        String[] array = config.getStringArray(KEY_PREFIX);
+        assertEquals("Weong number of elements", 1, array.length);
+        assertEquals("Wrong value", value.toString(), array[0]);
+    }
+
+    /**
      * Creates the source configuration for testing the copy() and append()
      * methods. This configuration contains keys with an odd index and values
      * starting with the prefix "src". There are also some list properties.

Modified: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationConverter.java?rev=1068122&r1=1068121&r2=1068122&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationConverter.java
(original)
+++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationConverter.java
Mon Feb  7 20:48:54 2011
@@ -22,6 +22,7 @@ import java.util.Map;
 import java.util.Properties;
 
 import junit.framework.TestCase;
+
 import org.apache.commons.collections.ExtendedProperties;
 
 /**
@@ -29,7 +30,7 @@ import org.apache.commons.collections.Ex
  *
  * @author Martin Poeschl
  * @author Emmanuel Bourg
- * @version $Revision$, $Date$
+ * @version $Id$
  */
 public class TestConfigurationConverter extends TestCase
 {
@@ -106,6 +107,18 @@ public class TestConfigurationConverter 
         assertEquals("'array' property", "item 1;item 2", props.getProperty("array"));
     }
 
+    /**
+     * Tests the conversion of a configuration object to properties if scalar
+     * values are involved. This test is related to CONFIGURATION-432.
+     */
+    public void testConfigurationToPropertiesScalarValue()
+    {
+        BaseConfiguration config = new BaseConfiguration();
+        config.addProperty("scalar", new Integer(42));
+        Properties props = ConfigurationConverter.getProperties(config);
+        assertEquals("Wrong value", "42", props.getProperty("scalar"));
+    }
+
     public void testConfigurationToMap()
     {
         Configuration config = new BaseConfiguration();



Mime
View raw message