package com.jmh.foundation.webapp.action;

import org.springframework.test.context.support.AbstractContextLoader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.core.io.FileSystemResourceLoader;

import javax.servlet.ServletContext;
import java.util.Arrays;

/**
 * User: dusty
 * Date: Jan 16, 2009
 */
public class StrutsContextLoader extends AbstractContextLoader {

    public ApplicationContext loadContext(String... locations) throws Exception {
        MockServletContext servletContext = new MockServletContext("/src/main/webapp",new FileSystemResourceLoader());
        servletContext.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, arrayToString(locations, ","));
        return (new ContextLoader()).initWebApplicationContext(servletContext);
    }

    protected String getResourceSuffix() {
        return null;
    }

    public static String arrayToString(String[] a, String separator) {
        StringBuffer result = new StringBuffer();
        if (a.length > 0) {
            result.append(a[0]);
            for (int i = 1; i < a.length; i++) {
                result.append(separator);
                result.append(a[i]);
            }
        }
        return result.toString();
    }

}
