You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2015/02/02 10:05:00 UTC

struts git commit: WW-4446 Adds wrapper around Dispatcher in tests to avoid overwhelming warnings

Repository: struts
Updated Branches:
  refs/heads/develop 5e74fcd94 -> 46b28e0b6


WW-4446 Adds wrapper around Dispatcher in tests to avoid overwhelming
 warnings


Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/46b28e0b
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/46b28e0b
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/46b28e0b

Branch: refs/heads/develop
Commit: 46b28e0b6365531bb25dac4a427258e70a2c24a9
Parents: 5e74fcd
Author: Lukasz Lenart <lu...@apache.org>
Authored: Mon Feb 2 10:04:40 2015 +0100
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Mon Feb 2 10:04:40 2015 +0100

----------------------------------------------------------------------
 .../struts2/util/StrutsTestCaseHelper.java      | 30 ++++++-
 .../freemarker/FreemarkerResultMockedTest.java  | 84 +++++---------------
 2 files changed, 50 insertions(+), 64 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/46b28e0b/core/src/main/java/org/apache/struts2/util/StrutsTestCaseHelper.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/util/StrutsTestCaseHelper.java b/core/src/main/java/org/apache/struts2/util/StrutsTestCaseHelper.java
index b19e43e..546c74d 100644
--- a/core/src/main/java/org/apache/struts2/util/StrutsTestCaseHelper.java
+++ b/core/src/main/java/org/apache/struts2/util/StrutsTestCaseHelper.java
@@ -31,8 +31,11 @@ import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.util.LocalizedTextUtil;
 import com.opensymphony.xwork2.util.ValueStack;
 import com.opensymphony.xwork2.util.ValueStackFactory;
