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(String resourceBasePath, @Nullable ResourceLoader resourceLoader) 

Source Link

Document

Create a new MockServletContext using the supplied resource base path and resource loader.

Usage

From source file:org.bonitasoft.web.designer.workspace.WorkspaceInitializerTest.java

@Before
public void initializeWorkspaceInitializer() {
    workspaceInitializer//from   w w w  . j a  va  2  s .  co  m
            .setServletContext(new MockServletContext(WAR_BASE_PATH, new FileSystemResourceLoader()));
    workspaceInitializer.setMigrations(Arrays.<LiveMigration>asList(pageLiveMigration, widgetLiveMigration));
}

From source file:springfox.documentation.spring.web.configuration.GenericWebContextLoader.java

private MockServletContext initServletContext(String warRootDir, ResourceLoader resourceLoader) {
    return new MockServletContext(warRootDir, resourceLoader) {
        // Required for DefaultServletHttpRequestHandler...
        public RequestDispatcher getNamedDispatcher(String path) {
            return path.equals("default") ? new MockRequestDispatcher(path) : super.getNamedDispatcher(path);
        }/*from w w w  .j a  v  a 2  s.  c  o m*/
    };
}

From source file:com.example.springsecurity.web.controllers.GenericWebContextLoader.java

private MockServletContext initServletContext(String warRootDir, ResourceLoader resourceLoader) {
    return new MockServletContext(warRootDir, resourceLoader) {
        // Required for DefaultServletHttpRequestHandler...
        public RequestDispatcher getNamedDispatcher(String path) {
            return (path.equals("default")) ? new MockRequestDispatcher(path) : super.getNamedDispatcher(path);
        }//from  ww w .  j  av  a2s.  co  m
    };
}

From source file:mx.edu.um.mateo.general.test.GenericWebXmlContextLoader.java

private MockServletContext initServletContext(String warRootDir, ResourceLoader resourceLoader) {
    return new MockServletContext(warRootDir, resourceLoader) {
        // Required for DefaultServletHttpRequestHandler...

        @Override//ww w.j av a 2 s.  com
        public RequestDispatcher getNamedDispatcher(String path) {
            return (path.equals("default")) ? new MockRequestDispatcher(path) : super.getNamedDispatcher(path);
        }
    };
}

From source file:org.jasig.portlet.calendar.util.MockWebApplicationContextLoader.java

public ApplicationContext loadContext(String... locations) throws Exception {
    // Establish the portlet context and config based on the test class's MockWebApplication annotation.
    MockServletContext mockServletContext = new MockServletContext(configuration.webapp(),
            new FileSystemResourceLoader());
    final ServletWrappingPortletContext portletContext = new ServletWrappingPortletContext(mockServletContext);
    final MockPortletConfig portletConfig = new MockPortletConfig(portletContext, configuration.name());

    // Create a WebApplicationContext and initialize it with the xml and portlet configuration.
    final XmlPortletApplicationContext portletApplicationContext = new XmlPortletApplicationContext();
    portletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
            portletApplicationContext);/* w  w w  . j  a  va 2 s.com*/
    portletApplicationContext.setPortletConfig(portletConfig);
    portletApplicationContext.setConfigLocations(locations);

    // Create a DispatcherPortlet that uses the previously established WebApplicationContext.
    final DispatcherPortlet dispatcherPortlet = new DispatcherPortlet() {
        @Override
        protected WebApplicationContext createPortletApplicationContext(ApplicationContext parent) {
            return portletApplicationContext;
        }
    };

    final ViewResolver viewResolver = new MockViewResolver();

    // Add the DispatcherPortlet (and anything else you want) to the context.
    // Note: this doesn't happen until refresh is called below.
    portletApplicationContext.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
            beanFactory.registerResolvableDependency(DispatcherPortlet.class, dispatcherPortlet);
            // Register any other beans here, including a ViewResolver if you are using JSPs.
            beanFactory.registerResolvableDependency(ViewResolver.class, viewResolver);
        }
    });

    // Have the context notify the portlet every time it is refreshed.
    portletApplicationContext.addApplicationListener(new SourceFilteringListener(portletApplicationContext,
            new ApplicationListener<ContextRefreshedEvent>() {
                public void onApplicationEvent(ContextRefreshedEvent event) {
                    dispatcherPortlet.onApplicationEvent(event);
                }
            }));

    // Prepare the context.
    portletApplicationContext.refresh();
    portletApplicationContext.registerShutdownHook();

    // Initialize the portlet.
    dispatcherPortlet.setContextConfigLocation("");
    dispatcherPortlet.init(portletConfig);

    return portletApplicationContext;
}

