Search in sources :

Example 6 with ContextLoaderListener

use of org.springframework.web.context.ContextLoaderListener in project spring-framework by spring-projects.

the class Spr8510Tests method abstractRefreshableWAC_fallsBackToInitParam.

/**
	 * If setConfigLocation has not been called explicitly against the application context,
	 * then fall back to the ContextLoaderListener init-param if present.
	 */
@Test
public void abstractRefreshableWAC_fallsBackToInitParam() {
    XmlWebApplicationContext ctx = new XmlWebApplicationContext();
    //ctx.setConfigLocation("programmatic.xml"); // nothing set programmatically
    ContextLoaderListener cll = new ContextLoaderListener(ctx);
    MockServletContext sc = new MockServletContext();
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "from-init-param.xml");
    try {
        cll.contextInitialized(new ServletContextEvent(sc));
        fail("expected exception");
    } catch (Throwable t) {
        // assert that an attempt was made to load the correct XML
        assertTrue(t.getMessage().endsWith("Could not open ServletContext resource [/from-init-param.xml]"));
    }
}
Also used : ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Example 7 with ContextLoaderListener

use of org.springframework.web.context.ContextLoaderListener in project spring-framework by spring-projects.

the class Spr8510Tests method genericWAC.

/**
	 * Ensure that ContextLoaderListener and GenericWebApplicationContext interact nicely.
	 */
@Test
public void genericWAC() {
    GenericWebApplicationContext ctx = new GenericWebApplicationContext();
    ContextLoaderListener cll = new ContextLoaderListener(ctx);
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(ctx);
    scanner.scan("bogus.pkg");
    cll.contextInitialized(new ServletContextEvent(new MockServletContext()));
}
Also used : ClassPathBeanDefinitionScanner(org.springframework.context.annotation.ClassPathBeanDefinitionScanner) ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) ServletContextEvent(javax.servlet.ServletContextEvent) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 8 with ContextLoaderListener

use of org.springframework.web.context.ContextLoaderListener in project spring-framework by spring-projects.

the class Spr8510Tests method abstractRefreshableWAC_respectsInitParam_overProgrammaticConfigLocations.

/**
	 * If a contextConfigLocation init-param has been specified for the ContextLoaderListener,
	 * then it should take precedence. This is generally not a recommended practice, but
	 * when it does happen, the init-param should be considered more specific than the
	 * programmatic configuration, given that it still quite possibly externalized in
	 * hybrid web.xml + WebApplicationInitializer cases.
	 */
@Test
public void abstractRefreshableWAC_respectsInitParam_overProgrammaticConfigLocations() {
    XmlWebApplicationContext ctx = new XmlWebApplicationContext();
    ctx.setConfigLocation("programmatic.xml");
    ContextLoaderListener cll = new ContextLoaderListener(ctx);
    MockServletContext sc = new MockServletContext();
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "from-init-param.xml");
    try {
        cll.contextInitialized(new ServletContextEvent(sc));
        fail("expected exception");
    } catch (Throwable t) {
        // assert that an attempt was made to load the correct XML
        assertTrue(t.getMessage(), t.getMessage().endsWith("Could not open ServletContext resource [/from-init-param.xml]"));
    }
}
Also used : ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Example 9 with ContextLoaderListener

use of org.springframework.web.context.ContextLoaderListener in project spring-framework by spring-projects.

the class Spr8510Tests method customAbstractRefreshableWAC_fallsBackToInitParam.

/**
	 * Ensure that any custom default locations are still respected.
	 */
@Test
public void customAbstractRefreshableWAC_fallsBackToInitParam() {
    XmlWebApplicationContext ctx = new XmlWebApplicationContext() {

        @Override
        protected String[] getDefaultConfigLocations() {
            return new String[] { "/WEB-INF/custom.xml" };
        }
    };
    //ctx.setConfigLocation("programmatic.xml"); // nothing set programmatically
    ContextLoaderListener cll = new ContextLoaderListener(ctx);
    MockServletContext sc = new MockServletContext();
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "from-init-param.xml");
    try {
        cll.contextInitialized(new ServletContextEvent(sc));
        fail("expected exception");
    } catch (Throwable t) {
        // assert that an attempt was made to load the correct XML
        System.out.println(t.getMessage());
        assertTrue(t.getMessage().endsWith("Could not open ServletContext resource [/from-init-param.xml]"));
    }
}
Also used : ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Example 10 with ContextLoaderListener

use of org.springframework.web.context.ContextLoaderListener in project spring-framework by spring-projects.

the class Spr8510Tests method annotationConfigWAC.

/**
	 * Ensure that ContextLoaderListener and AnnotationConfigApplicationContext interact nicely.
	 */
@Test
public void annotationConfigWAC() {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.scan("does.not.matter");
    ContextLoaderListener cll = new ContextLoaderListener(ctx);
    cll.contextInitialized(new ServletContextEvent(new MockServletContext()));
}
Also used : ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) ServletContextEvent(javax.servlet.ServletContextEvent) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Aggregations

ContextLoaderListener (org.springframework.web.context.ContextLoaderListener)11 ServletContextEvent (javax.servlet.ServletContextEvent)10 Test (org.junit.Test)7 MockServletContext (org.springframework.mock.web.test.MockServletContext)7 ServletException (javax.servlet.ServletException)1 CoupleKey (org.jessma.util.CoupleKey)1 MockDatabase (org.opennms.core.test.db.MockDatabase)1 DaoTestConfigBean (org.opennms.test.DaoTestConfigBean)1 ClassPathBeanDefinitionScanner (org.springframework.context.annotation.ClassPathBeanDefinitionScanner)1 MockFilterConfig (org.springframework.mock.web.MockFilterConfig)1 MockServletConfig (org.springframework.mock.web.MockServletConfig)1 MockServletContext (org.springframework.mock.web.MockServletContext)1 OpenSessionInViewFilter (org.springframework.orm.hibernate3.support.OpenSessionInViewFilter)1 WebApplicationContext (org.springframework.web.context.WebApplicationContext)1 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)1