Example usage for org.springframework.context.support StaticApplicationContext StaticApplicationContext

List of usage examples for org.springframework.context.support StaticApplicationContext StaticApplicationContext

Introduction

In this page you can find the example usage for org.springframework.context.support StaticApplicationContext StaticApplicationContext.

Prototype

public StaticApplicationContext() throws BeansException 

Source Link

Document

Create a new StaticApplicationContext.

Usage

From source file:com.urbanmania.spring.beans.factory.config.annotations.PropertyAnnotationAndPlaceholderConfigurerTest.java

@Test
public void testProcessPropertiesAndUpdate() throws Exception {
    configurer.setPropertyLoaders(new MockPropertyLoader[] { new MockPropertyLoader(properties) });

    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(UpdateableTestBean.class);
    beanFactory.registerBeanDefinition(TEST_BEAN_NAME, beanDefinition);

    properties.put(TEST_KEY, TEST_VALUE);

    configurer.loadProperties(properties);
    configurer.processProperties(beanFactory, properties);

    StaticApplicationContext context = new StaticApplicationContext();
    context.registerSingleton(TEST_BEAN_NAME, UpdateableTestBean.class);
    configurer.setApplicationContext(context);

    configurer.setBeanFactory(beanFactory);
    configurer.propertyChanged(new PropertyEvent(this, TEST_KEY, TEST_CHANGED_VALUE));

    assertEquals(TEST_CHANGED_VALUE, ((UpdateableTestBean) context.getBean(TEST_BEAN_NAME)).getProperty());
    assertNull(System.getProperties().get(TEST_KEY));
}

From source file:org.springframework.cloud.aws.messaging.listener.QueueMessageHandlerTest.java

@Test
public void receiveMessage_withHeaderAnnotationAsArgument_shouldReceiveRequestedHeader() throws Exception {
    // Arrange//from  w  w w. j a  va 2 s .c o  m
    StaticApplicationContext applicationContext = new StaticApplicationContext();
    applicationContext.registerSingleton("messageHandlerWithHeaderAnnotation",
            MessageReceiverWithHeaderAnnotation.class);
    applicationContext.registerSingleton("queueMessageHandler", QueueMessageHandler.class);
    applicationContext.refresh();

    QueueMessageHandler queueMessageHandler = applicationContext.getBean(QueueMessageHandler.class);
    MessageReceiverWithHeaderAnnotation messageReceiver = applicationContext
            .getBean(MessageReceiverWithHeaderAnnotation.class);

    // Act
    queueMessageHandler.handleMessage(
            MessageBuilder.withPayload("Hello from a sender").setHeader("SenderId", "elsUnitTest")
                    .setHeader(QueueMessageHandler.Headers.LOGICAL_RESOURCE_ID_MESSAGE_HEADER_KEY, "testQueue")
                    .build());

    // Assert
    assertEquals("Hello from a sender", messageReceiver.getPayload());
    assertEquals("elsUnitTest", messageReceiver.getSenderId());
}

From source file:org.terasoluna.gfw.web.codelist.CodeListInterceptorTest.java

/**
 * [afterPropertiesSet] Case of not define CodeList.
 * <p>//from  w w  w  .j av a 2 s  . c o  m
 * [Expected Result]
 * <ol>
 * <li>target is empty.(don't occur error.)</li>
 * </ol>
 * </p>
 * @throws Exception
 */
@Test
public void testAfterPropertiesSet_not_define_CodeList() throws Exception {

    // do setup.
    testTarget.setApplicationContext(new StaticApplicationContext());

    // do test.
    testTarget.afterPropertiesSet();

    // do assert.
    assertThat(testTarget.getCodeLists().isEmpty(), is(true));

}

From source file:com.urbanmania.spring.beans.factory.config.annotations.PropertyAnnotationAndPlaceholderConfigurerTest.java

@Test
public void testProcessPropertiesConvertAndUpdate() throws Exception {
    configurer.setPropertyLoaders(new MockPropertyLoader[] { new MockPropertyLoader(properties) });

    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(ConvertableTestBean.class);
    beanFactory.registerBeanDefinition(TEST_BEAN_NAME, beanDefinition);

    properties.put(TEST_KEY, "1");

    configurer.loadProperties(properties);
    configurer.processProperties(beanFactory, properties);

    StaticApplicationContext context = new StaticApplicationContext();
    context.registerSingleton(TEST_BEAN_NAME, ConvertableTestBean.class);
    configurer.setApplicationContext(context);

    configurer.setBeanFactory(beanFactory);
    configurer.propertyChanged(new PropertyEvent(this, TEST_KEY, "2"));

    assertEquals(2, ((ConvertableTestBean) context.getBean(TEST_BEAN_NAME)).getProperty());
    assertNull(System.getProperties().get(TEST_KEY));
}

