HHH-13831 Test listeners error replacement strategy

This commit is contained in:
Davide D'Alto 2020-01-30 11:53:42 +00:00 committed by Steve Ebersole
parent e38d2be431
commit 0268ba9a33
1 changed files with 32 additions and 0 deletions

View File

@ -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<ClearEventListener> 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;
}
}
}