Search in sources :

Example 1 with TestTransactionManager

use of org.springframework.integration.util.TestTransactionManager in project spring-integration by spring-projects.

the class PollingTransactionTests method commitFailureAndHandlerFailureTest.

@Test
public void commitFailureAndHandlerFailureTest() throws Throwable {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("transactionFailureTests.xml", this.getClass());
    TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManagerBad");
    PollableChannel inputTxFail = (PollableChannel) context.getBean("inputTxFailure");
    PollableChannel inputHandlerFail = (PollableChannel) context.getBean("inputHandlerFailure");
    PollableChannel output = (PollableChannel) context.getBean("output");
    PollableChannel errorChannel = (PollableChannel) context.getBean("errorChannel");
    assertEquals(0, txManager.getCommitCount());
    inputTxFail.send(new GenericMessage<>("commitFailureTest"));
    Message<?> errorMessage = errorChannel.receive(10000);
    assertNotNull(errorMessage);
    Object payload = errorMessage.getPayload();
    assertEquals(MessagingException.class, payload.getClass());
    MessagingException messagingException = (MessagingException) payload;
    assertEquals(IllegalTransactionStateException.class, messagingException.getCause().getClass());
    assertNotNull(messagingException.getFailedMessage());
    assertNotNull(output.receive(0));
    assertEquals(0, txManager.getCommitCount());
    inputHandlerFail.send(new GenericMessage<>("handlerFalilureTest"));
    errorMessage = errorChannel.receive(10000);
    assertNotNull(errorMessage);
    payload = errorMessage.getPayload();
    assertEquals(MessageHandlingException.class, payload.getClass());
    messagingException = (MessageHandlingException) payload;
    assertEquals(RuntimeException.class, messagingException.getCause().getClass());
    assertNotNull(messagingException.getFailedMessage());
    assertNull(output.receive(0));
    assertEquals(0, txManager.getCommitCount());
    context.close();
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) MessagingException(org.springframework.messaging.MessagingException) PollableChannel(org.springframework.messaging.PollableChannel) TestTransactionManager(org.springframework.integration.util.TestTransactionManager) Test(org.junit.Test)

Example 2 with TestTransactionManager

use of org.springframework.integration.util.TestTransactionManager in project spring-integration by spring-projects.

the class PollingTransactionTests method transactionWithCommit.

@Test
public void transactionWithCommit() throws InterruptedException {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("transactionTests.xml", this.getClass());
    TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager");
    MessageChannel input = (MessageChannel) context.getBean("goodInput");
    PollableChannel output = (PollableChannel) context.getBean("output");
    assertEquals(0, txManager.getCommitCount());
    assertEquals(0, txManager.getRollbackCount());
    input.send(new GenericMessage<String>("test"));
    txManager.waitForCompletion(1000);
    Message<?> message = output.receive(0);
    assertNotNull(message);
    assertEquals(1, txManager.getCommitCount());
    assertEquals(0, txManager.getRollbackCount());
    context.close();
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) PollableChannel(org.springframework.messaging.PollableChannel) TestTransactionManager(org.springframework.integration.util.TestTransactionManager) Test(org.junit.Test)

Example 3 with TestTransactionManager

use of org.springframework.integration.util.TestTransactionManager in project spring-integration by spring-projects.

the class PollingTransactionTests method transactionWithCommitAndAdvices.

