Search in sources :

Example 41 with StaticWebApplicationContext

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

the class ControllerTests method doTestServletForwardingController.

private void doTestServletForwardingController(ServletForwardingController sfc, boolean include) throws Exception {
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    ServletContext context = mock(ServletContext.class);
    RequestDispatcher dispatcher = mock(RequestDispatcher.class);
    given(request.getMethod()).willReturn("GET");
    given(context.getNamedDispatcher("action")).willReturn(dispatcher);
    if (include) {
        given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn("somePath");
    } else {
        given(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).willReturn(null);
    }
    StaticWebApplicationContext sac = new StaticWebApplicationContext();
    sac.setServletContext(context);
    sfc.setApplicationContext(sac);
    assertNull(sfc.handleRequest(request, response));
    if (include) {
        verify(dispatcher).include(request, response);
    } else {
        verify(dispatcher).forward(request, response);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletContext(javax.servlet.ServletContext) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 42 with StaticWebApplicationContext

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

the class ResponseEntityExceptionHandlerTests method controllerAdvice.

@Test
public void controllerAdvice() throws Exception {
    StaticWebApplicationContext cxt = new StaticWebApplicationContext();
    cxt.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
    cxt.refresh();
    ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver();
    resolver.setApplicationContext(cxt);
    resolver.afterPropertiesSet();
    ServletRequestBindingException ex = new ServletRequestBindingException("message");
    resolver.resolveException(this.servletRequest, this.servletResponse, null, ex);
    assertEquals(400, this.servletResponse.getStatus());
    assertEquals("error content", this.servletResponse.getContentAsString());
    assertEquals("someHeaderValue", this.servletResponse.getHeader("someHeader"));
}
Also used : ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.Test)

Example 43 with StaticWebApplicationContext

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

the class RequestMappingHandlerAdapterTests method modelAttributeAdviceInParentContext.

@Test
public void modelAttributeAdviceInParentContext() throws Exception {
    StaticWebApplicationContext parent = new StaticWebApplicationContext();
    parent.registerSingleton("maa", ModelAttributeAdvice.class);
    parent.refresh();
    this.webAppContext.setParent(parent);
    this.webAppContext.refresh();
    HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handle");
    this.handlerAdapter.afterPropertiesSet();
    ModelAndView mav = this.handlerAdapter.handle(this.request, this.response, handlerMethod);
    assertEquals("lAttr1", mav.getModel().get("attr1"));
    assertEquals("gAttr2", mav.getModel().get("attr2"));
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 44 with StaticWebApplicationContext

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

the class RequestMappingHandlerAdapterTests method setupOnce.

@BeforeClass
public static void setupOnce() {
    RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();
    adapter.setApplicationContext(new StaticWebApplicationContext());
    adapter.afterPropertiesSet();
    RESOLVER_COUNT = adapter.getArgumentResolvers().size();
    INIT_BINDER_RESOLVER_COUNT = adapter.getInitBinderArgumentResolvers().size();
    HANDLER_COUNT = adapter.getReturnValueHandlers().size();
}
Also used : StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) BeforeClass(org.junit.BeforeClass)

Example 45 with StaticWebApplicationContext

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

the class AbstractHtmlElementTagTests method getMockRequestDataValueProcessor.

@SuppressWarnings("deprecation")
protected RequestDataValueProcessor getMockRequestDataValueProcessor() {
    RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class);
    HttpServletRequest request = (HttpServletRequest) getPageContext().getRequest();
    StaticWebApplicationContext wac = (StaticWebApplicationContext) RequestContextUtils.findWebApplicationContext(request);
    wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor);
    return mockProcessor;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RequestDataValueProcessor(org.springframework.web.servlet.support.RequestDataValueProcessor) RequestDataValueProcessorWrapper(org.springframework.web.servlet.support.RequestDataValueProcessorWrapper) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext)

Aggregations

StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)75 Test (org.junit.Test)52 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)42 MockServletContext (org.springframework.mock.web.test.MockServletContext)38 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)30 Before (org.junit.Before)14 View (org.springframework.web.servlet.View)13 MockFilterConfig (org.springframework.mock.web.test.MockFilterConfig)12 ServletContext (javax.servlet.ServletContext)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 HttpServletResponse (javax.servlet.http.HttpServletResponse)10 TestBean (org.springframework.tests.sample.beans.TestBean)9 HashMap (java.util.HashMap)7 Map (java.util.Map)7 ServletRequest (javax.servlet.ServletRequest)6 ServletResponse (javax.servlet.ServletResponse)6 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)6 AcceptHeaderLocaleResolver (org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver)6 Locale (java.util.Locale)4 Properties (java.util.Properties)4