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
This commit is contained in:
parent
a43c5746fc
commit
c3b4a3d362
|
@ -17,6 +17,8 @@
|
||||||
|
|
||||||
package org.apache.commons.lang3.event;
|
package org.apache.commons.lang3.event;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.Validate;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationHandler;
|
import java.lang.reflect.InvocationHandler;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Proxy;
|
import java.lang.reflect.Proxy;
|
||||||
|
@ -41,6 +43,8 @@ import java.util.List;
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param <L> The event listener type
|
* @param <L> The event listener type
|
||||||
|
*
|
||||||
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public class EventListenerSupport<L>
|
public class EventListenerSupport<L>
|
||||||
{
|
{
|
||||||
|
@ -77,6 +81,9 @@ public class EventListenerSupport<L>
|
||||||
*/
|
*/
|
||||||
public EventListenerSupport(Class<L> listenerInterface, ClassLoader classLoader)
|
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},
|
proxy = listenerInterface.cast(Proxy.newProxyInstance(classLoader, new Class[]{listenerInterface},
|
||||||
new ProxyInvocationHandler()));
|
new ProxyInvocationHandler()));
|
||||||
}
|
}
|
||||||
|
@ -102,6 +109,7 @@ public class EventListenerSupport<L>
|
||||||
*/
|
*/
|
||||||
public void addListener(L listener)
|
public void addListener(L listener)
|
||||||
{
|
{
|
||||||
|
Validate.notNull(listener, "Listener object cannot be null.");
|
||||||
listeners.add(0, listener);
|
listeners.add(0, listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,6 +130,7 @@ public class EventListenerSupport<L>
|
||||||
*/
|
*/
|
||||||
public void removeListener(L listener)
|
public void removeListener(L listener)
|
||||||
{
|
{
|
||||||
|
Validate.notNull(listener, "Listener object cannot be null.");
|
||||||
listeners.remove(listener);
|
listeners.remove(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,6 +41,32 @@ public class EventListenerSupportTest extends TestCase
|
||||||
assertSame(calledListeners.get(1), listener2);
|
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()
|
public void testRemoveListenerDuringEvent()
|
||||||
{
|
{
|
||||||
final EventListenerSupport<ActionListener> listenerSupport = EventListenerSupport.create(ActionListener.class);
|
final EventListenerSupport<ActionListener> listenerSupport = EventListenerSupport.create(ActionListener.class);
|
||||||
|
|
Loading…
Reference in New Issue