private void registerMvcSingletons(StubWebApplicationContext cxt) { StandaloneConfiguration configuration = new StandaloneConfiguration(); RequestMappingHandlerMapping handlerMapping = configuration.requestMappingHandlerMapping(); handlerMapping.setServletContext(cxt.getServletContext()); handlerMapping.setApplicationContext(cxt); cxt.addBean("requestMappingHandlerMapping", handlerMapping); RequestMappingHandlerAdapter handlerAdapter = configuration.requestMappingHandlerAdapter(); handlerAdapter.setServletContext(cxt.getServletContext()); handlerAdapter.setApplicationContext(cxt); handlerAdapter.afterPropertiesSet(); cxt.addBean("requestMappingHandlerAdapter", handlerAdapter); cxt.addBean("handlerExceptionResolver", configuration.handlerExceptionResolver()); cxt.addBeans(initViewResolvers(cxt)); cxt.addBean(DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME, this.localeResolver); cxt.addBean(DispatcherServlet.THEME_RESOLVER_BEAN_NAME, new FixedThemeResolver()); cxt.addBean(DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME, new DefaultRequestToViewNameTranslator()); this.flashMapManager = new SessionFlashMapManager(); cxt.addBean(DispatcherServlet.FLASH_MAP_MANAGER_BEAN_NAME, this.flashMapManager); }
@Before public void setUp() { this.request = new MockHttpServletRequest(); this.response = new MockHttpServletResponse(); this.request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap()); this.request.setAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE, new SessionFlashMapManager()); }
@Test public void http11() throws Exception { RedirectView rv = new RedirectView(); rv.setUrl("http://url.somewhere.com"); rv.setHttp10Compatible(false); MockHttpServletRequest request = createRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap()); request.setAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE, new SessionFlashMapManager()); rv.render(new HashMap<String, Object>(), request, response); assertEquals(303, response.getStatus()); assertEquals("http://url.somewhere.com", response.getHeader("Location")); }
private void registerMvcSingletons(StubWebApplicationContext wac) { StandaloneConfiguration config = new StandaloneConfiguration(); config.setApplicationContext(wac); wac.addBeans(this.controllerAdvice); StaticRequestMappingHandlerMapping hm = config.getHandlerMapping(); hm.setServletContext(wac.getServletContext()); hm.setApplicationContext(wac); hm.afterPropertiesSet(); hm.registerHandlers(this.controllers); wac.addBean("requestMappingHandlerMapping", hm); RequestMappingHandlerAdapter handlerAdapter = config.requestMappingHandlerAdapter(); handlerAdapter.setServletContext(wac.getServletContext()); handlerAdapter.setApplicationContext(wac); handlerAdapter.afterPropertiesSet(); wac.addBean("requestMappingHandlerAdapter", handlerAdapter); wac.addBean("handlerExceptionResolver", config.handlerExceptionResolver()); wac.addBeans(initViewResolvers(wac)); wac.addBean(DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME, this.localeResolver); wac.addBean(DispatcherServlet.THEME_RESOLVER_BEAN_NAME, new FixedThemeResolver()); wac.addBean(DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME, new DefaultRequestToViewNameTranslator()); this.flashMapManager = new SessionFlashMapManager(); wac.addBean(DispatcherServlet.FLASH_MAP_MANAGER_BEAN_NAME, this.flashMapManager); }
@Test public void flashAttribute() { this.builder.flashAttr("foo", "bar"); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); FlashMap flashMap = new SessionFlashMapManager().retrieveAndUpdate(request, null); assertNotNull(flashMap); assertEquals("bar", flashMap.get("foo")); }
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { Object flashMapObj = RequestContextUtils.getOutputFlashMap(request); if (flashMapObj != null && !(flashMapObj instanceof FlashMap)) { // we need to create a FlashMap using the webapp classLoader FlashMap flashMapCopy = new FlashMap(); flashMapCopy.putAll((Map<? extends String, ?>) flashMapObj); // then put it in the request SessionFlashMapManager sessionFlashMapManager = new SessionFlashMapManager(); sessionFlashMapManager.saveOutputFlashMap(flashMapCopy, request, response); } return true; }
@Test public void flashAttribute() throws Exception { this.builder.flashAttr("foo", "bar"); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); FlashMap flashMap = new SessionFlashMapManager().retrieveAndUpdate(request, null); assertNotNull(flashMap); assertEquals("bar", flashMap.get("foo")); }
private MockHttpServletRequest createRequest() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap()); request.setAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE, new SessionFlashMapManager()); return request; }
private void doTest(final Map<String, ?> map, final String url, final boolean contextRelative, final boolean exposeModelAttributes, String expectedUrlForEncoding) throws Exception { class TestRedirectView extends RedirectView { public boolean queryPropertiesCalled = false; /** * Test whether this callback method is called with correct args */ @Override protected Map<String, Object> queryProperties(Map<String, Object> model) { // They may not be the same model instance, but they're still equal assertTrue("Map and model must be equal.", map.equals(model)); this.queryPropertiesCalled = true; return super.queryProperties(model); } } TestRedirectView rv = new TestRedirectView(); rv.setUrl(url); rv.setContextRelative(contextRelative); rv.setExposeModelAttributes(exposeModelAttributes); HttpServletRequest request = mock(HttpServletRequest.class, "request"); if (exposeModelAttributes) { given(request.getCharacterEncoding()).willReturn(WebUtils.DEFAULT_CHARACTER_ENCODING); } if (contextRelative) { expectedUrlForEncoding = "/context" + expectedUrlForEncoding; given(request.getContextPath()).willReturn("/context"); } given(request.getAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE)).willReturn(new FlashMap()); FlashMapManager flashMapManager = new SessionFlashMapManager(); given(request.getAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE)).willReturn(flashMapManager); HttpServletResponse response = mock(HttpServletResponse.class, "response"); given(response.encodeRedirectURL(expectedUrlForEncoding)).willReturn(expectedUrlForEncoding); response.sendRedirect(expectedUrlForEncoding); rv.render(map, request, response); if (exposeModelAttributes) { assertTrue("queryProperties() should have been called.", rv.queryPropertiesCalled); } }