/** * 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); }
@Test public void testSerialization() throws IOException, ClassNotFoundException, PropertyVetoException { final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class); listenerSupport.addListener(new VetoableChangeListener() { listenerSupport.addListener(EasyMock.createNiceMock(VetoableChangeListener.class)); final VetoableChangeListener[] listeners = deserializedListenerSupport.getListeners(); assertEquals(VetoableChangeListener.class, listeners.getClass().getComponentType()); assertEquals(1, listeners.length); listener.vetoableChange(evt); EasyMock.replay(listener); deserializedListenerSupport.fire().vetoableChange(evt); EasyMock.verify(listener); deserializedListenerSupport.removeListener(listener); assertEquals(0, deserializedListenerSupport.getListeners().length);
/** * 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(expected = NullPointerException.class) public void testRemoveNullListener() { final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class); listenerSupport.removeListener(null); }
@Test public void testRemoveListenerDuringEvent() throws PropertyVetoException { final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class); for (int i = 0; i < 10; ++i) { addDeregisterListener(listenerSupport); } assertEquals(listenerSupport.getListenerCount(), 10); listenerSupport.fire().vetoableChange(new PropertyChangeEvent(new Date(), "Day", 4, 5)); assertEquals(listenerSupport.getListenerCount(), 0); }
public void setProperty(final String property) { final String oldValue = this.property; this.property = property; listeners.fire().propertyChange(new PropertyChangeEvent(this, "property", oldValue, property)); }
@Test public void testEventDispatchOrder() throws PropertyVetoException { final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class); final List<VetoableChangeListener> calledListeners = new ArrayList<>(); final VetoableChangeListener listener1 = createListener(calledListeners); final VetoableChangeListener listener2 = createListener(calledListeners); listenerSupport.addListener(listener1); listenerSupport.addListener(listener2); listenerSupport.fire().vetoableChange(new PropertyChangeEvent(new Date(), "Day", 4, 5)); assertEquals(calledListeners.size(), 2); assertSame(calledListeners.get(0), listener1); assertSame(calledListeners.get(1), listener2); }
@Override public void vetoableChange(final PropertyChangeEvent e) { listenerSupport.removeListener(this); } });
eventListenerSupport.addListener(listener); final Object source = new Date(); final PropertyChangeEvent ignore = new PropertyChangeEvent(source, "Hour", 5, 6); listener.vetoableChange(respond); EasyMock.replay(listener); eventListenerSupport.fire().vetoableChange(ignore); eventListenerSupport.fire().vetoableChange(respond); EasyMock.verify(listener);
/** * Deserialize. * @param objectInputStream the input stream * @throws IOException if an IO error occurs * @throws ClassNotFoundException if the class cannot be resolved */ private void readObject(final ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException { @SuppressWarnings("unchecked") // Will throw CCE here if not correct final L[] srcListeners = (L[]) objectInputStream.readObject(); this.listeners = new CopyOnWriteArrayList<>(srcListeners); @SuppressWarnings("unchecked") // Will throw CCE here if not correct final Class<L> listenerInterface = (Class<L>) srcListeners.getClass().getComponentType(); initializeTransientFields(listenerInterface, Thread.currentThread().getContextClassLoader()); }
public void unregisterAllListeners() { ISlangTestCaseEventListener[] listeners = listenerList.getListeners(); if (listeners != null) { for (ISlangTestCaseEventListener testCaseEventListener : listeners) { listenerList.removeListener(testCaseEventListener); } } }
/** * 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); }
/** * Create the proxy object. * @param listenerInterface the class of the listener interface * @param classLoader the class loader to be used */ private void createProxy(final Class<L> listenerInterface, final ClassLoader classLoader) { proxy = listenerInterface.cast(Proxy.newProxyInstance(classLoader, new Class[] { listenerInterface }, createInvocationHandler())); }
@Test public void testAddListenerNoDuplicates() { final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class); final VetoableChangeListener[] listeners = listenerSupport.getListeners(); assertEquals(0, listeners.length); assertEquals(VetoableChangeListener.class, listeners.getClass().getComponentType()); final VetoableChangeListener[] empty = listeners; //for fun, show that the same empty instance is used assertSame(empty, listenerSupport.getListeners()); final VetoableChangeListener listener1 = EasyMock.createNiceMock(VetoableChangeListener.class); listenerSupport.addListener(listener1); assertEquals(1, listenerSupport.getListeners().length); listenerSupport.addListener(listener1, false); assertEquals(1, listenerSupport.getListeners().length); listenerSupport.removeListener(listener1); assertSame(empty, listenerSupport.getListeners()); }
@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()); }
public void removePropertyChangeListener(final PropertyChangeListener listener) { listeners.removeListener(listener); } }
/** * Creates an EventListenerSupport object which supports the provided * listener interface using the specified class loader to create the JDK * dynamic proxy. * * @param listenerInterface the listener interface. * @param classLoader the class loader. * * @throws NullPointerException if <code>listenerInterface</code> or * <code>classLoader</code> is <code>null</code>. * @throws IllegalArgumentException if <code>listenerInterface</code> is * not an interface. */ public EventListenerSupport(final Class<L> listenerInterface, final ClassLoader classLoader) { this(); 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()); initializeTransientFields(listenerInterface, classLoader); }
/** * 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); }
/** * Create the proxy object. * @param listenerInterface the class of the listener interface * @param classLoader the class loader to be used */ private void createProxy(final Class<L> listenerInterface, final ClassLoader classLoader) { proxy = listenerInterface.cast(Proxy.newProxyInstance(classLoader, new Class[] { listenerInterface }, createInvocationHandler())); }