Search in sources :

Example 46 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project camel by apache.

the class CamelSpringTestContextLoader method loadContext.

/**
     *  Modeled after the Spring implementation in {@link AbstractGenericContextLoader},
     *  this method creates and refreshes the application context while providing for
     *  processing of additional Camel specific post-refresh actions.  We do not provide the
     *  pre-post hooks for customization seen in {@link AbstractGenericContextLoader} because
     *  they probably are unnecessary for 90+% of users.
     *  <p/>
     *  For some functionality, we cannot use {@link org.springframework.test.context.TestExecutionListener} because we need
     *  to both produce the desired outcome during application context loading, and also cleanup
     *  after ourselves even if the test class never executes.  Thus the listeners, which
     *  only run if the application context is successfully initialized are insufficient to
     *  provide the behavior described above.
     */
@Override
public ApplicationContext loadContext(String... locations) throws Exception {
    Class<?> testClass = getTestClass();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Loading ApplicationContext for locations [" + StringUtils.arrayToCommaDelimitedString(locations) + "].");
    }
    try {
        GenericApplicationContext context = createContext(testClass, null);
        loadBeanDefinitions(context, locations);
        return loadContext(context, testClass);
    } finally {
        cleanup(testClass);
    }
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext)

Example 47 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project spring-framework by spring-projects.

the class EvalTagTests method environmentAccess.

@Test
public void environmentAccess() throws Exception {
    Map<String, Object> map = new HashMap<>();
    map.put("key.foo", "value.foo");
    GenericApplicationContext wac = (GenericApplicationContext) context.getRequest().getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    wac.getEnvironment().getPropertySources().addFirst(new MapPropertySource("mapSource", map));
    wac.getDefaultListableBeanFactory().registerSingleton("bean2", context.getRequest().getAttribute("bean"));
    tag.setExpression("@environment['key.foo']");
    int action = tag.doStartTag();
    assertEquals(Tag.EVAL_BODY_INCLUDE, action);
    action = tag.doEndTag();
    assertEquals(Tag.EVAL_PAGE, action);
    assertEquals("value.foo", ((MockHttpServletResponse) context.getResponse()).getContentAsString());
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) HashMap(java.util.HashMap) MapPropertySource(org.springframework.core.env.MapPropertySource) Test(org.junit.Test)

Example 48 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project spring-framework by spring-projects.

the class EnvironmentSystemIntegrationTests method genericApplicationContext_standardEnv.

@Test
public void genericApplicationContext_standardEnv() {
    ConfigurableApplicationContext ctx = new GenericApplicationContext(newBeanFactoryWithEnvironmentAwareBean());
    ctx.refresh();
    assertHasStandardEnvironment(ctx);
    assertEnvironmentBeanRegistered(ctx);
    assertEnvironmentAwareInvoked(ctx, ctx.getEnvironment());
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Test(org.junit.Test)

Example 49 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project spring-framework by spring-projects.

the class EnvironmentSystemIntegrationTests method classPathBeanDefinitionScanner_inheritsEnvironmentFromEnvironmentCapableBDR_scanProfileAnnotatedComponents.

@Test
public void classPathBeanDefinitionScanner_inheritsEnvironmentFromEnvironmentCapableBDR_scanProfileAnnotatedComponents() {
    GenericApplicationContext ctx = new GenericApplicationContext();
    ctx.setEnvironment(prodEnv);
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(ctx);
    scanner.scan("org.springframework.core.env.scan2");
    ctx.refresh();
    assertThat(scanner.getEnvironment(), is((Environment) ctx.getEnvironment()));
    assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false));
    assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true));
}
Also used : ClassPathBeanDefinitionScanner(org.springframework.context.annotation.ClassPathBeanDefinitionScanner) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) StandardServletEnvironment(org.springframework.web.context.support.StandardServletEnvironment) MockEnvironment(org.springframework.mock.env.MockEnvironment) Test(org.junit.Test)

Example 50 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project camel by apache.

the class Configuration method shouldOnlyCollectRoutesOnce.

@Test
public void shouldOnlyCollectRoutesOnce() {
    GenericApplicationContext parent = new GenericApplicationContext();
    parent.refresh();
    ConfigurableApplicationContext context = new SpringApplicationBuilder(Configuration.class).web(false).parent(parent).run();
    ContextRefreshedEvent refreshEvent = new ContextRefreshedEvent(context);
    RoutesCollector collector = context.getBean(RoutesCollector.class);
    //no changes should happen here
    collector.onApplicationEvent(refreshEvent);
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) RoutesCollector(org.apache.camel.spring.boot.RoutesCollector) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) Test(org.junit.Test)

Aggregations

GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)245 Test (org.junit.Test)190 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)80 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)50 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)38 BeanCreationException (org.springframework.beans.factory.BeanCreationException)26 ClassPathResource (org.springframework.core.io.ClassPathResource)19 TestBean (org.springframework.tests.sample.beans.TestBean)17 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)15 DefaultAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator)12 MBeanExporter (org.springframework.jmx.export.MBeanExporter)11 BeansException (org.springframework.beans.BeansException)8 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)8 HashMap (java.util.HashMap)7 UnsatisfiedDependencyException (org.springframework.beans.factory.UnsatisfiedDependencyException)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 Before (org.junit.Before)6 Properties (java.util.Properties)5 EntityManager (javax.persistence.EntityManager)5 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)5