Search in sources :

Example 1 with FileSystemResourceLoader

use of org.springframework.core.io.FileSystemResourceLoader in project entando-core by entando.

the class ApsAdminBaseTestCase method setUp.

@Override
protected void setUp() throws Exception {
    boolean refresh = false;
    if (null == applicationContext) {
        // Link the servlet context and the Spring context
        servletContext = new MockServletContext("", new FileSystemResourceLoader());
        applicationContext = this.getConfigUtils().createApplicationContext(servletContext);
        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);
    } else {
        refresh = true;
    }
    RequestContext reqCtx = BaseTestCase.createRequestContext(applicationContext, servletContext);
    this.request = new MockHttpServletRequest();
    this.request.setAttribute(RequestContext.REQCTX, reqCtx);
    this.response = new MockHttpServletResponse();
    this.request.setSession(new MockHttpSession(servletContext));
    if (refresh) {
        try {
            ApsWebApplicationUtils.executeSystemRefresh(this.request);
            this.waitNotifyingThread();
        } catch (Throwable e) {
        }
    }
    // Use spring as the object factory for Struts
    StrutsSpringObjectFactory ssf = new StrutsSpringObjectFactory(null, null, null, null, servletContext, null, this.createContainer());
    ssf.setApplicationContext(applicationContext);
    // Dispatcher is the guy that actually handles all requests.  Pass in
    // an empty Map as the parameters but if you want to change stuff like
    // what config files to read, you need to specify them here
    // (see Dispatcher's source code)
    java.net.URL url = ClassLoader.getSystemResource("struts.properties");
    Properties props = new Properties();
    props.load(url.openStream());
    this.setInitParameters(props);
    Map params = new HashMap(props);
    this.dispatcher = new Dispatcher(servletContext, params);
    this.dispatcher.init();
    Dispatcher.setInstance(this.dispatcher);
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FileSystemResourceLoader(org.springframework.core.io.FileSystemResourceLoader) Properties(java.util.Properties) Dispatcher(org.apache.struts2.dispatcher.Dispatcher) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpSession(org.springframework.mock.web.MockHttpSession) StrutsSpringObjectFactory(org.apache.struts2.spring.StrutsSpringObjectFactory) RequestContext(com.agiletec.aps.system.RequestContext) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 2 with FileSystemResourceLoader

use of org.springframework.core.io.FileSystemResourceLoader in project entando-core by entando.

the class BaseTestCase method setUp.

@Override
protected void setUp() throws Exception {
    try {
        super.setUp();
        ServletContext srvCtx = new MockServletContext("", new FileSystemResourceLoader());
        ApplicationContext applicationContext = this.getConfigUtils().createApplicationContext(srvCtx);
        this.setApplicationContext(applicationContext);
        RequestContext reqCtx = createRequestContext(applicationContext, srvCtx);
        this.setRequestContext(reqCtx);
        this.setUserOnSession("guest");
    } catch (Exception e) {
        throw e;
    }
}
Also used : WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) FileSystemResourceLoader(org.springframework.core.io.FileSystemResourceLoader) ServletContext(javax.servlet.ServletContext) MockServletContext(org.springframework.mock.web.MockServletContext) RequestContext(com.agiletec.aps.system.RequestContext) MockServletContext(org.springframework.mock.web.MockServletContext)

Example 3 with FileSystemResourceLoader

use of org.springframework.core.io.FileSystemResourceLoader in project entando-core by entando.

the class UserProfileManagerTest method setUp.

@Before
public void setUp() throws Exception {
    MockServletContext servletContext = new MockServletContext("", new FileSystemResourceLoader());
    new ConfigTestUtils().createApplicationContext(servletContext);
    MockitoAnnotations.initMocks(this);
    this.userProfileManager.setEntityClassName(className);
    this.userProfileManager.setConfigItemName(configItemName);
    this.userProfileManager.setBeanName(this.beanName);
}
Also used : FileSystemResourceLoader(org.springframework.core.io.FileSystemResourceLoader) MockServletContext(org.springframework.mock.web.MockServletContext) ConfigTestUtils(com.agiletec.ConfigTestUtils) Before(org.junit.Before)

Example 4 with FileSystemResourceLoader

use of org.springframework.core.io.FileSystemResourceLoader in project spring-framework by spring-projects.

the class AbstractGenericWebContextLoader method configureWebResources.

/**
 * 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).
 * <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>
 * </ul>
 * @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();
    // set the current context as the root WebApplicationContext:
    if (!(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);
    }
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) FileSystemResourceLoader(org.springframework.core.io.FileSystemResourceLoader) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) FileSystemResourceLoader(org.springframework.core.io.FileSystemResourceLoader) ServletContext(jakarta.servlet.ServletContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockServletContext(org.springframework.mock.web.MockServletContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader)

Example 5 with FileSystemResourceLoader

use of org.springframework.core.io.FileSystemResourceLoader in project spring-framework by spring-projects.

the class WebMvcConfigurationSupportExtensionTests method setUp.

@BeforeEach
public void setUp() {
    this.context = new StaticWebApplicationContext();
    this.context.setServletContext(new MockServletContext(new FileSystemResourceLoader()));
    this.context.registerSingleton("controller", TestController.class);
    this.context.registerSingleton("userController", UserController.class);
    this.config = new TestWebMvcConfigurationSupport();
    this.config.setApplicationContext(this.context);
    this.config.setServletContext(this.context.getServletContext());
}
Also used : FileSystemResourceLoader(org.springframework.core.io.FileSystemResourceLoader) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

FileSystemResourceLoader (org.springframework.core.io.FileSystemResourceLoader)5 MockServletContext (org.springframework.mock.web.MockServletContext)4 RequestContext (com.agiletec.aps.system.RequestContext)2 ApplicationContext (org.springframework.context.ApplicationContext)2 WebApplicationContext (org.springframework.web.context.WebApplicationContext)2 ConfigTestUtils (com.agiletec.ConfigTestUtils)1 ServletContext (jakarta.servlet.ServletContext)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 ServletContext (javax.servlet.ServletContext)1 Dispatcher (org.apache.struts2.dispatcher.Dispatcher)1 StrutsSpringObjectFactory (org.apache.struts2.spring.StrutsSpringObjectFactory)1 Before (org.junit.Before)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 DefaultResourceLoader (org.springframework.core.io.DefaultResourceLoader)1 ResourceLoader (org.springframework.core.io.ResourceLoader)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1