* 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
This commit is contained in:
Maurizio Cucchiara
2011-08-15 17:48:07 +00:00
parent 3343161e12
commit c035f4cb4a
10 changed files with 103 additions and 15 deletions
@@ -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<T> 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<T> 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<T> extends XWorkJUnit4TestCase {
namespace, name, method, new HashMap<String, Object>(), true, false);
ActionContext invocationContext = proxy.getInvocation().getInvocationContext();
invocationContext.setParameters(new HashMap<String,Object>(request.getParameterMap()));
invocationContext.setParameters(new HashMap<String, Object>(request.getParameterMap()));
// set the action context to the one used by the proxy
ActionContext.setContext(invocationContext);
@@ -211,9 +209,17 @@ public abstract class StrutsJUnit4TestCase<T> extends XWorkJUnit4TestCase {
super.setUp();
initServletMockObjects();
setupBeforeInitDispatcher();
initDispatcherParams();
initDispatcher(dispatcherInitParams);
}
protected void initDispatcherParams() {
if (StringUtils.isNotBlank(getConfigPath())) {
dispatcherInitParams = new HashMap<String, String>();
dispatcherInitParams.put("config", "struts-default.xml," + getConfigPath());
}
}
protected Dispatcher initDispatcher(Map<String, String> params) {
Dispatcher du = StrutsTestCaseHelper.initDispatcher(servletContext, params);
configurationManager = du.getConfigurationManager();
@@ -222,6 +228,10 @@ public abstract class StrutsJUnit4TestCase<T> extends XWorkJUnit4TestCase {
return du;
}
protected String getConfigPath() {
return null;
}
@After
public void tearDown() throws Exception {
@@ -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() {
@@ -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<JUnitTestAction>{
@Test
public void testExecuteActionAgainstCustomStrutsConfigFile() throws Exception {
String output = executeAction("/test2/testAction2.action");
Assert.assertEquals("Test2", output);
}
@Override
protected String getConfigPath() {
return "struts-test.xml";
}
}
@@ -34,8 +34,8 @@ import java.io.UnsupportedEncodingException;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:applicationContext.xml"})
public class StrutsSpringJUnit4TestCaseTest extends StrutsSpringJUnit4TestCase<JunitTestAction> {
public class StrutsSpringJUnit4TestCaseTest extends StrutsSpringJUnit4TestCase<JUnitTestAction> {
@Test
public void getActionMapping() {
ActionMapping mapping = getActionMapping("/test/testAction.action");
@@ -52,7 +52,7 @@ public class StrutsSpringJUnit4TestCaseTest extends StrutsSpringJUnit4TestCase<J
ActionProxy proxy = getActionProxy("/test/testAction.action");
Assert.assertNotNull(proxy);
JunitTestAction action = (JunitTestAction) proxy.getAction();
JUnitTestAction action = (JUnitTestAction) proxy.getAction();
Assert.assertNotNull(action);
String result = proxy.execute();
@@ -23,7 +23,7 @@ package org.apache.struts2;
public class StrutsSpringTestCaseTest extends StrutsSpringTestCase {
public void testApplicationContext() {
assertNotNull(applicationContext);
JunitTestAction action = (JunitTestAction) applicationContext.getBean("testAction");
JUnitTestAction action = (JUnitTestAction) applicationContext.getBean("testAction");
assertNotNull(action);
}
}
@@ -42,7 +42,7 @@ public class StrutsTestCaseTest extends StrutsSpringTestCase {
ActionProxy proxy = getActionProxy("/test/testAction.action");
assertNotNull(proxy);
JunitTestAction action = (JunitTestAction) proxy.getAction();
JUnitTestAction action = (JUnitTestAction) proxy.getAction();
assertNotNull(action);
String result = proxy.execute();
@@ -2,5 +2,5 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="testAction" class="org.apache.struts2.JunitTestAction"/>
<bean id="testAction" class="org.apache.struts2.JUnitTestAction"/>
</beans>
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
~ $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.
-->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="test2" namespace="/test2" extends="struts-default">
<action name="testAction2" class="org.apache.struts2.JUnitTestAction">
<result type="freemarker">/template2.ftl</result>
</action>
</package>
</struts>
+1 -1
View File
@@ -7,7 +7,7 @@
<struts>
<constant name="struts.objectFactory" value="spring"/>
<package name="test" namespace="/test" extends="struts-default">
<action name="testAction" class="org.apache.struts2.JunitTestAction">
<action name="testAction" class="org.apache.struts2.JUnitTestAction">
<result type="freemarker">/template.ftl</result>
</action>
</package>
@@ -0,0 +1 @@
Test2