aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorJames W. Carman <jcarman@apache.org>2010-07-22 11:35:07 +0000
committerJames W. Carman <jcarman@apache.org>2010-07-22 11:35:07 +0000
commit6ef1437ef4449459bd6167b427119c7c827d3af7 (patch)
tree6e0a6360f91a9418cdfe074297cbf60bd73ec6a8 /src/test/java
parent427ec8621ae471901a29e9c14a255aa4c038fec2 (diff)
downloadapache-commons-lang-6ef1437ef4449459bd6167b427119c7c827d3af7.tar.gz
Misc. event utils.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@966589 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java77
-rw-r--r--src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java123
2 files changed, 200 insertions, 0 deletions
diff --git a/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java b/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java
new file mode 100644
index 000000000..a1e3da162
--- /dev/null
+++ b/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.lang3.event;
+
+import junit.framework.TestCase;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.util.ArrayList;
+import java.util.List;
+
+public class EventListenerSupportTest extends TestCase
+{
+ public void testEventDispatchOrder()
+ {
+ EventListenerSupport<ActionListener> listenerSupport = EventListenerSupport.create(ActionListener.class);
+ final List<ActionListener> calledListeners = new ArrayList<ActionListener>();
+
+ final ActionListener listener1 = createListener(calledListeners);
+ final ActionListener listener2 = createListener(calledListeners);
+ listenerSupport.addListener(listener1);
+ listenerSupport.addListener(listener2);
+ listenerSupport.fire().actionPerformed(new ActionEvent("Hello", 0, "Hello"));
+ assertEquals(calledListeners.size(), 2);
+ assertSame(calledListeners.get(0), listener1);
+ assertSame(calledListeners.get(1), listener2);
+ }
+
+ public void testRemoveListenerDuringEvent()
+ {
+ final EventListenerSupport<ActionListener> listenerSupport = EventListenerSupport.create(ActionListener.class);
+ for (int i = 0; i < 10; ++i)
+ {
+ addDeregisterListener(listenerSupport);
+ }
+ assertEquals(listenerSupport.getListenerCount(), 10);
+ listenerSupport.fire().actionPerformed(new ActionEvent("Hello", 0, "Hello"));
+ assertEquals(listenerSupport.getListenerCount(), 0);
+ }
+
+ private void addDeregisterListener(final EventListenerSupport<ActionListener> listenerSupport)
+ {
+ listenerSupport.addListener(new ActionListener()
+ {
+ public void actionPerformed(ActionEvent e)
+ {
+ listenerSupport.removeListener(this);
+ }
+ });
+ }
+
+ private ActionListener createListener(final List<ActionListener> calledListeners)
+ {
+ return new ActionListener()
+ {
+ public void actionPerformed(ActionEvent e)
+ {
+ calledListeners.add(this);
+ }
+ };
+ }
+}
diff --git a/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java b/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java
new file mode 100644
index 000000000..3c4dd8d2d
--- /dev/null
+++ b/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java
@@ -0,0 +1,123 @@
+/*
+ * 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.lang3.event;
+
+import junit.framework.TestCase;
+
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.Map;
+import java.util.TreeMap;
+
+public class EventUtilsTest extends TestCase
+{
+ public void testAddEventListener()
+ {
+ final PropertyChangeSource src = new PropertyChangeSource();
+ EventCountingInvociationHandler handler = new EventCountingInvociationHandler();
+ PropertyChangeListener listener = handler.createListener(PropertyChangeListener.class);
+ assertEquals(0, handler.getEventCount("propertyChange"));
+ EventUtils.addEventListener(src, PropertyChangeListener.class, listener);
+ assertEquals(0, handler.getEventCount("propertyChange"));
+ src.setProperty("newValue");
+ assertEquals(1, handler.getEventCount("propertyChange"));
+ }
+
+ public void testBindEventsToMethod()
+ {
+ final PropertyChangeSource src = new PropertyChangeSource();
+ final EventCounter counter = new EventCounter();
+ EventUtils.bindEventsToMethod(counter, "eventOccurred", src, PropertyChangeListener.class);
+ assertEquals(0, counter.getCount());
+ src.setProperty("newValue");
+ assertEquals(1, counter.getCount());
+ }
+
+ public static class EventCounter
+ {
+ private int count;
+
+ public void eventOccurred()
+ {
+ count++;
+ }
+
+ public int getCount()
+ {
+ return count;
+ }
+ }
+
+ private static class EventCountingInvociationHandler implements InvocationHandler
+ {
+ private Map<String, Integer> eventCounts = new TreeMap<String, Integer>();
+
+ public <L> L createListener(Class<L> listenerType)
+ {
+ return listenerType.cast(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
+ new Class[]{listenerType},
+ this));
+ }
+
+ public int getEventCount(String eventName)
+ {
+ Integer count = eventCounts.get(eventName);
+ return count == null ? 0 : count;
+ }
+
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
+ {
+ Integer count = eventCounts.get(method.getName());
+ if (count == null)
+ {
+ eventCounts.put(method.getName(), 1);
+ }
+ else
+ {
+ eventCounts.put(method.getName(), count + 1);
+ }
+ return null;
+ }
+ }
+
+ public static class PropertyChangeSource
+ {
+ private EventListenerSupport<PropertyChangeListener> listeners = EventListenerSupport.create(PropertyChangeListener.class);
+
+ private String property;
+
+ public void setProperty(String property)
+ {
+ String oldValue = this.property;
+ this.property = property;
+ listeners.fire().propertyChange(new PropertyChangeEvent(this, "property", "oldValue", property));
+ }
+
+ public void addPropertyChangeListener(PropertyChangeListener listener)
+ {
+ listeners.addListener(listener);
+ }
+
+ public void removePropertyChangeListener(PropertyChangeListener listener)
+ {
+ listeners.removeListener(listener);
+ }
+ }
+}