@Test public void renderSimpleBeanWithJsonView() throws Exception { Object bean = new TestBeanSimple(); Map<String, Object> model = new HashMap<>(); model.put("bindingResult", mock(BindingResult.class, "binding_result")); model.put("foo", bean); model.put(JsonView.class.getName(), MyJacksonView1.class); view.setUpdateContentLength(true); view.render(model, request, response); String content = response.getContentAsString(); assertTrue(content.length() > 0); assertEquals(content.length(), response.getContentLength()); assertTrue(content.contains("foo")); assertFalse(content.contains("boo")); assertFalse(content.contains(JsonView.class.getName())); }
@Test public void renderSimpleBean() throws Exception { Object bean = new TestBeanSimple(); Map<String, Object> model = new HashMap<>(); model.put("bindingResult", mock(BindingResult.class, "binding_result")); model.put("foo", bean); view.setUpdateContentLength(true); view.render(model, request, response); assertTrue(response.getContentAsString().length() > 0); assertEquals(response.getContentAsString().length(), response.getContentLength()); validateResult(); }
@SuppressWarnings("rawtypes") @Test public void filterTwoKeyModel() throws Exception { view.setExtractValueFromSingleKeyModel(true); Map<String, Object> model = new HashMap<>(); TestBeanSimple bean1 = new TestBeanSimple(); TestBeanSimple bean2 = new TestBeanSimple(); model.put("foo1", bean1); model.put("foo2", bean2); Object actual = view.filterModel(model); assertTrue(actual instanceof Map); assertSame(bean1, ((Map) actual).get("foo1")); assertSame(bean2, ((Map) actual).get("foo2")); }
@Test public void renderWithCustomSerializerLocatedByFactory() throws Exception { SerializerFactory factory = new DelegatingSerializerFactory(null); ObjectMapper mapper = new ObjectMapper(); mapper.setSerializerFactory(factory); view.setObjectMapper(mapper); Object bean = new TestBeanSimple(); Map<String, Object> model = new HashMap<>(); model.put("foo", bean); model.put("bar", new TestChildBean()); view.render(model, request, response); String result = response.getContentAsString(); assertTrue(result.length() > 0); assertTrue(result.contains("\"foo\":{\"testBeanSimple\":\"custom\"}")); validateResult(); }
@Test public void filterSingleKeyModel() throws Exception { view.setExtractValueFromSingleKeyModel(true); Map<String, Object> model = new HashMap<>(); TestBeanSimple bean = new TestBeanSimple(); model.put("foo", bean); Object actual = view.filterModel(model); assertSame(bean, actual); }
@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(); }