|
| 1 | +package org.hibernate.envers.test.integration.basic; |
| 2 | + |
| 3 | +import org.junit.Assert; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import org.hibernate.Session; |
| 7 | +import org.hibernate.action.spi.AfterTransactionCompletionProcess; |
| 8 | +import org.hibernate.action.spi.BeforeTransactionCompletionProcess; |
| 9 | +import org.hibernate.engine.spi.SessionImplementor; |
| 10 | +import org.hibernate.envers.test.BaseEnversFunctionalTestCase; |
| 11 | +import org.hibernate.envers.test.entities.StrTestEntity; |
| 12 | +import org.hibernate.envers.tools.MutableInteger; |
| 13 | +import org.hibernate.event.service.spi.EventListenerRegistry; |
| 14 | +import org.hibernate.event.spi.EventType; |
| 15 | +import org.hibernate.event.spi.PostInsertEvent; |
| 16 | +import org.hibernate.event.spi.PostInsertEventListener; |
| 17 | +import org.hibernate.persister.entity.EntityPersister; |
| 18 | +import org.hibernate.testing.TestForIssue; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) |
| 22 | + */ |
| 23 | +public class RegisterUserEventListenersTest extends BaseEnversFunctionalTestCase { |
| 24 | + @Override |
| 25 | + protected Class<?>[] getAnnotatedClasses() { |
| 26 | + return new Class<?>[] { StrTestEntity.class }; |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + @TestForIssue( jiraKey = "HHH-7478" ) |
| 31 | + public void testTransactionProcessSynchronization() { |
| 32 | + final EventListenerRegistry registry = sessionFactory().getServiceRegistry().getService( EventListenerRegistry.class ); |
| 33 | + final CountingPostInsertTransactionBoundaryListener listener = new CountingPostInsertTransactionBoundaryListener(); |
| 34 | + |
| 35 | + registry.getEventListenerGroup( EventType.POST_INSERT ).appendListener( listener ); |
| 36 | + |
| 37 | + Session session = openSession(); |
| 38 | + session.getTransaction().begin(); |
| 39 | + StrTestEntity entity = new StrTestEntity( "str1" ); |
| 40 | + session.save( entity ); |
| 41 | + session.getTransaction().commit(); |
| 42 | + session.close(); |
| 43 | + |
| 44 | + // Post insert listener invoked three times - before/after insertion of original data, |
| 45 | + // revision entity and audit row. |
| 46 | + Assert.assertEquals( 3, listener.getBeforeCount() ); |
| 47 | + Assert.assertEquals( 3, listener.getAfterCount() ); |
| 48 | + } |
| 49 | + |
| 50 | + private static class CountingPostInsertTransactionBoundaryListener implements PostInsertEventListener { |
| 51 | + private final MutableInteger beforeCounter = new MutableInteger(); |
| 52 | + private final MutableInteger afterCounter = new MutableInteger(); |
| 53 | + |
| 54 | + @Override |
| 55 | + public void onPostInsert(PostInsertEvent event) { |
| 56 | + event.getSession().getActionQueue().registerProcess(new BeforeTransactionCompletionProcess() { |
| 57 | + @Override |
| 58 | + public void doBeforeTransactionCompletion(SessionImplementor session) { |
| 59 | + beforeCounter.increase(); |
| 60 | + } |
| 61 | + }); |
| 62 | + event.getSession().getActionQueue().registerProcess(new AfterTransactionCompletionProcess() { |
| 63 | + @Override |
| 64 | + public void doAfterTransactionCompletion(boolean success, SessionImplementor session) { |
| 65 | + afterCounter.increase(); |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public boolean requiresPostCommitHanding(EntityPersister persister) { |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
| 75 | + public int getBeforeCount() { |
| 76 | + return beforeCounter.get(); |
| 77 | + } |
| 78 | + |
| 79 | + public int getAfterCount() { |
| 80 | + return afterCounter.get(); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments