Merge branch 'LANG-1137'
LANG-1137: Add check for duplicate event listener in EventListenerSupport. Thanks to Metthew Aguirre.
This commit is contained in:
commit
19dace1b8a
|
@ -22,6 +22,7 @@
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<release version="3.5" date="tba" description="tba">
|
<release version="3.5" date="tba" description="tba">
|
||||||
|
<action issue="LANG-1137" type="add" dev="britter" due-to="Matthew Aguirre">Add check for duplicate event listener in EventListenerSupport</action>
|
||||||
<action issue="LANG-1133" type="bug" dev="chas" due-to="Pascal Schumacher">FastDateParser_TimeZoneStrategyTest#testTimeZoneStrategyPattern fails on Windows with German Locale</action>
|
<action issue="LANG-1133" type="bug" dev="chas" due-to="Pascal Schumacher">FastDateParser_TimeZoneStrategyTest#testTimeZoneStrategyPattern fails on Windows with German Locale</action>
|
||||||
<action issue="LANG-1135" type="add" dev="britter" due-to="Eduardo Martins">Add method containsAllWords to WordUtils</action>
|
<action issue="LANG-1135" type="add" dev="britter" due-to="Eduardo Martins">Add method containsAllWords to WordUtils</action>
|
||||||
<action issue="LANG-1132" type="add" dev="britter" due-to="Jack Tan">ReflectionToStringBuilder doesn't throw IllegalArgumentException when the constructor's object param is null</action>
|
<action issue="LANG-1132" type="add" dev="britter" due-to="Jack Tan">ReflectionToStringBuilder doesn't throw IllegalArgumentException when the constructor's object param is null</action>
|
||||||
|
|
|
@ -179,8 +179,27 @@ public class EventListenerSupport<L> implements Serializable {
|
||||||
* <code>null</code>.
|
* <code>null</code>.
|
||||||
*/
|
*/
|
||||||
public void addListener(final L listener) {
|
public void addListener(final L listener) {
|
||||||
|
addListener(listener, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers an event listener. Will not add a pre-existing listener
|
||||||
|
* object to the list if <code>allowDuplicate</code> is false.
|
||||||
|
*
|
||||||
|
* @param listener the event listener (may not be <code>null</code>).
|
||||||
|
* @param allowDuplicate the flag for determining if duplicate listener
|
||||||
|
* objects are allowed to be registered.
|
||||||
|
*
|
||||||
|
* @throws NullPointerException if <code>listener</code> is
|
||||||
|
* <code>null</code>.
|
||||||
|
*/
|
||||||
|
public void addListener(final L listener, boolean allowDuplicate) {
|
||||||
Validate.notNull(listener, "Listener object cannot be null.");
|
Validate.notNull(listener, "Listener object cannot be null.");
|
||||||
listeners.add(listener);
|
if (allowDuplicate) {
|
||||||
|
listeners.add(listener);
|
||||||
|
} else if (!listeners.contains(listener)) {
|
||||||
|
listeners.add(listener);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -39,25 +39,42 @@ import org.junit.Test;
|
||||||
/**
|
/**
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public class EventListenerSupportTest
|
public class EventListenerSupportTest {
|
||||||
{
|
|
||||||
@Test(expected=NullPointerException.class)
|
@Test
|
||||||
public void testAddNullListener()
|
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(expected = NullPointerException.class)
|
||||||
|
public void testAddNullListener() {
|
||||||
final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class);
|
final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class);
|
||||||
listenerSupport.addListener(null);
|
listenerSupport.addListener(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=NullPointerException.class)
|
@Test(expected = NullPointerException.class)
|
||||||
public void testRemoveNullListener()
|
public void testRemoveNullListener() {
|
||||||
{
|
|
||||||
final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class);
|
final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class);
|
||||||
listenerSupport.removeListener(null);
|
listenerSupport.removeListener(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEventDispatchOrder() throws PropertyVetoException
|
public void testEventDispatchOrder() throws PropertyVetoException {
|
||||||
{
|
|
||||||
final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class);
|
final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class);
|
||||||
final List<VetoableChangeListener> calledListeners = new ArrayList<VetoableChangeListener>();
|
final List<VetoableChangeListener> calledListeners = new ArrayList<VetoableChangeListener>();
|
||||||
|
|
||||||
|
@ -71,24 +88,20 @@ public class EventListenerSupportTest
|
||||||
assertSame(calledListeners.get(1), listener2);
|
assertSame(calledListeners.get(1), listener2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testCreateWithNonInterfaceParameter()
|
public void testCreateWithNonInterfaceParameter() {
|
||||||
{
|
|
||||||
EventListenerSupport.create(String.class);
|
EventListenerSupport.create(String.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=NullPointerException.class)
|
@Test(expected = NullPointerException.class)
|
||||||
public void testCreateWithNullParameter()
|
public void testCreateWithNullParameter() {
|
||||||
{
|
|
||||||
EventListenerSupport.create(null);
|
EventListenerSupport.create(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRemoveListenerDuringEvent() throws PropertyVetoException
|
public void testRemoveListenerDuringEvent() throws PropertyVetoException {
|
||||||
{
|
|
||||||
final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class);
|
final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class);
|
||||||
for (int i = 0; i < 10; ++i)
|
for (int i = 0; i < 10; ++i) {
|
||||||
{
|
|
||||||
addDeregisterListener(listenerSupport);
|
addDeregisterListener(listenerSupport);
|
||||||
}
|
}
|
||||||
assertEquals(listenerSupport.getListenerCount(), 10);
|
assertEquals(listenerSupport.getListenerCount(), 10);
|
||||||
|
@ -123,7 +136,7 @@ public class EventListenerSupportTest
|
||||||
public void testSerialization() throws IOException, ClassNotFoundException, PropertyVetoException {
|
public void testSerialization() throws IOException, ClassNotFoundException, PropertyVetoException {
|
||||||
final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class);
|
final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class);
|
||||||
listenerSupport.addListener(new VetoableChangeListener() {
|
listenerSupport.addListener(new VetoableChangeListener() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void vetoableChange(final PropertyChangeEvent e) {
|
public void vetoableChange(final PropertyChangeEvent e) {
|
||||||
}
|
}
|
||||||
|
@ -198,25 +211,19 @@ public class EventListenerSupportTest
|
||||||
EasyMock.verify(listener);
|
EasyMock.verify(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addDeregisterListener(final EventListenerSupport<VetoableChangeListener> listenerSupport)
|
private void addDeregisterListener(final EventListenerSupport<VetoableChangeListener> listenerSupport) {
|
||||||
{
|
listenerSupport.addListener(new VetoableChangeListener() {
|
||||||
listenerSupport.addListener(new VetoableChangeListener()
|
|
||||||
{
|
|
||||||
@Override
|
@Override
|
||||||
public void vetoableChange(final PropertyChangeEvent e)
|
public void vetoableChange(final PropertyChangeEvent e) {
|
||||||
{
|
|
||||||
listenerSupport.removeListener(this);
|
listenerSupport.removeListener(this);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private VetoableChangeListener createListener(final List<VetoableChangeListener> calledListeners)
|
private VetoableChangeListener createListener(final List<VetoableChangeListener> calledListeners) {
|
||||||
{
|
return new VetoableChangeListener() {
|
||||||
return new VetoableChangeListener()
|
|
||||||
{
|
|
||||||
@Override
|
@Override
|
||||||
public void vetoableChange(final PropertyChangeEvent e)
|
public void vetoableChange(final PropertyChangeEvent e) {
|
||||||
{
|
|
||||||
calledListeners.add(this);
|
calledListeners.add(this);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue