Author: jcarman
Date: Thu Jul 22 13:30:28 2010
New Revision: 966644
URL: http://svn.apache.org/viewvc?rev=966644&view=rev
Log:
Improving test coverage.
Modified:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/event/EventUtils.java
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java
Modified:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/event/EventUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/event/EventUtils.java?rev=966644&r1=966643&r2=966644&view=diff
==============================================================================
---
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/event/EventUtils.java
(original)
+++
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/event/EventUtils.java
Thu Jul 22 13:30:28 2010
@@ -29,6 +29,16 @@ import java.util.Set;
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 <L> the event listener type
+ *
+ * @throws IllegalArgumentException if the object doesn't support the
listener type
+ */
public static <L> void addEventListener(Object eventSource, Class<L>
listenerType, L listener)
{
try
@@ -37,11 +47,11 @@ public class EventUtils
}
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 class EventUtils
}
else
{
- return MethodUtils.invokeMethod(target, methodName, new
Object[]{});
+ return MethodUtils.invokeMethod(target, methodName);
}
}
return null;
Modified:
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java?rev=966644&r1=966643&r2=966644&view=diff
==============================================================================
---
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java
(original)
+++
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/event/EventUtilsTest.java
Thu Jul 22 13:30:28 2010
@@ -18,8 +18,11 @@ package org.apache.commons.lang3.event;
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 class EventUtilsTest extends Test
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 class EventUtilsTest extends Test
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 class EventUtilsTest extends Test
}
}
+ 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<String, Integer> eventCounts = new TreeMap<String,
Integer>();
@@ -97,6 +199,24 @@ public class EventUtilsTest extends Test
}
}
+ public static class MultipleEventSource
+ {
+ private EventListenerSupport<MultipleEventListener> 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<PropertyChangeListener> listeners =
EventListenerSupport.create(PropertyChangeListener.class);
@@ -107,7 +227,12 @@ public class EventUtilsTest extends Test
{
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)