From source file:com.github.carlomicieli.nerdmovies.MockWebApplicationContextLoader.java

@Override
public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
    final MockServletContext servletContext = new MockServletContext(configuration.webapp(),
            new FileSystemResourceLoader());
    final MockServletConfig servletConfig = new MockServletConfig(servletContext, configuration.name());

    final AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webContext);
    webContext.getEnvironment().setActiveProfiles(mergedConfig.getActiveProfiles());
    webContext.setServletConfig(servletConfig);
    webContext.setConfigLocations(mergedConfig.getLocations());
    webContext.register(mergedConfig.getClasses());

    // Create a DispatcherServlet that uses the previously established
    // WebApplicationContext.
    @SuppressWarnings("serial")
    final DispatcherServlet dispatcherServlet = new DispatcherServlet() {
        @Override/*w  ww  .  j av a 2 s  . c  o m*/
        protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) {
            return webContext;
        }
    };

    // Add the DispatcherServlet (and anything else you want) to the
    // context.
    // Note: this doesn't happen until refresh is called below.
    webContext.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {
        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
            beanFactory.registerResolvableDependency(DispatcherServlet.class, dispatcherServlet);
            // Register any other beans here, including a ViewResolver if
            // you are using JSPs.
        }
    });

    // Have the context notify the servlet every time it is refreshed.
    webContext.addApplicationListener(
            new SourceFilteringListener(webContext, new ApplicationListener<ContextRefreshedEvent>() {
                @Override
                public void onApplicationEvent(ContextRefreshedEvent event) {
                    dispatcherServlet.onApplicationEvent(event);
                }
            }));

    // Prepare the context.
    webContext.refresh();
    webContext.registerShutdownHook();

    // Initialize the servlet.
    dispatcherServlet.setContextConfigLocation("");
    dispatcherServlet.init(servletConfig);

    return webContext;
}

From source file:de.fau.amos4.test.BaseWebApplicationContextTests.java

@Before
public void initDispatcherServlet() throws Exception {
    servlet = new DispatcherServlet() {
        private static final long serialVersionUID = 1L;

        @Override/*from   ww  w  .  j  ava 2  s  .c o  m*/
        protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent)
                throws BeansException {

            GenericWebApplicationContext gwac = new GenericWebApplicationContext();
            gwac.setParent(applicationContext);
            gwac.refresh();
            return gwac;
        }
    };

    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();

    MockServletContext servletContext = new MockServletContext("src/main/java/amos4/model",
            new FileSystemResourceLoader());

    servlet.init(new MockServletConfig(servletContext));
}

From source file:org.broadleafcommerce.test.BroadleafGenericGroovyXmlWebContextLoader.java

/**
 * Configures web resources for the supplied web application context (WAC).
 *
 * <h4>Implementation Details</h4>
 *
 * <p>If the supplied WAC has no parent or its parent is not a WAC, the
 * supplied WAC will be configured as the Root WAC (see "<em>Root WAC
 * Configuration</em>" below).//from  w w  w .j ava  2 s  .c o m
 *
 * <p>Otherwise the context hierarchy of the supplied WAC will be traversed
 * to find the top-most WAC (i.e., the root); and the {@link ServletContext}
 * of the Root WAC will be set as the {@code ServletContext} for the supplied
 * WAC.
 *
 * <h4>Root WAC Configuration</h4>
 *
 * <ul>
 * <li>The resource base path is retrieved from the supplied
 * {@code WebMergedContextConfiguration}.</li>
 * <li>A {@link ResourceLoader} is instantiated for the {@link MockServletContext}:
 * if the resource base path is prefixed with "{@code classpath:/}", a
 * {@link DefaultResourceLoader} will be used; otherwise, a
 * {@link FileSystemResourceLoader} will be used.</li>
 * <li>A {@code MockServletContext} will be created using the resource base
 * path and resource loader.</li>
 * <li>The supplied {@link MergeXmlWebApplicationContext} is then stored in
 * the {@code MockServletContext} under the
 * {@link MergeXmlWebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE} key.</li>
 * <li>Finally, the {@code MockServletContext} is set in the
 * {@code MergeXmlWebApplicationContext}.</li>
 *
 * @param context the merge xml web application context for which to configure the web
 * resources
 * @param webMergedConfig the merged context configuration to use to load the
 * merge xml web application context
 */
