Author: jcarman
Date: Thu Jul 22 19:09:18 2010
New Revision: 966807

URL: http://svn.apache.org/viewvc?rev=966807&view=rev
Log:
Improving error checking logic and adding test cases to verify.

Modified:
    
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java
    
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java

Modified: 
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java
URL: 
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java?rev=966807&r1=966806&r2=966807&view=diff
==============================================================================
--- 
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java
 (original)
+++ 
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/event/EventListenerSupport.java
 Thu Jul 22 19:09:18 2010
@@ -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;
  * </pre>
  *
  * @param <L> The event listener type
+ *
+ * @since 3.0
  */
 public class EventListenerSupport<L>
 {
@@ -77,6 +81,9 @@ public class EventListenerSupport<L>
      */
     public EventListenerSupport(Class<L> 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<L>
      */
     public void addListener(L listener)
     {
+        Validate.notNull(listener, "Listener object cannot be null.");
         listeners.add(0, listener);
     }
 
@@ -122,6 +130,7 @@ public class EventListenerSupport<L>
      */
     public void removeListener(L listener)
     {
+        Validate.notNull(listener, "Listener object cannot be null.");
         listeners.remove(listener);
     }
 

Modified: 
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java?rev=966807&r1=966806&r2=966807&view=diff
==============================================================================
--- 
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java
 (original)
+++ 
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/event/EventListenerSupportTest.java
 Thu Jul 22 19:09:18 2010
@@ -41,6 +41,32 @@ public class EventListenerSupportTest ex
         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<ActionListener> listenerSupport = 
EventListenerSupport.create(ActionListener.class);


Reply via email to