From e38d2be43122131bb8e8761b2b560d059eeb3ffd Mon Sep 17 00:00:00 2001 From: Davide D'Alto Date: Thu, 30 Jan 2020 11:51:03 +0000 Subject: [PATCH] HHH-13831 Test listener keep original replacement strategy --- .../EventListenerReplacementStrategyTest.java | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/hibernate-core/src/test/java/org/hibernate/event/service/internal/EventListenerReplacementStrategyTest.java b/hibernate-core/src/test/java/org/hibernate/event/service/internal/EventListenerReplacementStrategyTest.java index 05c4333950..649a50c55e 100644 --- a/hibernate-core/src/test/java/org/hibernate/event/service/internal/EventListenerReplacementStrategyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/event/service/internal/EventListenerReplacementStrategyTest.java @@ -136,6 +136,39 @@ public class EventListenerReplacementStrategyTest { assertThat( tracker.callers ).containsExactly( ExpectedListener.class, ExtraListener.class ); } + @Test + public void testListenersIteratorWithMultipleListenersAndKeepOriginalStrategy() { + listenerGroup.addDuplicationStrategy( KeepOriginalStrategy.INSTANCE ); + listenerGroup.appendListener( new OriginalListener( tracker ) ); + listenerGroup.appendListener( new ExpectedListener( tracker ) ); + listenerGroup.appendListener( new ExtraListener( tracker ) ); + listenerGroup.listeners().forEach( listener -> listener.onClear( event ) ); + + assertThat( tracker.callers ).containsExactly( OriginalListener.class, ExtraListener.class ); + } + + @Test + public void testFireLazyEventOnEachListenerWithMultipleListenersAndKeepOriginalStrategy() { + listenerGroup.addDuplicationStrategy( KeepOriginalStrategy.INSTANCE ); + listenerGroup.appendListener( new OriginalListener( tracker ) ); + listenerGroup.appendListener( new ExpectedListener( tracker ) ); + listenerGroup.appendListener( new ExtraListener( tracker ) ); + listenerGroup.fireLazyEventOnEachListener( () -> event, ClearEventListener::onClear ); + + assertThat( tracker.callers ).containsExactly( OriginalListener.class, ExtraListener.class ); + } + + @Test + public void testFireEventOnEachListenerWithMultipleListenersAndKeepOriginalStrategy() { + listenerGroup.addDuplicationStrategy( KeepOriginalStrategy.INSTANCE ); + listenerGroup.appendListener( new OriginalListener( tracker ) ); + listenerGroup.appendListener( new ExpectedListener( tracker ) ); + listenerGroup.appendListener( new ExtraListener( tracker ) ); + listenerGroup.fireEventOnEachListener( event, ClearEventListener::onClear ); + + assertThat( tracker.callers ).containsExactly( OriginalListener.class, ExtraListener.class ); + } + /** * Keep track of which listener is called and how many listeners are called. */ @@ -167,7 +200,6 @@ public class EventListenerReplacementStrategyTest { } } - /** * The expected listener to be called if everything goes well */ @@ -215,4 +247,20 @@ public class EventListenerReplacementStrategyTest { return Action.REPLACE_ORIGINAL; } } + + private static class KeepOriginalStrategy implements DuplicationStrategy { + + static final KeepOriginalStrategy INSTANCE = new KeepOriginalStrategy(); + + @Override + public boolean areMatch(Object listener, Object original) { + // We just want this to work for original and expected listener + return original instanceof OriginalListener && listener instanceof ExpectedListener; + } + + @Override + public Action getAction() { + return Action.KEEP_ORIGINAL; + } + } }