commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1516578 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/convert/DefaultConversionHandler.java test/java/org/apache/commons/configuration/convert/TestDefaultConversionHandler.java
Date Thu, 22 Aug 2013 20:01:25 GMT
Author: oheger
Date: Thu Aug 22 20:01:24 2013
New Revision: 1516578

URL: http://svn.apache.org/r1516578
Log:
Added a special handling for empty elements.

When converting to a list or to an array it makes sense to handle empty
elements in a configuration in a special way - rather than returning a
collection with a single empty element, an empty collection should be
returned. This is now implemented by DefaultConversionHandler. A new method was
added which determines whether an element is empty. So this can be customized
by subclasses.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/convert/DefaultConversionHandler.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/convert/TestDefaultConversionHandler.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/convert/DefaultConversionHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/convert/DefaultConversionHandler.java?rev=1516578&r1=1516577&r2=1516578&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/convert/DefaultConversionHandler.java
(original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/convert/DefaultConversionHandler.java
Thu Aug 22 20:01:24 2013
@@ -128,6 +128,10 @@ public class DefaultConversionHandler im
         {
             return null;
         }
+        if (isEmptyElement(src))
+        {
+            return Array.newInstance(elemClass, 0);
+        }
 
         ConfigurationInterpolator interpolator = fetchInterpolator(ci);
         return elemClass.isPrimitive() ? toPrimitiveArray(src, elemClass,
@@ -151,7 +155,7 @@ public class DefaultConversionHandler im
                     "Target collection must not be null!");
         }
 
-        if (src != null)
+        if (src != null && !isEmptyElement(src))
         {
             ConfigurationInterpolator interpolator = fetchInterpolator(ci);
             convertToCollection(src, elemClass, interpolator, dest);
@@ -181,6 +185,23 @@ public class DefaultConversionHandler im
     }
 
     /**
+     * Tests whether the passed in object represents an empty element. This
+     * method is called by conversion methods to arrays or collections. If it
+     * returns <b>true</b>, the resulting array or collection will be empty.
+     * This implementation returns <b>true</b> if and only if the passed in
+     * object is an empty string. With this method it can be controlled if and
+     * how empty elements in configurations are handled.
+     *
+     * @param src the object to be tested
+     * @return a flag whether this object is an empty element
+     */
+    protected boolean isEmptyElement(Object src)
+    {
+        return (src instanceof CharSequence)
+                && ((CharSequence) src).length() == 0;
+    }
+
+    /**
      * Performs the conversion from the passed in source object to the specified
      * target class. This method is called for each conversion to be done. The
      * source object has already been passed to the

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/convert/TestDefaultConversionHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/convert/TestDefaultConversionHandler.java?rev=1516578&r1=1516577&r2=1516578&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/convert/TestDefaultConversionHandler.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/convert/TestDefaultConversionHandler.java
Thu Aug 22 20:01:24 2013
@@ -209,6 +209,18 @@ public class TestDefaultConversionHandle
     }
 
     /**
+     * Tests a conversion to a collection if an empty string is passed in. An
+     * empty string should be interpreted as a list with no values.
+     */
+    @Test
+    public void testToCollectionEmptyString()
+    {
+        List<Integer> col = new ArrayList<Integer>(1);
+        handler.toCollection("", Integer.class, null, col);
+        assertTrue("Got elements", col.isEmpty());
+    }
+
+    /**
      * Tests toArray() if the source object is null.
      */
     @Test
@@ -287,6 +299,17 @@ public class TestDefaultConversionHandle
     }
 
     /**
+     * Tests a conversion to an array from an empty string. An empty string
+     * should be interpreted as an empty array.
+     */
+    @Test
+    public void testToArrayEmptyString()
+    {
+        int[] array = (int[]) handler.toArray("", Integer.TYPE, null);
+        assertEquals("Got elements", 0, array.length);
+    }
+
+    /**
      * Tests whether the default date format is used if no format has been set.
      */
     @Test



Mime
View raw message