use of org.springframework.integration.test.util.TestUtils.TestApplicationContext in project spring-integration by spring-projects.
the class ServiceActivatorAnnotationPostProcessorTests method testAnnotatedMethod.
@Test
public void testAnnotatedMethod() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
TestApplicationContext context = TestUtils.createTestApplicationContext();
RootBeanDefinition postProcessorDef = new RootBeanDefinition(MessagingAnnotationPostProcessor.class);
context.registerBeanDefinition("postProcessor", postProcessorDef);
context.registerBeanDefinition("testChannel", new RootBeanDefinition(DirectChannel.class));
RootBeanDefinition beanDefinition = new RootBeanDefinition(SimpleServiceActivatorAnnotationTestBean.class);
beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(latch);
context.registerBeanDefinition("testBean", beanDefinition);
context.refresh();
SimpleServiceActivatorAnnotationTestBean testBean = (SimpleServiceActivatorAnnotationTestBean) context.getBean("testBean");
assertEquals(1, latch.getCount());
assertNull(testBean.getMessageText());
MessageChannel testChannel = (MessageChannel) context.getBean("testChannel");
testChannel.send(new GenericMessage<String>("test-123"));
latch.await(1000, TimeUnit.MILLISECONDS);
assertEquals(0, latch.getCount());
assertEquals("test-123", testBean.getMessageText());
context.stop();
}
use of org.springframework.integration.test.util.TestUtils.TestApplicationContext in project spring-integration by spring-projects.
the class SourcePollingChannelAdapterFactoryBeanTests method testTransactionalAdviceChain.
@Test
public void testTransactionalAdviceChain() throws Throwable {
SourcePollingChannelAdapterFactoryBean factoryBean = new SourcePollingChannelAdapterFactoryBean();
QueueChannel outputChannel = new QueueChannel();
TestApplicationContext context = TestUtils.createTestApplicationContext();
factoryBean.setBeanFactory(context.getBeanFactory());
factoryBean.setBeanClassLoader(ClassUtils.getDefaultClassLoader());
factoryBean.setOutputChannel(outputChannel);
factoryBean.setSource(() -> new GenericMessage<>("test"));
PollerMetadata pollerMetadata = new PollerMetadata();
List<Advice> adviceChain = new ArrayList<Advice>();
final AtomicBoolean adviceApplied = new AtomicBoolean(false);
adviceChain.add((MethodInterceptor) invocation -> {
adviceApplied.set(true);
return invocation.proceed();
});
pollerMetadata.setTrigger(new PeriodicTrigger(5000));
pollerMetadata.setMaxMessagesPerPoll(1);
final AtomicInteger count = new AtomicInteger();
final MethodInterceptor txAdvice = mock(MethodInterceptor.class);
adviceChain.add((MethodInterceptor) invocation -> {
count.incrementAndGet();
return invocation.proceed();
});
when(txAdvice.invoke(any(MethodInvocation.class))).thenAnswer(invocation -> {
count.incrementAndGet();
return ((MethodInvocation) invocation.getArgument(0)).proceed();
});
pollerMetadata.setAdviceChain(adviceChain);
factoryBean.setPollerMetadata(pollerMetadata);
factoryBean.setAutoStartup(true);
factoryBean.afterPropertiesSet();
context.registerEndpoint("testPollingEndpoint", factoryBean.getObject());
context.refresh();
Message<?> message = outputChannel.receive(5000);
assertEquals("test", message.getPayload());
assertEquals(1, count.get());
assertTrue("adviceChain was not applied", adviceApplied.get());
}
use of org.springframework.integration.test.util.TestUtils.TestApplicationContext in project spring-integration by spring-projects.
the class SourcePollingChannelAdapterFactoryBeanTests method testAdviceChain.
@Test
public void testAdviceChain() throws Exception {
SourcePollingChannelAdapterFactoryBean factoryBean = new SourcePollingChannelAdapterFactoryBean();
QueueChannel outputChannel = new QueueChannel();
TestApplicationContext context = TestUtils.createTestApplicationContext();
factoryBean.setBeanFactory(context.getBeanFactory());
factoryBean.setBeanClassLoader(ClassUtils.getDefaultClassLoader());
factoryBean.setOutputChannel(outputChannel);
factoryBean.setSource(() -> new GenericMessage<>("test"));
PollerMetadata pollerMetadata = new PollerMetadata();
List<Advice> adviceChain = new ArrayList<Advice>();
final AtomicBoolean adviceApplied = new AtomicBoolean(false);
adviceChain.add((MethodInterceptor) invocation -> {
adviceApplied.set(true);
return invocation.proceed();
});
pollerMetadata.setTrigger(new PeriodicTrigger(5000));
pollerMetadata.setMaxMessagesPerPoll(1);
pollerMetadata.setAdviceChain(adviceChain);
factoryBean.setPollerMetadata(pollerMetadata);
factoryBean.setAutoStartup(true);
factoryBean.afterPropertiesSet();
context.registerEndpoint("testPollingEndpoint", factoryBean.getObject());
context.refresh();
Message<?> message = outputChannel.receive(5000);
assertEquals("test", message.getPayload());
assertTrue("adviceChain was not applied", adviceApplied.get());
}
use of org.springframework.integration.test.util.TestUtils.TestApplicationContext in project spring-integration by spring-projects.
the class ApplicationContextMessageBusTests method consumerSubscribedToErrorChannel.
@Test
public void consumerSubscribedToErrorChannel() throws InterruptedException {
TestApplicationContext context = TestUtils.createTestApplicationContext();
QueueChannel errorChannel = new QueueChannel();
context.registerChannel(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, errorChannel);
final CountDownLatch latch = new CountDownLatch(1);
AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
@Override
public Object handleRequestMessage(Message<?> message) {
latch.countDown();
return null;
}
};
PollingConsumer endpoint = new PollingConsumer(errorChannel, handler);
endpoint.setBeanFactory(mock(BeanFactory.class));
context.registerEndpoint("testEndpoint", endpoint);
context.refresh();
errorChannel.send(new ErrorMessage(new RuntimeException("test-exception")));
latch.await(1000, TimeUnit.MILLISECONDS);
assertEquals("handler should have received error message", 0, latch.getCount());
context.stop();
}
use of org.springframework.integration.test.util.TestUtils.TestApplicationContext in project spring-integration by spring-projects.
the class ApplicationContextMessageBusTests method channelsWithoutHandlers.
@Test
public void channelsWithoutHandlers() {
TestApplicationContext context = TestUtils.createTestApplicationContext();
QueueChannel sourceChannel = new QueueChannel();
context.registerChannel("sourceChannel", sourceChannel);
sourceChannel.send(new GenericMessage<String>("test"));
QueueChannel targetChannel = new QueueChannel();
context.registerChannel("targetChannel", targetChannel);
context.refresh();
Message<?> result = targetChannel.receive(10);
assertNull(result);
context.stop();
}
Aggregations