org.springside.modules.test.spring.SpringWebTestHelper.java Source code

Java tutorial

Introduction

Here is the source code for org.springside.modules.test.spring.SpringWebTestHelper.java

Source

/**
 * Copyright (c) 2005-2012 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.springside.modules.test.spring;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;

/**
 * Spring WebApplicationContext?ServletContext?.
 * 
 * @author calvin
 */
public class SpringWebTestHelper {

    private SpringWebTestHelper() {
    }

    /**
     * ServletContext?Spring WebApplicationContext.
     * 
     * @param configLocations application context.
     */
    public static void initWebApplicationContext(MockServletContext servletContext, String... configLocations) {
        String configLocationsString = StringUtils.join(configLocations, ",");
        servletContext.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, configLocationsString);
        new ContextLoader().initWebApplicationContext(servletContext);
    }

    /**
     * ServletContext?Spring WebApplicationContext.
     * 
     * @param applicationContext ApplicationContext.
     */
    public static void initWebApplicationContext(MockServletContext servletContext,
            ApplicationContext applicationContext) {
        ConfigurableWebApplicationContext wac = new XmlWebApplicationContext();
        wac.setParent(applicationContext);
        wac.setServletContext(servletContext);
        wac.setConfigLocation("");
        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
        wac.refresh();
    }

    /**
     * ServletContextSpring WebApplicationContext.
     */
    public static void closeWebApplicationContext(MockServletContext servletContext) {
        new ContextLoader().closeWebApplicationContext(servletContext);
    }
}