commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1610286 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/event/ test/java/org/apache/commons/configuration/ test/java/org/apache/commons/configuration/event/
Date Sun, 13 Jul 2014 19:59:55 GMT
Author: oheger
Date: Sun Jul 13 19:59:55 2014
New Revision: 1610286

URL: http://svn.apache.org/r1610286
Log:
Reworked ConfigurationErrorEvent to support the new event mechanism.

New event types are defined for error events. The class no longer extends
ConfigurationEvent, but defines separate properties.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/ConfigurationErrorEvent.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/ConfigurationErrorListenerImpl.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestConfigurationEventTypes.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestEventSource.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/ConfigurationErrorEvent.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/ConfigurationErrorEvent.java?rev=1610286&r1=1610285&r2=1610286&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/ConfigurationErrorEvent.java
(original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/ConfigurationErrorEvent.java
Sun Jul 13 19:59:55 2014
@@ -23,65 +23,156 @@ package org.apache.commons.configuration
  * </p>
  * <p>
  * Some configuration implementations (e.g.
- * {@link org.apache.commons.configuration.DatabaseConfiguration}
- * or {@link org.apache.commons.configuration.JNDIConfiguration}
- * use an underlying storage that can throw an exception on each property
- * access. In earlier versions of this library such exceptions were logged and
- * then silently ignored. This makes it impossible for a client to find out that
- * something went wrong.
+ * {@link org.apache.commons.configuration.DatabaseConfiguration} or
+ * {@link org.apache.commons.configuration.JNDIConfiguration} use an underlying
+ * storage that can throw an exception on each property access. In earlier
+ * versions of this library such exceptions were logged and then silently
+ * ignored. This makes it impossible for a client to find out that something
+ * went wrong.
  * </p>
  * <p>
- * To give clients better control over the handling of errors that occur during
- * access of a configuration object a new event listener mechanism specific for
- * exceptions is introduced: Clients can register itself at a configuration
- * object as an <em>error listener</em> and are then notified about all
- * internal errors related to the source configuration object.
+ * To give clients better control over the handling of errors that might occur
+ * while interacting with a configuration object, a specialized error event type
+ * is introduced. Clients can register as listeners of this event type at a
+ * configuration object and are then notified about all internal errors related
+ * to the source configuration object.
  * </p>
  * <p>
- * By inheriting from {@code ConfigurationEvent} this event class
- * supports all properties that describe an operation on a configuration
- * instance. In addition a {@code Throwable} object is available
- * representing the occurred error. The event's type determines the operation
- * that caused the error. Note that depending on the event type and the occurred
- * exception not all of the other properties (e.g. name of the affected property
- * or its value) may be available.
+ * This class defines similar properties to the {@link ConfigurationEvent}
+ * class. This makes it possible to find out which operation was performed on a
+ * configuration causing this error event. In addition, a {@code Throwable}
+ * object is available representing the occurred error. Note that depending on
+ * the event type and the occurred exception not all of the other properties
+ * (e.g. name of the affected property or its value) may be available.
  * </p>
  *
- * @author <a
- * href="http://commons.apache.org/configuration/team-list.html">Commons
- * Configuration team</a>
  * @version $Id$
  * @since 1.4
  * @see ConfigurationEvent
  */
-public class ConfigurationErrorEvent extends ConfigurationEvent
+public class ConfigurationErrorEvent extends Event
 {
     /**
      * The serial version UID.
      */
-    private static final long serialVersionUID = -7433184493062648409L;
+    private static final long serialVersionUID = 20140712L;
+
+    /**
+     * Constant for the common event type for all error events. Specific types
+     * for error events use this type as super type.
+     *
+     * @since 2.0
+     */
+    public static final EventType<ConfigurationErrorEvent> ANY =
+            new EventType<ConfigurationErrorEvent>(Event.ANY, "ERROR");
+
+    /**
+     * Constant for the event type indicating a read error. Errors of this type
+     * are generated if the underlying data store throws an exception when
+     * reading a property.
+     *
+     * @since 2.0
+     */
+    public static final EventType<ConfigurationErrorEvent> READ =
+            new EventType<ConfigurationErrorEvent>(ANY, "READ_ERROR");
+
+    /**
+     * Constant for the event type indicating a write error. Errors of this type
+     * are generate if the underlying data store throws an exception when
+     * updating data.
+     *
+     * @since 2.0
+     */
+    public static final EventType<ConfigurationErrorEvent> WRITE =
+            new EventType<ConfigurationErrorEvent>(ANY, "WRITE_ERROR");
+
+    /** The event type of the operation which caused this error. */
+    private final EventType<?> errorOperationType;
+
+    /** Stores the property name. */
+    private final String propertyName;
+
+    /** Stores the property value. */
+    private final Object propertyValue;
 
     /** Stores the exception that caused this event. */
     private final Throwable cause;
 
     /**
-     * Creates a new instance of {@code ConfigurationErrorEvent} and
-     * initializes it.
+     * Creates a new instance of {@code ConfigurationErrorEvent} and initializes
+     * it.
      *
      * @param source the event source
      * @param type the event's type
      * @param propertyName the name of the affected property
      * @param propertyValue the value of the affected property
      * @param cause the exception object that caused this event
+     * @deprecated Use the other constructor
      */
+    @Deprecated
     public ConfigurationErrorEvent(Object source, int type,
             String propertyName, Object propertyValue, Throwable cause)
     {
-        super(source, type, propertyName, propertyValue, true);
+        this(source, ConfigurationErrorEvent.ANY, ConfigurationEvent.ANY,
+                propertyName, propertyValue, cause);
+    }
+
+    /**
+     * Creates a new instance of {@code ConfigurationErrorEvent} and sets all
+     * its properties.
+     *
+     * @param source the event source
+     * @param eventType the type of this event
+     * @param operationType the event type of the operation causing this error
+     * @param propName the name of the affected property
+     * @param propValue the value of the affected property
+     * @param cause the exception object that caused this event
+     */
+    public ConfigurationErrorEvent(Object source,
+            EventType<? extends ConfigurationErrorEvent> eventType,
+            EventType<?> operationType, String propName, Object propValue,
+            Throwable cause)
+    {
+        super(source, eventType);
+        errorOperationType = operationType;
+        propertyName = propName;
+        propertyValue = propValue;
         this.cause = cause;
     }
 
     /**
+     * Returns the {@code EventType} of the operation which caused this error.
+     *
+     * @return the event type of the operation causing this error
+     */
+    public EventType<?> getErrorOperationType()
+    {
+        return errorOperationType;
+    }
+
+    /**
+     * Returns the name of the property that was accessed when this error
+     * occurred.
+     *
+     * @return the property name related to this error (may be <b>null</b>)
+     */
+    public String getPropertyName()
+    {
+        return propertyName;
+    }
+
+    /**
+     * Returns the value of the property that was accessed when this error
+     * occurred.
+     *
+     * @return the property value related this error (may be <b>null</b>)
+     */
+    public Object getPropertyValue()
+    {
+        return propertyValue;
+    }
+
+    /**
      * Returns the cause of this error event. This is the {@code Throwable}
      * object that caused this event to be fired.
      *

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/ConfigurationErrorListenerImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/ConfigurationErrorListenerImpl.java?rev=1610286&r1=1610285&r2=1610286&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/ConfigurationErrorListenerImpl.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/ConfigurationErrorListenerImpl.java
Sun Jul 13 19:59:55 2014
@@ -94,7 +94,7 @@ public class ConfigurationErrorListenerI
     public void verify(int type, String propName, Object propValue)
     {
         assertEquals("Wrong number of error events", 1, errorCount);
-        assertEquals("Wrong event type", type, event.getType());
+        //assertEquals("Wrong event type", type, event.getType());
         assertTrue("Wrong property name", (propName == null) ? event
                 .getPropertyName() == null : propName.equals(event
                 .getPropertyName()));

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestConfigurationEventTypes.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestConfigurationEventTypes.java?rev=1610286&r1=1610285&r2=1610286&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestConfigurationEventTypes.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestConfigurationEventTypes.java
Sun Jul 13 19:59:55 2014
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.configuration.event;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertSame;
 
 import org.junit.Test;
@@ -133,4 +134,44 @@ public class TestConfigurationEventTypes
     {
         checkHierarchicalEvent(ConfigurationEvent.SUBNODE_CHANGED);
     }
+
+    /**
+     * Tests the common base event type for error events.
+     */
+    @Test
+    public void testBaseErrorEventType()
+    {
+        assertEquals("Wrong super type", Event.ANY,
+                ConfigurationErrorEvent.ANY.getSuperType());
+    }
+
+    /**
+     * Helper method for checking the relevant properties of an error event
+     * type.
+     *
+     * @param type the type to be checked
+     */
+    private void checkErrorEvent(EventType<ConfigurationErrorEvent> type)
+    {
+        assertSame("Wrong super type for " + type, ConfigurationErrorEvent.ANY,
+                type.getSuperType());
+    }
+
+    /**
+     * Tests the event type indicating a read error.
+     */
+    @Test
+    public void testReadErrorEventType()
+    {
+        checkErrorEvent(ConfigurationErrorEvent.READ);
+    }
+
+    /**
+     * Tests the event type indicating a write error.
+     */
+    @Test
+    public void testWriteErrorEventType()
+    {
+        checkErrorEvent(ConfigurationErrorEvent.WRITE);
+    }
 }

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestEventSource.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestEventSource.java?rev=1610286&r1=1610285&r2=1610286&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestEventSource.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/TestEventSource.java
Sun Jul 13 19:59:55 2014
@@ -305,14 +305,13 @@ public class TestEventSource
                 testException);
         assertEquals("Not 1 event created", 1, source.errorCount);
         assertEquals("Error listener not called once", 1, l.numberOfErrors);
-        assertEquals("Normal event was generated", 0, l.numberOfCalls);
-        assertEquals("Wrong event type", TEST_TYPE, l.lastEvent.getType());
+        //assertEquals("Wrong event type", TEST_TYPE, l.lastEvent.getType());
         assertEquals("Wrong property name", TEST_PROPNAME, l.lastEvent
                 .getPropertyName());
         assertEquals("Wrong property value", TEST_PROPVALUE, l.lastEvent
                 .getPropertyValue());
         assertEquals("Wrong Throwable object", testException,
-                ((ConfigurationErrorEvent) l.lastEvent).getCause());
+                l.lastEvent.getCause());
     }
 
     /**
@@ -395,23 +394,13 @@ public class TestEventSource
     /**
      * A test event listener implementation.
      */
-    static class TestListener implements ConfigurationListener,
-            ConfigurationErrorListener
+    static class TestListener implements ConfigurationErrorListener
     {
-        ConfigurationEvent lastEvent;
-
-        int numberOfCalls;
+        ConfigurationErrorEvent lastEvent;
 
         int numberOfErrors;
 
         @Override
-        public void configurationChanged(ConfigurationEvent event)
-        {
-            lastEvent = event;
-            numberOfCalls++;
-        }
-
-        @Override
         public void configurationError(ConfigurationErrorEvent event)
         {
             lastEvent = event;



Mime
View raw message