Author: oheger
Date: Fri Jul 11 20:30:34 2014
New Revision: 1609813
URL: http://svn.apache.org/r1609813
Log:
Added a method to BaseEventSource to copy event listener registrations.
This is useful if event listeners registered for one object should be active
for another event source, too.
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=1609813&r1=1609812&r2=1609813&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:30:34 2014
@@ -249,6 +249,25 @@ public class BaseEventSource implements
}
/**
+ * Copies all event listener registrations maintained by this object to the
+ * specified {@code BaseEventSource} object.
+ *
+ * @param source the target source for the copy operation (must not be
+ * <b>null</b>)
+ * @throws IllegalArgumentException if the target source is <b>null</b>
+ * @since 2.0
+ */
+ public void copyEventListeners(BaseEventSource source)
+ {
+ if (source == null)
+ {
+ throw new IllegalArgumentException(
+ "Target event source must not be null!");
+ }
+ source.eventListeners.addAll(eventListeners);
+ }
+
+ /**
* Creates an event object and delivers it to all registered event
* listeners. The method will check first if sending an event is allowed
* (making use of the {@code detailEvents} property), and if
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=1609813&r1=1609812&r2=1609813&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
Fri Jul 11 20:30:34 2014
@@ -361,6 +361,38 @@ public class TestEventSource
}
/**
+ * Tests whether event listeners can be copied to another source.
+ */
+ @Test
+ public void testCopyEventListeners()
+ {
+ EventListenerTestImpl l1 = new EventListenerTestImpl(source);
+ EventListenerTestImpl l2 = new EventListenerTestImpl(source);
+ source.addEventListener(ConfigurationEvent.ANY, l1);
+ source.addEventListener(ConfigurationEvent.ANY_HIERARCHICAL, l2);
+
+ BaseEventSource source2 = new BaseEventSource();
+ source.copyEventListeners(source2);
+ Collection<EventListener<? super ConfigurationEvent>> listeners =
+ source2.getEventListeners(ConfigurationEvent.ANY_HIERARCHICAL);
+ assertEquals("Wrong number of listeners (1)", 2, listeners.size());
+ assertTrue("l1 not found", listeners.contains(l1));
+ assertTrue("l2 not found", listeners.contains(l2));
+ listeners = source2.getEventListeners(ConfigurationEvent.ANY);
+ assertEquals("Wrong number of listeners (2)", 1, listeners.size());
+ assertTrue("Wrong listener", listeners.contains(l1));
+ }
+
+ /**
+ * Tries to copy event listeners to a null source.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testCopyEventListenersNullSource()
+ {
+ source.copyEventListeners(null);
+ }
+
+ /**
* A test event listener implementation.
*/
static class TestListener implements ConfigurationListener,