Improving test coverage.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@966644 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James W. Carman 2010-07-22 13:30:28 +00:00
parent a7cb9414f7
commit d8547e9de1
2 changed files with 139 additions and 4 deletions

View File

@ -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 <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 static <L> void addEventListener(Object eventSource, Class<L> 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;

View File

@ -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<String, Integer> eventCounts = new TreeMap<String, Integer>();
@ -97,6 +199,24 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
}
}
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 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)