Search in sources :

Example 66 with StaticWebApplicationContext

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

the class StaticResourceRequestTests method assertMatcher.

private RequestMatcherAssert assertMatcher(ServerWebExchangeMatcher matcher) {
    StaticWebApplicationContext context = new StaticWebApplicationContext();
    context.registerBean(ServerProperties.class);
    return assertThat(new RequestMatcherAssert(context, matcher));
}
Also used : StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext)

Example 67 with StaticWebApplicationContext

use of org.springframework.web.context.support.StaticWebApplicationContext in project brave by openzipkin.

the class ITSpanCustomizingHandlerInterceptor method init.

@Override
public void init(ServletContextHandler handler) {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.getBeanFactory().registerSingleton("httpTracing", httpTracing);
    wac.getBeanFactory().registerSingleton("testController", // the test resource
    new TestController(httpTracing));
    DefaultAnnotationHandlerMapping mapping = new DefaultAnnotationHandlerMapping();
    mapping.setInterceptors(new Object[] { new SpanCustomizingHandlerInterceptor() });
    mapping.setApplicationContext(wac);
    wac.getBeanFactory().registerSingleton(HANDLER_MAPPING_BEAN_NAME, mapping);
    wac.getBeanFactory().registerSingleton(HANDLER_ADAPTER_BEAN_NAME, new AnnotationMethodHandlerAdapter());
    handler.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    handler.addServlet(new ServletHolder(new DispatcherServlet() {

        {
            wac.refresh();
            setDetectAllHandlerMappings(false);
            setDetectAllHandlerAdapters(false);
            setPublishEvents(false);
        }

        @Override
        protected WebApplicationContext initWebApplicationContext() throws BeansException {
            onRefresh(wac);
            return wac;
        }
    }), "/*");
    handler.addFilter(DelegatingTracingFilter.class, "/*", EnumSet.allOf(DispatcherType.class));
}
Also used : DefaultAnnotationHandlerMapping(org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) DispatcherType(org.eclipse.jetty.server.DispatcherType) AnnotationMethodHandlerAdapter(org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter)

Example 68 with StaticWebApplicationContext

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

the class EnvironmentSystemIntegrationTests method registerServletParamPropertySources_StaticWebApplicationContext.

@Test
void registerServletParamPropertySources_StaticWebApplicationContext() {
    MockServletContext servletContext = new MockServletContext();
    servletContext.addInitParameter("pCommon", "pCommonContextValue");
    servletContext.addInitParameter("pContext1", "pContext1Value");
    MockServletConfig servletConfig = new MockServletConfig(servletContext);
    servletConfig.addInitParameter("pCommon", "pCommonConfigValue");
    servletConfig.addInitParameter("pConfig1", "pConfig1Value");
    StaticWebApplicationContext ctx = new StaticWebApplicationContext();
    ctx.setServletConfig(servletConfig);
    ctx.refresh();
    ConfigurableEnvironment environment = ctx.getEnvironment();
    MutablePropertySources propertySources = environment.getPropertySources();
    assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)).isTrue();
    assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)).isTrue();
    // ServletConfig gets precedence
    assertThat(environment.getProperty("pCommon")).isEqualTo("pCommonConfigValue");
    assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME))).isLessThan(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)));
    // but all params are available
    assertThat(environment.getProperty("pContext1")).isEqualTo("pContext1Value");
    assertThat(environment.getProperty("pConfig1")).isEqualTo("pConfig1Value");
    // Servlet* PropertySources have precedence over System* PropertySources
    assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME))).isLessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)));
    // Replace system properties with a mock property source for convenience
    MockPropertySource mockSystemProperties = new MockPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME);
    mockSystemProperties.setProperty("pCommon", "pCommonSysPropsValue");
    mockSystemProperties.setProperty("pSysProps1", "pSysProps1Value");
    propertySources.replace(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties);
    // assert that servletconfig params resolve with higher precedence than sysprops
    assertThat(environment.getProperty("pCommon")).isEqualTo("pCommonConfigValue");
    assertThat(environment.getProperty("pSysProps1")).isEqualTo("pSysProps1Value");
}
Also used : MockServletConfig(org.springframework.mock.web.MockServletConfig) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockPropertySource(org.springframework.mock.env.MockPropertySource) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 69 with StaticWebApplicationContext

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

the class EnvironmentSystemIntegrationTests method staticWebApplicationContext.

@Test
void staticWebApplicationContext() {
    StaticWebApplicationContext ctx = new StaticWebApplicationContext();
    assertHasStandardServletEnvironment(ctx);
    registerEnvironmentBeanDefinition(ctx);
    ctx.setEnvironment(prodWebEnv);
    ctx.refresh();
    assertHasEnvironment(ctx, prodWebEnv);
    assertEnvironmentBeanRegistered(ctx);
    assertEnvironmentAwareInvoked(ctx, prodWebEnv);
}
Also used : StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 70 with StaticWebApplicationContext

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

the class RequestMappingInfoHandlerMappingTests method pathPatternsArguments.

@SuppressWarnings("unused")
static Stream<?> pathPatternsArguments() {
    TestController controller = new TestController();
    TestRequestMappingInfoHandlerMapping mapping1 = new TestRequestMappingInfoHandlerMapping();
    mapping1.setPatternParser(new PathPatternParser());
    TestRequestMappingInfoHandlerMapping mapping2 = new TestRequestMappingInfoHandlerMapping();
    mapping2.setRemoveSemicolonContent(false);
    return Stream.of(mapping1, mapping2).peek(mapping -> {
        mapping.setApplicationContext(new StaticWebApplicationContext());
        mapping.registerHandler(controller);
        mapping.afterPropertiesSet();
    });
}
Also used : PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext)

Aggregations

StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)101 Test (org.junit.jupiter.api.Test)54 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)29 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)21 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)20 MockServletContext (org.springframework.mock.web.test.MockServletContext)16 Test (org.junit.Test)15 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)14 ServletContext (jakarta.servlet.ServletContext)11 View (org.springframework.web.servlet.View)11 BeforeEach (org.junit.jupiter.api.BeforeEach)10 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)10 MockFilterConfig (org.springframework.web.testfixture.servlet.MockFilterConfig)10 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 Before (org.junit.Before)8 WebApplicationContext (org.springframework.web.context.WebApplicationContext)8 HashMap (java.util.HashMap)6 Map (java.util.Map)6 AcceptHeaderLocaleResolver (org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver)6 Properties (java.util.Properties)5