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 2016/02/07 19:13:58 UTC

svn commit: r1728998 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration2/ImmutableConfigurationInvocationHandler.java test/java/org/apache/commons/configuration2/TestImmutableConfiguration.java

Author: oheger
Date: Sun Feb  7 18:13:58 2016
New Revision: 1728998

URL: http://svn.apache.org/viewvc?rev=1728998&view=rev
Log:
[CONFIGURATION-618] Improved exception handling for immutable configurations.

The invocation handler now handles InvocationTargetExceptions. The
cause of the exception is thrown which is the original exception thrown
by the wrapped configuration.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/ImmutableConfigurationInvocationHandler.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestImmutableConfiguration.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/ImmutableConfigurationInvocationHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/ImmutableConfigurationInvocationHandler.java?rev=1728998&r1=1728997&r2=1728998&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/ImmutableConfigurationInvocationHandler.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/ImmutableConfigurationInvocationHandler.java Sun Feb  7 18:13:58 2016
@@ -17,6 +17,7 @@
 package org.apache.commons.configuration2;
 
 import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.Iterator;
 
@@ -76,7 +77,15 @@ class ImmutableConfigurationInvocationHa
     public Object invoke(Object proxy, Method method, Object[] args)
             throws Throwable
     {
-        return handleResult(method.invoke(wrappedConfiguration, args));
+        try
+        {
+            return handleResult(method.invoke(wrappedConfiguration, args));
+        }
+        catch (InvocationTargetException e)
+        {
+            // unwrap
+            throw e.getCause();
+        }
     }
 
     /**

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestImmutableConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestImmutableConfiguration.java?rev=1728998&r1=1728997&r2=1728998&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestImmutableConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestImmutableConfiguration.java Sun Feb  7 18:13:58 2016
@@ -16,12 +16,16 @@
  */
 package org.apache.commons.configuration2;
 
+import static org.hamcrest.CoreMatchers.containsString;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.NoSuchElementException;
 import java.util.Set;
 
 import org.apache.commons.configuration2.builder.FileBasedBuilderParametersImpl;
@@ -206,4 +210,28 @@ public class TestImmutableConfiguration
         assertTrue("Property not found", ihc.getBoolean(key));
         assertEquals("Wrong max index", 0, ihc.getMaxIndex(key));
     }
+
+    /**
+     * Tests that exceptions thrown by the wrapped configuration are handled
+     * correctly.
+     */
+    @Test
+    public void testExceptionHandling()
+    {
+        PropertiesConfiguration config = new PropertiesConfiguration();
+        final String property = "nonExistingProperty";
+        config.setThrowExceptionOnMissing(true);
+        ImmutableConfiguration ic =
+                ConfigurationUtils.unmodifiableConfiguration(config);
+        try
+        {
+            ic.getString(property);
+            fail("Exception for missing property not thrown!");
+        }
+        catch (NoSuchElementException e)
+        {
+            assertThat("Wrong message", e.getMessage(),
+                    containsString(property));
+        }
+    }
 }