@Test
@SuppressWarnings("unchecked")
public void transactionWithCommitAndAdvices() throws InterruptedException {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("transactionTests.xml", this.getClass());
    PollingConsumer advicedPoller = context.getBean("advicedSa", PollingConsumer.class);
    List<Advice> adviceChain = TestUtils.getPropertyValue(advicedPoller, "adviceChain", List.class);
    assertEquals(4, adviceChain.size());
    Runnable poller = TestUtils.getPropertyValue(advicedPoller, "poller", Runnable.class);
    Callable<?> pollingTask = TestUtils.getPropertyValue(poller, "pollingTask", Callable.class);
    assertTrue("Poller is not Advised", pollingTask instanceof Advised);
    Advisor[] advisors = ((Advised) pollingTask).getAdvisors();
    assertEquals(4, advisors.length);
    assertThat("First advisor is not TX", advisors[0].getAdvice(), instanceOf(TransactionInterceptor.class));
    TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager");
    MessageChannel input = (MessageChannel) context.getBean("goodInputWithAdvice");
    PollableChannel output = (PollableChannel) context.getBean("output");
    assertEquals(0, txManager.getCommitCount());
    assertEquals(0, txManager.getRollbackCount());
    input.send(new GenericMessage<>("test"));
    txManager.waitForCompletion(10000);
    Message<?> message = output.receive(0);
    assertNotNull(message);
    assertEquals(0, txManager.getRollbackCount());
    context.close();
}
Also used : PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) Advisor(org.springframework.aop.Advisor) TransactionInterceptor(org.springframework.transaction.interceptor.TransactionInterceptor) MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Advised(org.springframework.aop.framework.Advised) PollableChannel(org.springframework.messaging.PollableChannel) Advice(org.aopalliance.aop.Advice) TestTransactionManager(org.springframework.integration.util.TestTransactionManager) Test(org.junit.Test)

Example 4 with TestTransactionManager

use of org.springframework.integration.util.TestTransactionManager in project spring-integration by spring-projects.

the class PollingTransactionTests method propagationRequired.

@Test
public void propagationRequired() throws InterruptedException {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("propagationRequiredTests.xml", this.getClass());
    TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager");
    PollableChannel input = (PollableChannel) context.getBean("input");
    PollableChannel output = (PollableChannel) context.getBean("output");
    assertEquals(0, txManager.getCommitCount());
    input.send(new GenericMessage<String>("test"));
    Message<?> reply = output.receive(3000);
    assertNotNull(reply);
    txManager.waitForCompletion(3000);
    assertEquals(1, txManager.getCommitCount());
    assertEquals(Propagation.REQUIRED.value(), txManager.getLastDefinition().getPropagationBehavior());
    context.close();
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) PollableChannel(org.springframework.messaging.PollableChannel) TestTransactionManager(org.springframework.integration.util.TestTransactionManager) Test(org.junit.Test)

Example 5 with TestTransactionManager

use of org.springframework.integration.util.TestTransactionManager in project spring-integration by spring-projects.

the class PollingTransactionTests method propagationMandatory.

@Test
public void propagationMandatory() throws Throwable {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("propagationMandatoryTests.xml", this.getClass());
    TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager");
    PollableChannel input = (PollableChannel) context.getBean("input");
    PollableChannel output = (PollableChannel) context.getBean("output");
    PollableChannel errorChannel = (PollableChannel) context.getBean("errorChannel");
    assertEquals(0, txManager.getCommitCount());
    input.send(new GenericMessage<String>("test"));
    Message<?> errorMessage = errorChannel.receive(3000);
    assertNotNull(errorMessage);
    Object payload = errorMessage.getPayload();
    assertEquals(MessagingException.class, payload.getClass());
    MessagingException messagingException = (MessagingException) payload;
    assertEquals(IllegalTransactionStateException.class, messagingException.getCause().getClass());
    assertNull(output.receive(0));
    assertEquals(0, txManager.getCommitCount());
    context.close();
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) MessagingException(org.springframework.messaging.MessagingException) PollableChannel(org.springframework.messaging.PollableChannel) TestTransactionManager(org.springframework.integration.util.TestTransactionManager) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)9 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)9 TestTransactionManager (org.springframework.integration.util.TestTransactionManager)9 PollableChannel (org.springframework.messaging.PollableChannel)9 MessageChannel (org.springframework.messaging.MessageChannel)3 MessagingException (org.springframework.messaging.MessagingException)2 Advice (org.aopalliance.aop.Advice)1 Advisor (org.springframework.aop.Advisor)1 Advised (org.springframework.aop.framework.Advised)1 PollingConsumer (org.springframework.integration.endpoint.PollingConsumer)1 TransactionInterceptor (org.springframework.transaction.interceptor.TransactionInterceptor)1