Search in sources :

Example 1 with AspectInterceptorBuilder

use of org.apache.tapestry5.ioc.services.AspectInterceptorBuilder in project tapestry-5 by apache.

the class JpaTransactionAdvisorImplTest method persistence_unit_missing.

@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();
}
Also used : EntityManager(javax.persistence.EntityManager) EntityTransactionManager(org.apache.tapestry5.jpa.EntityTransactionManager) EntityManagerManager(org.apache.tapestry5.jpa.EntityManagerManager) JpaTransactionAdvisor(org.apache.tapestry5.jpa.JpaTransactionAdvisor) SQLException(java.sql.SQLException) Test(org.testng.annotations.Test)

Example 2 with AspectInterceptorBuilder

use of org.apache.tapestry5.ioc.services.AspectInterceptorBuilder in project tapestry-5 by apache.

the class JpaTransactionAdvisorImplTest method transaction_inactive.

@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();
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) EntityTransactionManager(org.apache.tapestry5.jpa.EntityTransactionManager) EntityManagerManager(org.apache.tapestry5.jpa.EntityManagerManager) JpaTransactionAdvisor(org.apache.tapestry5.jpa.JpaTransactionAdvisor) Test(org.testng.annotations.Test)

Example 3 with AspectInterceptorBuilder

use of org.apache.tapestry5.ioc.services.AspectInterceptorBuilder in project tapestry-5 by apache.

the class JpaTransactionAdvisorImplTest method undecorated.

@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();
}
Also used : EntityTransactionManager(org.apache.tapestry5.jpa.EntityTransactionManager) EntityManagerManager(org.apache.tapestry5.jpa.EntityManagerManager) JpaTransactionAdvisor(org.apache.tapestry5.jpa.JpaTransactionAdvisor) Test(org.testng.annotations.Test)

Example 4 with AspectInterceptorBuilder

use of org.apache.tapestry5.ioc.services.AspectInterceptorBuilder in project tapestry-5 by apache.

the class JpaTransactionAdvisorImplTest method persistence_unit_name_missing_single_unit_configured.

@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();
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) EntityTransactionManager(org.apache.tapestry5.jpa.EntityTransactionManager) EntityManagerManager(org.apache.tapestry5.jpa.EntityManagerManager) JpaTransactionAdvisor(org.apache.tapestry5.jpa.JpaTransactionAdvisor) Test(org.testng.annotations.Test)

Example 5 with AspectInterceptorBuilder

use of org.apache.tapestry5.ioc.services.AspectInterceptorBuilder in project tapestry-5 by apache.

the class DecorateByMarkerModule method doDecorate.

private static <T> T doDecorate(final String decoratorId, ServiceResources resources, T delegate, AspectDecorator aspectDecorator) {
    Class<T> serviceInterface = resources.getServiceInterface();
    AspectInterceptorBuilder<T> builder = aspectDecorator.createBuilder(serviceInterface, delegate, String.format("<Interceptor for %s(%s)>", resources.getServiceId(), serviceInterface.getName()));
    builder.adviseAllMethods(new MethodAdvice() {

        @Override
        public void advise(MethodInvocation invocation) {
            invocation.proceed();
            Object result = invocation.getReturnValue();
            invocation.setReturnValue(String.format("Decorated by %s[%s]", decoratorId, result));
        }
    });
    return builder.build();
}
Also used : MethodInvocation(org.apache.tapestry5.plastic.MethodInvocation) MethodAdvice(org.apache.tapestry5.plastic.MethodAdvice)

Aggregations

EntityManagerManager (org.apache.tapestry5.jpa.EntityManagerManager)12 EntityTransactionManager (org.apache.tapestry5.jpa.EntityTransactionManager)12 JpaTransactionAdvisor (org.apache.tapestry5.jpa.JpaTransactionAdvisor)12 Test (org.testng.annotations.Test)12 EntityManager (javax.persistence.EntityManager)11 EntityTransaction (javax.persistence.EntityTransaction)9 SQLException (java.sql.SQLException)3 MethodAdvice (org.apache.tapestry5.plastic.MethodAdvice)2 MethodInvocation (org.apache.tapestry5.plastic.MethodInvocation)2 ServiceAdvisor (org.apache.tapestry5.ioc.ServiceAdvisor)1 AspectInterceptorBuilder (org.apache.tapestry5.ioc.services.AspectInterceptorBuilder)1