Polishing

This commit is contained in:
Juergen Hoeller 2014-02-09 01:08:56 +01:00
parent 74ad2081aa
commit 53aab24690
3 changed files with 15 additions and 25 deletions

View File

@ -48,8 +48,13 @@ import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
/** /**
* Enhances {@link Configuration} classes by generating a CGLIB subclass capable of * Enhances {@link Configuration} classes by generating a CGLIB subclass which
* interacting with the Spring container to respect bean semantics. * interacts with the Spring container to respect bean scoping semantics for
* {@code @Bean} methods. Each such {@code @Bean} method will be overridden in
* the generated subclass, only delegating to the actual {@code @Bean} method
* implementation if the container actually requests the construction of a new
* instance. Otherwise, a call to such an {@code @Bean} method serves as a
* reference back to the container, obtaining the corresponding bean by name.
* *
* @author Chris Beams * @author Chris Beams
* @author Juergen Hoeller * @author Juergen Hoeller

View File

@ -39,7 +39,7 @@ import org.springframework.web.servlet.view.AbstractView;
/** /**
* Spring MVC {@link View} that renders JSON content by serializing the model for the current request * Spring MVC {@link View} that renders JSON content by serializing the model for the current request
* using <a href="http://jackson.codehaus.org/">Jackson's</a> {@link ObjectMapper}. * using <a href="http://jackson.codehaus.org/">Jackson 1.x's</a> {@link ObjectMapper}.
* *
* <p>By default, the entire contents of the model map (with the exception of framework-specific classes) * <p>By default, the entire contents of the model map (with the exception of framework-specific classes)
* will be encoded as JSON. If the model contains only one key, you can have it extracted encoded as JSON * will be encoded as JSON. If the model contains only one key, you can have it extracted encoded as JSON

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -39,6 +39,7 @@ import org.junit.Test;
import org.mozilla.javascript.Context; import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextFactory; import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.ScriptableObject; import org.mozilla.javascript.ScriptableObject;
import org.springframework.mock.web.test.MockHttpServletRequest; import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse; import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.ui.ModelMap; import org.springframework.ui.ModelMap;
@ -68,10 +69,8 @@ public class MappingJacksonJsonViewTests {
public void setUp() { public void setUp() {
request = new MockHttpServletRequest(); request = new MockHttpServletRequest();
response = new MockHttpServletResponse(); response = new MockHttpServletResponse();
jsContext = ContextFactory.getGlobal().enterContext(); jsContext = ContextFactory.getGlobal().enterContext();
jsScope = jsContext.initStandardObjects(); jsScope = jsContext.initStandardObjects();
view = new MappingJacksonJsonView(); view = new MappingJacksonJsonView();
} }
@ -93,13 +92,11 @@ public class MappingJacksonJsonViewTests {
assertEquals("no-cache", response.getHeader("Pragma")); assertEquals("no-cache", response.getHeader("Pragma"));
assertEquals("no-cache, no-store, max-age=0", response.getHeader("Cache-Control")); assertEquals("no-cache, no-store, max-age=0", response.getHeader("Cache-Control"));
assertNotNull(response.getHeader("Expires")); assertNotNull(response.getHeader("Expires"));
assertEquals(MappingJacksonJsonView.DEFAULT_CONTENT_TYPE, response.getContentType()); assertEquals(MappingJacksonJsonView.DEFAULT_CONTENT_TYPE, response.getContentType());
String jsonResult = response.getContentAsString(); String jsonResult = response.getContentAsString();
assertTrue(jsonResult.length() > 0); assertTrue(jsonResult.length() > 0);
assertEquals(jsonResult.length(), response.getContentLength()); assertEquals(jsonResult.length(), response.getContentLength());
validateResult(); validateResult();
} }
@ -110,7 +107,6 @@ public class MappingJacksonJsonViewTests {
Map<String, Object> model = new HashMap<String, Object>(); Map<String, Object> model = new HashMap<String, Object>();
model.put("bindingResult", mock(BindingResult.class, "binding_result")); model.put("bindingResult", mock(BindingResult.class, "binding_result"));
model.put("foo", "bar"); model.put("foo", "bar");
view.render(model, request, response); view.render(model, request, response);
assertNull(response.getHeader("Pragma")); assertNull(response.getHeader("Pragma"));
@ -130,26 +126,22 @@ public class MappingJacksonJsonViewTests {
Map<String, Object> model = new HashMap<String, Object>(); Map<String, Object> model = new HashMap<String, Object>();
model.put("bindingResult", mock(BindingResult.class, "binding_result")); model.put("bindingResult", mock(BindingResult.class, "binding_result"));
model.put("foo", bean); model.put("foo", bean);
view.setUpdateContentLength(true); view.setUpdateContentLength(true);
view.render(model, request, response); view.render(model, request, response);
assertTrue(response.getContentAsString().length() > 0); assertTrue(response.getContentAsString().length() > 0);
assertEquals(response.getContentAsString().length(), response.getContentLength()); assertEquals(response.getContentAsString().length(), response.getContentLength());
validateResult(); validateResult();
} }
@Test @Test
public void renderWithPrettyPrint() throws Exception { public void renderWithPrettyPrint() throws Exception {
ModelMap model = new ModelMap("foo", new TestBeanSimple()); ModelMap model = new ModelMap("foo", new TestBeanSimple());
view.setPrettyPrint(true); view.setPrettyPrint(true);
view.render(model, request, response); view.render(model, request, response);
String result = response.getContentAsString().replace("\r\n", "\n"); String result = response.getContentAsString().replace("\r\n", "\n");
assertTrue("Pretty printing not applied:\n" + result, result.startsWith("{\n \"foo\" : {\n ")); assertTrue("Pretty printing not applied:\n" + result, result.startsWith("{\n \"foo\" : {\n "));
validateResult(); validateResult();
} }
@ -172,12 +164,10 @@ public class MappingJacksonJsonViewTests {
Object bean = new TestBeanSimpleAnnotated(); Object bean = new TestBeanSimpleAnnotated();
Map<String, Object> model = new HashMap<String, Object>(); Map<String, Object> model = new HashMap<String, Object>();
model.put("foo", bean); model.put("foo", bean);
view.render(model, request, response); view.render(model, request, response);
assertTrue(response.getContentAsString().length() > 0); assertTrue(response.getContentAsString().length() > 0);
assertEquals("{\"foo\":{\"testBeanSimple\":\"custom\"}}", response.getContentAsString()); assertEquals("{\"foo\":{\"testBeanSimple\":\"custom\"}}", response.getContentAsString());
validateResult(); validateResult();
} }
@ -192,13 +182,11 @@ public class MappingJacksonJsonViewTests {
Map<String, Object> model = new HashMap<String, Object>(); Map<String, Object> model = new HashMap<String, Object>();
model.put("foo", bean); model.put("foo", bean);
model.put("bar", new TestChildBean()); model.put("bar", new TestChildBean());
view.render(model, request, response); view.render(model, request, response);
String result = response.getContentAsString(); String result = response.getContentAsString();
assertTrue(result.length() > 0); assertTrue(result.length() > 0);
assertTrue(result.contains("\"foo\":{\"testBeanSimple\":\"custom\"}")); assertTrue(result.contains("\"foo\":{\"testBeanSimple\":\"custom\"}"));
validateResult(); validateResult();
} }
@ -214,14 +202,12 @@ public class MappingJacksonJsonViewTests {
model.put("foo", "foo"); model.put("foo", "foo");
model.put("bar", "bar"); model.put("bar", "bar");
model.put("baz", "baz"); model.put("baz", "baz");
view.render(model, request, response); view.render(model, request, response);
String result = response.getContentAsString(); String result = response.getContentAsString();
assertTrue(result.length() > 0); assertTrue(result.length() > 0);
assertTrue(result.contains("\"foo\":\"foo\"")); assertTrue(result.contains("\"foo\":\"foo\""));
assertTrue(result.contains("\"baz\":\"baz\"")); assertTrue(result.contains("\"baz\":\"baz\""));
validateResult(); validateResult();
} }
@ -232,7 +218,6 @@ public class MappingJacksonJsonViewTests {
Map<String, Object> model = new HashMap<String, Object>(); Map<String, Object> model = new HashMap<String, Object>();
TestBeanSimple bean = new TestBeanSimple(); TestBeanSimple bean = new TestBeanSimple();
model.put("foo", bean); model.put("foo", bean);
Object actual = view.filterModel(model); Object actual = view.filterModel(model);
assertSame(bean, actual); assertSame(bean, actual);
@ -329,9 +314,7 @@ public class MappingJacksonJsonViewTests {
public static class TestBeanSimpleSerializer extends JsonSerializer<Object> { public static class TestBeanSimpleSerializer extends JsonSerializer<Object> {
@Override @Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
throws IOException {
jgen.writeStartObject(); jgen.writeStartObject();
jgen.writeFieldName("testBeanSimple"); jgen.writeFieldName("testBeanSimple");
jgen.writeString("custom"); jgen.writeString("custom");
@ -348,7 +331,9 @@ public class MappingJacksonJsonViewTests {
} }
@Override @Override
public JsonSerializer<Object> createSerializer(SerializationConfig config, JavaType baseType, BeanProperty property) throws JsonMappingException { public JsonSerializer<Object> createSerializer(SerializationConfig config, JavaType baseType, BeanProperty property)
throws JsonMappingException {
if (baseType.getRawClass() == TestBeanSimple.class) { if (baseType.getRawClass() == TestBeanSimple.class) {
return new TestBeanSimpleSerializer(); return new TestBeanSimpleSerializer();
} }