JpaTransactionAdvisorImpl implements JpaTransactionAdvisor { @Override public void addTransactionCommitAdvice(MethodAdviceReceiver receiver) { for (final Method m : receiver.getInterface().getMethods()) { if (m.getAnnotation(CommitAfter.class) != null) { PersistenceContext annotation = receiver.getMethodAnnotation(m, PersistenceContext.class); receiver.adviseMethod(m, methodAdvices.get(annotation == null ? null : annotation.unitName())); } } } JpaTransactionAdvisorImpl(EntityManagerManager manager,
            EntityTransactionManager transactionManager); @Override void addTransactionCommitAdvice(MethodAdviceReceiver receiver);  }
@Test public void checked_exception_will_commit_transaction() throws Exception { final Performer delegate = newMock(Performer.class); final EntityManagerManager manager = newMock(EntityManagerManager.class); final EntityTransactionManager transactionManager = newMock(EntityTransactionManager.class); final JpaTransactionAdvisor advisor = newJpaTransactionAdvisor(manager, transactionManager); final EntityManager entityManager = newMock(EntityManager.class); final EntityTransaction transaction = newMock(EntityTransaction.class); final SQLException se = new SQLException("Checked."); final AspectInterceptorBuilder<Performer> builder = aspectDecorator.createBuilder( Performer.class, delegate, "foo.Bar"); advisor.addTransactionCommitAdvice(builder); final Performer interceptor = builder.build(); train_getActiveTransaction(manager, entityManager, transaction); delegate.perform(); setThrowable(se); train_commitActiveTransaction(transaction); replay(); try { interceptor.perform(); unreachable(); } catch (final SQLException ex) { Assert.assertSame(ex, se); } verify(); }
@Test public void return_type_method() { final ReturnTypeService delegate = newTestService(); final EntityManagerManager manager = newMock(EntityManagerManager.class); final EntityTransactionManager transactionManager = newMock(EntityTransactionManager.class); final JpaTransactionAdvisor advisor = newJpaTransactionAdvisor(manager, transactionManager); final EntityManager entityManager = newMock(EntityManager.class); final EntityTransaction transaction = newMock(EntityTransaction.class); final AspectInterceptorBuilder<ReturnTypeService> builder = aspectDecorator.createBuilder( ReturnTypeService.class, delegate, "foo.Bar"); advisor.addTransactionCommitAdvice(builder); final ReturnTypeService interceptor = builder.build(); train_getActiveTransaction(manager, entityManager, transaction); delegate.returnTypeMethod(); train_commitActiveTransaction(transaction); replay(); Assert.assertEquals(interceptor.returnTypeMethod(), "Foo"); verify(); }
@Test public void return_type_method_with_param() { final ReturnTypeService delegate = newTestService(); final EntityManagerManager manager = newMock(EntityManagerManager.class); final EntityTransactionManager transactionManager = newMock(EntityTransactionManager.class); final JpaTransactionAdvisor advisor = newJpaTransactionAdvisor(manager, transactionManager); final EntityManager entityManager = newMock(EntityManager.class); final EntityTransaction transaction = newMock(EntityTransaction.class); final AspectInterceptorBuilder<ReturnTypeService> builder = aspectDecorator.createBuilder( ReturnTypeService.class, delegate, "foo.Bar"); advisor.addTransactionCommitAdvice(builder); final ReturnTypeService interceptor = builder.build(); train_getActiveTransaction(manager, entityManager, transaction); delegate.returnTypeMethodWithParam(5, 3); train_commitActiveTransaction(transaction); replay(); Assert.assertEquals(interceptor.returnTypeMethodWithParam(5, 3), 8); verify(); }
@Test public void undecorated() { final VoidService delegate = newMock(VoidService.class); final EntityManagerManager manager = newMock(EntityManagerManager.class); final EntityTransactionManager transactionManager = newMock(EntityTransactionManager.class); final JpaTransactionAdvisor advisor = newJpaTransactionAdvisor(manager, transactionManager); final AspectInterceptorBuilder<VoidService> builder = aspectDecorator.createBuilder( VoidService.class, delegate, "foo.Bar"); advisor.addTransactionCommitAdvice(builder); final VoidService interceptor = builder.build(); delegate.undecorated(); replay(); interceptor.undecorated(); verify(); }
@Test public void persistence_unit_name_missing() { final VoidService delegate = newMock(VoidService.class); final EntityManagerManager manager = newMock(EntityManagerManager.class); final EntityTransactionManager transactionManager = newMock(EntityTransactionManager.class); final JpaTransactionAdvisor advisor = newJpaTransactionAdvisor(manager, transactionManager); Map<String, EntityManager> managers = CollectionFactory.newMap(); managers.put("A", newMock(EntityManager.class)); managers.put("B", newMock(EntityManager.class)); final AspectInterceptorBuilder<VoidService> builder = aspectDecorator.createBuilder( VoidService.class, delegate, "foo.Bar"); advisor.addTransactionCommitAdvice(builder); final VoidService interceptor = builder.build(); expect(manager.getEntityManagers()).andReturn(managers); replay(); try { interceptor.persistenceUnitNameMissing(); TestBase.unreachable(); } catch (Exception e) { Assert.assertEquals(e.getMessage(), "Unable to locate a single EntityManager. " + "You must provide the persistence unit name as defined in the persistence.xml using the @PersistenceContext annotation."); } verify(); }
@Test public void persistence_unit_name_missing_single_unit_configured() { final VoidService delegate = newMock(VoidService.class); final EntityManagerManager manager = newMock(EntityManagerManager.class); final EntityTransactionManager transactionManager = newMock(EntityTransactionManager.class); final JpaTransactionAdvisor advisor = newJpaTransactionAdvisor(manager, transactionManager); final EntityTransaction transaction = newMock(EntityTransaction.class); EntityManager em = newMock(EntityManager.class); Map<String, EntityManager> managers = CollectionFactory.newMap(); managers.put("A", em); final AspectInterceptorBuilder<VoidService> builder = aspectDecorator.createBuilder( VoidService.class, delegate, "foo.Bar"); advisor.addTransactionCommitAdvice(builder); final VoidService interceptor = builder.build(); expect(manager.getEntityManagers()).andReturn(managers); train_getTransaction(em, transaction, true); delegate.persistenceUnitNameMissing(); train_commitActiveTransaction(transaction); replay(); interceptor.persistenceUnitNameMissing(); verify(); }
@Test public void persistence_unit_missing() { final VoidService delegate = newMock(VoidService.class); final EntityManagerManager manager = newMock(EntityManagerManager.class); final EntityTransactionManager transactionManager = newMock(EntityTransactionManager.class); final JpaTransactionAdvisor advisor = newJpaTransactionAdvisor(manager, transactionManager); Map<String, EntityManager> managers = CollectionFactory.newMap(); managers.put("A", newMock(EntityManager.class)); managers.put("B", newMock(EntityManager.class)); final AspectInterceptorBuilder<VoidService> builder = aspectDecorator.createBuilder( VoidService.class, delegate, "foo.Bar"); advisor.addTransactionCommitAdvice(builder); final VoidService interceptor = builder.build(); expect(manager.getEntityManagers()).andReturn(managers); replay(); try { interceptor.persistenceUnitMissing(); TestBase.unreachable(); } catch (Exception e) { assertMessageContains(e, "Unable to locate a single EntityManager"); } verify(); }
@Test public void persistence_unit_missing_single_unit_configured() { final VoidService delegate = newMock(VoidService.class); final EntityManagerManager manager = newMock(EntityManagerManager.class); final EntityTransactionManager transactionManager = newMock(EntityTransactionManager.class); final JpaTransactionAdvisor advisor = newJpaTransactionAdvisor(manager, transactionManager); final EntityTransaction transaction = newMock(EntityTransaction.class); EntityManager em = newMock(EntityManager.class); Map<String, EntityManager> managers = CollectionFactory.newMap(); managers.put("A", em); final AspectInterceptorBuilder<VoidService> builder = aspectDecorator.createBuilder( VoidService.class, delegate, "foo.Bar"); advisor.addTransactionCommitAdvice(builder); final VoidService interceptor = builder.build(); expect(manager.getEntityManagers()).andReturn(managers); train_getTransaction(em, transaction, true); delegate.persistenceUnitMissing(); train_commitActiveTransaction(transaction); replay(); interceptor.persistenceUnitMissing(); verify(); }
@Test public void transaction_inactive() { final VoidService delegate = newMock(VoidService.class); final EntityManagerManager manager = newMock(EntityManagerManager.class); final EntityTransactionManager transactionManager = newMock(EntityTransactionManager.class); final JpaTransactionAdvisor advisor = newJpaTransactionAdvisor(manager, transactionManager); final EntityManager entityManager = newMock(EntityManager.class); final EntityTransaction transaction = newMock(EntityTransaction.class); final AspectInterceptorBuilder<VoidService> builder = aspectDecorator.createBuilder( VoidService.class, delegate, "foo.Bar"); advisor.addTransactionCommitAdvice(builder); final VoidService interceptor = builder.build(); train_getAndBeginTransaction(manager, entityManager, transaction); delegate.voidMethod(); train_commitActiveTransaction(transaction); replay(); interceptor.voidMethod(); verify(); }
@Test public void void_method() { final VoidService delegate = newMock(VoidService.class); final EntityManagerManager manager = newMock(EntityManagerManager.class); final EntityTransactionManager transactionManager = newMock(EntityTransactionManager.class); final JpaTransactionAdvisor advisor = newJpaTransactionAdvisor(manager, transactionManager); final EntityManager entityManager = newMock(EntityManager.class); final EntityTransaction transaction = newMock(EntityTransaction.class); final AspectInterceptorBuilder<VoidService> builder = aspectDecorator.createBuilder( VoidService.class, delegate, "foo.Bar"); advisor.addTransactionCommitAdvice(builder); final VoidService interceptor = builder.build(); train_getActiveTransaction(manager, entityManager, transaction); delegate.voidMethod(); train_commitActiveTransaction(transaction); replay(); interceptor.voidMethod(); verify(); }
@Test public void void_method_with_param() { final VoidService delegate = newMock(VoidService.class); final EntityManagerManager manager = newMock(EntityManagerManager.class); final EntityTransactionManager transactionManager = newMock(EntityTransactionManager.class); final JpaTransactionAdvisor advisor = newJpaTransactionAdvisor(manager, transactionManager); final EntityManager entityManager = newMock(EntityManager.class); final EntityTransaction transaction = newMock(EntityTransaction.class); final AspectInterceptorBuilder<VoidService> builder = aspectDecorator.createBuilder( VoidService.class, delegate, "foo.Bar"); advisor.addTransactionCommitAdvice(builder); final VoidService interceptor = builder.build(); train_getActiveTransaction(manager, entityManager, transaction); delegate.voidMethodWithParam(777); train_commitActiveTransaction(transaction); replay(); interceptor.voidMethodWithParam(777); verify(); }
@Test public void runtime_exception_will_abort_transaction() throws Exception { final Performer delegate = newMock(Performer.class); final EntityManagerManager manager = newMock(EntityManagerManager.class); final EntityTransactionManager transactionManager = newMock(EntityTransactionManager.class); final JpaTransactionAdvisor advisor = newJpaTransactionAdvisor(manager, transactionManager); final EntityManager entityManager = newMock(EntityManager.class); final EntityTransaction transaction = newMock(EntityTransaction.class); final RuntimeException re = new RuntimeException("Unexpected."); final AspectInterceptorBuilder<Performer> builder = aspectDecorator.createBuilder( Performer.class, delegate, "foo.Bar"); advisor.addTransactionCommitAdvice(builder); final Performer interceptor = builder.build(); train_getActiveTransaction(manager, entityManager, transaction); delegate.perform(); TestBase.setThrowable(re); train_rollBackActiveTransaction(transaction); replay(); try { interceptor.perform(); TestBase.unreachable(); } catch (final RuntimeException ex) { Assert.assertSame(ex, re); } verify(); }