Search in sources :

Example 6 with StaticApplicationContext

use of org.springframework.context.support.StaticApplicationContext in project spring-integration by spring-projects.

the class DelayHandlerTests method errorChannelNameHeaderAndHandlerThrowsExceptionWithDelay.

@Test
public void errorChannelNameHeaderAndHandlerThrowsExceptionWithDelay() throws Exception {
    String errorChannelName = "customErrorChannel";
    StaticApplicationContext context = new StaticApplicationContext();
    context.registerSingleton(errorChannelName, DirectChannel.class);
    context.registerSingleton(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, DirectChannel.class);
    context.refresh();
    DirectChannel customErrorChannel = (DirectChannel) context.getBean(errorChannelName);
    MessagePublishingErrorHandler errorHandler = new MessagePublishingErrorHandler();
    errorHandler.setBeanFactory(context);
    taskScheduler.setErrorHandler(errorHandler);
    this.setDelayExpression();
    this.startDelayerHandler();
    output.unsubscribe(resultHandler);
    customErrorChannel.subscribe(resultHandler);
    output.subscribe(message -> {
        throw new UnsupportedOperationException("intentional test failure");
    });
    Message<?> message = MessageBuilder.withPayload("test").setHeader("delay", "10").setErrorChannelName(errorChannelName).build();
    input.send(message);
    waitForLatch(10000);
    Message<?> errorMessage = resultHandler.lastMessage;
    assertEquals(MessageDeliveryException.class, errorMessage.getPayload().getClass());
    MessageDeliveryException exceptionPayload = (MessageDeliveryException) errorMessage.getPayload();
    assertSame(message.getPayload(), exceptionPayload.getFailedMessage().getPayload());
    assertEquals(UnsupportedOperationException.class, exceptionPayload.getCause().getClass());
    assertNotSame(Thread.currentThread(), resultHandler.lastThread);
}
Also used : MessagePublishingErrorHandler(org.springframework.integration.channel.MessagePublishingErrorHandler) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) DirectChannel(org.springframework.integration.channel.DirectChannel) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Example 7 with StaticApplicationContext

use of org.springframework.context.support.StaticApplicationContext in project spring-integration by spring-projects.

the class DelayHandlerTests method defaultErrorChannelAndHandlerThrowsExceptionWithDelay.

@Test
public void defaultErrorChannelAndHandlerThrowsExceptionWithDelay() throws Exception {
    StaticApplicationContext context = new StaticApplicationContext();
    context.registerSingleton(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, DirectChannel.class);
    context.refresh();
    DirectChannel defaultErrorChannel = (DirectChannel) context.getBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME);
    MessagePublishingErrorHandler errorHandler = new MessagePublishingErrorHandler();
    errorHandler.setBeanFactory(context);
    taskScheduler.setErrorHandler(errorHandler);
    this.setDelayExpression();
    this.startDelayerHandler();
    output.unsubscribe(resultHandler);
    defaultErrorChannel.subscribe(resultHandler);
    output.subscribe(message -> {
        throw new UnsupportedOperationException("intentional test failure");
    });
    Message<?> message = MessageBuilder.withPayload("test").setHeader("delay", "10").build();
    input.send(message);
    waitForLatch(10000);
    Message<?> errorMessage = resultHandler.lastMessage;
    assertEquals(MessageDeliveryException.class, errorMessage.getPayload().getClass());
    MessageDeliveryException exceptionPayload = (MessageDeliveryException) errorMessage.getPayload();
    assertSame(message.getPayload(), exceptionPayload.getFailedMessage().getPayload());
    assertEquals(UnsupportedOperationException.class, exceptionPayload.getCause().getClass());
    assertNotSame(Thread.currentThread(), resultHandler.lastThread);
}
Also used : MessagePublishingErrorHandler(org.springframework.integration.channel.MessagePublishingErrorHandler) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) DirectChannel(org.springframework.integration.channel.DirectChannel) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Example 8 with StaticApplicationContext

use of org.springframework.context.support.StaticApplicationContext in project spring-integration by spring-projects.

the class ExpressionEvaluatingMessageProcessorTests method testProcessMessageWithMethodCallOnBean.

