/** * Add non-null return values to the {@link ModelAndViewContainer}. */ @Override public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { if (returnValue != null) { String name = ModelFactory.getNameForReturnValue(returnValue, returnType); mavContainer.addAttribute(name, returnValue); } }
@Test public void bindingResult() throws Exception { ModelAndViewContainer mavContainer = new ModelAndViewContainer(); mavContainer.addAttribute("ignore1", "value1"); mavContainer.addAttribute("ignore2", "value2"); mavContainer.addAttribute("ignore3", "value3"); mavContainer.addAttribute("ignore4", "value4"); mavContainer.addAttribute("ignore5", "value5"); mavContainer.addAllAttributes(bindingResult.getModel()); Object actual = resolver.resolveArgument(paramErrors, mavContainer, webRequest, null); assertSame(actual, bindingResult); }
@Test // SPR-9378 public void resolveArgumentOrdering() throws Exception { String name = "testBean"; Object testBean = new TestBean(name); this.container.addAttribute(name, testBean); this.container.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, testBean); Object anotherTestBean = new TestBean(); this.container.addAttribute("anotherTestBean", anotherTestBean); StubRequestDataBinder dataBinder = new StubRequestDataBinder(testBean, name); WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class); given(binderFactory.createBinder(this.request, testBean, name)).willReturn(dataBinder); this.processor.resolveArgument(this.paramModelAttr, this.container, this.request, binderFactory); Object[] values = this.container.getModel().values().toArray(); assertSame("Resolved attribute should be updated to be last", testBean, values[1]); assertSame("BindingResult of resolved attr should be last", dataBinder.getBindingResult(), values[2]); }
@Test(expected = IllegalStateException.class) public void bindingResultNotFound() throws Exception { ModelAndViewContainer mavContainer = new ModelAndViewContainer(); mavContainer.addAllAttributes(bindingResult.getModel()); mavContainer.addAttribute("ignore1", "value1"); resolver.resolveArgument(paramErrors, mavContainer, webRequest, null); }
@Before public void setup() { this.sessionAttributeStore = new DefaultSessionAttributeStore(); this.webRequest = new ServletWebRequest(new MockHttpServletRequest(), new MockHttpServletResponse()); this.mavContainer = new ModelAndViewContainer(); this.mavContainer.addAttribute("methods", new ArrayList<String>()); }
@Test public void ignoreDefaultModel() { this.mavContainer.setIgnoreDefaultModelOnRedirect(true); this.mavContainer.addAttribute("name", "value"); this.mavContainer.setRedirectModelScenario(true); assertTrue(this.mavContainer.getModel().isEmpty()); }
@Test public void handleModelReturnValue() throws Exception { mavContainer.addAttribute("attr1", "value1"); Model returnValue = new ExtendedModelMap(); returnValue.addAttribute("attr2", "value2"); processor.handleReturnValue(returnValue , returnParamModel, mavContainer, webRequest); assertEquals("value1", mavContainer.getModel().get("attr1")); assertEquals("value2", mavContainer.getModel().get("attr2")); }
@Test public void getModel() { this.mavContainer.addAttribute("name", "value"); assertEquals(1, this.mavContainer.getModel().size()); assertEquals("value", this.mavContainer.getModel().get("name")); }
@Test public void redirectScenarioWithoutRedirectModel() { this.mavContainer.addAttribute("name", "value"); this.mavContainer.setRedirectModelScenario(true); assertEquals(1, this.mavContainer.getModel().size()); assertEquals("value", this.mavContainer.getModel().get("name")); }
@Test public void handleMapReturnValue() throws Exception { mavContainer.addAttribute("attr1", "value1"); Map<String, Object> returnValue = new ModelMap("attr2", "value2"); processor.handleReturnValue(returnValue , returnParamMap, mavContainer, webRequest); assertEquals("value1", mavContainer.getModel().get("attr1")); assertEquals("value2", mavContainer.getModel().get("attr2")); }
private void testGetAttributeFromModel(String expectedAttrName, MethodParameter param) throws Exception { Object target = new TestBean(); this.container.addAttribute(expectedAttrName, target); WebDataBinder dataBinder = new WebRequestDataBinder(target); WebDataBinderFactory factory = mock(WebDataBinderFactory.class); given(factory.createBinder(this.request, target, expectedAttrName)).willReturn(dataBinder); this.processor.resolveArgument(param, this.container, this.request, factory); verify(factory).createBinder(this.request, target, expectedAttrName); }
@Test // SPR-14045 public void ignoreDefaultModelAndWithoutRedirectModel() { this.mavContainer.setIgnoreDefaultModelOnRedirect(true); this.mavContainer.setRedirectModelScenario(true); this.mavContainer.addAttribute("name", "value"); assertEquals(1, this.mavContainer.getModel().size()); assertEquals("value", this.mavContainer.getModel().get("name")); }
@Test public void redirectScenarioWithRedirectModel() { this.mavContainer.addAttribute("name1", "value1"); this.mavContainer.setRedirectModel(new ModelMap("name2", "value2")); this.mavContainer.setRedirectModelScenario(true); assertEquals(1, this.mavContainer.getModel().size()); assertEquals("value2", this.mavContainer.getModel().get("name2")); }
@Test public void resolveArgumentBindingDisabled() throws Exception { String name = "noBindAttr"; Object target = new TestBean(); this.container.addAttribute(name, target); StubRequestDataBinder dataBinder = new StubRequestDataBinder(target, name); WebDataBinderFactory factory = mock(WebDataBinderFactory.class); given(factory.createBinder(this.request, target, name)).willReturn(dataBinder); this.processor.resolveArgument(this.paramBindingDisabledAttr, this.container, this.request, factory); assertFalse(dataBinder.isBindInvoked()); assertTrue(dataBinder.isValidateInvoked()); }
@Test public void resolveArgumentValidation() throws Exception { String name = "attrName"; Object target = new TestBean(); this.container.addAttribute(name, target); StubRequestDataBinder dataBinder = new StubRequestDataBinder(target, name); WebDataBinderFactory factory = mock(WebDataBinderFactory.class); given(factory.createBinder(this.request, target, name)).willReturn(dataBinder); this.processor.resolveArgument(this.paramNamedValidModelAttr, this.container, this.request, factory); assertTrue(dataBinder.isBindInvoked()); assertTrue(dataBinder.isValidateInvoked()); }
@Test public void resolveArgumentBindingDisabledPreviously() throws Exception { String name = "attrName"; Object target = new TestBean(); this.container.addAttribute(name, target); // Declare binding disabled (e.g. via @ModelAttribute method) this.container.setBindingDisabled(name); StubRequestDataBinder dataBinder = new StubRequestDataBinder(target, name); WebDataBinderFactory factory = mock(WebDataBinderFactory.class); given(factory.createBinder(this.request, target, name)).willReturn(dataBinder); this.processor.resolveArgument(this.paramNamedValidModelAttr, this.container, this.request, factory); assertFalse(dataBinder.isBindInvoked()); assertTrue(dataBinder.isValidateInvoked()); }
@Test public void updateModelBindingResult() throws Exception { String commandName = "attr1"; Object command = new Object(); ModelAndViewContainer container = new ModelAndViewContainer(); container.addAttribute(commandName, command); WebDataBinder dataBinder = new WebDataBinder(command, commandName); WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class); given(binderFactory.createBinder(this.webRequest, command, commandName)).willReturn(dataBinder); ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.attributeHandler); modelFactory.updateModel(this.webRequest, container); assertEquals(command, container.getModel().get(commandName)); String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + commandName; assertSame(dataBinder.getBindingResult(), container.getModel().get(bindingResultKey)); assertEquals(2, container.getModel().size()); }
@Test public void updateModelSessionAttributesSaved() throws Exception { String attributeName = "sessionAttr"; String attribute = "value"; ModelAndViewContainer container = new ModelAndViewContainer(); container.addAttribute(attributeName, attribute); WebDataBinder dataBinder = new WebDataBinder(attribute, attributeName); WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class); given(binderFactory.createBinder(this.webRequest, attribute, attributeName)).willReturn(dataBinder); ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.attributeHandler); modelFactory.updateModel(this.webRequest, container); assertEquals(attribute, container.getModel().get(attributeName)); assertEquals(attribute, this.attributeStore.retrieveAttribute(this.webRequest, attributeName)); }
@Test public void updateModelSessionAttributesRemoved() throws Exception { String attributeName = "sessionAttr"; String attribute = "value"; ModelAndViewContainer container = new ModelAndViewContainer(); container.addAttribute(attributeName, attribute); this.attributeStore.storeAttribute(this.webRequest, attributeName, attribute); WebDataBinder dataBinder = new WebDataBinder(attribute, attributeName); WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class); given(binderFactory.createBinder(this.webRequest, attribute, attributeName)).willReturn(dataBinder); container.getSessionStatus().setComplete(); ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.attributeHandler); modelFactory.updateModel(this.webRequest, container); assertEquals(attribute, container.getModel().get(attributeName)); assertNull(this.attributeStore.retrieveAttribute(this.webRequest, attributeName)); }
@Test // SPR-12542 public void updateModelWhenRedirecting() throws Exception { String attributeName = "sessionAttr"; String attribute = "value"; ModelAndViewContainer container = new ModelAndViewContainer(); container.addAttribute(attributeName, attribute); String queryParam = "123"; String queryParamName = "q"; container.setRedirectModel(new ModelMap(queryParamName, queryParam)); container.setRedirectModelScenario(true); WebDataBinder dataBinder = new WebDataBinder(attribute, attributeName); WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class); given(binderFactory.createBinder(this.webRequest, attribute, attributeName)).willReturn(dataBinder); ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.attributeHandler); modelFactory.updateModel(this.webRequest, container); assertEquals(queryParam, container.getModel().get(queryParamName)); assertEquals(1, container.getModel().size()); assertEquals(attribute, this.attributeStore.retrieveAttribute(this.webRequest, attributeName)); }