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:org.terasoluna.gfw.web.codelist.CodeListInterceptorTest.java

/**
 * [preHandle] Case of CodeList is one.//from  w  w w  .  j  a  v  a 2  s.c  o m
 * <p>
 * [Expected Result]
 * <ol>
 * <li>set CodeList to attribute of HttpServletRequest.</li>
 * </ol>
 * </p>
 * @throws Exception
 */
@Test
public void testPreHandle_one() throws Exception {

    // do setup.
    StaticApplicationContext mockApplicationContext = new StaticApplicationContext();
    mockApplicationContext.registerSingleton("simpleMapCodeList", SimpleMapCodeList.class);
    SimpleMapCodeList simpleMapCodeList = mockApplicationContext.getBean(SimpleMapCodeList.class);
    simpleMapCodeList.setMap(Collections.singletonMap("key", "value"));

    testTarget.setApplicationContext(mockApplicationContext);
    testTarget.afterPropertiesSet();

    // do test.
    boolean actualReturnValue = testTarget.preHandle(mockRequest, mockResponse, null);

    // do assert.
    Enumeration<String> actualAttributeNames = mockRequest.getAttributeNames();
    assertThat(actualAttributeNames.hasMoreElements(), is(true));
    actualAttributeNames.nextElement();
    assertThat(actualAttributeNames.hasMoreElements(), is(false));
    assertThat(mockRequest.getAttribute("simpleMapCodeList").toString(),
            is(simpleMapCodeList.asMap().toString()));
    assertThat(actualReturnValue, is(true));

}

From source file:org.springframework.ws.transport.http.HttpComponentsMessageSenderIntegrationTest.java

@Test
public void testContextClose() throws Exception {
    MessageFactory messageFactory = MessageFactory.newInstance();
    int port = FreePortScanner.getFreePort();
    Server jettyServer = new Server(port);
    Context jettyContext = new Context(jettyServer, "/");
    jettyContext.addServlet(new ServletHolder(new EchoServlet()), "/");
    jettyServer.start();//from  w  ww .ja va2s. com
    WebServiceConnection connection = null;
    try {

        StaticApplicationContext appContext = new StaticApplicationContext();
        appContext.registerSingleton("messageSender", HttpComponentsMessageSender.class);
        appContext.refresh();

        HttpComponentsMessageSender messageSender = appContext.getBean("messageSender",
                HttpComponentsMessageSender.class);
        connection = messageSender.createConnection(new URI("http://localhost:" + port));

        connection.send(new SaajSoapMessage(messageFactory.createMessage()));
        connection.receive(new SaajSoapMessageFactory(messageFactory));

        appContext.close();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (IOException ex) {
                // ignore
            }
        }
        if (jettyServer.isRunning()) {
            jettyServer.stop();
        }
    }

}

From source file:de.itsvs.cwtrpc.controller.AutowiredRemoteServiceGroupConfigTest.java

@Test
public void testAfterPropertiesSet5() {
    final StaticApplicationContext appContext;
    final AutowiredRemoteServiceGroupConfig config;

    appContext = new StaticApplicationContext();
    appContext.registerSingleton("tService1", TestService1Impl.class);
    appContext.registerSingleton("tService2", TestService2Impl.class);
    appContext.registerSingleton("tService3", TestService3Impl.class);
    appContext.registerSingleton("tService5", TestService5Impl.class);

    config = new AutowiredRemoteServiceGroupConfig();
    config.setBasePackages(Arrays.asList(new String[] { "de.itsvs.cwtrpc.controller.config." }));
    config.setIncludeFilters(Arrays.asList(new Pattern[] {
            PatternFactory.compile(PatternType.REGEX, MatcherType.PACKAGE, "de\\..+\\.TestService[13]"),
            PatternFactory.compile(PatternType.REGEX, MatcherType.PACKAGE, "de\\..+\\.TestService5") }));
    config.setApplicationContext(appContext);
    config.afterPropertiesSet();/*from  w  ww.  ja  v  a  2s  . c om*/

    Assert.assertTrue(config.getChildGroupConfigs().isEmpty());
    Assert.assertEquals(3, config.getServiceConfigs().size());
    Assert.assertTrue(containsServiceConfig(appContext, config.getServiceConfigs(), TestService1Impl.class));
    Assert.assertTrue(containsServiceConfig(appContext, config.getServiceConfigs(), TestService3Impl.class));
    Assert.assertTrue(containsServiceConfig(appContext, config.getServiceConfigs(), TestService5Impl.class));
}

