commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1610291 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/event/BaseEventSource.java test/java/org/apache/commons/configuration/event/TestEventSource.java
Date Sun, 13 Jul 2014 20:03:18 GMT
Author: oheger
Date: Sun Jul 13 20:03:18 2014
New Revision: 1610291

URL: http://svn.apache.org/r1610291
Log:
Added a fireError() method compatible with the new listener mechanism.

The method calls regular EventListeners supporting the current error event
type.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/BaseEventSource.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/BaseEventSource.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/BaseEventSource.java?rev=1610291&r1=1610290&r2=1610291&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/BaseEventSource.java
(original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/event/BaseEventSource.java
Sun Jul 13 20:03:18 2014
@@ -318,6 +318,36 @@ public class BaseEventSource implements 
     }
 
     /**
+     * Creates an error event object and delivers it to all registered error
+     * listeners of a matching type.
+     *
+     * @param eventType the event's type
+     * @param operationType the type of the failed operation
+     * @param propertyName the name of the affected property (can be
+     *        <b>null</b>)
+     * @param propertyValue the value of the affected property (can be
+     *        <b>null</b>)
+     * @param cause the {@code Throwable} object that caused this error event
+     */
+    public <T extends ConfigurationErrorEvent> void fireError(
+            EventType<T> eventType, EventType<?> operationType,
+            String propertyName, Object propertyValue, Throwable cause)
+    {
+        EventListenerList.EventListenerIterator<T> iterator =
+                eventListeners.getEventListenerIterator(eventType);
+        if (iterator.hasNext())
+        {
+            ConfigurationErrorEvent event =
+                    createErrorEvent(eventType, operationType, propertyName,
+                            propertyValue, cause);
+            while (iterator.hasNext())
+            {
+                iterator.invokeNext(event);
+            }
+        }
+    }
+
+    /**
      * Creates a {@code ConfigurationErrorEvent} object based on the
      * passed in parameters. This is called by {@code fireError()} if it
      * decides that an event needs to be generated.
@@ -329,13 +359,35 @@ public class BaseEventSource implements 
      * event
      * @return the event object
      * @since 1.4
+     * @deprecated Use the method expecting an EventType
      */
+    @Deprecated
     protected ConfigurationErrorEvent createErrorEvent(int type, String propName, Object
propValue, Throwable ex)
     {
         return new ConfigurationErrorEvent(this, type, propName, propValue, ex);
     }
 
     /**
+     * Creates a {@code ConfigurationErrorEvent} object based on the passed in
+     * parameters. This is called by {@code fireError()} if it decides that an
+     * event needs to be generated.
+     *
+     * @param type the event's type
+     * @param opType the operation type related to this error
+     * @param propName the name of the affected property (can be <b>null</b>)
+     * @param propValue the value of the affected property (can be <b>null</b>)
+     * @param ex the {@code Throwable} object that caused this error event
+     * @return the event object
+     */
+    protected ConfigurationErrorEvent createErrorEvent(
+            EventType<? extends ConfigurationErrorEvent> type,
+            EventType<?> opType, String propName, Object propValue, Throwable ex)
+    {
+        return new ConfigurationErrorEvent(this, type, opType, propName,
+                propValue, ex);
+    }
+
+    /**
      * Overrides the {@code clone()} method to correctly handle so far
      * registered event listeners. This implementation ensures that the clone
      * will have empty event listener lists, i.e. the listeners registered at an

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=1610291&r1=1610290&r2=1610291&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 20:03:18 2014
@@ -300,20 +300,26 @@ public class TestEventSource
     @Test
     public void testFireError()
     {
-        TestListener l = new TestListener();
-        source.addErrorListener(l);
+        ErrorListenerTestImpl lstRead = new ErrorListenerTestImpl(source);
+        ErrorListenerTestImpl lstWrite = new ErrorListenerTestImpl(source);
+        ErrorListenerTestImpl lstAll = new ErrorListenerTestImpl(source);
+        source.addEventListener(ConfigurationErrorEvent.READ, lstRead);
+        source.addEventListener(ConfigurationErrorEvent.WRITE, lstWrite);
+        source.addEventListener(ConfigurationErrorEvent.ANY, lstAll);
         Exception testException = new Exception("A test");
-        source.fireError(TEST_TYPE, TEST_PROPNAME, TEST_PROPVALUE,
+
+        source.fireError(ConfigurationErrorEvent.WRITE,
+                ConfigurationEvent.ADD_PROPERTY, TEST_PROPNAME, TEST_PROPVALUE,
                 testException);
-        assertEquals("Not 1 event created", 1, source.errorCount);
-        assertEquals("Error listener not called once", 1, l.numberOfErrors);
-        //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,
-                l.lastEvent.getCause());
+        lstRead.done();
+        lstWrite.checkEvent(ConfigurationErrorEvent.WRITE,
+                ConfigurationEvent.ADD_PROPERTY, TEST_PROPNAME, TEST_PROPVALUE);
+        lstWrite.done();
+        lstAll.checkEvent(ConfigurationErrorEvent.WRITE,
+                ConfigurationEvent.ADD_PROPERTY, TEST_PROPNAME, TEST_PROPVALUE);
+        lstAll.done();
+        assertEquals("Wrong number of error events created", 1,
+                source.errorCount);
     }
 
     /**
@@ -322,8 +328,8 @@ public class TestEventSource
     @Test
     public void testFireErrorNoListeners()
     {
-        source.fireError(TEST_TYPE, TEST_PROPNAME, TEST_PROPVALUE,
-                new Exception());
+        source.fireError(ConfigurationErrorEvent.ANY, ConfigurationEvent.ANY,
+                TEST_PROPNAME, TEST_PROPVALUE, new Exception());
         assertEquals("An error event object was created", 0, source.errorCount);
     }
 
@@ -455,6 +461,17 @@ public class TestEventSource
         }
 
         @Override
+        protected ConfigurationErrorEvent createErrorEvent(
+                EventType<? extends ConfigurationErrorEvent> type,
+                EventType<?> opType, String propName, Object propValue,
+                Throwable ex)
+        {
+            errorCount++;
+            return super
+                    .createErrorEvent(type, opType, propName, propValue, ex);
+        }
+
+        @Override
         protected ConfigurationErrorEvent createErrorEvent(int type,
                 String propName, Object value, Throwable ex)
         {



Mime
View raw message