@Test public void testOneArgCtorWithEmptyCollection() throws Exception { ModelMap model = new ModelMap(new HashSet<>()); // must not add if collection is empty... assertEquals(0, model.size()); }
@Test public void testAddListOfTheSameObjects() throws Exception { List<TestBean> beans = new ArrayList<>(); beans.add(new TestBean("one")); beans.add(new TestBean("two")); beans.add(new TestBean("three")); ModelMap model = new ModelMap(); model.addAllAttributes(beans); assertEquals(1, model.size()); }
@Test public void testAddMap() throws Exception { Map<String, String> map = new HashMap<>(); map.put("one", "one-value"); map.put("two", "two-value"); ModelMap model = new ModelMap(); model.addAttribute(map); assertEquals(1, model.size()); String key = StringUtils.uncapitalize(ClassUtils.getShortName(map.getClass())); assertTrue(model.containsKey(key)); }
@Test public void testNoArgCtorYieldsEmptyModel() throws Exception { assertEquals(0, new ModelMap().size()); }
@Test public void testAddAllObjectsWithNullMap() throws Exception { ModelMap model = new ModelMap(); model.addAllAttributes((Map<String, ?>) null); assertEquals(0, model.size()); }
@Test public void testAddAllObjectsWithNullCollection() throws Exception { ModelMap model = new ModelMap(); model.addAllAttributes((Collection<Object>) null); assertEquals(0, model.size()); }
@Test public void testMergeMapWithOverriding() throws Exception { Map<String, TestBean> beans = new HashMap<>(); beans.put("one", new TestBean("one")); beans.put("two", new TestBean("two")); beans.put("three", new TestBean("three")); ModelMap model = new ModelMap(); model.put("one", new TestBean("oneOld")); model.mergeAttributes(beans); assertEquals(3, model.size()); assertEquals("oneOld", ((TestBean) model.get("one")).getName()); }
@Test public void testOneArgCtorWithScalar() throws Exception { ModelMap model = new ModelMap("bing"); assertEquals(1, model.size()); String string = (String) model.get("string"); assertNotNull(string); assertEquals("bing", string); }
@Test public void testUnnamedCtorScalar() throws Exception { ModelMap model = new ModelMap("foo", "bing"); assertEquals(1, model.size()); String bing = (String) model.get("foo"); assertNotNull(bing); assertEquals("bing", bing); }
@Test public void testAddObjectWithEmptyArray() throws Exception { ModelMap model = new ModelMap(new int[]{}); assertEquals(1, model.size()); int[] ints = (int[]) model.get("intList"); assertNotNull(ints); assertEquals(0, ints.length); }
@Test public void testNamedObjectCtor() throws Exception { ModelMap model = new ModelMap("foo", "bing"); assertEquals(1, model.size()); String bing = (String) model.get("foo"); assertNotNull(bing); assertEquals("bing", bing); }
@Test public void getModel() { this.mavContainer.addAttribute("name", "value"); assertEquals(1, this.mavContainer.getModel().size()); assertEquals("value", this.mavContainer.getModel().get("name")); }
@Test // SPR-14045 public void handleRedirectWithIgnoreDefaultModel() throws Exception { mavContainer.setIgnoreDefaultModelOnRedirect(true); RedirectView redirectView = new RedirectView(); ModelAndView mav = new ModelAndView(redirectView, "name", "value"); handler.handleReturnValue(mav, returnParamModelAndView, mavContainer, webRequest); ModelMap model = mavContainer.getModel(); assertSame(redirectView, mavContainer.getView()); assertEquals(1, model.size()); assertEquals("value", model.get("name")); }
@Test public void testAddObjectNoKeyOfSameTypeOverrides() throws Exception { ModelMap model = new ModelMap(); model.addAttribute("foo"); model.addAttribute("bar"); assertEquals(1, model.size()); String bar = (String) model.get("string"); assertEquals("bar", bar); }
@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 // SPR-13546 public void resolveExceptionModelAtArgument() throws Exception { IllegalArgumentException ex = new IllegalArgumentException(); HandlerMethod handlerMethod = new HandlerMethod(new ModelArgumentController(), "handle"); this.resolver.afterPropertiesSet(); ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex); assertNotNull(mav); assertEquals(1, mav.getModelMap().size()); assertEquals("IllegalArgumentException", mav.getModelMap().get("exceptionClassName")); }
@Test public void testOneArgCtorWithCollection() throws Exception { ModelMap model = new ModelMap(new String[]{"foo", "boing"}); assertEquals(1, model.size()); String[] strings = (String[]) model.get("stringList"); assertNotNull(strings); assertEquals(2, strings.length); assertEquals("foo", strings[0]); assertEquals("boing", strings[1]); }
@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 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()); }