From source file:io.milton.grizzly.GrizzlyLoadBalancer.java

@SuppressWarnings("resource")
protected ConfigurableApplicationContext initSpringApplicationContext() {

    log.info("No root spring context");
    parent = new StaticApplicationContext();

    ConfigurableApplicationContext ctx = null;
    String[] contextFiles = new String[] { "applicationContext.xml" };
    parent.refresh();/*from ww w . j  ava 2s  .  c  om*/
    try {
        ctx = new ClassPathXmlApplicationContext(contextFiles, parent);
    } catch (BeansException e) {
        log.error("Unable to create a child context for Milton", e);
    }
    return ctx;
}

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

@Test
public void receiveAndReplyMessage_methodAnnotatedWithMessageMappingAnnotation_methodInvokedForIncomingMessageAndReplySentBackToSendToDestination()
        throws Exception {
    StaticApplicationContext applicationContext = new StaticApplicationContext();
    applicationContext.registerSingleton("incomingMessageHandler", IncomingMessageHandler.class);
    applicationContext.registerBeanDefinition("queueMessageHandler", getQueueMessageHandlerBeanDefinition());
    applicationContext.refresh();/*from   w w  w.  j  a  v  a 2  s  .c om*/

    MessageHandler messageHandler = applicationContext.getBean(MessageHandler.class);
    messageHandler.handleMessage(MessageBuilder.withPayload("testContent")
            .setHeader(QueueMessageHandler.Headers.LOGICAL_RESOURCE_ID_MESSAGE_HEADER_KEY, "receiveAndReply")
            .build());

    IncomingMessageHandler messageListener = applicationContext.getBean(IncomingMessageHandler.class);
    assertEquals("testContent", messageListener.getLastReceivedMessage());
    verify(this.messageTemplate).convertAndSend(eq("sendTo"), eq("TESTCONTENT"));
}

From source file:io.milton.grizzly.GrizzlyServer.java

@SuppressWarnings("resource")
protected ConfigurableApplicationContext initSpringApplicationContext() {

    log.info("No root spring context");
    parent = new StaticApplicationContext();

    ConfigurableApplicationContext ctx = null;
    String[] contextFiles = new String[] { "applicationContext.xml" };
    parent.refresh();//from   w  w w . ja  v a2s.c om
    try {
        ctx = new ClassPathXmlApplicationContext(contextFiles, parent);
    } catch (BeansException e) {
        log.error("Unable to create a child context for Milton", e);
    }
    return ctx;

}

From source file:de.itsvs.cwtrpc.controller.AutowiredRemoteServiceGroupConfigTest.java

@Test
public void testAfterPropertiesSet6() {
    final StaticApplicationContext appContext;
    final AutowiredRemoteServiceGroupConfig config;

    appContext = new StaticApplicationContext();
    appContext.registerSingleton("tService1", TestService1Impl.class);
    appContext.registerSingleton("tService2", TestService2Impl.class);
    appContext.registerSingleton("tService3", TestService3Impl.class);
    appContext.registerSingleton("tService5", TestService5Impl.class);

    config = new AutowiredRemoteServiceGroupConfig();
    config.setBasePackages(Arrays.asList(new String[] { "de.itsvs.cwtrpc.controller.config." }));
    config.setExcludeFilters(Arrays.asList(new Pattern[] {
            PatternFactory.compile(PatternType.REGEX, MatcherType.PACKAGE, "de\\..+\\.TestService[13]"),
            PatternFactory.compile(PatternType.REGEX, MatcherType.PACKAGE, "de\\..+\\.TestService5") }));
    config.setApplicationContext(appContext);
    config.afterPropertiesSet();//from  www  .  ja  v a 2 s . c o m

    Assert.assertTrue(config.getChildGroupConfigs().isEmpty());
    Assert.assertEquals(1, config.getServiceConfigs().size());
    Assert.assertTrue(containsServiceConfig(appContext, config.getServiceConfigs(), TestService2Impl.class));
}

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  w  w w  . ja  v a2s .  c o m

    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:net.frontlinesms.FrontlineSMS.java

