From bebf1a53f744bd591c692ef2b721bd28078bb029 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Wed, 1 Dec 2010 07:18:16 +0000 Subject: [PATCH] Moving away from testing with java.awt code because on OS X it kicks off a Java UI window, also causing Gump to fail when running headless. See: http://mail-archives.apache.org/mod_mbox/commons-dev/201011.mbox/%3C87lj4p1ekq.fsf@v35516.1blu.de%3E and its replies git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1040879 13f79535-47bb-0310-9956-ffa450edef68 --- .../lang3/event/EventListenerSupportTest.java | 100 +++++++++--------- .../commons/lang3/event/EventUtilsTest.java | 24 ++--- 2 files changed, 63 insertions(+), 61 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 index 22316651b..87c5ab8e5 100644 --- a/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java +++ b/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java @@ -17,8 +17,9 @@ package org.apache.commons.lang3.event; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; +import java.beans.PropertyVetoException; +import java.beans.PropertyChangeEvent; +import java.beans.VetoableChangeListener; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -26,6 +27,7 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.Date; import java.util.List; import junit.framework.TestCase; @@ -40,7 +42,7 @@ public class EventListenerSupportTest extends TestCase { public void testAddNullListener() { - EventListenerSupport listenerSupport = EventListenerSupport.create(ActionListener.class); + EventListenerSupport listenerSupport = EventListenerSupport.create(VetoableChangeListener.class); try { listenerSupport.addListener(null); @@ -54,7 +56,7 @@ public class EventListenerSupportTest extends TestCase public void testRemoveNullListener() { - EventListenerSupport listenerSupport = EventListenerSupport.create(ActionListener.class); + EventListenerSupport listenerSupport = EventListenerSupport.create(VetoableChangeListener.class); try { listenerSupport.removeListener(null); @@ -66,16 +68,16 @@ public class EventListenerSupportTest extends TestCase } } - public void testEventDispatchOrder() + public void testEventDispatchOrder() throws PropertyVetoException { - EventListenerSupport listenerSupport = EventListenerSupport.create(ActionListener.class); - final List calledListeners = new ArrayList(); + EventListenerSupport listenerSupport = EventListenerSupport.create(VetoableChangeListener.class); + final List calledListeners = new ArrayList(); - final ActionListener listener1 = createListener(calledListeners); - final ActionListener listener2 = createListener(calledListeners); + final VetoableChangeListener listener1 = createListener(calledListeners); + final VetoableChangeListener listener2 = createListener(calledListeners); listenerSupport.addListener(listener1); listenerSupport.addListener(listener2); - listenerSupport.fire().actionPerformed(new ActionEvent("Hello", 0, "Hello")); + listenerSupport.fire().vetoableChange(new PropertyChangeEvent(new Date(), "Day", 4, 5)); assertEquals(calledListeners.size(), 2); assertSame(calledListeners.get(0), listener1); assertSame(calledListeners.get(1), listener2); @@ -107,32 +109,32 @@ public class EventListenerSupportTest extends TestCase } } - public void testRemoveListenerDuringEvent() + public void testRemoveListenerDuringEvent() throws PropertyVetoException { - final EventListenerSupport listenerSupport = EventListenerSupport.create(ActionListener.class); + final EventListenerSupport listenerSupport = EventListenerSupport.create(VetoableChangeListener.class); for (int i = 0; i < 10; ++i) { addDeregisterListener(listenerSupport); } assertEquals(listenerSupport.getListenerCount(), 10); - listenerSupport.fire().actionPerformed(new ActionEvent("Hello", 0, "Hello")); + listenerSupport.fire().vetoableChange(new PropertyChangeEvent(new Date(), "Day", 4, 5)); assertEquals(listenerSupport.getListenerCount(), 0); } public void testGetListeners() { - final EventListenerSupport listenerSupport = EventListenerSupport.create(ActionListener.class); + final EventListenerSupport listenerSupport = EventListenerSupport.create(VetoableChangeListener.class); - ActionListener[] listeners = listenerSupport.getListeners(); + VetoableChangeListener[] listeners = listenerSupport.getListeners(); assertEquals(0, listeners.length); - assertEquals(ActionListener.class, listeners.getClass().getComponentType()); - ActionListener[] empty = listeners; + assertEquals(VetoableChangeListener.class, listeners.getClass().getComponentType()); + VetoableChangeListener[] empty = listeners; //for fun, show that the same empty instance is used assertSame(empty, listenerSupport.getListeners()); - ActionListener listener1 = EasyMock.createNiceMock(ActionListener.class); + VetoableChangeListener listener1 = EasyMock.createNiceMock(VetoableChangeListener.class); listenerSupport.addListener(listener1); assertEquals(1, listenerSupport.getListeners().length); - ActionListener listener2 = EasyMock.createNiceMock(ActionListener.class); + VetoableChangeListener listener2 = EasyMock.createNiceMock(VetoableChangeListener.class); listenerSupport.addListener(listener2); assertEquals(2, listenerSupport.getListeners().length); listenerSupport.removeListener(listener1); @@ -141,14 +143,14 @@ public class EventListenerSupportTest extends TestCase assertSame(empty, listenerSupport.getListeners()); } - public void testSerialization() throws IOException, ClassNotFoundException { - EventListenerSupport listenerSupport = EventListenerSupport.create(ActionListener.class); - listenerSupport.addListener(new ActionListener() { + public void testSerialization() throws IOException, ClassNotFoundException, PropertyVetoException { + EventListenerSupport listenerSupport = EventListenerSupport.create(VetoableChangeListener.class); + listenerSupport.addListener(new VetoableChangeListener() { - public void actionPerformed(ActionEvent e) { + public void vetoableChange(PropertyChangeEvent e) { } }); - listenerSupport.addListener(EasyMock.createNiceMock(ActionListener.class)); + listenerSupport.addListener(EasyMock.createNiceMock(VetoableChangeListener.class)); //serialize: ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); @@ -159,20 +161,20 @@ public class EventListenerSupportTest extends TestCase //deserialize: @SuppressWarnings("unchecked") - EventListenerSupport deserializedListenerSupport = (EventListenerSupport) new ObjectInputStream( + EventListenerSupport deserializedListenerSupport = (EventListenerSupport) new ObjectInputStream( new ByteArrayInputStream(outputStream.toByteArray())).readObject(); //make sure we get a listener array back, of the correct component type, and that it contains only the serializable mock - ActionListener[] listeners = deserializedListenerSupport.getListeners(); - assertEquals(ActionListener.class, listeners.getClass().getComponentType()); + VetoableChangeListener[] listeners = deserializedListenerSupport.getListeners(); + assertEquals(VetoableChangeListener.class, listeners.getClass().getComponentType()); assertEquals(1, listeners.length); //now verify that the mock still receives events; we can infer that the proxy was correctly reconstituted - ActionListener listener = listeners[0]; - ActionEvent evt = new ActionEvent(new Object(), 666, "sit"); - listener.actionPerformed(evt); + VetoableChangeListener listener = listeners[0]; + PropertyChangeEvent evt = new PropertyChangeEvent(new Date(), "Day", 7, 9); + listener.vetoableChange(evt); EasyMock.replay(listener); - deserializedListenerSupport.fire().actionPerformed(evt); + deserializedListenerSupport.fire().vetoableChange(evt); EasyMock.verify(listener); //remove listener and verify we get an empty array of listeners @@ -180,11 +182,11 @@ public class EventListenerSupportTest extends TestCase assertEquals(0, deserializedListenerSupport.getListeners().length); } - public void testSubclassInvocationHandling() { + public void testSubclassInvocationHandling() throws PropertyVetoException { @SuppressWarnings("serial") - EventListenerSupport eventListenerSupport = new EventListenerSupport( - ActionListener.class) { + EventListenerSupport eventListenerSupport = new EventListenerSupport( + VetoableChangeListener.class) { protected java.lang.reflect.InvocationHandler createInvocationHandler() { return new ProxyInvocationHandler() { /** @@ -193,42 +195,42 @@ public class EventListenerSupportTest extends TestCase @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - return "actionPerformed".equals(method.getName()) - && "ignore".equals(((ActionEvent) args[0]).getActionCommand()) ? null + return "vetoableChange".equals(method.getName()) + && "Hour".equals(((PropertyChangeEvent) args[0]).getPropertyName()) ? null : super.invoke(proxy, method, args); } }; }; }; - ActionListener listener = EasyMock.createNiceMock(ActionListener.class); + VetoableChangeListener listener = EasyMock.createNiceMock(VetoableChangeListener.class); eventListenerSupport.addListener(listener); - Object source = new Object(); - ActionEvent ignore = new ActionEvent(source, 0, "ignore"); - ActionEvent respond = new ActionEvent(source, 1, "respond"); - listener.actionPerformed(respond); + Object source = new Date(); + PropertyChangeEvent ignore = new PropertyChangeEvent(source, "Hour", 5, 6); + PropertyChangeEvent respond = new PropertyChangeEvent(source, "Day", 6, 7); + listener.vetoableChange(respond); EasyMock.replay(listener); - eventListenerSupport.fire().actionPerformed(ignore); - eventListenerSupport.fire().actionPerformed(respond); + eventListenerSupport.fire().vetoableChange(ignore); + eventListenerSupport.fire().vetoableChange(respond); EasyMock.verify(listener); } - private void addDeregisterListener(final EventListenerSupport listenerSupport) + private void addDeregisterListener(final EventListenerSupport listenerSupport) { - listenerSupport.addListener(new ActionListener() + listenerSupport.addListener(new VetoableChangeListener() { - public void actionPerformed(ActionEvent e) + public void vetoableChange(PropertyChangeEvent e) { listenerSupport.removeListener(this); } }); } - private ActionListener createListener(final List calledListeners) + private VetoableChangeListener createListener(final List calledListeners) { - return new ActionListener() + return new VetoableChangeListener() { - public void actionPerformed(ActionEvent e) + public void vetoableChange(PropertyChangeEvent 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 index e28b93cd9..db80ea1f3 100644 --- a/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java @@ -16,14 +16,14 @@ */ package org.apache.commons.lang3.event; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; +import javax.naming.event.ObjectChangeListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.beans.VetoableChangeListener; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; +import java.util.Date; import java.util.Map; import java.util.TreeMap; @@ -51,15 +51,15 @@ public class EventUtilsTest extends TestCase { final PropertyChangeSource src = new PropertyChangeSource(); EventCountingInvociationHandler handler = new EventCountingInvociationHandler(); - ActionListener listener = handler.createListener(ActionListener.class); + ObjectChangeListener listener = handler.createListener(ObjectChangeListener.class); try { - EventUtils.addEventListener(src, ActionListener.class, listener); + EventUtils.addEventListener(src, ObjectChangeListener.class, listener); fail("Should not be allowed to add a listener to an object that doesn't support it."); } catch (IllegalArgumentException e) { - assertEquals("Class " + src.getClass().getName() + " does not have a public add" + ActionListener.class.getSimpleName() + " method which takes a parameter of type " + ActionListener.class.getName() + ".", e.getMessage()); + assertEquals("Class " + src.getClass().getName() + " does not have a public add" + ObjectChangeListener.class.getSimpleName() + " method which takes a parameter of type " + ObjectChangeListener.class.getName() + ".", e.getMessage()); } } @@ -68,9 +68,9 @@ public class EventUtilsTest extends TestCase final ExceptionEventSource src = new ExceptionEventSource(); try { - EventUtils.addEventListener(src, ActionListener.class, new ActionListener() + EventUtils.addEventListener(src, PropertyChangeListener.class, new PropertyChangeListener() { - public void actionPerformed(ActionEvent e) + public void propertyChange(PropertyChangeEvent e) { // Do nothing! } @@ -127,17 +127,17 @@ public class EventUtilsTest extends TestCase final EventCounter counter = new EventCounter(); EventUtils.bindEventsToMethod(counter, "eventOccurred", src, MultipleEventListener.class, "event1"); assertEquals(0, counter.getCount()); - src.listeners.fire().event1(new ActionEvent(src, ActionEvent.ACTION_PERFORMED, "event1")); + src.listeners.fire().event1(new PropertyChangeEvent(new Date(), "Day", 0, 1)); assertEquals(1, counter.getCount()); - src.listeners.fire().event2(new ActionEvent(src, ActionEvent.ACTION_PERFORMED, "event2")); + src.listeners.fire().event2(new PropertyChangeEvent(new Date(), "Day", 1, 2)); assertEquals(1, counter.getCount()); } public static interface MultipleEventListener { - public void event1(ActionEvent e); + public void event1(PropertyChangeEvent e); - public void event2(ActionEvent e); + public void event2(PropertyChangeEvent e); } public static class EventCounter @@ -215,7 +215,7 @@ public class EventUtilsTest extends TestCase public static class ExceptionEventSource { - public void addActionListener(ActionListener listener) + public void addPropertyChangeListener(PropertyChangeListener listener) { throw new RuntimeException(); }