+import org.apache.struts2.dispatcher.DispatcherErrorHandler;
 
 import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
 /**
  * Generic test setup methods to be used with any unit testing framework. 
@@ -51,7 +54,7 @@ public class StrutsTestCaseHelper {
         if (params == null) {
             params = new HashMap<String,String>();
         }
-        Dispatcher du = new Dispatcher(ctx, params);
+        Dispatcher du = new DispatcherWrapper(ctx, params);
         du.init();
         Dispatcher.setInstance(du);
 
@@ -68,4 +71,29 @@ public class StrutsTestCaseHelper {
         Dispatcher.setInstance(null);
         ActionContext.setContext(null);
     }
+
+    private static class DispatcherWrapper extends Dispatcher {
+
+        public DispatcherWrapper(ServletContext ctx, Map<String, String> params) {
+            super(ctx, params);
+            super.setDispatcherErrorHandler(new MockErrorHandler());
+        }
+
+        @Override
+        public void setDispatcherErrorHandler(DispatcherErrorHandler errorHandler) {
+            // ignore
+        }
+    }
+
+    private static class MockErrorHandler implements DispatcherErrorHandler {
+        public void init(ServletContext ctx) {
+            // ignore
+        }
+
+        public void handleError(HttpServletRequest request, HttpServletResponse response, int code, Exception e) {
+            System.out.println("Dispatcher#sendError: " + code);
+            e.printStackTrace(System.out);
+        }
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/struts/blob/46b28e0b/core/src/test/java/org/apache/struts2/views/freemarker/FreemarkerResultMockedTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/struts2/views/freemarker/FreemarkerResultMockedTest.java b/core/src/test/java/org/apache/struts2/views/freemarker/FreemarkerResultMockedTest.java
index 9a9584a..3c19077 100644
--- a/core/src/test/java/org/apache/struts2/views/freemarker/FreemarkerResultMockedTest.java
+++ b/core/src/test/java/org/apache/struts2/views/freemarker/FreemarkerResultMockedTest.java
@@ -6,6 +6,7 @@ import com.opensymphony.xwork2.util.ClassLoaderUtil;
 import com.opensymphony.xwork2.util.ValueStack;
 import com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory;
 import freemarker.template.Configuration;
+import freemarker.template.TemplateException;
 import freemarker.template.TemplateExceptionHandler;
 import org.apache.struts2.ServletActionContext;
 import org.apache.struts2.StrutsInternalTestCase;
@@ -34,26 +35,17 @@ public class FreemarkerResultMockedTest extends StrutsInternalTestCase {
     PrintWriter writer;
     StringWriter stringWriter;
     ServletContext servletContext;
-    FreemarkerManager mgr;
     MockHttpServletRequest request;
 
-    public void testActionThatThrowsExceptionTag() throws Exception {
-        //get fm config to use it in mock servlet context
-        FreemarkerManager freemarkerManager = container.getInstance(FreemarkerManager.class);
-        Configuration freemarkerConfig = freemarkerManager.getConfiguration(ServletActionContext.getServletContext());
-        freemarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
+    Configuration freemarkerConfig;
 
-        servletContext = EasyMock.createNiceMock(ServletContext.class);
+    public void testActionThatThrowsExceptionTag() throws Exception {
         File file = new File(FreeMarkerResultTest.class.getResource("callActionFreeMarker2.ftl").toURI());
         EasyMock.expect(servletContext.getRealPath("/tutorial/org/apache/struts2/views/freemarker/callActionFreeMarker.ftl")).andReturn(file.getAbsolutePath());
         file = new File(FreeMarkerResultTest.class.getResource("nested.ftl").toURI());
         EasyMock.expect(servletContext.getRealPath("/tutorial/org/apache/struts2/views/freemarker/nested.ftl")).andReturn(file.getAbsolutePath());
-        EasyMock.expect(servletContext.getAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY)).andReturn(freemarkerConfig).anyTimes();
         EasyMock.replay(servletContext);
 
-        freemarkerConfig.setServletContextForTemplateLoading(servletContext, null);
-        ServletActionContext.setServletContext(servletContext);
-
         init();
 
         request.setRequestURI("/tutorial/test2.action");
@@ -63,22 +55,12 @@ public class FreemarkerResultMockedTest extends StrutsInternalTestCase {
     }
 
     public void testActionThatSucceedsTag() throws Exception {
-        //get fm config to use it in mock servlet context
-        FreemarkerManager freemarkerManager = container.getInstance(FreemarkerManager.class);
-        Configuration freemarkerConfig = freemarkerManager.getConfiguration(ServletActionContext.getServletContext());
-        freemarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
-
-        servletContext = EasyMock.createNiceMock(ServletContext.class);
         File file = new File(FreeMarkerResultTest.class.getResource("callActionFreeMarker2.ftl").toURI());
         EasyMock.expect(servletContext.getRealPath("/tutorial/org/apache/struts2/views/freemarker/callActionFreeMarker2.ftl")).andReturn(file.getAbsolutePath());
         file = new File(FreeMarkerResultTest.class.getResource("nested.ftl").toURI());
         EasyMock.expect(servletContext.getRealPath("/tutorial/org/apache/struts2/views/freemarker/nested.ftl")).andReturn(file.getAbsolutePath());
-        EasyMock.expect(servletContext.getAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY)).andReturn(freemarkerConfig).anyTimes();
         EasyMock.replay(servletContext);
 
-        freemarkerConfig.setServletContextForTemplateLoading(servletContext, null);
-        ServletActionContext.setServletContext(servletContext);
-
         init();
 
         request.setRequestURI("/tutorial/test5.action");
@@ -88,13 +70,6 @@ public class FreemarkerResultMockedTest extends StrutsInternalTestCase {
     }
 
     public void testDynamicAttributesSupport() throws Exception {
-        //get fm config to use it in mock servlet context
-        FreemarkerManager freemarkerManager = container.getInstance(FreemarkerManager.class);
-        Configuration freemarkerConfig = freemarkerManager.getConfiguration(ServletActionContext.getServletContext());
-        freemarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
-
-        servletContext = EasyMock.createNiceMock(ServletContext.class);
-
         File file = new File(FreeMarkerResultTest.class.getResource("dynaAttributes.ftl").toURI());
         EasyMock.expect(servletContext.getRealPath("/tutorial/org/apache/struts2/views/freemarker/dynaAttributes.ftl")).andReturn(file.getAbsolutePath());
 
@@ -117,12 +92,8 @@ public class FreemarkerResultMockedTest extends StrutsInternalTestCase {
         EasyMock.expect(servletContext.getRealPath("/template/simple/dynamic-attributes.ftl")).andReturn(file.getAbsolutePath());
         EasyMock.expect(servletContext.getRealPath("/template/~~~simple/dynamic-attributes.ftl")).andReturn(file.getAbsolutePath());
 
-        EasyMock.expect(servletContext.getAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY)).andReturn(freemarkerConfig).anyTimes();
         EasyMock.replay(servletContext);
 
-        freemarkerConfig.setServletContextForTemplateLoading(servletContext, null);
-        ServletActionContext.setServletContext(servletContext);
-
         init();
 
         request.setRequestURI("/tutorial/test6.action");
@@ -150,12 +121,6 @@ public class FreemarkerResultMockedTest extends StrutsInternalTestCase {
     }
 
     public void testManualListInTemplate() throws Exception {
-        FreemarkerManager freemarkerManager = container.getInstance(FreemarkerManager.class);
-        Configuration freemarkerConfig = freemarkerManager.getConfiguration(ServletActionContext.getServletContext());
-        freemarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
-
-        servletContext = EasyMock.createNiceMock(ServletContext.class);
-
         File file = new File(FreeMarkerResultTest.class.getResource("manual-list.ftl").toURI());
         EasyMock.expect(servletContext.getRealPath("/tutorial/org/apache/struts2/views/freemarker/manual-list.ftl")).andReturn(file.getAbsolutePath());
 
@@ -178,12 +143,8 @@ public class FreemarkerResultMockedTest extends StrutsInternalTestCase {
         EasyMock.expect(servletContext.getRealPath("/template/simple/dynamic-attributes.ftl")).andReturn(file.getAbsolutePath());
         EasyMock.expect(servletContext.getRealPath("/template/~~~simple/dynamic-attributes.ftl")).andReturn(file.getAbsolutePath());
 
-        EasyMock.expect(servletContext.getAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY)).andReturn(freemarkerConfig).anyTimes();
         EasyMock.replay(servletContext);
 
-        freemarkerConfig.setServletContextForTemplateLoading(servletContext, null);
-        ServletActionContext.setServletContext(servletContext);
-
         init();
 
         request.setRequestURI("/tutorial/test7.action");
@@ -198,24 +159,14 @@ public class FreemarkerResultMockedTest extends StrutsInternalTestCase {
     }
 
     public void testDynamicAttributesInTheme() throws Exception {
-        FreemarkerManager freemarkerManager = container.getInstance(FreemarkerManager.class);
-        Configuration freemarkerConfig = freemarkerManager.getConfiguration(ServletActionContext.getServletContext());
-        freemarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
-
-        servletContext = EasyMock.createNiceMock(ServletContext.class);
-
         File file = new File(FreeMarkerResultTest.class.getResource("customTextField.ftl").toURI());
         EasyMock.expect(servletContext.getRealPath("/tutorial/org/apache/struts2/views/freemarker/customTextField.ftl")).andReturn(file.getAbsolutePath());
 
         file = new File(ClassLoaderUtil.getResource("template/test/text.ftl", getClass()).toURI());
         EasyMock.expect(servletContext.getRealPath("/template/test/text.ftl")).andReturn(file.getAbsolutePath());
 
-        EasyMock.expect(servletContext.getAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY)).andReturn(freemarkerConfig).anyTimes();
         EasyMock.replay(servletContext);
 
-        freemarkerConfig.setServletContextForTemplateLoading(servletContext, null);
-        ServletActionContext.setServletContext(servletContext);
-
         init();
 
         request.setRequestURI("/tutorial/test8.action");
@@ -226,17 +177,6 @@ public class FreemarkerResultMockedTest extends StrutsInternalTestCase {
     }
 
     private void init() throws MalformedURLException, URISyntaxException {
-        mgr = new FreemarkerManager();
-        mgr.setEncoding("UTF-8");
-
-        DefaultFileManagerFactory factory = new DefaultFileManagerFactory();
-        container.inject(factory);
-        mgr.setFileManagerFactory(factory);
-
-        FreemarkerThemeTemplateLoader themeLoader = new FreemarkerThemeTemplateLoader();
-        container.inject(themeLoader);
-        mgr.setThemeTemplateLoader(themeLoader);
-
         stringWriter = new StringWriter();
         writer = new PrintWriter(stringWriter);
         response = new StrutsMockHttpServletResponse();
@@ -257,6 +197,24 @@ public class FreemarkerResultMockedTest extends StrutsInternalTestCase {
         invocation = new MockActionInvocation();
         invocation.setStack(stack);
         invocation.setInvocationContext(context);
+
+        //get fm config to use it in mock servlet context
+        FreemarkerManager freemarkerManager = container.getInstance(FreemarkerManager.class);
+
+        freemarkerConfig = freemarkerManager.getConfiguration(ServletActionContext.getServletContext());
+        freemarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
+        freemarkerConfig.setServletContextForTemplateLoading(servletContext, null);
+    }
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+
+        servletContext = EasyMock.createNiceMock(ServletContext.class);
+        EasyMock.expect(servletContext.getInitParameter("TemplatePath")).andReturn(null);
+        EasyMock.expect(servletContext.getInitParameter("templatePath")).andReturn(null);
+
+        EasyMock.expect(servletContext.getAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY)).andReturn(freemarkerConfig).anyTimes();
     }
 
 }