Search in sources :

Example 6 with ConfigurableWebApplicationContext

use of org.springframework.web.context.ConfigurableWebApplicationContext in project Gemma by PavlidisLab.

the class WebContextLoader method loadContext.

@Override
public ApplicationContext loadContext(String... locations) {
    if (WebContextLoader.logger.isDebugEnabled()) {
        WebContextLoader.logger.debug("Loading WebApplicationContext for locations [" + StringUtils.arrayToCommaDelimitedString(locations) + "].");
    }
    ConfigurableWebApplicationContext context = new XmlWebApplicationContext();
    context.setConfigLocations(locations);
    context.setServletContext(new MockServletContext(""));
    context.refresh();
    AnnotationConfigUtils.registerAnnotationConfigProcessors((BeanDefinitionRegistry) context.getBeanFactory());
    context.registerShutdownHook();
    return context;
}
Also used : ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext)

Example 7 with ConfigurableWebApplicationContext

use of org.springframework.web.context.ConfigurableWebApplicationContext in project spring-security by spring-projects.

the class InterceptUrlConfigTests method requestWhenUsingMvcMatchersAndServletPathThenAuthorizesRequestsAccordingly.

@Test
public void requestWhenUsingMvcMatchersAndServletPathThenAuthorizesRequestsAccordingly() throws Exception {
    this.spring.configLocations(this.xml("MvcMatchersServletPath")).autowire();
    MockServletContext servletContext = mockServletContext("/spring");
    ConfigurableWebApplicationContext context = this.spring.getContext();
    context.setServletContext(servletContext);
    // @formatter:off
    this.mvc.perform(get("/spring/path").servletPath("/spring")).andExpect(status().isUnauthorized());
    this.mvc.perform(get("/spring/path.html").servletPath("/spring")).andExpect(status().isUnauthorized());
    this.mvc.perform(get("/spring/path/").servletPath("/spring")).andExpect(status().isUnauthorized());
// @formatter:on
}
Also used : ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 8 with ConfigurableWebApplicationContext

use of org.springframework.web.context.ConfigurableWebApplicationContext in project spring-security by spring-projects.

the class ServletApiConfigurerTests method logoutServletApiWhenCsrfDisabled.

@Test
public void logoutServletApiWhenCsrfDisabled() throws Exception {
    ConfigurableWebApplicationContext context = this.spring.register(CsrfDisabledConfig.class).getContext();
    MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
    MvcResult mvcResult = mockMvc.perform(get("/")).andReturn();
    assertThat(mvcResult.getRequest().getSession(false)).isNull();
}
Also used : ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) MvcResult(org.springframework.test.web.servlet.MvcResult) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.jupiter.api.Test)

Example 9 with ConfigurableWebApplicationContext

use of org.springframework.web.context.ConfigurableWebApplicationContext in project spring-framework by spring-projects.

the class PathMatchingUrlHandlerMappingTests method initConfig.

private static WebApplicationContext initConfig(String... configLocations) {
    MockServletContext sc = new MockServletContext("");
    ConfigurableWebApplicationContext context = new XmlWebApplicationContext();
    context.setServletContext(sc);
    context.setConfigLocations(configLocations);
    context.refresh();
    return context;
}
Also used : ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext)

Example 10 with ConfigurableWebApplicationContext

use of org.springframework.web.context.ConfigurableWebApplicationContext in project spring-boot by spring-projects.

the class WebApplicationContextRunner method withMockServletContext.

/**
 * Decorate the specified {@code contextFactory} to set a {@link MockServletContext}
 * on each newly created {@link WebApplicationContext}.
 * @param contextFactory the context factory to decorate
 * @return an updated supplier that will set the {@link MockServletContext}
 */
public static Supplier<ConfigurableWebApplicationContext> withMockServletContext(Supplier<ConfigurableWebApplicationContext> contextFactory) {
    return (contextFactory != null) ? () -> {
        ConfigurableWebApplicationContext context = contextFactory.get();
        context.setServletContext(new MockServletContext());
        return context;
    } : null;
}
Also used : ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext)

Aggregations

ConfigurableWebApplicationContext (org.springframework.web.context.ConfigurableWebApplicationContext)20 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)7 Test (org.junit.jupiter.api.Test)6 File (java.io.File)4 FileSystemResource (org.springframework.core.io.FileSystemResource)4 CubaXmlWebApplicationContext (com.haulmont.cuba.core.sys.CubaXmlWebApplicationContext)3 List (java.util.List)3 ContextMappings (org.springframework.boot.actuate.web.mappings.MappingsEndpoint.ContextMappings)3 ReactiveWebApplicationContextRunner (org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner)3 WebApplicationContextRunner (org.springframework.boot.test.context.runner.WebApplicationContextRunner)3 MockServletContext (org.springframework.mock.web.MockServletContext)3 WebApplicationContext (org.springframework.web.context.WebApplicationContext)3 SingleAppResourcePatternResolver (com.haulmont.cuba.core.sys.SingleAppResourcePatternResolver)2 DispatcherServletMappingDescription (org.springframework.boot.actuate.web.mappings.servlet.DispatcherServletMappingDescription)2 FilterRegistrationMappingDescription (org.springframework.boot.actuate.web.mappings.servlet.FilterRegistrationMappingDescription)2 ServletRegistrationMappingDescription (org.springframework.boot.actuate.web.mappings.servlet.ServletRegistrationMappingDescription)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 PageContext (jakarta.servlet.jsp.PageContext)1