Search in sources :

Example 21 with TestApplicationContext

use of org.springframework.integration.test.util.TestUtils.TestApplicationContext in project spring-integration by spring-projects.

the class ApplicationContextMessageBusTests method errorChannelWithFailedDispatch.

@Test
public void errorChannelWithFailedDispatch() throws InterruptedException {
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    QueueChannel errorChannel = new QueueChannel();
    QueueChannel outputChannel = new QueueChannel();
    context.registerChannel("errorChannel", errorChannel);
    CountDownLatch latch = new CountDownLatch(1);
    SourcePollingChannelAdapter channelAdapter = new SourcePollingChannelAdapter();
    channelAdapter.setSource(new FailingSource(latch));
    PollerMetadata pollerMetadata = new PollerMetadata();
    pollerMetadata.setTrigger(new PeriodicTrigger(1000));
    channelAdapter.setOutputChannel(outputChannel);
    context.registerEndpoint("testChannel", channelAdapter);
    context.refresh();
    latch.await(2000, TimeUnit.MILLISECONDS);
    Message<?> message = errorChannel.receive(5000);
    context.stop();
    assertNull(outputChannel.receive(100));
    assertNotNull("message should not be null", message);
    assertTrue(message instanceof ErrorMessage);
    Throwable exception = ((ErrorMessage) message).getPayload();
    assertEquals("intentional test failure", exception.getCause().getMessage());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) ErrorMessage(org.springframework.messaging.support.ErrorMessage) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Test(org.junit.Test)

Example 22 with TestApplicationContext

use of org.springframework.integration.test.util.TestUtils.TestApplicationContext in project spring-integration by spring-projects.

the class ApplicationContextMessageBusTests method bothConsumersReceivePublishSubscribeMessage.

@Test
public void bothConsumersReceivePublishSubscribeMessage() throws InterruptedException {
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    PublishSubscribeChannel inputChannel = new PublishSubscribeChannel();
    QueueChannel outputChannel1 = new QueueChannel();
    QueueChannel outputChannel2 = new QueueChannel();
    final CountDownLatch latch = new CountDownLatch(2);
    AbstractReplyProducingMessageHandler handler1 = new AbstractReplyProducingMessageHandler() {

        @Override
        public Object handleRequestMessage(Message<?> message) {
            latch.countDown();
            return message;
        }
    };
    AbstractReplyProducingMessageHandler handler2 = new AbstractReplyProducingMessageHandler() {

        @Override
        public Object handleRequestMessage(Message<?> message) {
            latch.countDown();
            return message;
        }
    };
    context.registerChannel("input", inputChannel);
    context.registerChannel("output1", outputChannel1);
    context.registerChannel("output2", outputChannel2);
    handler1.setOutputChannel(outputChannel1);
    handler2.setOutputChannel(outputChannel2);
    EventDrivenConsumer endpoint1 = new EventDrivenConsumer(inputChannel, handler1);
    EventDrivenConsumer endpoint2 = new EventDrivenConsumer(inputChannel, handler2);
    context.registerEndpoint("testEndpoint1", endpoint1);
    context.registerEndpoint("testEndpoint2", endpoint2);
    context.refresh();
    inputChannel.send(new GenericMessage<String>("testing"));
    latch.await(500, TimeUnit.MILLISECONDS);
    assertEquals("both handlers should have been invoked", 0, latch.getCount());
    Message<?> message1 = outputChannel1.receive(500);
    Message<?> message2 = outputChannel2.receive(500);
    context.stop();
    assertNotNull("both handlers should have replied to the message", message1);
    assertNotNull("both handlers should have replied to the message", message2);
}
Also used : EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) PublishSubscribeChannel(org.springframework.integration.channel.PublishSubscribeChannel) QueueChannel(org.springframework.integration.channel.QueueChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) CountDownLatch(java.util.concurrent.CountDownLatch) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) Test(org.junit.Test)

Example 23 with TestApplicationContext

use of org.springframework.integration.test.util.TestUtils.TestApplicationContext in project spring-integration by spring-projects.

the class DirectChannelSubscriptionTests method sendAndReceiveForRegisteredEndpoint.

