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

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

Introduction

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

Prototype

@Override
    public Object getBean(String name) throws BeansException 

Source Link

Usage

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

@Test
public void receiveMessage_withWrongHeaderAnnotationValueAsArgument_shouldReceiveNullAsHeaderValue()
        throws Exception {
    // Arrange/*from   w  ww.j a v a 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/*from w w w  .ja  va  2 s . co m*/
    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_methodAnnotatedWithMessageMappingContainingMultipleQueueNames_methodInvokedForEachQueueName()
        throws Exception {
    StaticApplicationContext applicationContext = new StaticApplicationContext();
    applicationContext.registerSingleton("incomingMessageHandlerWithMultipleQueueNames",
            IncomingMessageHandlerWithMultipleQueueNames.class);
    applicationContext.registerSingleton("queueMessageHandler", QueueMessageHandler.class);
    applicationContext.refresh();//from  ww w  .  j a  v  a 2s. c om

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

    queueMessageHandler.handleMessage(MessageBuilder.withPayload("Hello from queue one!")
            .setHeader(QueueMessageHandler.Headers.LOGICAL_RESOURCE_ID_MESSAGE_HEADER_KEY, "queueOne").build());
    assertEquals("Hello from queue one!", incomingMessageHandler.getLastReceivedMessage());

    queueMessageHandler.handleMessage(MessageBuilder.withPayload("Hello from queue two!")
            .setHeader(QueueMessageHandler.Headers.LOGICAL_RESOURCE_ID_MESSAGE_HEADER_KEY, "queueTwo").build());
    assertEquals("Hello from queue two!", incomingMessageHandler.getLastReceivedMessage());
}

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

@Test
public void receiveMessage_withCustomReturnValueHandlers_shouldCallThemBeforeTheDefaultOnes() throws Exception {
    // Arrange//from w w w . java2s  .  co  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));

}

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

@Test
public void receiveMessage_withCustomArgumentResolvers_shouldCallThemBeforeTheDefaultOnes() throws Exception {
    // Arrange// ww  w .  j av 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:ome.client.utests.Preferences3Test.java

@Test
public void test_makeOurOwnDefault() throws Exception {
    // Others:/*from  w  w w. ja v a  2  s .  c om*/
    // new ManagedMap();
    // BeanWrapper bw = new BeanWrapperImpl( defaultMap );

    Map defaultMap = new HashMap();
    defaultMap.put("omero.user", "foo");
    ConstructorArgumentValues cav = new ConstructorArgumentValues();
    cav.addGenericArgumentValue(defaultMap);
    BeanDefinition def = new RootBeanDefinition(HashMap.class, cav, null);
    StaticApplicationContext ac = new StaticApplicationContext();
    ac.registerBeanDefinition("map", def);
    ac.refresh();

    ConstructorArgumentValues testCav = new ConstructorArgumentValues();
    testCav.addGenericArgumentValue(new RuntimeBeanReference("map"));
    BeanDefinition testDef = new RootBeanDefinition(HashMap.class, testCav, null);
    StaticApplicationContext defaultTest = new StaticApplicationContext(ac);
    defaultTest.registerBeanDefinition("test", testDef);
    defaultTest.refresh();
    assertTrue("foo".equals(((Map) defaultTest.getBean("test")).get("omero.user")));

}

From source file:ome.client.utests.Preferences3Test.java

@Test
public void test_makeOurOwnRuntime() throws Exception {

    // use properties
    // if no Properties given, then is static (global)

    Map runtimeMap = new HashMap();
    runtimeMap.put("omero.user", "bar");
    ConstructorArgumentValues cav2 = new ConstructorArgumentValues();
    cav2.addGenericArgumentValue(runtimeMap);
    BeanDefinition def2 = new RootBeanDefinition(HashMap.class, cav2, null);
    StaticApplicationContext ac2 = new StaticApplicationContext();
    ac2.registerBeanDefinition("map", def2);
    ac2.refresh();/*w w w  . j av a 2  s  .c  o  m*/

    ConstructorArgumentValues testCav2 = new ConstructorArgumentValues();
    testCav2.addGenericArgumentValue(new RuntimeBeanReference("map"));
    BeanDefinition testDef2 = new RootBeanDefinition(HashMap.class, testCav2, null);
    StaticApplicationContext defaultTest2 = new StaticApplicationContext(ac2);
    defaultTest2.registerBeanDefinition("test", testDef2);
    defaultTest2.refresh();
    assertTrue("bar".equals(((Map) defaultTest2.getBean("test")).get("omero.user")));

}

From source file:ome.client.utests.Preferences3Test.java

@Test(groups = "ticket:1058")
public void testOmeroUserIsProperlySetWithSpring2_5_5Manual() {

    Server s = new Server("localhost", 1099);
    Login l = new Login("me", "password");
    Properties p = s.asProperties();
    p.putAll(l.asProperties());//w  ww . j  a  v  a2s .  c o  m

    // This is copied from OmeroContext. This is the parent context which
    // should contain the properties;
    Properties copy = new Properties(p);
    ConstructorArgumentValues ctorArg1 = new ConstructorArgumentValues();
    ctorArg1.addGenericArgumentValue(copy);
    BeanDefinition definition1 = new RootBeanDefinition(Properties.class, ctorArg1, null);
    StaticApplicationContext staticContext = new StaticApplicationContext();
    staticContext.registerBeanDefinition("properties", definition1);
    staticContext.refresh();

    // This is the child context and contains a definition of a
    // PlaceHolderConfigurer
    // as well as a user of
    StaticApplicationContext childContext = new StaticApplicationContext();

    MutablePropertyValues mpv2 = new MutablePropertyValues();
    mpv2.addPropertyValue("properties", new RuntimeBeanReference("properties"));
    mpv2.addPropertyValue("systemPropertiesModeName", "SYSTEM_PROPERTIES_MODE_FALLBACK");
    mpv2.addPropertyValue("localOverride", "true");
    BeanDefinition definitionConfigurer = new RootBeanDefinition(PreferencesPlaceholderConfigurer.class, null,
            mpv2);
    childContext.registerBeanDefinition("propertiesPlaceholderConfigurer", definitionConfigurer);

    ConstructorArgumentValues cav2 = new ConstructorArgumentValues();
    cav2.addGenericArgumentValue("${omero.user}");
    BeanDefinition definitionTest = new RootBeanDefinition(String.class, cav2, null);
    childContext.registerBeanDefinition("test", definitionTest);

    childContext.setParent(staticContext);
    childContext.refresh();

    String test = (String) childContext.getBean("test");
    assertEquals(test, "me");

}

From source file:org.marketcetera.orderloader.Main.java

/**
 * Reads the orders from the supplied and sends them to the server.
 *
 * @throws Exception if there were errors.
 *//*from www .  jav a  2 s .co m*/
protected void doProcessing() throws Exception {
    //Create the order processor
    StaticApplicationContext context = new StaticApplicationContext(
            new FileSystemXmlApplicationContext(CFG_BASE_FILE_NAME));
    String clientURL = (String) context.getBean("clientURL"); //$NON-NLS-1$
    String clientWSHost = (String) context.getBean("clientWSHost"); //$NON-NLS-1$
    Integer clientWSPort = (Integer) context.getBean("clientWSPort"); //$NON-NLS-1$
    String clientIDPrefix = (String) context.getBean("clientIDPrefix"); //$NON-NLS-1$
    ClientParameters parameters = new ClientParameters(mAuthentication.getUser(), mAuthentication.getPassword(),
            clientURL, clientWSHost, clientWSPort, clientIDPrefix);
    OrderProcessor processor = createProcessor(parameters);
    //Run the order loader and display the summary of results.
    try {
        displaySummary(new OrderLoader(mMode, mBrokerID, processor, new File(mFileName)));
    } finally {
        processor.done();
    }
}