Search in sources :

Example 86 with GenericWebApplicationContext

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

the class RequestMappingHandlerAdapterIntegrationTests method setup.

@BeforeEach
public void setup() throws Exception {
    ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
    bindingInitializer.setValidator(new StubValidator());
    List<HandlerMethodArgumentResolver> customResolvers = new ArrayList<>();
    customResolvers.add(new ServletWebArgumentResolverAdapter(new ColorArgumentResolver()));
    customResolvers.add(new CustomPrincipalArgumentResolver());
    GenericWebApplicationContext context = new GenericWebApplicationContext();
    context.refresh();
    handlerAdapter = new RequestMappingHandlerAdapter();
    handlerAdapter.setWebBindingInitializer(bindingInitializer);
    handlerAdapter.setCustomArgumentResolvers(customResolvers);
    handlerAdapter.setApplicationContext(context);
    handlerAdapter.setBeanFactory(context.getBeanFactory());
    handlerAdapter.afterPropertiesSet();
    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
    request.setMethod("POST");
    // Expose request to the current thread (for SpEL expressions)
    RequestContextHolder.setRequestAttributes(new ServletWebRequest(request));
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ArrayList(java.util.ArrayList) HandlerMethodArgumentResolver(org.springframework.web.method.support.HandlerMethodArgumentResolver) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 87 with GenericWebApplicationContext

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

the class ServletAnnotationControllerHandlerMethodTests method routerFunction.

@Test
void routerFunction() throws ServletException, IOException {
    GenericWebApplicationContext wac = new GenericWebApplicationContext();
    wac.registerBean(RouterFunction.class, () -> RouterFunctions.route().GET("/foo", request -> ServerResponse.ok().body("foo-body")).build());
    wac.refresh();
    DispatcherServlet servlet = new DispatcherServlet();
    servlet.setApplicationContext(wac);
    servlet.init(new MockServletConfig());
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
    MockHttpServletResponse response = new MockHttpServletResponse();
    servlet.service(request, response);
    assertThat(response.getStatus()).isEqualTo(200);
    assertThat(response.getContentAsString()).isEqualTo("foo-body");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 88 with GenericWebApplicationContext

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

the class HandlerMethodAnnotationDetectionTests method setUp.

private void setUp(Class<?> controllerType, boolean useAutoProxy) {
    GenericWebApplicationContext context = new GenericWebApplicationContext();
    context.registerBeanDefinition("controller", new RootBeanDefinition(controllerType));
    context.registerBeanDefinition("handlerMapping", new RootBeanDefinition(RequestMappingHandlerMapping.class));
    context.registerBeanDefinition("handlerAdapter", new RootBeanDefinition(RequestMappingHandlerAdapter.class));
    context.registerBeanDefinition("exceptionResolver", new RootBeanDefinition(ExceptionHandlerExceptionResolver.class));
    if (useAutoProxy) {
        DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        autoProxyCreator.setBeanFactory(context.getBeanFactory());
        context.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
        context.registerBeanDefinition("controllerAdvice", new RootBeanDefinition(ControllerAdvisor.class));
    }
    context.refresh();
    this.handlerMapping = context.getBean(RequestMappingHandlerMapping.class);
    this.handlerAdapter = context.getBean(RequestMappingHandlerAdapter.class);
    this.exceptionResolver = context.getBean(ExceptionHandlerExceptionResolver.class);
    context.close();
}
Also used : DefaultAdvisorAutoProxyCreator(org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext)

Example 89 with GenericWebApplicationContext

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

the class HandlerMappingIntrospectorTests method getMatchable.

@ParameterizedTest
@ValueSource(booleans = { true, false })
void getMatchable(boolean usePathPatterns) throws Exception {
    TestPathPatternParser parser = new TestPathPatternParser();
    GenericWebApplicationContext context = new GenericWebApplicationContext();
    context.registerBean("mapping", SimpleUrlHandlerMapping.class, () -> {
        SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
        if (usePathPatterns) {
            mapping.setPatternParser(parser);
        }
        mapping.setUrlMap(Collections.singletonMap("/path/*", new Object()));
        return mapping;
    });
    context.refresh();
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path/123");
    MatchableHandlerMapping mapping = initIntrospector(context).getMatchableHandlerMapping(request);
    assertThat(mapping).isNotNull();
    assertThat(request.getAttribute(BEST_MATCHING_PATTERN_ATTRIBUTE)).as("Attribute changes not ignored").isNull();
    assertThat(request.getAttribute(ServletRequestPathUtils.PATH_ATTRIBUTE)).as("Parsed path not cleaned").isNull();
    assertThat(mapping.match(request, "/p*/*")).isNotNull();
    assertThat(mapping.match(request, "/b*/*")).isNull();
    if (usePathPatterns) {
        assertThat(parser.getParsedPatterns()).containsExactly("/path/*", "/p*/*", "/b*/*");
    }
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 90 with GenericWebApplicationContext

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

the class HandlerMappingIntrospectorTests method detectHandlerMappingsOrdered.

@Test
void detectHandlerMappingsOrdered() {
    GenericWebApplicationContext context = new GenericWebApplicationContext();
    context.registerBean("B", SimpleUrlHandlerMapping.class, () -> {
        SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
        mapping.setOrder(2);
        return mapping;
    });
    context.registerBean("C", SimpleUrlHandlerMapping.class, () -> {
        SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
        mapping.setOrder(3);
        return mapping;
    });
    context.registerBean("A", SimpleUrlHandlerMapping.class, () -> {
        SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
        mapping.setOrder(1);
        return mapping;
    });
    context.refresh();
    List<?> expected = Arrays.asList(context.getBean("A"), context.getBean("B"), context.getBean("C"));
    List<HandlerMapping> actual = initIntrospector(context).getHandlerMappings();
    assertThat(actual).isEqualTo(expected);
}
Also used : HandlerMapping(org.springframework.web.servlet.HandlerMapping) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)93 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)33 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)32 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)31 Test (org.junit.Test)30 Test (org.junit.jupiter.api.Test)30 DispatcherServlet (org.springframework.web.servlet.DispatcherServlet)9 MockServletContext (org.springframework.mock.web.MockServletContext)8 BeforeEach (org.junit.jupiter.api.BeforeEach)6 DefaultAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator)6 ApplicationContext (org.springframework.context.ApplicationContext)6 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)6 WebApplicationContext (org.springframework.web.context.WebApplicationContext)6 Method (java.lang.reflect.Method)5 HttpSession (javax.servlet.http.HttpSession)5 SimpleTraceInterceptor (org.springframework.aop.interceptor.SimpleTraceInterceptor)5 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)5 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4