@Test
public void testProcessMessageWithMethodCallOnBean() throws Exception {
    StaticApplicationContext context = new StaticApplicationContext();
    BeanDefinition beanDefinition = new RootBeanDefinition(String.class);
    beanDefinition.getConstructorArgumentValues().addGenericArgumentValue("bar");
    context.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME, new RootBeanDefinition(IntegrationEvaluationContextFactoryBean.class));
    context.registerBeanDefinition("testString", beanDefinition);
    context.refresh();
    Expression expression = expressionParser.parseExpression("@testString.concat(payload)");
    ExpressionEvaluatingMessageProcessor<String> processor = new ExpressionEvaluatingMessageProcessor<>(expression);
    processor.setBeanFactory(context);
    processor.afterPropertiesSet();
    GenericMessage<String> message = new GenericMessage<>("foo");
    assertEquals("barfoo", processor.processMessage(message));
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) Expression(org.springframework.expression.Expression) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) IntegrationEvaluationContextFactoryBean(org.springframework.integration.config.IntegrationEvaluationContextFactoryBean) Test(org.junit.Test)

Example 9 with StaticApplicationContext

use of org.springframework.context.support.StaticApplicationContext in project spring-integration by spring-projects.

the class AsyncMessagingTemplateTests method asyncReceiveAndConvertWithResolvedChannel.

@Test
public void asyncReceiveAndConvertWithResolvedChannel() throws Exception {
    StaticApplicationContext context = new StaticApplicationContext();
    context.registerSingleton("testChannel", QueueChannel.class);
    context.refresh();
    QueueChannel channel = context.getBean("testChannel", QueueChannel.class);
    AsyncMessagingTemplate template = new AsyncMessagingTemplate();
    template.setBeanFactory(context);
    Future<?> result = template.asyncReceiveAndConvert("testChannel");
    sendMessageAfterDelay(channel, new GenericMessage<String>("test"), 200);
    long start = System.currentTimeMillis();
    assertNotNull(result.get(10000, TimeUnit.MILLISECONDS));
    long elapsed = System.currentTimeMillis() - start;
    assertTrue(elapsed >= 200 - safety);
    assertEquals("test", result.get());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) Test(org.junit.Test)

Example 10 with StaticApplicationContext

use of org.springframework.context.support.StaticApplicationContext in project spring-integration by spring-projects.

the class AsyncMessagingTemplateTests method asyncConvertAndSendWithResolvedChannel.

@Test
public void asyncConvertAndSendWithResolvedChannel() throws Exception {
    StaticApplicationContext context = new StaticApplicationContext();
    context.registerSingleton("testChannel", QueueChannel.class);
    context.refresh();
    QueueChannel channel = context.getBean("testChannel", QueueChannel.class);
    AsyncMessagingTemplate template = new AsyncMessagingTemplate();
    template.setBeanFactory(context);
    Future<?> future = template.asyncConvertAndSend("testChannel", "test");
    assertNull(future.get(10000, TimeUnit.MILLISECONDS));
    Message<?> result = channel.receive(0);
    assertEquals("test", result.getPayload());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) Test(org.junit.Test)

Aggregations

StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)322 Test (org.junit.jupiter.api.Test)218 lombok.val (lombok.val)159 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)59 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)39 BeforeEach (org.junit.jupiter.api.BeforeEach)36 Test (org.junit.Test)34 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)31 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)25 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)20 HashSet (java.util.HashSet)19 MockRequestContext (org.springframework.webflow.test.MockRequestContext)19 InMemoryServiceRegistry (org.apereo.cas.services.InMemoryServiceRegistry)16 MockServletContext (org.springframework.mock.web.MockServletContext)16 MultifactorAuthenticationProviderBypassProperties (org.apereo.cas.configuration.model.support.mfa.MultifactorAuthenticationProviderBypassProperties)15 BeanDefinitionReader (org.springframework.beans.factory.support.BeanDefinitionReader)13 ApplicationContext (org.springframework.context.ApplicationContext)12 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)11 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)11 QueueChannel (org.springframework.integration.channel.QueueChannel)11