commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1068130 - in /commons/proper/configuration/branches/configuration2_experimental/src: changes/ main/java/org/apache/commons/configuration2/ test/java/org/apache/commons/configuration2/
Date Mon, 07 Feb 2011 21:01:00 GMT
Author: oheger
Date: Mon Feb  7 21:00:59 2011
New Revision: 1068130

URL: http://svn.apache.org/viewvc?rev=1068130&view=rev
Log:
CONFIGURATION-432: Improved getList() and getStringArray() of AbstractConfiguration to support
single-valued properties of primitive types.
Ported fix to configuration2 branch. Note: The generics version of getList() probably needs
some more work.

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/changes/changes.xml
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestConfigurationConverter.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=1068130&r1=1068129&r2=1068130&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 21:00:59 2011
@@ -83,6 +83,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/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java?rev=1068130&r1=1068129&r2=1068130&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java
Mon Feb  7 21:00:59 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.configuration2
 import org.apache.commons.configuration2.event.EventSource;
 import org.apache.commons.configuration2.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;
@@ -1134,6 +1136,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");
@@ -1181,6 +1188,10 @@ public abstract class AbstractConfigurat
             T[] array = (T[]) value;
             return Arrays.asList(array);
         }
+        else if (isScalarValue(value))
+        {
+            list = Collections.singletonList(value.toString());
+        }
         else
         {
             throw new ConversionException('\'' + key + "' doesn't map to a List object: "
+ value + ", a "
@@ -1217,6 +1228,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/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java?rev=1068130&r1=1068129&r2=1068130&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java
Mon Feb  7 21:00:59 2011
@@ -332,6 +332,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/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestConfigurationConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestConfigurationConverter.java?rev=1068130&r1=1068129&r2=1068130&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestConfigurationConverter.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestConfigurationConverter.java
Mon Feb  7 21:00:59 2011
@@ -22,8 +22,7 @@ import java.util.Map;
 import java.util.Properties;
 
 import junit.framework.TestCase;
-import org.apache.commons.configuration2.Configuration;
-import org.apache.commons.configuration2.ConfigurationConverter;
+
 import org.apache.commons.configuration2.flat.BaseConfiguration;
 
 /**
@@ -31,7 +30,7 @@ import org.apache.commons.configuration2
  *
  * @author Martin Poeschl
  * @author Emmanuel Bourg
- * @version $Revision$, $Date$
+ * @version $Id$
  */
 public class TestConfigurationConverter extends TestCase
 {
@@ -75,6 +74,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