@Test
public void sendAndReceiveForRegisteredEndpoint() {
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(new TestBean(), "handle");
    serviceActivator.setOutputChannel(targetChannel);
    EventDrivenConsumer endpoint = new EventDrivenConsumer(sourceChannel, serviceActivator);
    context.registerEndpoint("testEndpoint", endpoint);
    context.refresh();
    this.sourceChannel.send(new GenericMessage<String>("foo"));
    Message<?> response = this.targetChannel.receive();
    assertEquals("foo!", response.getPayload());
    context.stop();
}
Also used : EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Test(org.junit.Test)

Example 24 with TestApplicationContext

use of org.springframework.integration.test.util.TestUtils.TestApplicationContext in project spring-integration by spring-projects.

the class MessageProducerSupportTests method validateSuccessfulErrorFlowDoesNotThrowErrors.

@Test
public void validateSuccessfulErrorFlowDoesNotThrowErrors() {
    TestApplicationContext testApplicationContext = TestUtils.createTestApplicationContext();
    testApplicationContext.refresh();
    DirectChannel outChannel = new DirectChannel();
    outChannel.subscribe(message -> {
        throw new RuntimeException("problems");
    });
    PublishSubscribeChannel errorChannel = new PublishSubscribeChannel();
    SuccessfulErrorService errorService = new SuccessfulErrorService();
    ServiceActivatingHandler handler = new ServiceActivatingHandler(errorService);
    handler.setBeanFactory(testApplicationContext);
    handler.afterPropertiesSet();
    errorChannel.subscribe(handler);
    MessageProducerSupport mps = new MessageProducerSupport() {
    };
    mps.setOutputChannel(outChannel);
    mps.setErrorChannel(errorChannel);
    mps.setBeanFactory(testApplicationContext);
    mps.afterPropertiesSet();
    mps.start();
    Message<?> message = new GenericMessage<String>("hello");
    mps.sendMessage(message);
    assertThat(errorService.lastMessage, instanceOf(ErrorMessage.class));
    ErrorMessage errorMessage = (ErrorMessage) errorService.lastMessage;
    assertEquals(MessageDeliveryException.class, errorMessage.getPayload().getClass());
    MessageDeliveryException exception = (MessageDeliveryException) errorMessage.getPayload();
    assertEquals(message, exception.getFailedMessage());
    testApplicationContext.close();
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) PublishSubscribeChannel(org.springframework.integration.channel.PublishSubscribeChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) ErrorMessage(org.springframework.messaging.support.ErrorMessage) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Test(org.junit.Test)

Example 25 with TestApplicationContext

use of org.springframework.integration.test.util.TestUtils.TestApplicationContext in project spring-integration by spring-projects.

the class MessageProducerSupportTests method testWithChannelName.

@Test
public void testWithChannelName() {
    DirectChannel outChannel = new DirectChannel();
    MessageProducerSupport mps = new MessageProducerSupport() {
    };
    mps.setOutputChannelName("foo");
    TestApplicationContext testApplicationContext = TestUtils.createTestApplicationContext();
    testApplicationContext.registerBean("foo", outChannel);
    testApplicationContext.refresh();
    mps.setBeanFactory(testApplicationContext);
    mps.afterPropertiesSet();
    mps.start();
    assertSame(outChannel, mps.getOutputChannel());
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) Test(org.junit.Test)

Aggregations

TestApplicationContext (org.springframework.integration.test.util.TestUtils.TestApplicationContext)25 Test (org.junit.Test)24 QueueChannel (org.springframework.integration.channel.QueueChannel)17 DirectChannel (org.springframework.integration.channel.DirectChannel)14 GenericMessage (org.springframework.messaging.support.GenericMessage)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 BeanFactory (org.springframework.beans.factory.BeanFactory)6 Message (org.springframework.messaging.Message)6 ErrorMessage (org.springframework.messaging.support.ErrorMessage)5 PollingConsumer (org.springframework.integration.endpoint.PollingConsumer)4 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)4 ProxyFactory (org.springframework.aop.framework.ProxyFactory)3 SourcePollingChannelAdapter (org.springframework.integration.endpoint.SourcePollingChannelAdapter)3 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)3 PollerMetadata (org.springframework.integration.scheduling.PollerMetadata)3 TestUtils (org.springframework.integration.test.util.TestUtils)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2