From d8547e9de14a53f50dd3589eaaa317b2049f20f4 Mon Sep 17 00:00:00 2001 From: "James W. Carman" Date: Thu, 22 Jul 2010 13:30:28 +0000 Subject: [PATCH] Improving test coverage. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@966644 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/lang3/event/EventUtils.java | 16 ++- .../commons/lang3/event/EventUtilsTest.java | 127 +++++++++++++++++- 2 files changed, 139 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/event/EventUtils.java b/src/main/java/org/apache/commons/lang3/event/EventUtils.java index 8a86777a7..d9169eef5 100644 --- a/src/main/java/org/apache/commons/lang3/event/EventUtils.java +++ b/src/main/java/org/apache/commons/lang3/event/EventUtils.java @@ -29,6 +29,16 @@ public class EventUtils { + /** + * Adds an event listener to the specified source. This looks for an "add" method corresponding to the event + * type (addActionListener, for example). + * @param eventSource the event source + * @param listenerType the event listener type + * @param listener the listener + * @param the event listener type + * + * @throws IllegalArgumentException if the object doesn't support the listener type + */ public static void addEventListener(Object eventSource, Class listenerType, L listener) { try @@ -37,11 +47,11 @@ public static void addEventListener(Object eventSource, Class listenerTyp } catch (NoSuchMethodException e) { - throw new IllegalArgumentException("Class " + eventSource.getClass() + " does not have an accesible add" + listenerType.getSimpleName() + " method which takes a parameter of type " + listenerType.getClass().getName() + "."); + throw new IllegalArgumentException("Class " + eventSource.getClass().getName() + " does not have a public add" + listenerType.getSimpleName() + " method which takes a parameter of type " + listenerType.getName() + "."); } catch (IllegalAccessException e) { - throw new IllegalArgumentException("Class " + eventSource.getClass() + " does not have an accesible add" + listenerType.getSimpleName () + " method which takes a parameter of type " + listenerType.getClass().getName() + "."); + throw new IllegalArgumentException("Class " + eventSource.getClass().getName() + " does not have an accesible add" + listenerType.getSimpleName () + " method which takes a parameter of type " + listenerType.getName() + "."); } catch (InvocationTargetException e) { @@ -88,7 +98,7 @@ public Object invoke(final Object proxy, final Method method, final Object[] par } else { - return MethodUtils.invokeMethod(target, methodName, new Object[]{}); + return MethodUtils.invokeMethod(target, methodName); } } return null; 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 f10464907..64b7f9162 100644 --- a/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java @@ -18,8 +18,11 @@ import junit.framework.TestCase; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; 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; @@ -40,6 +43,58 @@ public void testAddEventListener() assertEquals(1, handler.getEventCount("propertyChange")); } + public void testAddEventListenerWithNoAddMethod() + { + final PropertyChangeSource src = new PropertyChangeSource(); + EventCountingInvociationHandler handler = new EventCountingInvociationHandler(); + ActionListener listener = handler.createListener(ActionListener.class); + try + { + EventUtils.addEventListener(src, ActionListener.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()); + } + } + + public void testAddEventListenerThrowsException() + { + final ExceptionEventSource src = new ExceptionEventSource(); + try + { + EventUtils.addEventListener(src, ActionListener.class, new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + // Do nothing! + } + }); + fail("Add method should have thrown an exception, so method should fail."); + } + catch (RuntimeException e) + { + + } + } + + public void testAddEventListenerWithPrivateAddMethod() + { + final PropertyChangeSource src = new PropertyChangeSource(); + EventCountingInvociationHandler handler = new EventCountingInvociationHandler(); + VetoableChangeListener listener = handler.createListener(VetoableChangeListener.class); + try + { + EventUtils.addEventListener(src, VetoableChangeListener.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" + VetoableChangeListener.class.getSimpleName() + " method which takes a parameter of type " + VetoableChangeListener.class.getName() + ".", e.getMessage()); + } + } + public void testBindEventsToMethod() { final PropertyChangeSource src = new PropertyChangeSource(); @@ -50,6 +105,37 @@ public void testBindEventsToMethod() assertEquals(1, counter.getCount()); } + + public void testBindEventsToMethodWithEvent() + { + final PropertyChangeSource src = new PropertyChangeSource(); + final EventCounterWithEvent counter = new EventCounterWithEvent(); + EventUtils.bindEventsToMethod(counter, "eventOccurred", src, PropertyChangeListener.class); + assertEquals(0, counter.getCount()); + src.setProperty("newValue"); + assertEquals(1, counter.getCount()); + } + + + public void testBindFilteredEventsToMethod() + { + final MultipleEventSource src = new MultipleEventSource(); + 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")); + assertEquals(1, counter.getCount()); + src.listeners.fire().event2(new ActionEvent(src, ActionEvent.ACTION_PERFORMED, "event2")); + assertEquals(1, counter.getCount()); + } + + public static interface MultipleEventListener + { + public void event1(ActionEvent e); + + public void event2(ActionEvent e); + } + public static class EventCounter { private int count; @@ -65,6 +151,22 @@ public int getCount() } } + public static class EventCounterWithEvent + { + private int count; + + public void eventOccurred(PropertyChangeEvent e) + { + count++; + } + + public int getCount() + { + return count; + } + } + + private static class EventCountingInvociationHandler implements InvocationHandler { private Map eventCounts = new TreeMap(); @@ -97,6 +199,24 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl } } + public static class MultipleEventSource + { + private EventListenerSupport listeners = EventListenerSupport.create(MultipleEventListener.class); + + public void addMultipleEventListener(MultipleEventListener listener) + { + listeners.addListener(listener); + } + } + + public static class ExceptionEventSource + { + public void addActionListener(ActionListener listener) + { + throw new RuntimeException(); + } + } + public static class PropertyChangeSource { private EventListenerSupport listeners = EventListenerSupport.create(PropertyChangeListener.class); @@ -107,7 +227,12 @@ public void setProperty(String property) { String oldValue = this.property; this.property = property; - listeners.fire().propertyChange(new PropertyChangeEvent(this, "property", "oldValue", property)); + listeners.fire().propertyChange(new PropertyChangeEvent(this, "property", oldValue, property)); + } + + protected void addVetoableChangeListener(VetoableChangeListener listener) + { + // Do nothing! } public void addPropertyChangeListener(PropertyChangeListener listener)