From c035f4cb4a06456889a2936fc18f69e11b71cd03 Mon Sep 17 00:00:00 2001 From: Maurizio Cucchiara Date: Mon, 15 Aug 2011 17:48:07 +0000 Subject: [PATCH] * Exposed getConfigPath method in order to use custom struts.xml file. * Renamed JUnitTestAction git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1157925 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/struts2/StrutsJUnit4TestCase.java | 24 ++++++++--- ...itTestAction.java => JUnitTestAction.java} | 2 +- .../struts2/StrutsJUnit4TestCaseTest.java | 43 +++++++++++++++++++ .../StrutsSpringJUnit4TestCaseTest.java | 6 +-- .../struts2/StrutsSpringTestCaseTest.java | 2 +- .../apache/struts2/StrutsTestCaseTest.java | 2 +- .../src/test/resources/applicationContext.xml | 2 +- .../junit/src/test/resources/struts-test.xml | 34 +++++++++++++++ plugins/junit/src/test/resources/struts.xml | 2 +- .../junit/src/test/resources/template2.ftl | 1 + 10 files changed, 103 insertions(+), 15 deletions(-) rename plugins/junit/src/test/java/org/apache/struts2/{JunitTestAction.java => JUnitTestAction.java} (92%) create mode 100644 plugins/junit/src/test/java/org/apache/struts2/StrutsJUnit4TestCaseTest.java create mode 100644 plugins/junit/src/test/resources/struts-test.xml create mode 100644 plugins/junit/src/test/resources/template2.ftl diff --git a/plugins/junit/src/main/java/org/apache/struts2/StrutsJUnit4TestCase.java b/plugins/junit/src/main/java/org/apache/struts2/StrutsJUnit4TestCase.java index a920691a9..102454158 100644 --- a/plugins/junit/src/main/java/org/apache/struts2/StrutsJUnit4TestCase.java +++ b/plugins/junit/src/main/java/org/apache/struts2/StrutsJUnit4TestCase.java @@ -27,6 +27,7 @@ import com.opensymphony.xwork2.interceptor.annotations.After; import com.opensymphony.xwork2.interceptor.annotations.Before; import com.opensymphony.xwork2.util.logging.LoggerFactory; import com.opensymphony.xwork2.util.logging.jdk.JdkLoggerFactory; +import org.apache.commons.lang.StringUtils; import org.apache.struts2.dispatcher.Dispatcher; import org.apache.struts2.dispatcher.mapper.ActionMapper; import org.apache.struts2.dispatcher.mapper.ActionMapping; @@ -45,11 +46,7 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; -import java.util.logging.ConsoleHandler; -import java.util.logging.Formatter; -import java.util.logging.Level; -import java.util.logging.LogRecord; -import java.util.logging.Logger; +import java.util.logging.*; import static org.junit.Assert.assertNotNull; @@ -101,6 +98,7 @@ public abstract class StrutsJUnit4TestCase extends XWorkJUnit4TestCase { /** * gets an object from the stack after an action is executed + * * @return The executed action */ @SuppressWarnings("unchecked") @@ -110,7 +108,7 @@ public abstract class StrutsJUnit4TestCase extends XWorkJUnit4TestCase { protected boolean containsErrors() { T action = this.getAction(); - if(action instanceof ValidationAware) { + if (action instanceof ValidationAware) { return ((ValidationAware) action).hasActionErrors(); } throw new UnsupportedOperationException("Current action does not implement ValidationAware interface"); @@ -152,7 +150,7 @@ public abstract class StrutsJUnit4TestCase extends XWorkJUnit4TestCase { namespace, name, method, new HashMap(), true, false); ActionContext invocationContext = proxy.getInvocation().getInvocationContext(); - invocationContext.setParameters(new HashMap(request.getParameterMap())); + invocationContext.setParameters(new HashMap(request.getParameterMap())); // set the action context to the one used by the proxy ActionContext.setContext(invocationContext); @@ -211,9 +209,17 @@ public abstract class StrutsJUnit4TestCase extends XWorkJUnit4TestCase { super.setUp(); initServletMockObjects(); setupBeforeInitDispatcher(); + initDispatcherParams(); initDispatcher(dispatcherInitParams); } + protected void initDispatcherParams() { + if (StringUtils.isNotBlank(getConfigPath())) { + dispatcherInitParams = new HashMap(); + dispatcherInitParams.put("config", "struts-default.xml," + getConfigPath()); + } + } + protected Dispatcher initDispatcher(Map params) { Dispatcher du = StrutsTestCaseHelper.initDispatcher(servletContext, params); configurationManager = du.getConfigurationManager(); @@ -222,6 +228,10 @@ public abstract class StrutsJUnit4TestCase extends XWorkJUnit4TestCase { return du; } + protected String getConfigPath() { + return null; + } + @After public void tearDown() throws Exception { diff --git a/plugins/junit/src/test/java/org/apache/struts2/JunitTestAction.java b/plugins/junit/src/test/java/org/apache/struts2/JUnitTestAction.java similarity index 92% rename from plugins/junit/src/test/java/org/apache/struts2/JunitTestAction.java rename to plugins/junit/src/test/java/org/apache/struts2/JUnitTestAction.java index 03a4c4353..a3d0ad3e3 100644 --- a/plugins/junit/src/test/java/org/apache/struts2/JunitTestAction.java +++ b/plugins/junit/src/test/java/org/apache/struts2/JUnitTestAction.java @@ -22,7 +22,7 @@ package org.apache.struts2; import com.opensymphony.xwork2.ActionSupport; -public class JunitTestAction extends ActionSupport { +public class JUnitTestAction extends ActionSupport { private String name; public String getName() { diff --git a/plugins/junit/src/test/java/org/apache/struts2/StrutsJUnit4TestCaseTest.java b/plugins/junit/src/test/java/org/apache/struts2/StrutsJUnit4TestCaseTest.java new file mode 100644 index 000000000..4259f7a22 --- /dev/null +++ b/plugins/junit/src/test/java/org/apache/struts2/StrutsJUnit4TestCaseTest.java @@ -0,0 +1,43 @@ +/* + * $Id: $ + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.struts2; + +import org.junit.Assert; +import org.junit.Test; + +/** + * User: maurizio cucchiara + * Date: 8/15/11 + * Time: 7:04 PM + */ +public class StrutsJUnit4TestCaseTest extends StrutsJUnit4TestCase{ + @Test + public void testExecuteActionAgainstCustomStrutsConfigFile() throws Exception { + String output = executeAction("/test2/testAction2.action"); + Assert.assertEquals("Test2", output); + } + + @Override + protected String getConfigPath() { + return "struts-test.xml"; + } +} diff --git a/plugins/junit/src/test/java/org/apache/struts2/StrutsSpringJUnit4TestCaseTest.java b/plugins/junit/src/test/java/org/apache/struts2/StrutsSpringJUnit4TestCaseTest.java index 8e5caf683..6bbee62eb 100644 --- a/plugins/junit/src/test/java/org/apache/struts2/StrutsSpringJUnit4TestCaseTest.java +++ b/plugins/junit/src/test/java/org/apache/struts2/StrutsSpringJUnit4TestCaseTest.java @@ -34,8 +34,8 @@ import java.io.UnsupportedEncodingException; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath*:applicationContext.xml"}) -public class StrutsSpringJUnit4TestCaseTest extends StrutsSpringJUnit4TestCase { - +public class StrutsSpringJUnit4TestCaseTest extends StrutsSpringJUnit4TestCase { + @Test public void getActionMapping() { ActionMapping mapping = getActionMapping("/test/testAction.action"); @@ -52,7 +52,7 @@ public class StrutsSpringJUnit4TestCaseTest extends StrutsSpringJUnit4TestCase - + \ No newline at end of file diff --git a/plugins/junit/src/test/resources/struts-test.xml b/plugins/junit/src/test/resources/struts-test.xml new file mode 100644 index 000000000..82778fb14 --- /dev/null +++ b/plugins/junit/src/test/resources/struts-test.xml @@ -0,0 +1,34 @@ + + + + + + + + + + /template2.ftl + + + diff --git a/plugins/junit/src/test/resources/struts.xml b/plugins/junit/src/test/resources/struts.xml index 7c77fcf9e..114e92a95 100644 --- a/plugins/junit/src/test/resources/struts.xml +++ b/plugins/junit/src/test/resources/struts.xml @@ -7,7 +7,7 @@ - + /template.ftl diff --git a/plugins/junit/src/test/resources/template2.ftl b/plugins/junit/src/test/resources/template2.ftl new file mode 100644 index 000000000..da8f20989 --- /dev/null +++ b/plugins/junit/src/test/resources/template2.ftl @@ -0,0 +1 @@ +Test2 \ No newline at end of file