From 77c854b6f6db7f5da111c338e85df0c474cd1f60 Mon Sep 17 00:00:00 2001 From: AzureusNation Date: Sat, 6 Aug 2016 14:41:46 +0100 Subject: [PATCH] Converted the ModelAndView to return a Map. Updated GenericIntegrationTest to pass with the change. --- src/main/java/spark/ModelAndView.java | 9 +++++---- src/main/java/spark/Spark.java | 4 +++- src/main/java/spark/TemplateEngine.java | 4 +++- src/main/java/spark/TemplateViewRouteImpl.java | 4 +++- src/test/java/spark/GenericIntegrationTest.java | 7 +++++-- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/main/java/spark/ModelAndView.java b/src/main/java/spark/ModelAndView.java index 3acae41ffc..7e9ce10427 100644 --- a/src/main/java/spark/ModelAndView.java +++ b/src/main/java/spark/ModelAndView.java @@ -16,6 +16,8 @@ */ package spark; +import java.util.Map; + /** * Model And View class is used to set the name of the view and the model object * to be rendered. @@ -27,7 +29,7 @@ public class ModelAndView { /** * Model object. */ - private Object model; + private Map model; /** * View name used to render output. */ @@ -39,8 +41,7 @@ public class ModelAndView { * @param model the model * @param viewName the view name */ - public ModelAndView(Object model, String viewName) { - super(); + public ModelAndView(Map model, String viewName) { this.model = model; this.viewName = viewName; } @@ -48,7 +49,7 @@ public ModelAndView(Object model, String viewName) { /** * @return the model object */ - public Object getModel() { + public Map getModel() { return model; } diff --git a/src/main/java/spark/Spark.java b/src/main/java/spark/Spark.java index 1ad7731a9a..a6ea3a99e9 100644 --- a/src/main/java/spark/Spark.java +++ b/src/main/java/spark/Spark.java @@ -16,6 +16,8 @@ */ package spark; +import java.util.Map; + import static spark.Service.ignite; /** @@ -1077,7 +1079,7 @@ public static void init() { * @param viewName the view name * @return the model and view */ - public static ModelAndView modelAndView(Object model, String viewName) { + public static ModelAndView modelAndView(Map model, String viewName) { return new ModelAndView(model, viewName); } diff --git a/src/main/java/spark/TemplateEngine.java b/src/main/java/spark/TemplateEngine.java index 65b00bde6a..23eda7e5a7 100644 --- a/src/main/java/spark/TemplateEngine.java +++ b/src/main/java/spark/TemplateEngine.java @@ -1,6 +1,8 @@ package spark; +import java.util.Map; + /** * A Template holds the implementation of the 'render' method. * TemplateViewRoute instead of returning the result of calling toString() as body, it returns the result of calling render method. @@ -28,7 +30,7 @@ public String render(Object object) { * @param viewName to be rendered. * @return object with model and view set. */ - public ModelAndView modelAndView(Object model, String viewName) { + public ModelAndView modelAndView(Map model, String viewName) { return new ModelAndView(model, viewName); } diff --git a/src/main/java/spark/TemplateViewRouteImpl.java b/src/main/java/spark/TemplateViewRouteImpl.java index 6001f4bea6..7a0c0b0843 100644 --- a/src/main/java/spark/TemplateViewRouteImpl.java +++ b/src/main/java/spark/TemplateViewRouteImpl.java @@ -17,6 +17,8 @@ package spark; +import java.util.Map; + /** * A TemplateViewRoute is built up by a path (for url-matching) and the implementation of the 'render' method. * TemplateViewRoute instead of returning the result of calling toString() as body, it returns the result of calling render method. @@ -92,7 +94,7 @@ public Object render(Object object) { * @param viewName t be rendered. * @return object with model and view set. */ - public ModelAndView modelAndView(Object model, String viewName) { + public ModelAndView modelAndView(Map model, String viewName) { return new ModelAndView(model, viewName); } diff --git a/src/test/java/spark/GenericIntegrationTest.java b/src/test/java/spark/GenericIntegrationTest.java index ce65d76665..a7b2db3bb5 100644 --- a/src/test/java/spark/GenericIntegrationTest.java +++ b/src/test/java/spark/GenericIntegrationTest.java @@ -117,7 +117,10 @@ public static void setup() throws IOException { }); get("/templateView", (q, a) -> { - return new ModelAndView("Hello", "my view"); + return new ModelAndView(new HashMap() { + { + put("hello", "Hello"); + }}, "my view"); }, new TemplateEngine() { public String render(ModelAndView modelAndView) { return modelAndView.getModel() + " from " + modelAndView.getViewName(); @@ -214,7 +217,7 @@ public void routes_should_be_accept_type_aware() throws Exception { public void template_view_should_be_rendered_with_given_model_view_object() throws Exception { UrlResponse response = testUtil.doMethod("GET", "/templateView", null); Assert.assertEquals(200, response.status); - Assert.assertEquals("Hello from my view", response.body); + Assert.assertEquals("{hello=Hello} from my view", response.body); } @Test