Search in sources :

Example 16 with StaticApplicationContext

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

the class HeaderValueRouterTests method resolveChannelNameFromMapAndCustomeResolver.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void resolveChannelNameFromMapAndCustomeResolver() {
    final StaticApplicationContext context = new StaticApplicationContext();
    ManagedMap channelMappings = new ManagedMap();
    channelMappings.put("testKey", "testChannel");
    RootBeanDefinition routerBeanDefinition = new RootBeanDefinition(HeaderValueRouter.class);
    routerBeanDefinition.getConstructorArgumentValues().addGenericArgumentValue("testHeaderName");
    routerBeanDefinition.getPropertyValues().addPropertyValue("resolutionRequired", "true");
    routerBeanDefinition.getPropertyValues().addPropertyValue("channelMappings", channelMappings);
    routerBeanDefinition.getPropertyValues().addPropertyValue("beanFactory", context);
    routerBeanDefinition.getPropertyValues().addPropertyValue("channelResolver", (DestinationResolver<MessageChannel>) channelName -> context.getBean("anotherChannel", MessageChannel.class));
    context.registerBeanDefinition("router", routerBeanDefinition);
    context.registerBeanDefinition("testChannel", new RootBeanDefinition(QueueChannel.class));
    context.registerBeanDefinition("anotherChannel", new RootBeanDefinition(QueueChannel.class));
    context.refresh();
    MessageHandler handler = (MessageHandler) context.getBean("router");
    Message<?> message = MessageBuilder.withPayload("test").setHeader("testHeaderName", "testKey").build();
    handler.handleMessage(message);
    QueueChannel channel = (QueueChannel) context.getBean("anotherChannel");
    Message<?> result = channel.receive(1000);
    assertNotNull(result);
    assertSame(message, result);
    context.close();
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) QueueChannel(org.springframework.integration.channel.QueueChannel) ManagedMap(org.springframework.beans.factory.support.ManagedMap) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) MessageChannel(org.springframework.messaging.MessageChannel) Assert.assertSame(org.junit.Assert.assertSame) Assert.assertThat(org.junit.Assert.assertThat) MessageBuilder(org.springframework.integration.support.MessageBuilder) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) Matchers.contains(org.hamcrest.Matchers.contains) MessageHandler(org.springframework.messaging.MessageHandler) Matchers.equalTo(org.hamcrest.Matchers.equalTo) DestinationResolver(org.springframework.messaging.core.DestinationResolver) Message(org.springframework.messaging.Message) MessageChannel(org.springframework.messaging.MessageChannel) QueueChannel(org.springframework.integration.channel.QueueChannel) MessageHandler(org.springframework.messaging.MessageHandler) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ManagedMap(org.springframework.beans.factory.support.ManagedMap) Test(org.junit.Test)

Example 17 with StaticApplicationContext

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

the class HeaderValueRouterTests method resolveMultipleChannelsWithStringArray.

@Test
public void resolveMultipleChannelsWithStringArray() {
    StaticApplicationContext context = new StaticApplicationContext();
    RootBeanDefinition routerBeanDefinition = new RootBeanDefinition(HeaderValueRouter.class);
    routerBeanDefinition.getConstructorArgumentValues().addGenericArgumentValue("testHeaderName");
    routerBeanDefinition.getPropertyValues().addPropertyValue("resolutionRequired", "true");
    context.registerBeanDefinition("router", routerBeanDefinition);
    context.registerBeanDefinition("channel1", new RootBeanDefinition(QueueChannel.class));
    context.registerBeanDefinition("channel2", new RootBeanDefinition(QueueChannel.class));
    context.refresh();
    MessageHandler handler = (MessageHandler) context.getBean("router");
    String[] channels = new String[] { "channel1", "channel2" };
    Message<?> message = MessageBuilder.withPayload("test").setHeader("testHeaderName", channels).build();
    handler.handleMessage(message);
    QueueChannel channel1 = (QueueChannel) context.getBean("channel1");
    QueueChannel channel2 = (QueueChannel) context.getBean("channel2");
    Message<?> result1 = channel1.receive(1000);
    Message<?> result2 = channel2.receive(1000);
    assertNotNull(result1);
    assertNotNull(result2);
    assertSame(message, result1);
    assertSame(message, result2);
    context.close();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) MessageHandler(org.springframework.messaging.MessageHandler) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 18 with StaticApplicationContext

