package edu.northwestern.bioinformatics.studycalendar.web;

import junit.framework.TestCase;
import org.springframework.mock.web.MockServletContext;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.restlet.resource.Resource;
import edu.northwestern.bioinformatics.studycalendar.StudyCalendarError;
import edu.northwestern.bioinformatics.studycalendar.core.StudyCalendarTestCase;

/**
 * @author Rhett Sutphin
 */
public class DispatcherServletInitializationTest extends TestCase {
    private MockServletContext servletContext;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        servletContext = new MockServletContext("src/main/webapp", new FileSystemResourceLoader());
    }

    public void testSpringServletContext() throws Exception {
        assertDispatcherServletConfigLoads("spring");
    }

    public void testPublicServletContext() throws Exception {
        assertDispatcherServletConfigLoads("public");
    }

    public void testSetupServletContext() throws Exception {
        assertDispatcherServletConfigLoads("setup");
    }

    public void testRestfulApiServletContext() throws Exception {
        assertDispatcherServletConfigLoads("restful-api");
    }

    public void testAllResourcesArePrototypeScope() throws Exception {
        ApplicationContext ctxt = createWebApplicationContextForServlet("restful-api");

        String[] beanNames = ctxt.getBeanNamesForType(Resource.class);
        assertTrue("Should have found at least one resource", beanNames.length > 0);

        for (String beanName : beanNames) {
            Object firstTime = ctxt.getBean(beanName);
            Object secondTime = ctxt.getBean(beanName);
            assertNotSame(beanName + " is not a prototype", firstTime, secondTime);
        }
    }

    private void assertDispatcherServletConfigLoads(String servletName) throws Exception {
        try {
            createWebApplicationContextForServlet(servletName);
        } catch (Exception e) {
            throw new StudyCalendarError("Loading the configuration for MVC servlet '" + servletName
                + "' failed:  " + e.getMessage(), e);
        }
    }

    private XmlWebApplicationContext createWebApplicationContextForServlet(String servletName) {
        ApplicationContext parent = StudyCalendarTestCase.getDeployedApplicationContext();
        XmlWebApplicationContext context = new XmlWebApplicationContext();
        context.setParent(parent);
        context.setServletContext(servletContext);
        context.setConfigLocations(new String[] { "WEB-INF/" + servletName + "-servlet.xml" });
        context.refresh();
        return context;
    }
}

@@ -64,204 +64,406 @@
 ort 
-junit.framework.TestCase;%0Aimport org.springframework.mock.web.MockServletContext;%0Aimport org.springframework.core.io.FileSystemResourceLoader;%0Aimport org.springframework.context.ApplicationContext
+edu.northwestern.bioinformatics.studycalendar.StudyCalendarError;%0Aimport edu.northwestern.bioinformatics.studycalendar.core.StudyCalendarApplicationContextTestHelper;%0Aimport edu.northwestern.bioinformatics.studycalendar.core.StudyCalendarTestCase;%0Aimport static gov.nih.nci.cabig.ctms.testing.MoreJUnitAssertions.assertPositive;%0Aimport junit.framework.TestCase;%0Aimport org.restlet.resource.Resource
 ;%0Aim
@@ -491,20 +491,16 @@
 ork.
-web.
 context.
 supp
@@ -499,22 +499,8 @@
 ext.
-support.XmlWeb
 Appl
@@ -530,33 +530,50 @@
 org.
-restlet.resource.
+springframework.core.io.FileSystem
 Resource
 ;%0Aim
@@ -560,32 +560,38 @@
 leSystemResource
+Loader
 ;%0Aimport edu.nor
@@ -587,153 +587,128 @@
 ort 
-edu.northwestern.bioinformatics.studycalendar.StudyCalendarError;%0Aimport edu.northwestern.bioinformatics.studycalendar.core.StudyCalendarTestCase
+org.springframework.mock.web.MockServletContext;%0Aimport org.springframework.web.context.support.XmlWebApplicationContext
 ;%0A%0A/
@@ -1037,32 +1037,333 @@
 ader());%0A    %7D%0A%0A
+    public void testWebApplicationContextItself() throws Exception %7B%0A        ApplicationContext context = StudyCalendarApplicationContextTestHelper.getDeployedApplicationContext();%0A        // no exceptions%0A        assertPositive(%22No bean definitions loaded%22, context.getBeanDefinitionCount());%0A    %7D%0A%0A
     public void 

Add another application context load test here, since there are more contexts in the web module.