/** Initialise {@link #applicationContext}. */
public void initApplicationContext() throws DataAccessResourceFailureException {
    // Load the data mode from the app.properties file
    AppProperties appProperties = AppProperties.getInstance();

    LOG.info("Load Spring/Hibernate application context to initialise DAOs");

    // Create a base ApplicationContext defining the hibernate config file we need to import
    StaticApplicationContext baseApplicationContext = new StaticApplicationContext();
    baseApplicationContext.registerBeanDefinition("hibernateConfigLocations",
            createHibernateConfigLocationsBeanDefinition());

    // Get the spring config locations
    String databaseExternalConfigPath = ResourceUtils.getConfigDirectoryPath()
            + ResourceUtils.PROPERTIES_DIRECTORY_NAME + File.separatorChar
            + appProperties.getDatabaseConfigPath();
    String[] configLocations = getSpringConfigLocations(databaseExternalConfigPath);
    baseApplicationContext.refresh();//  ww  w.ja v  a 2  s  .co m

    FileSystemXmlApplicationContext applicationContext = new FileSystemXmlApplicationContext(configLocations,
            false, baseApplicationContext);
    this.applicationContext = applicationContext;

    // Add post-processor to handle substituted database properties
    PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
    String databasePropertiesPath = ResourceUtils.getConfigDirectoryPath()
            + ResourceUtils.PROPERTIES_DIRECTORY_NAME + File.separatorChar
            + appProperties.getDatabaseConfigPath() + ".properties";
    propertyPlaceholderConfigurer.setLocation(new FileSystemResource(new File(databasePropertiesPath)));
    propertyPlaceholderConfigurer.setIgnoreResourceNotFound(true);
    applicationContext.addBeanFactoryPostProcessor(propertyPlaceholderConfigurer);
    applicationContext.refresh();

    LOG.info("Context loaded successfully.");

    this.pluginManager = new PluginManager(this, applicationContext);

    LOG.info("Getting DAOs from application context...");
    groupDao = (GroupDao) applicationContext.getBean("groupDao");
    groupMembershipDao = (GroupMembershipDao) applicationContext.getBean("groupMembershipDao");
    contactDao = (ContactDao) applicationContext.getBean("contactDao");
    keywordDao = (KeywordDao) applicationContext.getBean("keywordDao");
    keywordActionDao = (KeywordActionDao) applicationContext.getBean("keywordActionDao");
    messageDao = (MessageDao) applicationContext.getBean("messageDao");
    emailDao = (EmailDao) applicationContext.getBean("emailDao");
    emailAccountDao = (EmailAccountDao) applicationContext.getBean("emailAccountDao");
    smsInternetServiceSettingsDao = (SmsInternetServiceSettingsDao) applicationContext
            .getBean("smsInternetServiceSettingsDao");
    smsModemSettingsDao = (SmsModemSettingsDao) applicationContext.getBean("smsModemSettingsDao");
    eventBus = (EventBus) applicationContext.getBean("eventBus");
}

From source file:de.itsvs.cwtrpc.controller.AutowiredRemoteServiceGroupConfigTest.java

@Test
public void testAfterPropertiesSet7() {
    final StaticApplicationContext appContext;
    final AutowiredRemoteServiceGroupConfig config;

    appContext = new StaticApplicationContext();
    appContext.registerSingleton("tService1", TestService1Impl.class);
    appContext.registerSingleton("tService2", TestService2Impl.class);
    appContext.registerSingleton("tService3", TestService3Impl.class);
    appContext.registerSingleton("tService5", TestService5Impl.class);

    config = new AutowiredRemoteServiceGroupConfig();
    config.setBasePackages(Arrays.asList(new String[] { "de.itsvs.cwtrpc.controller.config." }));
    config.setIncludeFilters(Arrays.asList(new Pattern[] {
            PatternFactory.compile(PatternType.REGEX, MatcherType.PACKAGE, "de\\..+\\.TestService[13]"),
            PatternFactory.compile(PatternType.REGEX, MatcherType.PACKAGE, "de\\..+\\.TestService5") }));
    config.setExcludeFilters(Arrays.asList(new Pattern[] {
            PatternFactory.compile(PatternType.REGEX, MatcherType.PACKAGE, "de\\..+\\.TestService1") }));
    config.setApplicationContext(appContext);
    config.afterPropertiesSet();/*w w w  .j  av a  2  s.  c om*/

    Assert.assertTrue(config.getChildGroupConfigs().isEmpty());
    Assert.assertEquals(2, config.getServiceConfigs().size());
    Assert.assertTrue(containsServiceConfig(appContext, config.getServiceConfigs(), TestService3Impl.class));
    Assert.assertTrue(containsServiceConfig(appContext, config.getServiceConfigs(), TestService5Impl.class));
}