use of org.springframework.context.support.StaticApplicationContext in project spring-cloud-function by spring-cloud.

the class FunctionProxyApplicationListener method bind.

private void bind(ConfigurableApplicationContext context) {
    ConfigurationPropertiesBindingPostProcessor post = new ConfigurationPropertiesBindingPostProcessor();
    post.setBeanFactory(new DefaultListableBeanFactory());
    post.setEnvironment(context.getEnvironment());
    post.setApplicationContext(new StaticApplicationContext());
    try {
        post.afterPropertiesSet();
    } catch (Exception e) {
        throw new IllegalStateException("Cannot bind properties", e);
    }
    post.postProcessBeforeInitialization(this, getClass().getName());
}
Also used : ConfigurationPropertiesBindingPostProcessor(org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory)

Example 19 with StaticApplicationContext

use of org.springframework.context.support.StaticApplicationContext in project spring-cloud-stream by spring-cloud.

the class BinderPropertiesTests method testSerializationWithNonStringValues.

@SuppressWarnings("unchecked")
@Test
public void testSerializationWithNonStringValues() {
    StaticApplicationContext context = new StaticApplicationContext();
    DefaultListableBeanFactory bf = (DefaultListableBeanFactory) context.getBeanFactory();
    BindingServiceProperties bindingServiceProperties = new BindingServiceProperties();
    bindingServiceProperties.setApplicationContext(context);
    bf.registerSingleton("bindingServiceProperties", bindingServiceProperties);
    BindingServiceProperties bsp = context.getBean(BindingServiceProperties.class);
    bsp.setApplicationContext(context);
    BinderProperties bp = new BinderProperties();
    bsp.setBinders(Collections.singletonMap("testBinder", bp));
    bp.getEnvironment().put("spring.rabbitmq.connection-timeout", 2345);
    bp.getEnvironment().put("foo", Collections.singletonMap("bar", "hello"));
    // using Spring Boot class to ensure that reliance on the same ObjectMapper configuration
    ConfigurationPropertiesReportEndpoint endpoint = new ConfigurationPropertiesReportEndpoint();
    endpoint.setApplicationContext(context);
    ContextConfigurationProperties configurationProperties = endpoint.configurationProperties().getContexts().values().iterator().next();
    Map<String, Object> properties = configurationProperties.getBeans().get("bindingServiceProperties").getProperties();
    assertFalse(properties.containsKey("error"));
    assertTrue(properties.containsKey("binders"));
    Map<String, Object> testBinder = (Map<String, Object>) ((Map<String, Object>) properties.get("binders")).get("testBinder");
    Map<String, Object> environment = (Map<String, Object>) testBinder.get("environment");
    assertTrue(environment.get("spring.rabbitmq.connection-timeout") instanceof Integer);
    assertTrue(environment.get("foo") instanceof Map);
}
Also used : ContextConfigurationProperties(org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpoint.ContextConfigurationProperties) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) ConfigurationPropertiesReportEndpoint(org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpoint) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Map(java.util.Map) Test(org.junit.Test)

Example 20 with StaticApplicationContext

use of org.springframework.context.support.StaticApplicationContext in project jetcache by alibaba.

the class DefaultSpringKeyConvertorTest method setup.

@BeforeEach
public void setup() {
    context = new StaticApplicationContext();
    beanFactory = context.getDefaultListableBeanFactory();
    parser = new DefaultSpringKeyConvertorParser();
    parser.setApplicationContext(context);
}
Also used : StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

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