freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject [04/16] incubator-freemarker git commit: FREEMARKER-55: Adding more tests regarding FreemarkerView's model
Date Tue, 04 Jul 2017 07:44:15 GMT
FREEMARKER-55: Adding more tests regarding FreemarkerView's model


Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/36b0694f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/36b0694f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/36b0694f

Branch: refs/heads/3
Commit: 36b0694f335c319946f908729be95f1808f23b2a
Parents: 334d9cd
Author: Woonsan Ko <woonsan@apache.org>
Authored: Thu Jun 29 00:41:17 2017 -0400
Committer: Woonsan Ko <woonsan@apache.org>
Committed: Thu Jun 29 00:41:17 2017 -0400

----------------------------------------------------------------------
 .../spring/web/view/FreemarkerView.java         |  7 +-
 .../spring/web/view/FreemarkerViewTest.java     | 73 ++++++++++++++++++--
 2 files changed, 68 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/36b0694f/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerView.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerView.java
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerView.java
index c13a49f..21b2a60 100644
--- a/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerView.java
+++ b/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerView.java
@@ -22,7 +22,6 @@ import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
 
 import org.apache.freemarker.core.model.ObjectWrapperAndUnwrapper;
 import org.apache.freemarker.core.model.TemplateHashModel;
@@ -41,6 +40,7 @@ public class FreemarkerView extends AbstractFreemarkerView {
     private TaglibFactory taglibFactory;
 
     public ServletContextHashModel getServletContextModel() {
+        // TODO
         return servletContextModel;
     }
 
@@ -62,10 +62,7 @@ public class FreemarkerView extends AbstractFreemarkerView {
             HttpServletRequest request, HttpServletResponse response) {
         AllHttpScopesHashModel model = new AllHttpScopesHashModel(objectWrapperForModel,
getServletContext(), request);
         model.putUnlistedModel(FreemarkerServlet.KEY_APPLICATION, getServletContextModel());
-        HttpSession session = request.getSession(false);
-        if (session != null) {
-            model.putUnlistedModel(FreemarkerServlet.KEY_SESSION, getHttpSessionModel(objectWrapperForModel,
request, response));
-        }
+        model.putUnlistedModel(FreemarkerServlet.KEY_SESSION, getHttpSessionModel(objectWrapperForModel,
request, response));
         model.putUnlistedModel(FreemarkerServlet.KEY_REQUEST, new HttpRequestHashModel(request,
response, objectWrapperForModel));
         model.putUnlistedModel(FreemarkerServlet.KEY_REQUEST_PARAMETERS,
                 new HttpRequestParametersHashModel(request, objectWrapperForModel));

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/36b0694f/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewTest.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewTest.java
b/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewTest.java
index 4d24632..0435004 100644
--- a/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewTest.java
+++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewTest.java
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertEquals;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicLong;
 
 import javax.servlet.ServletContext;
 
@@ -31,31 +32,36 @@ import org.junit.Before;
 import org.junit.Test;
 import org.springframework.mock.web.MockHttpServletRequest;
 import org.springframework.mock.web.MockHttpServletResponse;
+import org.springframework.mock.web.MockHttpSession;
 import org.springframework.mock.web.MockServletContext;
 
 public class FreemarkerViewTest {
 
     private ServletContext servletContext;
 
+    private StringTemplateLoader templateLoader;
+
     private Configuration configuration;
 
+    private AtomicLong visitorCount;
+
     @Before
     public void setUp() throws Exception {
         servletContext = new MockServletContext();
-
-        StringTemplateLoader templateLoader = new StringTemplateLoader();
-        templateLoader.putTemplate("hello.ftl", "Hello, ${name!\"World\"}! Visit count: ${visitCount!0}");
+        visitorCount = new AtomicLong();
+        servletContext.setAttribute("visitorCount", visitorCount);
+        templateLoader = new StringTemplateLoader();
         configuration = new Configuration.Builder(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS)
                 .templateLoader(templateLoader).build();
     }
 
     @Test
-    public void testViewRendering() throws Exception {
-        MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myservlet/handler.do");
+    public void testViewWithBasicModel() throws Exception {
+        MockHttpServletRequest request = new MockHttpServletRequest("GET", "/mytest.do");
         request.setContextPath("/mycontext");
         request.setServletPath("/myservlet");
-        request.setPathInfo(";mypathinfo");
-        request.setQueryString("?param1=value1");
+
+        templateLoader.putTemplate("hello.ftl", "Hello, ${name!\"World\"}! Visit count: ${visitCount!0}");
 
         FreemarkerView view = new FreemarkerView();
         view.setServletContext(servletContext);
@@ -75,4 +81,57 @@ public class FreemarkerViewTest {
         view.render(model, request, response);
         assertEquals("Hello, Dan! Visit count: 1", response.getContentAsString());
     }
+
+    @Test
+    public void testViewWithDefaultServletModel() throws Exception {
+        MockHttpSession session = new MockHttpSession(servletContext);
+        session.setAttribute("itemCountInCart", 3);
+
+        MockHttpServletRequest request = new MockHttpServletRequest("GET", "/mytest.do");
+        request.setContextPath("/mycontext");
+        request.setServletPath("/myservlet");
+        request.setPathInfo(";mypathinfo");
+        request.addParameter("token1", "value1");
+        request.setSession(session);
+        request.setAttribute("visitorCount", visitorCount);
+
+        // TODO: Add 'Application.attributeName' example, too.
+        templateLoader.putTemplate("default-model.ftl",
+                "${name!}, you have ${Session.itemCountInCart!0} items in cart. "
+                        + "BTW, you're ${Request.visitorCount}th visitor. "
+                        + "(token1: ${RequestParameters['token1']!})");
+
+        FreemarkerView view = new FreemarkerView();
+        view.setServletContext(servletContext);
+        view.setConfiguration(configuration);
+        view.setName("default-model.ftl");
+
+        Map<String, Object> model = new HashMap<String, Object>();
+        model.put("name", "Dan");
+
+        final long count = visitorCount.incrementAndGet();
+        MockHttpServletResponse response = new MockHttpServletResponse();
+        view.render(model, request, response);
+        assertEquals("Dan, you have 3 items in cart. BTW, you're " + count + "th visitor.
(token1: value1)",
+                response.getContentAsString());
+    }
+
+    @Test
+    public void testViewWithTaglibs() throws Exception {
+        MockHttpServletRequest request = new MockHttpServletRequest("GET", "/mytest.do");
+        request.setContextPath("/mycontext");
+        request.setServletPath("/myservlet");
+
+        // TODO: 
+        templateLoader.putTemplate("taglibs.ftl", "");
+
+        FreemarkerView view = new FreemarkerView();
+        view.setServletContext(servletContext);
+        view.setConfiguration(configuration);
+        view.setName("taglibs.ftl");
+
+        Map<String, Object> model = new HashMap<String, Object>();
+        MockHttpServletResponse response = new MockHttpServletResponse();
+        view.render(model, request, response);
+    }
 }


Mime
View raw message