freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject incubator-freemarker git commit: Continued FREEMARKER-1 "Option to not to overwrite response ContentType in FreemarkerServlet": More tests
Date Sat, 17 Oct 2015 17:28:17 GMT
Repository: incubator-freemarker
Updated Branches:
  refs/heads/2.3-gae fe08488da -> 64ec04bf8


Continued FREEMARKER-1 "Option to not to overwrite response ContentType in FreemarkerServlet":
More tests


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

Branch: refs/heads/2.3-gae
Commit: 64ec04bf8d53a260905a174876bba434d2a13eaf
Parents: fe08488
Author: ddekany <ddekany@apache.org>
Authored: Sat Oct 17 19:28:01 2015 +0200
Committer: ddekany <ddekany@apache.org>
Committed: Sat Oct 17 19:28:01 2015 +0200

----------------------------------------------------------------------
 .../ext/servlet/FreemarkerServletTest.java      | 112 ++++++++++++++-----
 1 file changed, 83 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/64ec04bf/src/test/java/freemarker/ext/servlet/FreemarkerServletTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/freemarker/ext/servlet/FreemarkerServletTest.java b/src/test/java/freemarker/ext/servlet/FreemarkerServletTest.java
index bf2ef8a..20ac78d 100644
--- a/src/test/java/freemarker/ext/servlet/FreemarkerServletTest.java
+++ b/src/test/java/freemarker/ext/servlet/FreemarkerServletTest.java
@@ -26,13 +26,13 @@ import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletResponse;
 
-import org.junit.After;
 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.MockServletConfig;
 import org.springframework.mock.web.MockServletContext;
+import org.springframework.util.Assert;
 
 import freemarker.log.Logger;
 
