Changed View.render method to take Map<String, ?>
Changed View's render method from taking a HandlerResult to taking a Map<String, ?>, in order to facilitate scenarios where a HandlerResult is not available (i.e. web.reactive.function).
This commit is contained in:
@@ -25,10 +25,8 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.codec.CharSequenceEncoder;
|
||||
import org.springframework.core.io.buffer.support.DataBufferTestUtils;
|
||||
import org.springframework.http.HttpMethod;
|
||||
@@ -41,18 +39,13 @@ import org.springframework.tests.TestSubscriber;
|
||||
import org.springframework.ui.ExtendedModelMap;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.util.MimeType;
|
||||
import org.springframework.web.reactive.HandlerResult;
|
||||
import org.springframework.web.reactive.result.ResolvableMethod;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.adapter.DefaultServerWebExchange;
|
||||
import org.springframework.web.server.session.DefaultWebSessionManager;
|
||||
import org.springframework.web.server.session.WebSessionManager;
|
||||
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
|
||||
/**
|
||||
@@ -63,18 +56,9 @@ public class HttpMessageWriterViewTests {
|
||||
|
||||
private HttpMessageWriterView view = new HttpMessageWriterView(new Jackson2JsonEncoder());
|
||||
|
||||
private HandlerResult result;
|
||||
|
||||
private ModelMap model = new ExtendedModelMap();
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
MethodParameter param = ResolvableMethod.onClass(this.getClass()).name("handle").resolveReturnType();
|
||||
this.result = new HandlerResult(this, null, param, this.model);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void supportedMediaTypes() throws Exception {
|
||||
List<MimeType> mimeTypes = Arrays.asList(
|
||||
@@ -91,7 +75,7 @@ public class HttpMessageWriterViewTests {
|
||||
this.model.addAttribute("foo2", "bar2");
|
||||
this.model.addAttribute("foo3", "bar3");
|
||||
|
||||
assertEquals("bar2", this.view.extractObjectToRender(this.result));
|
||||
assertEquals("bar2", this.view.extractObjectToRender(this.model));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -99,7 +83,7 @@ public class HttpMessageWriterViewTests {
|
||||
this.view.setModelKeys(Collections.singleton("foo2"));
|
||||
this.model.addAttribute("foo1", "bar1");
|
||||
|
||||
assertNull(this.view.extractObjectToRender(this.result));
|
||||
assertNull(this.view.extractObjectToRender(this.model));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -109,7 +93,7 @@ public class HttpMessageWriterViewTests {
|
||||
this.model.addAttribute("foo2", "bar2");
|
||||
this.model.addAttribute("foo3", "bar3");
|
||||
|
||||
Object value = this.view.extractObjectToRender(this.result);
|
||||
Object value = this.view.extractObjectToRender(this.model);
|
||||
assertNotNull(value);
|
||||
assertEquals(HashMap.class, value.getClass());
|
||||
|
||||
@@ -127,7 +111,7 @@ public class HttpMessageWriterViewTests {
|
||||
this.model.addAttribute("foo2", "bar2");
|
||||
|
||||
try {
|
||||
view.extractObjectToRender(this.result);
|
||||
view.extractObjectToRender(this.model);
|
||||
fail();
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
@@ -143,7 +127,7 @@ public class HttpMessageWriterViewTests {
|
||||
this.model.addAttribute("foo1", "bar1");
|
||||
|
||||
try {
|
||||
view.extractObjectToRender(this.result);
|
||||
view.extractObjectToRender(this.model);
|
||||
fail();
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
@@ -165,7 +149,7 @@ public class HttpMessageWriterViewTests {
|
||||
WebSessionManager manager = new DefaultWebSessionManager();
|
||||
ServerWebExchange exchange = new DefaultServerWebExchange(request, response, manager);
|
||||
|
||||
this.view.render(result, MediaType.APPLICATION_JSON, exchange);
|
||||
this.view.render(this.model, MediaType.APPLICATION_JSON, exchange);
|
||||
|
||||
TestSubscriber
|
||||
.subscribe(response.getBody())
|
||||
|
||||
@@ -347,8 +347,9 @@ public class ViewResolutionResultHandlerTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> render(HandlerResult result, MediaType mediaType, ServerWebExchange exchange) {
|
||||
String value = this.name + ": " + result.getModel().toString();
|
||||
public Mono<Void> render(Map<String, ?> model, MediaType mediaType,
|
||||
ServerWebExchange exchange) {
|
||||
String value = this.name + ": " + model.toString();
|
||||
assertNotNull(value);
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
if (mediaType != null) {
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.reactive.result.view.freemarker;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
@@ -27,7 +28,6 @@ import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.context.ApplicationContextException;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
|
||||
@@ -35,7 +35,6 @@ import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse
|
||||
import org.springframework.tests.TestSubscriber;
|
||||
import org.springframework.ui.ExtendedModelMap;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.reactive.HandlerResult;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.adapter.DefaultServerWebExchange;
|
||||
import org.springframework.web.server.session.DefaultWebSessionManager;
|
||||
@@ -122,9 +121,7 @@ public class FreeMarkerViewTests {
|
||||
|
||||
ModelMap model = new ExtendedModelMap();
|
||||
model.addAttribute("hello", "hi FreeMarker");
|
||||
MethodParameter returnType = new MethodParameter(getClass().getDeclaredMethod("handle"), -1);
|
||||
HandlerResult result = new HandlerResult(new Object(), "", returnType, model);
|
||||
view.render(result, null, this.exchange);
|
||||
view.render(model, null, this.exchange);
|
||||
|
||||
TestSubscriber
|
||||
.subscribe(this.response.getBody())
|
||||
|
||||
Reference in New Issue
Block a user