Example usage for org.springframework.mock.web MockServletContext MockServletContext

List of usage examples for org.springframework.mock.web MockServletContext MockServletContext

Introduction

In this page you can find the example usage for org.springframework.mock.web MockServletContext MockServletContext.

Prototype

public MockServletContext(@Nullable ResourceLoader resourceLoader) 

Source Link

Document

Create a new MockServletContext , using the specified ResourceLoader and no base path.

Usage

From source file:org.jamwiki.servlets.WikiPageInfoTest.java

/**
 *
 *//*from  w  w  w  .j  a va 2 s  .  co m*/
private MockHttpServletRequest getMockHttpServletRequest(String url) {
    MockServletContext mockContext = new MockServletContext("context");
    return new MockHttpServletRequest(mockContext, "GET", url);
}

From source file:org.parancoe.web.test.PluginTest.java

@Override
protected ConfigurableApplicationContext createApplicationContext(String[] locations) {
    FileSystemResourceLoader rl = new FileSystemResourceLoader();
    ServletContext servletContext = new MockServletContext(rl);
    XmlWebApplicationContext context = new XmlWebApplicationContext();
    context.setServletContext(servletContext);
    context.setConfigLocations(locations);
    context.refresh();/*from   w  ww. j a  va 2s  . c  o  m*/
    return context;
}

From source file:alpha.portal.webapp.listener.StartupListenerTest.java

@Override
protected void setUp() throws Exception {
    super.setUp();
    this.sc = new MockServletContext("");
    this.sc.addInitParameter(Constants.CSS_THEME, "simplicity");

    // initialize Spring
    this.sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "classpath:/applicationContext-dao.xml, "
            + "classpath:/applicationContext-service.xml, " + "classpath:/applicationContext-resources.xml");

    this.springListener = new ContextLoaderListener();
    this.springListener.contextInitialized(new ServletContextEvent(this.sc));
    this.listener = new StartupListener();
}

From source file:ar.com.zauber.commons.spring.beans.factory.impl.ContextPathRegexCaseBlockTest.java

/** test */
public final void testCtxDefault() {
    final MockServletContext servletCtx = new MockServletContext("/laralara");
    servletCtx.addInitParameter("contextConfigLocation", "classpath:/spring-test-switch-regex.xml");
    final TestFriendlyContextLoaderListener listener = new TestFriendlyContextLoaderListener();
    listener.contextInitialized(new ServletContextEvent(servletCtx));

    final WebApplicationContext ctx = ((TestFriendlyContextLoader) listener.getContextLoader()).getCtx();

    assertEquals("default", ctx.getBean("test1"));
}

From source file:org.parancoe.web.test.BaseTest.java

@Override
protected ConfigurableApplicationContext createApplicationContext(String[] locations) {
    FileSystemResourceLoader rl = new FileSystemResourceLoader();
    MockServletContext servletContext = new MockServletContext(rl);
    servletContext.setMinorVersion(4);/* w w w. ja v a  2 s . co  m*/
    servletContext.registerContext("/test", servletContext);
    servletContext.setServletContextName("/test");
    servletContext.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, arrayToString(locations));
    ContextLoader loader = new ContextLoader();
    WebApplicationContext context = loader.initWebApplicationContext(servletContext);
    return (ConfigurableApplicationContext) context;
}

From source file:org.grails.plugin.batch.Launcher.java

