From c3b4a3d362405f2e9fd3204e90eced2b80fc5776 Mon Sep 17 00:00:00 2001 From: "James W. Carman" Date: Thu, 22 Jul 2010 19:09:18 +0000 Subject: [PATCH] Improving error checking logic and adding test cases to verify. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@966807 13f79535-47bb-0310-9956-ffa450edef68 --- .../lang3/event/EventListenerSupport.java | 9 +++++++ .../lang3/event/EventListenerSupportTest.java | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java b/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java index fcee64514..82e3be04e 100644 --- a/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java +++ b/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java @@ -17,6 +17,8 @@ package org.apache.commons.lang3.event; +import org.apache.commons.lang3.Validate; + import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; @@ -41,6 +43,8 @@ import java.util.List; * * * @param The event listener type + * + * @since 3.0 */ public class EventListenerSupport { @@ -77,6 +81,9 @@ public class EventListenerSupport */ public EventListenerSupport(Class listenerInterface, ClassLoader classLoader) { + Validate.notNull(listenerInterface, "Listener interface cannot be null."); + Validate.notNull(classLoader, "ClassLoader cannot be null."); + Validate.isTrue(listenerInterface.isInterface(), "Class {0} is not an interface", listenerInterface.getName()); proxy = listenerInterface.cast(Proxy.newProxyInstance(classLoader, new Class[]{listenerInterface}, new ProxyInvocationHandler())); } @@ -102,6 +109,7 @@ public class EventListenerSupport */ public void addListener(L listener) { + Validate.notNull(listener, "Listener object cannot be null."); listeners.add(0, listener); } @@ -122,6 +130,7 @@ public class EventListenerSupport */ public void removeListener(L listener) { + Validate.notNull(listener, "Listener object cannot be null."); listeners.remove(listener); } 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 b4c11f6f1..85110d9e6 100644 --- a/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java +++ b/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java @@ -41,6 +41,32 @@ public class EventListenerSupportTest extends TestCase assertSame(calledListeners.get(1), listener2); } + public void testCreateWithNonInterfaceParameter() + { + try + { + EventListenerSupport.create(String.class); + fail("Should not be able to create using non-interface class."); + } + catch(IllegalArgumentException e) + { + + } + } + + public void testCreateWithNullParameter() + { + try + { + EventListenerSupport.create(null); + fail("Should not be able to create using null class."); + } + catch(NullPointerException e) + { + + } + } + public void testRemoveListenerDuringEvent() { final EventListenerSupport listenerSupport = EventListenerSupport.create(ActionListener.class);