From 0268ba9a33763f87033d26e0d4d5b560a42efb98 Mon Sep 17 00:00:00 2001 From: Davide D'Alto Date: Thu, 30 Jan 2020 11:53:42 +0000 Subject: [PATCH] HHH-13831 Test listeners error replacement strategy --- .../EventListenerReplacementStrategyTest.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) 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 649a50c55e..71eb8bc4c9 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 @@ -6,12 +6,15 @@ import java.util.concurrent.atomic.AtomicInteger; import org.hibernate.event.service.spi.DuplicationStrategy; import org.hibernate.event.service.spi.EventListenerGroup; +import org.hibernate.event.service.spi.EventListenerRegistrationException; import org.hibernate.event.spi.ClearEvent; import org.hibernate.event.spi.ClearEventListener; import org.hibernate.event.spi.EventType; import org.hibernate.testing.TestForIssue; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import static org.assertj.core.api.Assertions.assertThat; @@ -24,6 +27,9 @@ import static org.assertj.core.api.Assertions.assertThat; @TestForIssue(jiraKey = "HHH-13831") public class EventListenerReplacementStrategyTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + Tracker tracker = new Tracker(); ClearEvent event = new ClearEvent( null ); EventListenerGroup listenerGroup = new EventListenerGroupImpl( EventType.CLEAR, null ); @@ -169,6 +175,16 @@ public class EventListenerReplacementStrategyTest { assertThat( tracker.callers ).containsExactly( OriginalListener.class, ExtraListener.class ); } + @Test + public void testErrorStrategyOnAppend() { + thrown.expect( EventListenerRegistrationException.class ); + thrown.expectMessage( "Duplicate event listener found" ); + + listenerGroup.addDuplicationStrategy( ErrorStrategy.INSTANCE ); + listenerGroup.appendListener( new OriginalListener( tracker ) ); + listenerGroup.appendListener( new ExpectedListener( tracker ) ); + } + /** * Keep track of which listener is called and how many listeners are called. */ @@ -263,4 +279,20 @@ public class EventListenerReplacementStrategyTest { return Action.KEEP_ORIGINAL; } } + + private static class ErrorStrategy implements DuplicationStrategy { + + static final ErrorStrategy INSTANCE = new ErrorStrategy(); + + @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.ERROR; + } + } }