public void init(String[] args) {
    logDebug(true, "init(): begin");
    synchronized (initLock) {

        logDebug(true, "init(): this classLoader ", this.getClass().getClassLoader());
        logDebug(true, "init(): thread classLoader ", Thread.currentThread().getContextClassLoader());

        logDebug(true, "init(): env ", Environment.getCurrent());

        String resourcePath = getSystemProperty("resourcePath", null);
        if (resourcePath == null) {
            resourcePath = "war";
        }/*w  w w  .  j  a  va2  s  .c  o m*/

        logDebug(true, "init(): resourcePath ", resourcePath);

        servletContext = resourcePath != null ? new MockServletContext(resourcePath) : new MockServletContext();
        servletContext.setAttribute("args", args);

        servletContextListeners = new ServletContextListener[] { new Log4jConfigListener(),
                new GrailsContextLoaderListener() };

        this.shutdownHook = new Thread() {

            public void run() {
                logDebug(true, "shutdown hook run():");
                Launcher.this.destroy();
            }
        };

        this.destroyed = false;

        Runtime.getRuntime().addShutdownHook(this.shutdownHook);
        logDebug(true, "init(): shutdown hook added");

        try {
            ServletContextEvent event = new ServletContextEvent(servletContext);
            for (ServletContextListener l : servletContextListeners) {
                l.contextInitialized(event);
            }
        } catch (RuntimeException e) {
            log.error("init()", e);
            throw e;
        }

        logDebug("init(): thread classLoader ", Thread.currentThread().getContextClassLoader());

        GrailsApplication grailsApplication = ApplicationHolder.getApplication();
        DefaultGrailsMainClass main = getMainClass(grailsApplication);

        if (main != null) {
            final Object instance = main.getReferenceInstance();

            WebApplicationContext webContext = (WebApplicationContext) grailsApplication.getMainContext();
            webContext.getAutowireCapableBeanFactory().autowireBeanProperties(instance,
                    AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
            main.callInit();
        }

        logDebug("init(): end");
    }
}

From source file:org.jasig.springframework.web.portlet.context.PortletContextLoaderTests.java

@Test
public void testContextLoaderListenerWithDefaultContext() {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(PortletContextLoader.CONFIG_LOCATION_PARAM,
            "/org/springframework/web/context/WEB-INF/applicationContext.xml "
                    + "/org/springframework/web/context/WEB-INF/context-addition.xml");
    ServletContextListener listener = new PortletContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    listener.contextInitialized(event);/* www . jav  a  2  s.  c o m*/
    PortletContextLoader contextLoader = (PortletContextLoader) sc
            .getAttribute(PortletApplicationContextUtils2.ROOT_PORTLET_APPLICATION_CONTEXT_LOADER_ATTRIBUTE);
    assertNotNull(contextLoader);

    //initialize the portlet application context, needed because PortletContextLoaderListener.contextInitialized doesn't actually create
    //the portlet app context due to lack of PortletContext reference
    MockPortletContext pc = new MockPortletContext(sc);
    PortletApplicationContext context = PortletApplicationContextUtils2.getPortletApplicationContext(pc);

    assertTrue("Correct PortletApplicationContext exposed in PortletContext",
            context instanceof ContribXmlPortletApplicationContext);
    assertTrue(PortletContextLoader
            .getCurrentPortletApplicationContext() instanceof ContribXmlPortletApplicationContext);
    LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle");
    assertTrue("Has father", context.containsBean("father"));
    assertTrue("Has rod", context.containsBean("rod"));
    assertTrue("Has kerry", context.containsBean("kerry"));
    assertTrue("Not destroyed", !lb.isDestroyed());
    assertFalse(context.containsBean("beans1.bean1"));
    assertFalse(context.containsBean("beans1.bean2"));
    listener.contextDestroyed(event);
    assertTrue("Destroyed", lb.isDestroyed());
    assertNull(sc.getAttribute(PortletApplicationContext.ROOT_PORTLET_APPLICATION_CONTEXT_ATTRIBUTE));
    assertNull(PortletContextLoader.getCurrentPortletApplicationContext());
}

From source file:org.opennms.netmgt.ncs.rest.AbstractSpringJerseyRestTestCase.java

@Before
public void setUp() throws Throwable {
    beforeServletStart();//from   w w  w.j  a  v a2s .c  o  m

    setServletContext(new MockServletContext("file:src/main/webapp"));

    getServletContext().addInitParameter("contextConfigLocation",
            "file:src/main/resources/META-INF/opennms/component-service.xml");

    getServletContext().addInitParameter("parentContextKey", "testDaoContext");

    ServletContextEvent e = new ServletContextEvent(getServletContext());
    setContextListener(new ContextLoaderListener());
    getContextListener().contextInitialized(e);

    getServletContext().setContextPath(contextPath);
    setServletConfig(new MockServletConfig(getServletContext(), "dispatcher"));
    /*
    getServletConfig().addInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
    getServletConfig().addInitParameter("com.sun.jersey.config.property.packages", "org.opennms.netmgt.ncs.rest");
    */
    try {

        MockFilterConfig filterConfig = new MockFilterConfig(getServletContext(), "openSessionInViewFilter");
        setFilter(new OpenSessionInViewFilter());
        getFilter().init(filterConfig);

        setDispatcher(new SpringServlet());
        getDispatcher().init(getServletConfig());

    } catch (ServletException se) {
        throw se.getRootCause();
    }

    setWebAppContext(WebApplicationContextUtils.getWebApplicationContext(getServletContext()));
    afterServletStart();
    System.err.println("------------------------------------------------------------------------------");
}

From source file:org.openlmis.fulfillment.service.JasperReportsViewServiceTest.java

@Test
public void shouldGetRequestedViewAndSetDataSourceAndWebContextInJasperView() throws Exception {
    whenNew(JasperReportsMultiFormatView.class).withNoArguments().thenReturn(jasperReportsView);
    when(objectInputStream.readObject()).thenReturn(jasperReport);
    when(byteArrayOutputStream.toByteArray()).thenReturn(reportByteData);

    ServletContext servletContext = new MockServletContext("");
    HttpServletRequest httpServletRequest = new MockHttpServletRequest(servletContext);
    JasperReportsMultiFormatView reportView = viewFactory.getJasperReportsView(template, httpServletRequest);

    assertThat(reportView, is(jasperReportsView));
    verify(jasperReportsView).setJdbcDataSource(dataSource);
    verify(objectOutputStream).writeObject(jasperReport);
}