You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by oh...@apache.org on 2007/01/28 17:24:54 UTC

svn commit: r500822 - in /jakarta/commons/proper/configuration/trunk: src/java/org/apache/commons/configuration/ConfigurationUtils.java src/test/org/apache/commons/configuration/TestConfigurationUtils.java xdocs/changes.xml

Author: oheger
Date: Sun Jan 28 08:24:53 2007
New Revision: 500822

URL: http://svn.apache.org/viewvc?view=rev&rev=500822
Log:
CONFIGURATION-245: Added new enableRuntimeExceptions() method to ConfigurationUtils as a convenient way for generating runtime exceptions from error events.

Modified:
    jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationUtils.java
    jakarta/commons/proper/configuration/trunk/xdocs/changes.xml

Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java?view=diff&rev=500822&r1=500821&r2=500822
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java Sun Jan 28 08:24:53 2007
@@ -30,6 +30,9 @@
 import java.net.URLDecoder;
 import java.util.Iterator;
 
+import org.apache.commons.configuration.event.ConfigurationErrorEvent;
+import org.apache.commons.configuration.event.ConfigurationErrorListener;
+import org.apache.commons.configuration.event.EventSource;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -643,5 +646,36 @@
         {
             return null;
         }
+    }
+
+    /**
+     * Enables runtime exceptions for the specified configuration object. This
+     * method can be used for configuration implementations that may face errors
+     * on normal property access, e.g. <code>DatabaseConfiguration</code> or
+     * <code>JNDIConfiguration</code>. Per default such errors are simply
+     * logged and then ignored. This implementation will register a special
+     * <code>{@link ConfigurationErrorListener}</code> that throws a runtime
+     * exception (namely a <code>ConfigurationRuntimeException</code>) on
+     * each received error event.
+     *
+     * @param src the configuration, for which runtime exceptions are to be
+     * enabled; this configuration must be derived from
+     * <code>{@link EventSource}</code>
+     */
+    public static void enableRuntimeExceptions(Configuration src)
+    {
+        if (!(src instanceof EventSource))
+        {
+            throw new IllegalArgumentException(
+                    "Configuration must be derived from EventSource!");
+        }
+        ((EventSource) src).addErrorListener(new ConfigurationErrorListener()
+        {
+            public void configurationError(ConfigurationErrorEvent event)
+            {
+                // Throw a runtime exception
+                throw new ConfigurationRuntimeException(event.getCause());
+            }
+        });
     }
 }

Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationUtils.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationUtils.java?view=diff&rev=500822&r1=500821&r2=500822
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationUtils.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationUtils.java Sun Jan 28 08:24:53 2007
@@ -23,6 +23,8 @@
 import java.util.Iterator;
 import java.util.List;
 
+import com.mockobjects.dynamic.Mock;
+
 import junit.framework.TestCase;
 import junitx.framework.ListAssert;
 
@@ -283,5 +285,68 @@
     {
         assertNull("Wrong return value", ConfigurationUtils
                 .cloneConfiguration(null));
+    }
+
+    /**
+     * Tests whether runtime exceptions can be enabled.
+     */
+    public void testEnableRuntimeExceptions()
+    {
+        PropertiesConfiguration config = new PropertiesConfiguration()
+        {
+            protected void addPropertyDirect(String key, Object value)
+            {
+                // always simulate an exception
+                fireError(EVENT_ADD_PROPERTY, key, value, new RuntimeException(
+                        "A faked exception!"));
+            }
+        };
+        config.clearErrorListeners();
+        ConfigurationUtils.enableRuntimeExceptions(config);
+        try
+        {
+            config.addProperty("test", "testValue");
+            fail("No runtime exception was thrown!");
+        }
+        catch (ConfigurationRuntimeException crex)
+        {
+            // ok
+        }
+    }
+
+    /**
+     * Tries to enable runtime exceptions for a configurtion that does not
+     * inherit from EventSource. This should cause an exception.
+     */
+    public void testEnableRuntimeExceptionsInvalid()
+    {
+        try
+        {
+            ConfigurationUtils
+                    .enableRuntimeExceptions((Configuration) new Mock(
+                            Configuration.class).proxy());
+            fail("Could enable exceptions for non EventSource configuration!");
+        }
+        catch (IllegalArgumentException iex)
+        {
+            // ok
+        }
+    }
+
+    /**
+     * Tries to enable runtime exceptions for a null configuration. This should
+     * cause an exception.
+     */
+    public void testEnableRuntimeExceptionsNull()
+    {
+        try
+        {
+            ConfigurationUtils.enableRuntimeExceptions(null);
+            fail("Could enable exceptions for a null configuration!");
+        }
+        catch (IllegalArgumentException iex)
+        {
+            //ok
+        }
     }
 }

Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?view=diff&rev=500822&r1=500821&r2=500822
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Sun Jan 28 08:24:53 2007
@@ -27,6 +27,9 @@
         In addition to configuration event listeners now so-called configuration
         error listeners are supported. These listeners are notified about
         internal errors that had been logged and swallowed by privious versions.
+        The new enableRuntimeExceptions() method of ConfigurationUtils
+        registers a special error listener at the passed in configuration that
+        generates a runtime exception when an error event is received.
       </action>
       <action dev="oheger" type="add">
         AbstractConfiguration now allows to set an instance specific logger



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org