commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1609791 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/ main/java/org/apache/commons/configuration/event/ test/java/org/apache/commons/configuration/event/
Date Fri, 11 Jul 2014 20:16:54 GMT
Author: oheger
Date: Fri Jul 11 20:16:54 2014
New Revision: 1609791

URL: http://svn.apache.org/r1609791
Log:
Implemented methods for new event handlers in BaseEventSource.

Adapted event handling in AbstractConfiguration and made the base test for
event handling run again.

Added:
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/EventListenerTestImpl.java
Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/AbstractConfiguration.java
    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/AbstractTestConfigurationEvents.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/AbstractConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/AbstractConfiguration.java?rev=1609791&r1=1609790&r2=1609791&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/AbstractConfiguration.java
(original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/AbstractConfiguration.java
Fri Jul 11 20:16:54 2014
@@ -38,6 +38,7 @@ import org.apache.commons.configuration.
 import org.apache.commons.configuration.event.BaseEventSource;
 import org.apache.commons.configuration.event.ConfigurationErrorEvent;
 import org.apache.commons.configuration.event.ConfigurationErrorListener;
+import org.apache.commons.configuration.event.ConfigurationEvent;
 import org.apache.commons.configuration.ex.ConfigurationRuntimeException;
 import org.apache.commons.configuration.ex.ConversionException;
 import org.apache.commons.configuration.interpol.ConfigurationInterpolator;
@@ -710,9 +711,9 @@ public abstract class AbstractConfigurat
         beginWrite(false);
         try
         {
-            fireEvent(EVENT_ADD_PROPERTY, key, value, true);
+            fireEvent(ConfigurationEvent.ADD_PROPERTY, key, value, true);
             addPropertyInternal(key, value);
-            fireEvent(EVENT_ADD_PROPERTY, key, value, false);
+            fireEvent(ConfigurationEvent.ADD_PROPERTY, key, value, false);
         }
         finally
         {
@@ -793,9 +794,9 @@ public abstract class AbstractConfigurat
         beginWrite(false);
         try
         {
-            fireEvent(EVENT_SET_PROPERTY, key, value, true);
+            fireEvent(ConfigurationEvent.SET_PROPERTY, key, value, true);
             setPropertyInternal(key, value);
-            fireEvent(EVENT_SET_PROPERTY, key, value, false);
+            fireEvent(ConfigurationEvent.SET_PROPERTY, key, value, false);
         }
         finally
         {
@@ -841,9 +842,9 @@ public abstract class AbstractConfigurat
         beginWrite(false);
         try
         {
-            fireEvent(EVENT_CLEAR_PROPERTY, key, null, true);
+            fireEvent(ConfigurationEvent.CLEAR_PROPERTY, key, null, true);
             clearPropertyDirect(key);
-            fireEvent(EVENT_CLEAR_PROPERTY, key, null, false);
+            fireEvent(ConfigurationEvent.CLEAR_PROPERTY, key, null, false);
         }
         finally
         {
@@ -866,9 +867,9 @@ public abstract class AbstractConfigurat
         beginWrite(false);
         try
         {
-            fireEvent(EVENT_CLEAR, null, null, true);
+            fireEvent(ConfigurationEvent.CLEAR, null, null, true);
             clearInternal();
-            fireEvent(EVENT_CLEAR, null, null, false);
+            fireEvent(ConfigurationEvent.CLEAR, null, null, false);
         }
         finally
         {

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=1609791&r1=1609790&r2=1609791&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
Fri Jul 11 20:16:54 2014
@@ -75,6 +75,9 @@ public class BaseEventSource implements 
     /** A collection for the registered error listeners.*/
     private Collection<ConfigurationErrorListener> errorListeners;
 
+    /** The list for managing registered event listeners. */
+    private EventListenerList eventListeners;
+
     /** A lock object for guarding access to the detail events counter. */
     private final Object lockDetailEventsCount = new Object();
 
@@ -175,15 +178,14 @@ public class BaseEventSource implements 
     public <T extends Event> void addEventListener(EventType<T> eventType,
             EventListener<? super T> listener)
     {
-        // TODO implementation
+        eventListeners.addEventListener(eventType, listener);
     }
 
     @Override
     public <T extends Event> boolean removeEventListener(
             EventType<T> eventType, EventListener<? super T> listener)
     {
-        // TODO implementation
-        return false;
+        return eventListeners.removeEventListener(eventType, listener);
     }
 
     /**
@@ -220,7 +222,9 @@ public class BaseEventSource implements 
      * @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 before the before update flag
+     * @deprecated Use fireEvent() with an EventType
      */
+    @Deprecated
     protected void fireEvent(int type, String propName, Object propValue, boolean before)
     {
         if (checkDetailEvents(-1))
@@ -239,6 +243,36 @@ public class BaseEventSource implements 
     }
 
     /**
+     * Creates an event object and delivers it to all registered event
+     * listeners. The method checks first if sending an event is allowed (making
+     * use of the {@code detailEvents} property), and if listeners are
+     * registered.
+     *
+     * @param type the event's type
+     * @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 before the before update flag
+     */
+    protected <T extends ConfigurationEvent> void fireEvent(EventType<T> type,
+            String propName, Object propValue, boolean before)
+    {
+        if (checkDetailEvents(-1))
+        {
+            EventListenerList.EventListenerIterator<T> it =
+                    eventListeners.getEventListenerIterator(type);
+            if (it.hasNext())
+            {
+                ConfigurationEvent event =
+                        createEvent(type, propName, propValue, before);
+                while (it.hasNext())
+                {
+                    it.invokeNext(event);
+                }
+            }
+        }
+    }
+
+    /**
      * Creates a {@code ConfigurationEvent} object based on the passed in
      * parameters. This is called by {@code fireEvent()} if it decides
      * that an event needs to be generated.
@@ -255,6 +289,23 @@ public class BaseEventSource implements 
     }
 
     /**
+     * Creates a {@code ConfigurationEvent} object based on the passed in
+     * parameters. This method is called by {@code fireEvent()} if it decides
+     * that an event needs to be generated.
+     *
+     * @param type the event's type
+     * @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 before the before update flag
+     * @return the newly created event object
+     */
+    protected <T extends ConfigurationEvent> ConfigurationEvent createEvent(
+            EventType<T> type, String propName, Object propValue, boolean before)
+    {
+        return new ConfigurationEvent(this, type, propName, propValue, before);
+    }
+
+    /**
      * Creates an error event object and delivers it to all registered error
      * listeners.
      *
@@ -336,6 +387,7 @@ public class BaseEventSource implements 
     {
         listeners = new CopyOnWriteArrayList<ConfigurationListener>();
         errorListeners = new CopyOnWriteArrayList<ConfigurationErrorListener>();
+        eventListeners = new EventListenerList();
     }
 
     /**

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/AbstractTestConfigurationEvents.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/AbstractTestConfigurationEvents.java?rev=1609791&r1=1609790&r2=1609791&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/AbstractTestConfigurationEvents.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/AbstractTestConfigurationEvents.java
Fri Jul 11 20:16:54 2014
@@ -47,16 +47,23 @@ public abstract class AbstractTestConfig
     /** The configuration to be tested. */
     protected AbstractConfiguration config;
 
-    /** A test event listener. */
+    /**
+     * A test event listener.
+     * @deprecated Use the "modern" listener.
+     */
     protected ConfigurationListenerTestImpl l;
 
+    /** A test event listener. */
+    protected EventListenerTestImpl listener;
+
     @Before
     public void setUp() throws Exception
     {
         config = createConfiguration();
         config.addProperty(EXIST_PROPERTY, "existing value");
         l = new ConfigurationListenerTestImpl(config);
-        config.addConfigurationListener(l);
+        listener = new EventListenerTestImpl(config);
+        config.addEventListener(ConfigurationEvent.ANY, listener);
     }
 
     /**
@@ -73,11 +80,11 @@ public abstract class AbstractTestConfig
     public void testAddPropertyEvent()
     {
         config.addProperty(TEST_PROPNAME, TEST_PROPVALUE);
-        l.checkEvent(AbstractConfiguration.EVENT_ADD_PROPERTY, TEST_PROPNAME,
+        listener.checkEvent(ConfigurationEvent.ADD_PROPERTY, TEST_PROPNAME,
                 TEST_PROPVALUE, true);
-        l.checkEvent(AbstractConfiguration.EVENT_ADD_PROPERTY, TEST_PROPNAME,
+        listener.checkEvent(ConfigurationEvent.ADD_PROPERTY, TEST_PROPNAME,
                 TEST_PROPVALUE, false);
-        l.done();
+        listener.done();
     }
 
     /**
@@ -88,13 +95,13 @@ public abstract class AbstractTestConfig
     {
         config.setDetailEvents(true);
         config.addProperty(TEST_PROPNAME, TEST_PROPVALUE);
-        l.checkEventCount(2);
-        l.checkEvent(AbstractConfiguration.EVENT_ADD_PROPERTY, TEST_PROPNAME,
+        listener.checkEventCount(2);
+        listener.checkEvent(ConfigurationEvent.ADD_PROPERTY, TEST_PROPNAME,
                 TEST_PROPVALUE, true);
-        l.skipToLast(AbstractConfiguration.EVENT_ADD_PROPERTY);
-        l.checkEvent(AbstractConfiguration.EVENT_ADD_PROPERTY, TEST_PROPNAME,
+        listener.skipToLast(ConfigurationEvent.ADD_PROPERTY);
+        listener.checkEvent(ConfigurationEvent.ADD_PROPERTY, TEST_PROPNAME,
                 TEST_PROPVALUE, false);
-        l.done();
+        listener.done();
     }
 
     /**
@@ -104,11 +111,11 @@ public abstract class AbstractTestConfig
     public void testClearPropertyEvent()
     {
         config.clearProperty(EXIST_PROPERTY);
-        l.checkEvent(AbstractConfiguration.EVENT_CLEAR_PROPERTY,
+        listener.checkEvent(ConfigurationEvent.CLEAR_PROPERTY,
                 EXIST_PROPERTY, null, true);
-        l.checkEvent(AbstractConfiguration.EVENT_CLEAR_PROPERTY,
+        listener.checkEvent(ConfigurationEvent.CLEAR_PROPERTY,
                 EXIST_PROPERTY, null, false);
-        l.done();
+        listener.done();
     }
 
     /**
@@ -119,13 +126,13 @@ public abstract class AbstractTestConfig
     {
         config.setDetailEvents(true);
         config.clearProperty(EXIST_PROPERTY);
-        l.checkEventCount(2);
-        l.checkEvent(AbstractConfiguration.EVENT_CLEAR_PROPERTY,
+        listener.checkEventCount(2);
+        listener.checkEvent(ConfigurationEvent.CLEAR_PROPERTY,
                 EXIST_PROPERTY, null, true);
-        l.skipToLast(AbstractConfiguration.EVENT_CLEAR_PROPERTY);
-        l.checkEvent(AbstractConfiguration.EVENT_CLEAR_PROPERTY,
+        listener.skipToLast(ConfigurationEvent.CLEAR_PROPERTY);
+        listener.checkEvent(ConfigurationEvent.CLEAR_PROPERTY,
                 EXIST_PROPERTY, null, false);
-        l.done();
+        listener.done();
     }
 
     /**
@@ -135,11 +142,11 @@ public abstract class AbstractTestConfig
     public void testSetPropertyEvent()
     {
         config.setProperty(EXIST_PROPERTY, TEST_PROPVALUE);
-        l.checkEvent(AbstractConfiguration.EVENT_SET_PROPERTY, EXIST_PROPERTY,
+        listener.checkEvent(ConfigurationEvent.SET_PROPERTY, EXIST_PROPERTY,
                 TEST_PROPVALUE, true);
-        l.checkEvent(AbstractConfiguration.EVENT_SET_PROPERTY, EXIST_PROPERTY,
+        listener.checkEvent(ConfigurationEvent.SET_PROPERTY, EXIST_PROPERTY,
                 TEST_PROPVALUE, false);
-        l.done();
+        listener.done();
     }
 
     /**
@@ -150,13 +157,13 @@ public abstract class AbstractTestConfig
     {
         config.setDetailEvents(true);
         config.setProperty(EXIST_PROPERTY, TEST_PROPVALUE);
-        l.checkEventCount(2);
-        l.checkEvent(AbstractConfiguration.EVENT_SET_PROPERTY, EXIST_PROPERTY,
+        listener.checkEventCount(2);
+        listener.checkEvent(ConfigurationEvent.SET_PROPERTY, EXIST_PROPERTY,
                 TEST_PROPVALUE, true);
-        l.skipToLast(AbstractConfiguration.EVENT_SET_PROPERTY);
-        l.checkEvent(AbstractConfiguration.EVENT_SET_PROPERTY, EXIST_PROPERTY,
+        listener.skipToLast(ConfigurationEvent.SET_PROPERTY);
+        listener.checkEvent(ConfigurationEvent.SET_PROPERTY, EXIST_PROPERTY,
                 TEST_PROPVALUE, false);
-        l.done();
+        listener.done();
     }
 
     /**
@@ -166,9 +173,9 @@ public abstract class AbstractTestConfig
     public void testClearEvent()
     {
         config.clear();
-        l.checkEvent(AbstractConfiguration.EVENT_CLEAR, null, null, true);
-        l.checkEvent(AbstractConfiguration.EVENT_CLEAR, null, null, false);
-        l.done();
+        listener.checkEvent(ConfigurationEvent.CLEAR, null, null, true);
+        listener.checkEvent(ConfigurationEvent.CLEAR, null, null, false);
+        listener.done();
     }
 
     /**
@@ -180,10 +187,10 @@ public abstract class AbstractTestConfig
     {
         config.setDetailEvents(true);
         config.clear();
-        l.checkEventCount(2);
-        l.checkEvent(AbstractConfiguration.EVENT_CLEAR, null, null, true);
-        l.skipToLast(AbstractConfiguration.EVENT_CLEAR);
-        l.checkEvent(AbstractConfiguration.EVENT_CLEAR, null, null, false);
-        l.done();
+        listener.checkEventCount(2);
+        listener.checkEvent(ConfigurationEvent.CLEAR, null, null, true);
+        listener.skipToLast(ConfigurationEvent.CLEAR);
+        listener.checkEvent(ConfigurationEvent.CLEAR, null, null, false);
+        listener.done();
     }
 }

Added: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/EventListenerTestImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/EventListenerTestImpl.java?rev=1609791&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/EventListenerTestImpl.java
(added)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/event/EventListenerTestImpl.java
Fri Jul 11 20:16:54 2014
@@ -0,0 +1,128 @@
+/*
+ * 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;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * A test event listener class that can be used for testing whether
+ * event sources generated correct events.
+ *
+ * @version $Id$
+ */
+public class EventListenerTestImpl implements EventListener<ConfigurationEvent>
+{
+    /** The expected event source. */
+    private final Object expectedSource;
+
+    /** Stores the received events. */
+    private final List<ConfigurationEvent> events;
+
+    /**
+     * Creates a new instance of {@code EventListenerTestImpl} and sets
+     * the expected event source.
+     *
+     * @param source the event source (<b>null</b> if the source need not to
be
+     *        checked)
+     */
+    public EventListenerTestImpl(Object source)
+    {
+        expectedSource = source;
+        events = new LinkedList<ConfigurationEvent>();
+    }
+
+    @Override
+    public void onEvent(ConfigurationEvent event) {
+        events.add(event);
+    }
+
+    /**
+     * Checks if at least {@code minEvents} events have been received.
+     *
+     * @param minEvents the minimum number of expected events
+     */
+    public void checkEventCount(int minEvents)
+    {
+        assertTrue("Too view events received", events.size() >= minEvents);
+    }
+
+    /**
+     * Checks an expected event.
+     *
+     * @param type the event type
+     * @param propName the expected property name
+     * @param propValue the expected property value
+     * @param before the expected before flag
+     */
+    public void checkEvent(EventType<?> type, String propName, Object propValue,
+            boolean before)
+    {
+        ConfigurationEvent e = nextEvent(type);
+        assertEquals("Wrong property name", propName, e.getPropertyName());
+        assertEquals("Wrong property value", propValue, e.getPropertyValue());
+        assertEquals("Wrong before flag", before, e.isBeforeUpdate());
+    }
+
+    /**
+     * Returns the next received event and checks for the expected type. This
+     * method can be used instead of {@code checkEvent()} for comparing
+     * complex event values.
+     *
+     * @param expectedType the expected type of the event
+     * @return the event object
+     */
+    public ConfigurationEvent nextEvent(EventType<?> expectedType)
+    {
+        assertFalse("Too few events received", events.isEmpty());
+        ConfigurationEvent e = events.remove(0);
+        if (expectedSource != null)
+        {
+            assertEquals("Wrong event source", expectedSource, e.getSource());
+        }
+        assertEquals("Wrong event type", expectedType, e.getEventType());
+        return e;
+    }
+
+    /**
+     * Skips to the last received event and checks that no events of the given
+     * type have been received. This method is used by checks for detail events
+     * to ignore the detail events.
+     *
+     * @param type the event type
+     */
+    public void skipToLast(EventType<?> type)
+    {
+        while (events.size() > 1)
+        {
+            ConfigurationEvent e = events.remove(0);
+            assertTrue("Found end event in details", type != e.getEventType());
+        }
+    }
+
+    /**
+     * Checks if all events has been processed.
+     */
+    public void done()
+    {
+        assertTrue("Too many events received", events.isEmpty());
+    }
+}



Mime
View raw message