/** * Registers an event listener. * * @param listener the event listener (may not be <code>null</code>). * * @throws NullPointerException if <code>listener</code> is * <code>null</code>. */ public void addListener(final L listener) { addListener(listener, true); }
/** * Create the {@link InvocationHandler} responsible for broadcasting calls * to the managed listeners. Subclasses can override to provide custom behavior. * @return ProxyInvocationHandler */ protected InvocationHandler createInvocationHandler() { return new ProxyInvocationHandler(); }
/** * Creates an EventListenerSupport object which supports the specified * listener type. * * @param <T> the type of the listener interface * @param listenerInterface the type of listener interface that will receive * events posted using this class. * * @return an EventListenerSupport object which supports the specified * listener type. * * @throws NullPointerException if <code>listenerInterface</code> is * <code>null</code>. * @throws IllegalArgumentException if <code>listenerInterface</code> is * not an interface. */ public static <T> EventListenerSupport<T> create(final Class<T> listenerInterface) { return new EventListenerSupport<>(listenerInterface); }
@Test(expected = NullPointerException.class) public void testAddNullListener() { final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class); listenerSupport.addListener(null); }
@Test 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 PropertyChangeEvent(new Date(), "Day", Integer.valueOf(0), Integer.valueOf(1))); assertEquals(1, counter.getCount()); src.listeners.fire().event2(new PropertyChangeEvent(new Date(), "Day", Integer.valueOf(1), Integer.valueOf(2))); assertEquals(1, counter.getCount()); }
@Test public void testAddEventListener() { final PropertyChangeSource src = new PropertyChangeSource(); final EventCountingInvociationHandler handler = new EventCountingInvociationHandler(); final PropertyChangeListener listener = handler.createListener(PropertyChangeListener.class); assertEquals(0, handler.getEventCount("propertyChange")); EventUtils.addEventListener(src, PropertyChangeListener.class, listener); assertEquals(0, handler.getEventCount("propertyChange")); src.setProperty("newValue"); assertEquals(1, handler.getEventCount("propertyChange")); }
@Test public void testAddEventListenerWithNoAddMethod() { final PropertyChangeSource src = new PropertyChangeSource(); final EventCountingInvociationHandler handler = new EventCountingInvociationHandler(); final ObjectChangeListener listener = handler.createListener(ObjectChangeListener.class); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Class " + src.getClass().getName() + " does not have a public add" + ObjectChangeListener.class.getSimpleName() + " method which takes a parameter of type " + ObjectChangeListener.class.getName() + "."); EventUtils.addEventListener(src, ObjectChangeListener.class, listener); }
@Test(expected = NullPointerException.class) public void testRemoveNullListener() { final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class); listenerSupport.removeListener(null); }
@Override public void vetoableChange(final PropertyChangeEvent e) { listenerSupport.removeListener(this); } });
@Test public void testAddEventListenerThrowsException() { final ExceptionEventSource src = new ExceptionEventSource(); expectedException.expect(RuntimeException.class); EventUtils.addEventListener(src, PropertyChangeListener.class, new PropertyChangeListener() { @Override public void propertyChange(final PropertyChangeEvent e) { // Do nothing! } }); }
@Test 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()); }
@Test public void testBindEventsToMethod() { final PropertyChangeSource src = new PropertyChangeSource(); final EventCounter counter = new EventCounter(); EventUtils.bindEventsToMethod(counter, "eventOccurred", src, PropertyChangeListener.class); assertEquals(0, counter.getCount()); src.setProperty("newValue"); assertEquals(1, counter.getCount()); }
/** * Initialize transient fields. * @param listenerInterface the class of the listener interface * @param classLoader the class loader to be used */ private void initializeTransientFields(final Class<L> listenerInterface, final ClassLoader classLoader) { @SuppressWarnings("unchecked") // Will throw CCE here if not correct final L[] array = (L[]) Array.newInstance(listenerInterface, 0); this.prototypeArray = array; createProxy(listenerInterface, classLoader); }
@Test(expected = IllegalArgumentException.class) public void testCreateWithNonInterfaceParameter() { EventListenerSupport.create(String.class); }
private void addDeregisterListener(final EventListenerSupport<VetoableChangeListener> listenerSupport) { listenerSupport.addListener(new VetoableChangeListener() { @Override public void vetoableChange(final PropertyChangeEvent e) { listenerSupport.removeListener(this); } }); }
@Test public void testAddEventListenerWithPrivateAddMethod() { final PropertyChangeSource src = new PropertyChangeSource(); final EventCountingInvociationHandler handler = new EventCountingInvociationHandler(); final VetoableChangeListener listener = handler.createListener(VetoableChangeListener.class); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Class " + src.getClass().getName() + " does not have a public add" + VetoableChangeListener.class.getSimpleName() + " method which takes a parameter of type " + VetoableChangeListener.class.getName() + "."); EventUtils.addEventListener(src, VetoableChangeListener.class, listener); }
public void removePropertyChangeListener(final PropertyChangeListener listener) { listeners.removeListener(listener); } }
@Test(expected = NullPointerException.class) public void testCreateWithNullParameter() { EventListenerSupport.create(null); }
public void addMultipleEventListener(final MultipleEventListener listener) { listeners.addListener(listener); } }
public void addPropertyChangeListener(final PropertyChangeListener listener) { listeners.addListener(listener); }