You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2013/08/22 22:01:55 UTC

svn commit: r1516579 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/AbstractConfiguration.java test/java/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java

Author: oheger
Date: Thu Aug 22 20:01:55 2013
New Revision: 1516579

URL: http://svn.apache.org/r1516579
Log:
The get() methods now handle the throwExceptionOnMissing flag correctly.

Previously, the methods did not throw an exception if the key could not be
resolved and the flag was set.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/AbstractConfiguration.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/AbstractConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/AbstractConfiguration.java?rev=1516579&r1=1516578&r2=1516579&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/AbstractConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/AbstractConfiguration.java Thu Aug 22 20:01:55 2013
@@ -1362,7 +1362,7 @@ public abstract class AbstractConfigurat
 
     public <T> T get(Class<T> cls, String key)
     {
-        return get(cls, key, null);
+        return convert(cls, key, null, true);
     }
 
     /**
@@ -1371,21 +1371,7 @@ public abstract class AbstractConfigurat
      */
     public <T> T get(Class<T> cls, String key, T defaultValue)
     {
-        Object value = getProperty(key);
-        try
-        {
-            return ObjectUtils.defaultIfNull(
-                    getConversionHandler().to(value, cls, getInterpolator()),
-                    defaultValue);
-        }
-        catch (ConversionException cex)
-        {
-            // improve error message
-            throw new ConversionException(
-                    String.format(
-                            "Key '%s' cannot be converted to class %s. Value is: '%s'.",
-                            key, cls.getName(), String.valueOf(value)));
-        }
+        return convert(cls, key, defaultValue, false);
     }
 
     public Object getArray(Class<?> cls, String key)
@@ -1632,6 +1618,36 @@ public abstract class AbstractConfigurat
     }
 
     /**
+     * Obtains the property value for the specified key and converts it to the
+     * given target class.
+     *
+     * @param <T> the target type of the conversion
+     * @param cls the target class
+     * @param key the key of the desired property
+     * @param defaultValue a default value
+     * @return the converted value of this property
+     * @throws ConversionException if the conversion cannot be performed
+     */
+    private <T> T getAndConvertProperty(Class<T> cls, String key, T defaultValue)
+    {
+        Object value = getProperty(key);
+        try
+        {
+            return ObjectUtils.defaultIfNull(
+                    getConversionHandler().to(value, cls, getInterpolator()),
+                    defaultValue);
+        }
+        catch (ConversionException cex)
+        {
+            // improve error message
+            throw new ConversionException(
+                    String.format(
+                            "Key '%s' cannot be converted to class %s. Value is: '%s'.",
+                            key, cls.getName(), String.valueOf(value)));
+        }
+    }
+
+    /**
      * Helper method for obtaining a property value with a type conversion.
      *
      * @param <T> the target type of the conversion
@@ -1645,7 +1661,7 @@ public abstract class AbstractConfigurat
     private <T> T convert(Class<T> cls, String key, T defValue,
             boolean throwOnMissing)
     {
-        T result = get(cls, key, defValue);
+        T result = getAndConvertProperty(cls, key, defValue);
         if (result == null)
         {
             if (throwOnMissing && isThrowExceptionOnMissing())

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java?rev=1516579&r1=1516578&r2=1516579&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestAbstractConfigurationBasicFeatures.java Thu Aug 22 20:01:55 2013
@@ -30,6 +30,7 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.NoSuchElementException;
 
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.configuration.convert.ConversionHandler;
@@ -726,6 +727,33 @@ public class TestAbstractConfigurationBa
     }
 
     /**
+     * Tests get() for an unknown property if the throwExceptionOnMissing
+     * flag is set.
+     */
+    @Test(expected = NoSuchElementException.class)
+    public void testGetUnknownWithThrowExceptionOnMissing()
+    {
+        PropertiesConfiguration config = new PropertiesConfiguration();
+        config.setThrowExceptionOnMissing(true);
+        config.get(Integer.class, KEY_PREFIX);
+    }
+
+    /**
+     * Tests get() for an unknown property with a default value and the
+     * throwExceptionOnMissing flag. Because of the default value no exception
+     * should be thrown.
+     */
+    @Test
+    public void testGetUnownWithDefaultValueThrowExceptionOnMissing()
+    {
+        PropertiesConfiguration config = new PropertiesConfiguration();
+        config.setThrowExceptionOnMissing(true);
+        Integer defaultValue = 2121;
+        assertEquals("Wrong result", defaultValue,
+                config.get(Integer.class, KEY_PREFIX, defaultValue));
+    }
+
+    /**
      * Tests whether conversion to an array is possible.
      */
     @Test