@Test public void renderCaching() throws Exception { view.setDisableCaching(false); Map<String, Object> model = new HashMap<>(); model.put("bindingResult", mock(BindingResult.class, "binding_result")); model.put("foo", "bar"); view.render(model, request, response); assertNull(response.getHeader("Cache-Control")); }
@Test public void renderOnlyIncludedAttributes() throws Exception { Set<String> attrs = new HashSet<>(); attrs.add("foo"); attrs.add("baz"); attrs.add("nil"); view.setModelKeys(attrs); Map<String, Object> model = new HashMap<>(); model.put("foo", "foo"); model.put("bar", "bar"); model.put("baz", "baz"); view.render(model, request, response); String result = response.getContentAsString(); assertTrue(result.length() > 0); assertTrue(result.contains("\"foo\":\"foo\"")); assertTrue(result.contains("\"baz\":\"baz\"")); validateResult(); }
@Test public void renderWithSelectedContentType() throws Exception { Map<String, Object> model = new HashMap<>(); model.put("foo", "bar"); view.render(model, request, response); assertEquals("application/json", response.getContentType()); request.setAttribute(View.SELECTED_CONTENT_TYPE, new MediaType("application", "vnd.example-v2+xml")); view.render(model, request, response); assertEquals("application/vnd.example-v2+xml", response.getContentType()); }
@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(); }
@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 renderWithCustomSerializerLocatedByAnnotation() throws Exception { Object bean = new TestBeanSimpleAnnotated(); Map<String, Object> model = new HashMap<>(); model.put("foo", bean); view.render(model, request, response); assertTrue(response.getContentAsString().length() > 0); assertEquals("{\"foo\":{\"testBeanSimple\":\"custom\"}}", response.getContentAsString()); validateResult(); }
@Test public void renderSimpleBeanWithFilters() throws Exception { TestSimpleBeanFiltered bean = new TestSimpleBeanFiltered(); bean.setProperty1("value"); bean.setProperty2("value"); Map<String, Object> model = new HashMap<>(); model.put("bindingResult", mock(BindingResult.class, "binding_result")); model.put("foo", bean); FilterProvider filters = new SimpleFilterProvider().addFilter("myJacksonFilter", SimpleBeanPropertyFilter.serializeAllExcept("property2")); model.put(FilterProvider.class.getName(), filters); view.setUpdateContentLength(true); view.render(model, request, response); String content = response.getContentAsString(); assertTrue(content.length() > 0); assertEquals(content.length(), response.getContentLength()); assertThat(content, containsString("\"property1\":\"value\"")); assertThat(content, not(containsString("\"property2\":\"value\""))); assertFalse(content.contains(FilterProvider.class.getName())); }
@Test public void renderSimpleMap() throws Exception { Map<String, Object> model = new HashMap<>(); model.put("bindingResult", mock(BindingResult.class, "binding_result")); model.put("foo", "bar"); view.setUpdateContentLength(true); view.render(model, request, response); assertEquals("no-store", response.getHeader("Cache-Control")); assertEquals(MappingJackson2JsonView.DEFAULT_CONTENT_TYPE, response.getContentType()); String jsonResult = response.getContentAsString(); assertTrue(jsonResult.length() > 0); assertEquals(jsonResult.length(), response.getContentLength()); validateResult(); }
@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 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(); }