protected void configureWebResources(MergeXmlWebApplicationContext context,
        WebMergedContextConfiguration webMergedConfig) {

    ApplicationContext parent = context.getParent();

    // if the WAC has no parent or the parent is not a WAC, set the WAC as
    // the Root WAC:
    if (parent == null || (!(parent instanceof WebApplicationContext))) {
        String resourceBasePath = webMergedConfig.getResourceBasePath();
        ResourceLoader resourceLoader = resourceBasePath.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX)
                ? new DefaultResourceLoader()
                : new FileSystemResourceLoader();

        ServletContext servletContext = new MockServletContext(resourceBasePath, resourceLoader);
        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
        context.setServletContext(servletContext);
    } else {
        ServletContext servletContext = null;

        // find the Root WAC
        while (parent != null) {
            if (parent instanceof WebApplicationContext
                    && !(parent.getParent() instanceof WebApplicationContext)) {
                servletContext = ((WebApplicationContext) parent).getServletContext();
                break;
            }
            parent = parent.getParent();
        }
        Assert.state(servletContext != null,
                "Failed to find Root MergeXmlWebApplicationContext in the context hierarchy");
        context.setServletContext(servletContext);
    }
}

From source file:org.springframework.test.context.support.AbstractGenericWebContextLoader.java

/**
 * TODO [SPR-9864] Document configureWebResources().
 *//*w w  w .  j av  a2 s . c  om*/
protected void configureWebResources(GenericWebApplicationContext context,
        WebMergedContextConfiguration webMergedConfig) {

    String resourceBasePath = webMergedConfig.getResourceBasePath();
    ResourceLoader resourceLoader = resourceBasePath.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX)
            ? new DefaultResourceLoader()
            : new FileSystemResourceLoader();

    ServletContext servletContext = new MockServletContext(resourceBasePath, resourceLoader);
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
    context.setServletContext(servletContext);
}

From source file:org.springframework.test.context.web.AbstractGenericWebContextLoader.java

/**
 * Configures web resources for the supplied web application context (WAC).
 * <h4>Implementation Details</h4>
 * <p>If the supplied WAC has no parent or its parent is not a WAC, the
 * supplied WAC will be configured as the Root WAC (see "<em>Root WAC
 * Configuration</em>" below).//from w ww . ja  v a 2s  . co m
 * <p>Otherwise the context hierarchy of the supplied WAC will be traversed
 * to find the top-most WAC (i.e., the root); and the {@link ServletContext}
 * of the Root WAC will be set as the {@code ServletContext} for the supplied
 * WAC.
 * <h4>Root WAC Configuration</h4>
 * <ul>
 * <li>The resource base path is retrieved from the supplied
 * {@code WebMergedContextConfiguration}.</li>
 * <li>A {@link ResourceLoader} is instantiated for the {@link MockServletContext}:
 * if the resource base path is prefixed with "{@code classpath:}", a
 * {@link DefaultResourceLoader} will be used; otherwise, a
 * {@link FileSystemResourceLoader} will be used.</li>
 * <li>A {@code MockServletContext} will be created using the resource base
 * path and resource loader.</li>
 * <li>The supplied {@link GenericWebApplicationContext} is then stored in
 * the {@code MockServletContext} under the
 * {@link WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE} key.</li>
 * <li>Finally, the {@code MockServletContext} is set in the
 * {@code WebApplicationContext}.</li>
 * @param context the web application context for which to configure the web resources
 * @param webMergedConfig the merged context configuration to use to load the web application context
 */
protected void configureWebResources(GenericWebApplicationContext context,
        WebMergedContextConfiguration webMergedConfig) {

    ApplicationContext parent = context.getParent();

    // If the WebApplicationContext has no parent or the parent is not a WebApplicationContext,
    // set the current context as the root WebApplicationContext:
    if (parent == null || (!(parent instanceof WebApplicationContext))) {
        String resourceBasePath = webMergedConfig.getResourceBasePath();
        ResourceLoader resourceLoader = (resourceBasePath.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX)
                ? new DefaultResourceLoader()
                : new FileSystemResourceLoader());
        ServletContext servletContext = new MockServletContext(resourceBasePath, resourceLoader);
        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
        context.setServletContext(servletContext);
    } else {
        ServletContext servletContext = null;
        // Find the root WebApplicationContext
        while (parent != null) {
            if (parent instanceof WebApplicationContext
                    && !(parent.getParent() instanceof WebApplicationContext)) {
                servletContext = ((WebApplicationContext) parent).getServletContext();
                break;
            }
            parent = parent.getParent();
        }
        Assert.state(servletContext != null,
                "Failed to find root WebApplicationContext in the context hierarchy");
        context.setServletContext(servletContext);
    }
}