From source file:net.sourceforge.vulcan.spring.SpringPluginManagerTest.java

public void testPluginAppCtxGetsParentPropertyPlaceholder() throws Exception {
    final PropertyResourceConfigurer cfgr = new PropertyPlaceholderConfigurer();

    final StaticApplicationContext ctx = new StaticApplicationContext();

    ctx.getBeanFactory().registerSingleton("foo", cfgr);

    ctx.refresh();/*from   ww  w  .  ja  v  a 2s.c o m*/

    mgr.setApplicationContext(ctx);

    expect(store.getPluginConfigs()).andReturn(new PluginMetaDataDto[] { createFakePluginConfig(true) });

    expert.registerPlugin((ClassLoader) anyObject(), (String) anyObject());

    replay();

    mgr.init();

    verify();

    assertTrue("should contain cfgr",
            mgr.plugins.get("1").context.getBeanFactoryPostProcessors().contains(cfgr));
}

From source file:org.terasoluna.gfw.web.codelist.CodeListInterceptorTest.java

/**
 * [afterPropertiesSet] Case of define only one bean of codelist type.
 * <p>/*  w w w.  ja va2  s .c  o m*/
 * [Expected Result]
 * <ol>
 * <li>target is one.(don't occur error.)</li>
 * </ol>
 * </p>
 * @throws Exception
 */
@Test
public void testAfterPropertiesSet_not_define_one_CodeList() throws Exception {

    // do setup.
    StaticApplicationContext mockApplicationContext = new StaticApplicationContext();
    mockApplicationContext.registerSingleton("simpleMapCodeList", SimpleMapCodeList.class);
    testTarget.setApplicationContext(mockApplicationContext);

    // do test.
    testTarget.afterPropertiesSet();

    // do assert.
    List<CodeList> expectedCodeLists = new ArrayList<CodeList>();
    expectedCodeLists.add(mockApplicationContext.getBean("simpleMapCodeList", CodeList.class));

    assertThat(testTarget.getCodeLists().toString(), is(expectedCodeLists.toString()));

}

From source file:org.springframework.cloud.aws.messaging.listener.QueueMessageHandlerTest.java

@Test
public void receiveMessage_withWrongHeaderAnnotationValueAsArgument_shouldReceiveNullAsHeaderValue()
        throws Exception {
    // Arrange//from w  w w .j ava 2  s. c  o  m
    StaticApplicationContext applicationContext = new StaticApplicationContext();
    applicationContext.registerSingleton("messageHandlerWithHeaderAnnotation",
            MessageReceiverWithHeaderAnnotation.class);
    applicationContext.registerSingleton("queueMessageHandler", QueueMessageHandler.class);
    applicationContext.refresh();

    QueueMessageHandler queueMessageHandler = applicationContext.getBean(QueueMessageHandler.class);
    MessageReceiverWithHeaderAnnotation messageReceiver = applicationContext
            .getBean(MessageReceiverWithHeaderAnnotation.class);

    // Act
    queueMessageHandler.handleMessage(MessageBuilder.withPayload("Hello from a sender")
            .setHeader(QueueMessageHandler.Headers.LOGICAL_RESOURCE_ID_MESSAGE_HEADER_KEY, "testQueue")
            .build());

    // Assert
    assertEquals("Hello from a sender", messageReceiver.getPayload());
    assertNull(messageReceiver.getSenderId());
}

From source file:org.springframework.cloud.aws.messaging.listener.QueueMessageHandlerTest.java

@Test
public void receiveMessage_withHeadersAsArgumentAnnotation_shouldReceiveAllHeaders() throws Exception {
    // Arrange/*w  w  w  . j  av  a  2 s. com*/
    StaticApplicationContext applicationContext = new StaticApplicationContext();
    applicationContext.registerSingleton("messageHandlerWithHeadersAnnotation",
            MessageReceiverWithHeadersAnnotation.class);
    applicationContext.registerSingleton("queueMessageHandler", QueueMessageHandler.class);
    applicationContext.refresh();

    QueueMessageHandler queueMessageHandler = applicationContext.getBean(QueueMessageHandler.class);
    MessageReceiverWithHeadersAnnotation messageReceiver = applicationContext
            .getBean(MessageReceiverWithHeadersAnnotation.class);

    // Act
    queueMessageHandler.handleMessage(MessageBuilder.withPayload("Hello from a sender")
            .setHeader(QueueMessageHandler.Headers.LOGICAL_RESOURCE_ID_MESSAGE_HEADER_KEY, "testQueue")
            .setHeader("SenderId", "ID").build());

    // Assert
    assertNotNull(messageReceiver.getHeaders());
    assertEquals("ID", messageReceiver.getHeaders().get("SenderId"));
    assertEquals("testQueue", messageReceiver.getHeaders()
            .get(QueueMessageHandler.Headers.LOGICAL_RESOURCE_ID_MESSAGE_HEADER_KEY));
}

From source file:org.springframework.cloud.aws.messaging.listener.QueueMessageHandlerTest.java

@Test
public void receiveMessage_withCustomArgumentResolvers_shouldCallThemBeforeTheDefaultOnes() throws Exception {
    // Arrange/*from w  ww . j  a v a  2  s.c  o  m*/
    StaticApplicationContext applicationContext = new StaticApplicationContext();
    applicationContext.registerSingleton("incomingMessageHandler", IncomingMessageHandler.class);

    HandlerMethodArgumentResolver handlerMethodArgumentResolver = mock(HandlerMethodArgumentResolver.class);
    when(handlerMethodArgumentResolver.supportsParameter(any(MethodParameter.class))).thenReturn(true);
    when(handlerMethodArgumentResolver.resolveArgument(any(MethodParameter.class), any(Message.class)))
            .thenReturn("Hello from a sender");
    MutablePropertyValues properties = new MutablePropertyValues(Collections
            .singletonList(new PropertyValue("customArgumentResolvers", handlerMethodArgumentResolver)));
    applicationContext.registerSingleton("queueMessageHandler", QueueMessageHandler.class, properties);
    applicationContext.refresh();

    QueueMessageHandler queueMessageHandler = applicationContext.getBean(QueueMessageHandler.class);

    // Act
    queueMessageHandler.handleMessage(MessageBuilder.withPayload("Hello from a sender")
            .setHeader(QueueMessageHandler.Headers.LOGICAL_RESOURCE_ID_MESSAGE_HEADER_KEY, "receive").build());

    // Assert
    verify(handlerMethodArgumentResolver, times(1)).resolveArgument(any(MethodParameter.class),
            any(Message.class));
}

From source file:org.springframework.cloud.aws.messaging.listener.QueueMessageHandlerTest.java

@Test
public void receiveMessage_withCustomReturnValueHandlers_shouldCallThemBeforeTheDefaultOnes() throws Exception {
    // Arrange/*from  ww  w .ja  va 2  s  .c o m*/
    StaticApplicationContext applicationContext = new StaticApplicationContext();
    applicationContext.registerSingleton("incomingMessageHandler", IncomingMessageHandler.class);

    HandlerMethodReturnValueHandler handlerMethodReturnValueHandler = mock(
            HandlerMethodReturnValueHandler.class);
    when(handlerMethodReturnValueHandler.supportsReturnType(any(MethodParameter.class))).thenReturn(true);
    MutablePropertyValues properties = new MutablePropertyValues(Collections
            .singletonList(new PropertyValue("customReturnValueHandlers", handlerMethodReturnValueHandler)));
    applicationContext.registerSingleton("queueMessageHandler", QueueMessageHandler.class, properties);
    applicationContext.refresh();

    QueueMessageHandler queueMessageHandler = applicationContext.getBean(QueueMessageHandler.class);

    // Act
    queueMessageHandler.handleMessage(MessageBuilder.withPayload("Hello from a sender")
            .setHeader(QueueMessageHandler.Headers.LOGICAL_RESOURCE_ID_MESSAGE_HEADER_KEY, "receiveAndReply")
            .build());

    // Assert
    verify(handlerMethodReturnValueHandler, times(1)).handleReturnValue(any(Object.class),
            any(MethodParameter.class), any(Message.class));

}