/** * Return the underlying {@code ModelMap} instance (never {@code null}). */ public ModelMap getModelMap() { if (this.model == null) { this.model = new ModelMap(); } return this.model; }
@Test(expected = IllegalArgumentException.class) public void testOneArgCtorWithNull() { //Null model arguments added without a name being explicitly supplied are not allowed new ModelMap(null); }
@Test(expected = IllegalArgumentException.class) public void testAddAllObjectsWithSparseArrayList() throws Exception { // Null model arguments added without a name being explicitly supplied are not allowed ModelMap model = new ModelMap(); ArrayList<String> list = new ArrayList<>(); list.add("bing"); list.add(null); model.addAllAttributes(list); }
@Test public void testOneArgCtorWithEmptyCollection() throws Exception { ModelMap model = new ModelMap(new HashSet<>()); // must not add if collection is empty... assertEquals(0, model.size()); }
@Test(expected = IllegalArgumentException.class) public void testAddObjectWithNull() throws Exception { // Null model arguments added without a name being explicitly supplied are not allowed ModelMap model = new ModelMap(); model.addAttribute(null); }
@Test public void testAopJdkProxyWithDetectedInterfaces() throws Exception { ModelMap map = new ModelMap(); Map<?, ?> target = new HashMap<>(); ProxyFactory factory = new ProxyFactory(target); Object proxy = factory.getProxy(); map.addAttribute(proxy); assertSame(proxy, map.get("map")); }
@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 testAopJdkProxy() throws Exception { ModelMap map = new ModelMap(); ProxyFactory factory = new ProxyFactory(); Map<?, ?> target = new HashMap<>(); factory.setTarget(target); factory.addInterface(Map.class); Object proxy = factory.getProxy(); map.addAttribute(proxy); assertSame(proxy, map.get("map")); }
@Test public void renderWithPrettyPrint() throws Exception { ModelMap model = new ModelMap("foo", new TestBeanSimple()); view.setPrettyPrint(true); view.render(model, request, response); String result = response.getContentAsString().replace("\r\n", "\n"); assertTrue("Pretty printing not applied:\n" + result, result.startsWith("{\n \"foo\" : {\n ")); validateResult(); }
@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 testAddNullObjectViaCtorWithExplicitKey() throws Exception { ModelMap model = new ModelMap("foo", null); assertTrue(model.containsKey("foo")); assertNull(model.get("foo")); }
@Test public void testInnerClass() throws Exception { ModelMap map = new ModelMap(); SomeInnerClass inner = new SomeInnerClass(); map.addAttribute(inner); assertSame(inner, map.get("someInnerClass")); }
@Test public void storeAttributes() { ModelMap model = new ModelMap(); model.put("attr1", "value1"); model.put("attr2", "value2"); model.put("attr3", new TestBean()); WebSession session = new MockWebSession(); sessionAttributesHandler.storeAttributes(session, model); assertEquals("value1", session.getAttributes().get("attr1")); assertEquals("value2", session.getAttributes().get("attr2")); assertTrue(session.getAttributes().get("attr3") instanceof TestBean); }
@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 dontApplyUriVariables() throws Exception { String url = "/test#{'one','abc'}"; RedirectView redirectView = new RedirectView(url, true); redirectView.setExpandUriTemplateVariables(false); redirectView.renderMergedOutputModel(new ModelMap(), this.request, this.response); assertEquals(url, this.response.getRedirectedUrl()); }
@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 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 withFlashAttributes() throws Exception { UrlFilenameViewController ctrl = new UrlFilenameViewController(); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index"); request.setAttribute(DispatcherServlet.INPUT_FLASH_MAP_ATTRIBUTE, new ModelMap("name", "value")); MockHttpServletResponse response = new MockHttpServletResponse(); ModelAndView mv = ctrl.handleRequest(request, response); assertEquals("index", mv.getViewName()); assertEquals(1, mv.getModel().size()); assertEquals("value", mv.getModel().get("name")); }
@Test public void testAopCglibProxy() throws Exception { ModelMap map = new ModelMap(); ProxyFactory factory = new ProxyFactory(); SomeInnerClass val = new SomeInnerClass(); factory.setTarget(val); factory.setProxyTargetClass(true); map.addAttribute(factory.getProxy()); assertTrue(map.containsKey("someInnerClass")); assertEquals(val, map.get("someInnerClass")); }
@Test public void storeAttributes() throws Exception { ModelMap model = new ModelMap(); model.put("attr1", "value1"); model.put("attr2", "value2"); model.put("attr3", new TestBean()); sessionAttributesHandler.storeAttributes(request, model); assertEquals("value1", sessionAttributeStore.retrieveAttribute(request, "attr1")); assertEquals("value2", sessionAttributeStore.retrieveAttribute(request, "attr2")); assertTrue(sessionAttributeStore.retrieveAttribute(request, "attr3") instanceof TestBean); }