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:grails.util.GrailsUtil.java

private static ApplicationContext createGrailsApplicationContext(ApplicationContext parent,
        GrailsApplication application) {
    GrailsRuntimeConfigurator config = new GrailsRuntimeConfigurator(application, parent);
    MockServletContext servletContext = new MockServletContext(new MockResourceLoader());
    ConfigurableApplicationContext appCtx = (ConfigurableApplicationContext) config.configure(servletContext);
    servletContext.setAttribute(ApplicationAttributes.APPLICATION_CONTEXT, appCtx);
    Assert.notNull(appCtx);// w  w w .jav  a2s . c o m
    return appCtx;
}

From source file:com.github.jrialland.ajpclient.AbstractTomcatTest.java

/**
 * binds a servlet on the tomcat instance. the 'mapping' has the same format
 * that the 'servlet-mapping' parameter in web.xml. the path of the servlet
 * will be relative to ///from   w  w w  . j a va 2  s.co  m
 *
 * {@link Servlet#init(javax.servlet.ServletConfig)} is called with a mock
 * object as parameter.
 *
 * @param mapping
 * @param servlet
 */
protected void addServlet(final String mapping, final Servlet servlet) {
    final MockServletContext servletContext = new MockServletContext("/");
    final MockServletConfig config = new MockServletConfig(servletContext, servlet.toString());
    try {
        servlet.init(config);
    } catch (final Exception e) {
        throw new IllegalStateException(e);
    }
    servlets.put(mapping, servlet);
}

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

@Test
public void testContextLoaderListenerWithRegisteredContextInitializer() {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(PortletContextLoader.CONFIG_LOCATION_PARAM,
            "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
    sc.addInitParameter(PortletContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM,
            StringUtils.arrayToCommaDelimitedString(new Object[] { TestContextInitializer.class.getName(),
                    TestWebContextInitializer.class.getName() }));
    PortletContextLoaderListener listener = new PortletContextLoaderListener();
    listener.contextInitialized(new ServletContextEvent(sc));

    //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);
    PortletApplicationContextUtils2.getPortletApplicationContext(pc);

    PortletApplicationContext pac = PortletContextLoader.getCurrentPortletApplicationContext();
    TestBean testBean = pac.getBean(TestBean.class);
    assertThat(testBean.getName(), equalTo("testName"));
    //        assertThat(pac.getServletContext().getAttribute("initialized"), notNullValue());
    assertThat(pac.getPortletContext().getAttribute("initialized"), notNullValue());
}

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

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

    Map<JRExporterParameter, Object> exportParams = new HashMap<>();
    exportParams.put(IS_USING_IMAGES_TO_ALIGN, false);
    ServletContext servletContext = new MockServletContext("");
    HttpServletRequest httpServletRequest = new MockHttpServletRequest(servletContext);
    viewFactory.getJasperReportsView(template, httpServletRequest);

    verify(jasperReportsView).setExporterParameters(exportParams);
}

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

@Test
public void testRegisteredContextInitializerCanAccessServletContextParamsViaEnvironment() {
    MockServletContext sc = new MockServletContext("");
    // config file doesn't matter.  just a placeholder
    sc.addInitParameter(PortletContextLoader.CONFIG_LOCATION_PARAM,
            "/org/springframework/web/context/WEB-INF/empty-context.xml");

    sc.addInitParameter("someProperty", "someValue");
    sc.addInitParameter(PortletContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM,
            EnvApplicationContextInitializer.class.getName());
    PortletContextLoaderListener listener = new PortletContextLoaderListener();
    listener.contextInitialized(new ServletContextEvent(sc));

    //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);
    PortletApplicationContextUtils2.getPortletApplicationContext(pc);
}

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

@Test
public void testContextLoaderListenerWithUnkownContextInitializer() {
    MockServletContext sc = new MockServletContext("");
    // config file doesn't matter.  just a placeholder
    sc.addInitParameter(PortletContextLoader.CONFIG_LOCATION_PARAM,
            "/org/springframework/web/context/WEB-INF/empty-context.xml");
    sc.addInitParameter(PortletContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, StringUtils
            .arrayToCommaDelimitedString(new Object[] { UnknownContextInitializer.class.getName() }));
    PortletContextLoaderListener listener = new PortletContextLoaderListener();
    listener.contextInitialized(new ServletContextEvent(sc));

    try {/*from   w w  w . j  av  a2s  . c  o m*/
        //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);
        PortletApplicationContextUtils2.getPortletApplicationContext(pc);

        fail("expected exception");
    } catch (IllegalArgumentException ex) {
        assertTrue(ex.getMessage().contains("not assignable"));
    }
}

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

@Test
public void testContextLoaderWithCustomContextAndParent() throws Exception {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM, StaticWebApplicationContext.class.getName());
    sc.addInitParameter(PortletContextLoader.CONTEXT_CLASS_PARAM,
            SimplePortletApplicationContext.class.getName());
    ContextLoaderListener servletListener = new ContextLoaderListener();
    ServletContextListener listener = new PortletContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);

    servletListener.contextInitialized(event);
    listener.contextInitialized(event);/* w ww .  j av  a 2s .co  m*/

    //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);
    PortletApplicationContextUtils2.getPortletApplicationContext(pc);

    PortletApplicationContext wc = (PortletApplicationContext) pc
            .getAttribute(PortletApplicationContext.ROOT_PORTLET_APPLICATION_CONTEXT_ATTRIBUTE);
    assertTrue("Correct PortletApplicationContext exposed in PortletContext",
            wc instanceof SimplePortletApplicationContext);
}

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

@Test
public void testContextLoaderWithInvalidLocation() throws Exception {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(PortletContextLoader.CONFIG_LOCATION_PARAM, "/WEB-INF/myContext.xml");
    ServletContextListener listener = new PortletContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    listener.contextInitialized(event);/*from ww  w  .ja  va  2  s  .c om*/
    try {
        //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);
        PortletApplicationContextUtils2.getPortletApplicationContext(pc);

        fail("Should have thrown BeanDefinitionStoreException");
    } catch (BeanDefinitionStoreException ex) {
        // expected
        assertTrue(ex.getCause() instanceof FileNotFoundException);
    }
}

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

@Test
public void testContextLoaderWithInvalidContext() throws Exception {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(PortletContextLoader.CONTEXT_CLASS_PARAM,
            "org.springframework.web.context.support.InvalidWebApplicationContext");
    ServletContextListener listener = new PortletContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    listener.contextInitialized(event);//  w  w w . j a  va 2  s  .c  om
    try {
        //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);
        PortletApplicationContextUtils2.getPortletApplicationContext(pc);
        fail("Should have thrown ApplicationContextException");
    } catch (ApplicationContextException ex) {
        // expected
        assertTrue(ex.getCause() instanceof ClassNotFoundException);
    }
}

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

@Test
public void testContextLoaderWithDefaultLocation() throws Exception {
    MockServletContext sc = new MockServletContext("");
    ServletContextListener listener = new PortletContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    listener.contextInitialized(event);// ww  w.j av  a  2s. c o  m
    try {
        //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);
        PortletApplicationContextUtils2.getPortletApplicationContext(pc);
        fail("Should have thrown BeanDefinitionStoreException");
    } catch (BeanDefinitionStoreException ex) {
        // expected
        assertTrue(ex.getCause() instanceof IOException);
        assertTrue(ex.getCause().getMessage().contains("/WEB-INF/portletApplicationContext.xml"));
    }
}