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 2014/07/13 22:02:34 UTC

svn commit: r1610290 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/event/ test/java/org/apache/commons/configuration/event/

Author: oheger
Date: Sun Jul 13 20:02:33 2014
New Revision: 1610290

URL: http://svn.apache.org/r1610290
Log:
Reworked clearing of error listeners.

BaseEventSource.clearErrorListeners() now removes all event listener
registrations of an event type derived from an error event. The list with
deprecated error listeners is no longer used for this purpose.

Added:
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/ErrorListenerTestImpl.java
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=1610290&r1=1610289&r2=1610290&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:02:33 2014
@@ -115,6 +115,18 @@ public class BaseEventSource implements 
     }
 
     /**
+     * Returns a list with all {@code EventListenerRegistrationData} objects
+     * currently contained for this event source. This method allows access to
+     * all registered event listeners, independent on their type.
+     *
+     * @return a list with information about all registered event listeners
+     */
+    public List<EventListenerRegistrationData<?>> getEventListenerRegistrations()
+    {
+        return eventListeners.getRegistrations();
+    }
+
+    /**
      * Returns a flag whether detail events are enabled.
      *
      * @return a flag if detail events are generated
@@ -190,7 +202,11 @@ public class BaseEventSource implements 
      */
     public void clearErrorListeners()
     {
-        errorListeners.clear();
+        for (EventListenerRegistrationData<? extends ConfigurationErrorEvent> reg : eventListeners
+                .getRegistrationsForSuperType(ConfigurationErrorEvent.ANY))
+        {
+            eventListeners.removeEventListener(reg);
+        }
     }
 
     /**

Added: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/ErrorListenerTestImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/ErrorListenerTestImpl.java?rev=1610290&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/ErrorListenerTestImpl.java (added)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/ErrorListenerTestImpl.java Sun Jul 13 20:02:33 2014
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration.event;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * A test event listener implementation for error events.
+ *
+ * @version $Id$
+ */
+public class ErrorListenerTestImpl extends
+        AbstractEventListenerTestImpl<ConfigurationErrorEvent>
+{
+    /**
+     * Creates a new instance of {@code ErrorListenerTestImpl} and sets the
+     * expected event source.
+     *
+     * @param source the event source (<b>null</b> if the source need not to be
+     *        checked)
+     */
+    public ErrorListenerTestImpl(Object source)
+    {
+        super(source);
+    }
+
+    /**
+     * Checks the next event which has been received by this listener.
+     *
+     * @param type the expected event type
+     * @param opType the event type for the failed operation
+     * @param propName the expected property name
+     * @param propValue the expected property value
+     * @return the exception stored in the next error event
+     */
+    public Throwable checkEvent(EventType<?> type, EventType<?> opType,
+            String propName, Object propValue)
+    {
+        ConfigurationErrorEvent e = nextEvent(type);
+        assertEquals("Wrong operation event type", opType,
+                e.getErrorOperationType());
+        assertEquals("Wrong property name", propName, e.getPropertyName());
+        assertEquals("Wrong property value", propValue, e.getPropertyValue());
+        return e.getCause();
+    }
+}

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=1610290&r1=1610289&r2=1610290&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:02:33 2014
@@ -18,9 +18,11 @@ package org.apache.commons.configuration
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 
 import java.util.Collection;
+import java.util.List;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -392,6 +394,29 @@ public class TestEventSource
     }
 
     /**
+     * Tests whether all error listeners can be cleared.
+     */
+    @Test
+    public void testClearErrorListeners()
+    {
+        EventListener<ConfigurationEvent> cl = new EventListenerTestImpl(null);
+        ErrorListenerTestImpl el1 = new ErrorListenerTestImpl(null);
+        ErrorListenerTestImpl el2 = new ErrorListenerTestImpl(null);
+        ErrorListenerTestImpl el3 = new ErrorListenerTestImpl(null);
+        source.addEventListener(ConfigurationErrorEvent.READ, el1);
+        source.addEventListener(ConfigurationErrorEvent.ANY, el2);
+        source.addEventListener(ConfigurationEvent.ANY, cl);
+        source.addEventListener(ConfigurationErrorEvent.WRITE, el3);
+
+        source.clearErrorListeners();
+        List<EventListenerRegistrationData<?>> regs =
+                source.getEventListenerRegistrations();
+        assertEquals("Wrong number of event listener registrations", 1,
+                regs.size());
+        assertSame("Wrong remaining listener", cl, regs.get(0).getListener());
+    }
+
+    /**
      * A test event listener implementation.
      */
     static class TestListener implements ConfigurationErrorListener
@@ -414,7 +439,7 @@ public class TestEventSource
      * {@code fireEvent()} methods only creates event objects if
      * necessary. It also allows testing the clone() operation.
      */
-    static class CountingEventSource extends BaseEventSource implements Cloneable
+    private static class CountingEventSource extends BaseEventSource implements Cloneable
     {
         int eventCount;