package edu.northwestern.bioinformatics.studycalendar.web;

import edu.northwestern.bioinformatics.studycalendar.core.StudyCalendarTestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;

/**
 * @author Rhett Sutphin
 */
public class StudyCalendarTestWebApplicationContextBuilder {
    public static XmlWebApplicationContext createWebApplicationContext(
        ServletContext servletContext
    ) {
        return createPscWebApplicationContext(servletContext, new String[] {
            "classpath:applicationContext-command.xml",
            "classpath:applicationContext-webflow.xml",
            "classpath:applicationContext-web-osgi.xml",
            "classpath:applicationContext-web-testing-osgi.xml",
        });
    }

    public static WebApplicationContext createRealWebApplicationContext(ServletContext servletContext) {
        return createPscWebApplicationContext(servletContext, new String[] {
            "classpath:applicationContext-command.xml",
            "classpath:applicationContext-webflow.xml",
            "classpath:applicationContext-web-osgi.xml"
        });
    }

    public static XmlWebApplicationContext createWebApplicationContextForServlet(
        String servletName, ServletContext servletContext
    ) {
        ApplicationContext parent = createWebApplicationContext(servletContext);
        XmlWebApplicationContext context = new XmlWebApplicationContext();
        context.setParent(parent);
        context.setServletContext(servletContext);
        context.setConfigLocation("WEB-INF/" + servletName + "-servlet.xml");
        context.refresh();
        return context;
    }

    private static XmlWebApplicationContext createPscWebApplicationContext(ServletContext servletContext, String[] configLocations) {
        ApplicationContext parent = StudyCalendarTestCase.getDeployedApplicationContext();
        XmlWebApplicationContext context = new XmlWebApplicationContext();
        context.setParent(parent);
        context.setServletContext(servletContext);
        context.setConfigLocations(configLocations);
        context.refresh();
        return context;
    }
}

@@ -124,24 +124,41 @@
 Calendar
-TestCase
+ApplicationContextBuilder
 ;%0Aimport
@@ -249,19 +249,8 @@
 ext.
-support.Xml
 WebA
@@ -299,32 +299,43 @@
 ork.web.context.
+support.Xml
 WebApplicationCo
@@ -1963,88 +1963,403 @@
     
-ApplicationContext parent = StudyCalendarTestCase.getDeployedApplication
+int coreCount = StudyCalendarApplicationContextBuilder.DEPLOYED_CONFIG_LOCATIONS.length;%0A        String%5B%5D loc = new String%5BcoreCount + configLocations.length%5D;%0A        System.arraycopy(%0A            StudyCalendarApplicationContextBuilder.DEPLOYED_CONFIG_LOCATIONS, 0,%0A            loc, 0, coreCount);%0A        System.arraycopy(%0A            configLocations, 0, loc, core
 Co
+u
 nt
-ext(
+, configLocations.length
 );%0A 
@@ -2436,43 +2436,8 @@
 ();%0A
-        context.setParent(parent);%0A
     
@@ -2518,31 +2518,19 @@
 cations(
-configLocations
+loc
 );%0A     

Modify test web application context to behave the same as deployed context creation. Related to #651.