@@ -42,49 +42,50 @@ public class FreemarkerServletTest {
 
     private static final String TEST_TEMPLATE_PATH = "classpath:freemarker/ext/servlet";
 
-    private FreemarkerServlet freemarkerServlet;
-
     private MockServletContext servletContext;
-    private MockServletConfig servletConfig;
 
     @Before
     public void setUp() throws ServletException, IOException {
         servletContext = new MockServletContext();
         servletContext.setContextPath("/");
+    }
 
-        servletConfig = new MockServletConfig(servletContext);
-        servletConfig.addInitParameter(FreemarkerServlet.INIT_PARAM_TEMPLATE_PATH, TEST_TEMPLATE_PATH);
+    @Test
+    public void testContentTypeInitParams_withNoResponseContentType_DefaultOverriding() throws
ServletException, IOException {
+        MockHttpServletRequest request = createMockHttpServletRequest(servletContext, "/foo.ftl");
+        MockHttpServletResponse response = new MockHttpServletResponse();
+        assertNull(response.getContentType());
 
-        freemarkerServlet = new FreemarkerServlet();
-        freemarkerServlet.init(servletConfig);
-    }
+        createFreemarkerServlet().doGet(request, response);
+        LOG.debug("response content: " + response.getContentAsString());
 
-    @After
-    public void tearDown() {
-        freemarkerServlet.destroy();
+        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
+        assertTrue(response.getContentType().contains(FreemarkerServlet.DEFAULT_CONTENT_TYPE));
     }
 
     @Test
-    public void testContentTypeInitParams_withNoResponseContentType_ByDefault() throws ServletException,
IOException {
+    public void testContentTypeInitParams_withNoResponseContentType_DefaultOverriding2()
throws ServletException, IOException {
         MockHttpServletRequest request = createMockHttpServletRequest(servletContext, "/foo.ftl");
         MockHttpServletResponse response = new MockHttpServletResponse();
         assertNull(response.getContentType());
 
-        freemarkerServlet.doGet(request, response);
+        createFreemarkerServlet(
+                FreemarkerServlet.INIT_PARAM_CONTENT_TYPE, "text/css")
+                .doGet(request, response);
         LOG.debug("response content: " + response.getContentAsString());
 
         assertEquals(HttpServletResponse.SC_OK, response.getStatus());
-        assertTrue(response.getContentType().contains(FreemarkerServlet.DEFAULT_CONTENT_TYPE));
+        assertTrue(response.getContentType().contains("text/css"));
     }
 
     @Test
-    public void testContentTypeInitParams_withResponseContentType_ByDefault() throws ServletException,
IOException {
+    public void testContentTypeInitParams_withResponseContentType_DefaultOverriding() throws
ServletException, IOException {
         MockHttpServletRequest request = createMockHttpServletRequest(servletContext, "/foo.ftl");
         MockHttpServletResponse response = new MockHttpServletResponse();
         response.setContentType("application/json");
         assertEquals("application/json", response.getContentType());
 
-        freemarkerServlet.doGet(request, response);
+        createFreemarkerServlet().doGet(request, response);
         LOG.debug("response content: " + response.getContentAsString());
 
         assertEquals(HttpServletResponse.SC_OK, response.getStatus());
@@ -92,42 +93,94 @@ public class FreemarkerServletTest {
     }
 
     @Test
-    public void testContentTypeInitParams_withResponseContentType_NoOverriding() throws ServletException,
IOException {
-        servletConfig.addInitParameter(FreemarkerServlet.INIT_PARAM_OVERRIDE_RESPONSE_CONTENT_TYPE,
"false");
-        freemarkerServlet = new FreemarkerServlet();
-        freemarkerServlet.init(servletConfig);
+    public void testContentTypeInitParams_withResponseContentType_DefaultOverriding2() throws
ServletException, IOException {
+        MockHttpServletRequest request = createMockHttpServletRequest(servletContext, "/foo.ftl");
+        MockHttpServletResponse response = new MockHttpServletResponse();
+        response.setContentType("application/json");
+        assertEquals("application/json", response.getContentType());
 
+        createFreemarkerServlet(
+                FreemarkerServlet.INIT_PARAM_CONTENT_TYPE, "text/css")
+                .doGet(request, response);
+        LOG.debug("response content: " + response.getContentAsString());
+
+        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
+        assertTrue(response.getContentType().contains("text/css"));
+    }
+
+    @Test
+    public void testContentTypeInitParams_withResponseContentType_NoOverriding() throws ServletException,
IOException {
         MockHttpServletRequest request = createMockHttpServletRequest(servletContext, "/foo.ftl");
         MockHttpServletResponse response = new MockHttpServletResponse();
         response.setContentType("application/json");
         assertEquals("application/json", response.getContentType());
 
-        freemarkerServlet.doGet(request, response);
+        createFreemarkerServlet(FreemarkerServlet.INIT_PARAM_OVERRIDE_RESPONSE_CONTENT_TYPE,
"false")
+                .doGet(request, response);
         LOG.debug("response content: " + response.getContentAsString());
 
         assertEquals(HttpServletResponse.SC_OK, response.getStatus());
         assertEquals("application/json", response.getContentType());
+    }
+
+    @Test
+    public void testContentTypeInitParams_withResponseContentType_NoOverriding2() throws
ServletException, IOException {
+        MockHttpServletRequest request = createMockHttpServletRequest(servletContext, "/foo.ftl");
+        MockHttpServletResponse response = new MockHttpServletResponse();
+        response.setContentType("application/json");
+        assertEquals("application/json", response.getContentType());
 
-        freemarkerServlet.destroy();
+        createFreemarkerServlet(
+                FreemarkerServlet.INIT_PARAM_OVERRIDE_RESPONSE_CONTENT_TYPE, "false",
+                FreemarkerServlet.INIT_PARAM_CONTENT_TYPE, "text/css")
+                .doGet(request, response);
+        LOG.debug("response content: " + response.getContentAsString());
+
+        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
+        assertEquals("application/json", response.getContentType());
     }
 
     @Test
     public void testContentTypeInitParams_withNoResponseContentType_NoOverriding() throws
ServletException, IOException {
-        servletConfig.addInitParameter(FreemarkerServlet.INIT_PARAM_OVERRIDE_RESPONSE_CONTENT_TYPE,
"false");
-        freemarkerServlet = new FreemarkerServlet();
-        freemarkerServlet.init(servletConfig);
-
         MockHttpServletRequest request = createMockHttpServletRequest(servletContext, "/foo.ftl");
         MockHttpServletResponse response = new MockHttpServletResponse();
         assertNull(response.getContentType());
 
-        freemarkerServlet.doGet(request, response);
+        createFreemarkerServlet(FreemarkerServlet.INIT_PARAM_OVERRIDE_RESPONSE_CONTENT_TYPE,
"false")
+                .doGet(request, response);
         LOG.debug("response content: " + response.getContentAsString());
 
         assertEquals(HttpServletResponse.SC_OK, response.getStatus());
         assertTrue(response.getContentType().contains(FreemarkerServlet.DEFAULT_CONTENT_TYPE));
+    }
+
+    @Test
+    public void testContentTypeInitParams_withNoResponseContentType_NoOverriding2() throws
ServletException, IOException {
+        MockHttpServletRequest request = createMockHttpServletRequest(servletContext, "/foo.ftl");
+        MockHttpServletResponse response = new MockHttpServletResponse();
+        assertNull(response.getContentType());
 
-        freemarkerServlet.destroy();
+        createFreemarkerServlet(
+                FreemarkerServlet.INIT_PARAM_OVERRIDE_RESPONSE_CONTENT_TYPE, "false",
+                FreemarkerServlet.INIT_PARAM_CONTENT_TYPE, "text/css")
+                .doGet(request, response);
+        LOG.debug("response content: " + response.getContentAsString());
+
+        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
+        assertTrue(response.getContentType().contains("text/css"));
+    }
+    
+    private FreemarkerServlet createFreemarkerServlet(String... initParams) throws ServletException
{
+        MockServletConfig servletConfig = new MockServletConfig(servletContext);
+        servletConfig.addInitParameter(FreemarkerServlet.INIT_PARAM_TEMPLATE_PATH, TEST_TEMPLATE_PATH);
+        Assert.isTrue(initParams.length % 2 == 0);
+        for (int i = 0; i < initParams.length; i += 2) {
+            servletConfig.addInitParameter(initParams[i], initParams[i + 1]); 
+        }
+        
+        FreemarkerServlet freemarkerServlet = new FreemarkerServlet();
+        freemarkerServlet.init(servletConfig);
+        return freemarkerServlet;
     }
 
     private MockHttpServletRequest createMockHttpServletRequest(final ServletContext servletContext,
@@ -140,4 +193,5 @@ public class FreemarkerServletTest {
         servletRequest.setPathInfo(pathInfo);
         return servletRequest;
     }
+    
 }
\ No newline at end of file


Mime
View raw message