mirror of
https://github.com/apache/struts.git
synced 2026-08-07 07:37:20 +00:00
WW-5220 Moves JUnit related test into the JUnit plugin
Also moves all the plugin classes under junit package
This commit is contained in:
+1
-1
@@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.struts2;
|
||||
package org.apache.struts2.junit;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
+252
-252
@@ -1,252 +1,252 @@
|
||||
/*
|
||||
* 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 com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.ActionProxy;
|
||||
import com.opensymphony.xwork2.ActionProxyFactory;
|
||||
import com.opensymphony.xwork2.XWorkJUnit4TestCase;
|
||||
import com.opensymphony.xwork2.config.Configuration;
|
||||
import com.opensymphony.xwork2.interceptor.ValidationAware;
|
||||
import com.opensymphony.xwork2.interceptor.annotations.After;
|
||||
import com.opensymphony.xwork2.interceptor.annotations.Before;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.struts2.dispatcher.Dispatcher;
|
||||
import org.apache.struts2.dispatcher.HttpParameters;
|
||||
import org.apache.struts2.dispatcher.mapper.ActionMapper;
|
||||
import org.apache.struts2.dispatcher.mapper.ActionMapping;
|
||||
import org.apache.struts2.util.StrutsTestCaseHelper;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.MockHttpSession;
|
||||
import org.springframework.mock.web.MockPageContext;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
|
||||
public abstract class StrutsJUnit4TestCase<T> extends XWorkJUnit4TestCase {
|
||||
|
||||
protected MockHttpServletResponse response;
|
||||
protected MockHttpServletRequest request;
|
||||
protected MockPageContext pageContext;
|
||||
protected MockServletContext servletContext;
|
||||
protected Map<String, String> dispatcherInitParams;
|
||||
protected Dispatcher dispatcher;
|
||||
|
||||
protected DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
|
||||
|
||||
/**
|
||||
* gets an object from the stack after an action is executed
|
||||
*/
|
||||
protected Object findValueAfterExecute(String key) {
|
||||
return ServletActionContext.getValueStack(request).findValue(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* gets an object from the stack after an action is executed
|
||||
*
|
||||
* @return The executed action
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected T getAction() {
|
||||
return (T) findValueAfterExecute("action");
|
||||
}
|
||||
|
||||
protected boolean containsErrors() {
|
||||
T action = this.getAction();
|
||||
if (action instanceof ValidationAware) {
|
||||
return ((ValidationAware) action).hasActionErrors();
|
||||
}
|
||||
throw new UnsupportedOperationException("Current action does not implement ValidationAware interface");
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes an action and returns it's output (not the result returned from
|
||||
* execute()), but the actual output that would be written to the response.
|
||||
* For this to work the configured result for the action needs to be
|
||||
* FreeMarker, or Velocity (JSPs can be used with the Embedded JSP plugin)
|
||||
*/
|
||||
protected String executeAction(String uri) throws ServletException, UnsupportedEncodingException {
|
||||
request.setRequestURI(uri);
|
||||
ActionMapping mapping = getActionMapping(request);
|
||||
|
||||
assertNotNull(mapping);
|
||||
Dispatcher.getInstance().serviceAction(request, response, mapping);
|
||||
|
||||
if (response.getStatus() != HttpServletResponse.SC_OK)
|
||||
throw new ServletException("Error code [" + response.getStatus() + "], Error: ["
|
||||
+ response.getErrorMessage() + "]");
|
||||
|
||||
return response.getContentAsString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an action proxy for a request, and sets parameters of the ActionInvocation to the passed
|
||||
* parameters. Make sure to set the request parameters in the protected "request" object before calling this method.
|
||||
*/
|
||||
protected ActionProxy getActionProxy(String uri) {
|
||||
request.setRequestURI(uri);
|
||||
ActionMapping mapping = getActionMapping(request);
|
||||
String namespace = mapping.getNamespace();
|
||||
String name = mapping.getName();
|
||||
String method = mapping.getMethod();
|
||||
|
||||
Configuration config = configurationManager.getConfiguration();
|
||||
ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
|
||||
namespace, name, method, new HashMap<String, Object>(), true, false);
|
||||
|
||||
initActionContext(proxy.getInvocation().getInvocationContext());
|
||||
|
||||
// this is normally done in onSetUp(), but we are using Struts internal
|
||||
// objects (proxy and action invocation)
|
||||
// so we have to hack around so it works
|
||||
ServletActionContext.setServletContext(servletContext);
|
||||
ServletActionContext.setRequest(request);
|
||||
ServletActionContext.setResponse(response);
|
||||
|
||||
ServletActionContext.getContext().put(ServletActionContext.ACTION_MAPPING, mapping);
|
||||
|
||||
return proxy;
|
||||
}
|
||||
|
||||
protected void initActionContext(ActionContext actionContext) {
|
||||
actionContext.setParameters(HttpParameters.create(request.getParameterMap()).build());
|
||||
initSession(actionContext);
|
||||
// set the action context to the one used by the proxy
|
||||
ActionContext.bind(actionContext);
|
||||
}
|
||||
|
||||
protected void initSession(ActionContext actionContext) {
|
||||
if (actionContext.getSession() == null) {
|
||||
actionContext.withSession(new HashMap<>());
|
||||
request.setSession(new MockHttpSession(servletContext));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds an ActionMapping for a given request
|
||||
*/
|
||||
protected ActionMapping getActionMapping(HttpServletRequest request) {
|
||||
return container.getInstance(ActionMapper.class).getMapping(request, configurationManager);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds an ActionMapping for a given url
|
||||
*/
|
||||
protected ActionMapping getActionMapping(String url) {
|
||||
MockHttpServletRequest req = new MockHttpServletRequest();
|
||||
req.setRequestURI(url);
|
||||
return getActionMapping(req);
|
||||
}
|
||||
|
||||
/**
|
||||
* Injects dependencies on an Object using Struts internal IoC container
|
||||
*/
|
||||
protected void injectStrutsDependencies(Object object) {
|
||||
container.inject(object);
|
||||
}
|
||||
|
||||
protected void setupBeforeInitDispatcher() throws Exception {
|
||||
}
|
||||
|
||||
protected void initServletMockObjects() {
|
||||
servletContext = new MockServletContext(resourceLoader);
|
||||
response = new MockHttpServletResponse();
|
||||
request = new MockHttpServletRequest();
|
||||
pageContext = new MockPageContext(servletContext, request, response);
|
||||
}
|
||||
|
||||
public void finishExecution() {
|
||||
HttpSession session = this.request.getSession();
|
||||
Enumeration attributeNames = session.getAttributeNames();
|
||||
|
||||
MockHttpServletRequest nextRequest = new MockHttpServletRequest();
|
||||
|
||||
while (attributeNames.hasMoreElements()) {
|
||||
String key = (String) attributeNames.nextElement();
|
||||
Object attribute = session.getAttribute(key);
|
||||
nextRequest.getSession().setAttribute(key, attribute);
|
||||
}
|
||||
|
||||
this.response = new MockHttpServletResponse();
|
||||
this.request = nextRequest;
|
||||
this.pageContext = new MockPageContext(servletContext, request, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the configuration settings, XWork configuration, and
|
||||
* message resources
|
||||
*/
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
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<String, String> params) {
|
||||
dispatcher = StrutsTestCaseHelper.initDispatcher(servletContext, params);
|
||||
configurationManager = dispatcher.getConfigurationManager();
|
||||
configuration = configurationManager.getConfiguration();
|
||||
container = configuration.getContainer();
|
||||
container.inject(dispatcher);
|
||||
return dispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override this method to return a comma separated list of paths to a configuration
|
||||
* file.
|
||||
* <p>The default implementation simply returns <code>null</code>.
|
||||
* @return a comma separated list of config locations
|
||||
*/
|
||||
protected String getConfigPath() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
if (dispatcher != null && dispatcher.getConfigurationManager() != null) {
|
||||
dispatcher.cleanup();
|
||||
dispatcher = null;
|
||||
}
|
||||
StrutsTestCaseHelper.tearDown();
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* 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.junit;
|
||||
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.ActionProxy;
|
||||
import com.opensymphony.xwork2.ActionProxyFactory;
|
||||
import com.opensymphony.xwork2.config.Configuration;
|
||||
import com.opensymphony.xwork2.interceptor.ValidationAware;
|
||||
import com.opensymphony.xwork2.interceptor.annotations.After;
|
||||
import com.opensymphony.xwork2.interceptor.annotations.Before;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.struts2.ServletActionContext;
|
||||
import org.apache.struts2.dispatcher.Dispatcher;
|
||||
import org.apache.struts2.dispatcher.HttpParameters;
|
||||
import org.apache.struts2.dispatcher.mapper.ActionMapper;
|
||||
import org.apache.struts2.dispatcher.mapper.ActionMapping;
|
||||
import org.apache.struts2.util.StrutsTestCaseHelper;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.MockHttpSession;
|
||||
import org.springframework.mock.web.MockPageContext;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public abstract class StrutsJUnit4TestCase<T> extends XWorkJUnit4TestCase {
|
||||
|
||||
protected MockHttpServletResponse response;
|
||||
protected MockHttpServletRequest request;
|
||||
protected MockPageContext pageContext;
|
||||
protected MockServletContext servletContext;
|
||||
protected Map<String, String> dispatcherInitParams;
|
||||
protected Dispatcher dispatcher;
|
||||
|
||||
protected DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
|
||||
|
||||
/**
|
||||
* gets an object from the stack after an action is executed
|
||||
*/
|
||||
protected Object findValueAfterExecute(String key) {
|
||||
return ServletActionContext.getValueStack(request).findValue(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* gets an object from the stack after an action is executed
|
||||
*
|
||||
* @return The executed action
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected T getAction() {
|
||||
return (T) findValueAfterExecute("action");
|
||||
}
|
||||
|
||||
protected boolean containsErrors() {
|
||||
T action = this.getAction();
|
||||
if (action instanceof ValidationAware) {
|
||||
return ((ValidationAware) action).hasActionErrors();
|
||||
}
|
||||
throw new UnsupportedOperationException("Current action does not implement ValidationAware interface");
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes an action and returns it's output (not the result returned from
|
||||
* execute()), but the actual output that would be written to the response.
|
||||
* For this to work the configured result for the action needs to be
|
||||
* FreeMarker, or Velocity (JSPs can be used with the Embedded JSP plugin)
|
||||
*/
|
||||
protected String executeAction(String uri) throws ServletException, UnsupportedEncodingException {
|
||||
request.setRequestURI(uri);
|
||||
ActionMapping mapping = getActionMapping(request);
|
||||
|
||||
assertNotNull(mapping);
|
||||
Dispatcher.getInstance().serviceAction(request, response, mapping);
|
||||
|
||||
if (response.getStatus() != HttpServletResponse.SC_OK)
|
||||
throw new ServletException("Error code [" + response.getStatus() + "], Error: ["
|
||||
+ response.getErrorMessage() + "]");
|
||||
|
||||
return response.getContentAsString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an action proxy for a request, and sets parameters of the ActionInvocation to the passed
|
||||
* parameters. Make sure to set the request parameters in the protected "request" object before calling this method.
|
||||
*/
|
||||
protected ActionProxy getActionProxy(String uri) {
|
||||
request.setRequestURI(uri);
|
||||
ActionMapping mapping = getActionMapping(request);
|
||||
String namespace = mapping.getNamespace();
|
||||
String name = mapping.getName();
|
||||
String method = mapping.getMethod();
|
||||
|
||||
Configuration config = configurationManager.getConfiguration();
|
||||
ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
|
||||
namespace, name, method, new HashMap<String, Object>(), true, false);
|
||||
|
||||
initActionContext(proxy.getInvocation().getInvocationContext());
|
||||
|
||||
// this is normally done in onSetUp(), but we are using Struts internal
|
||||
// objects (proxy and action invocation)
|
||||
// so we have to hack around so it works
|
||||
ServletActionContext.setServletContext(servletContext);
|
||||
ServletActionContext.setRequest(request);
|
||||
ServletActionContext.setResponse(response);
|
||||
|
||||
ServletActionContext.getContext().put(ServletActionContext.ACTION_MAPPING, mapping);
|
||||
|
||||
return proxy;
|
||||
}
|
||||
|
||||
protected void initActionContext(ActionContext actionContext) {
|
||||
actionContext.setParameters(HttpParameters.create(request.getParameterMap()).build());
|
||||
initSession(actionContext);
|
||||
// set the action context to the one used by the proxy
|
||||
ActionContext.bind(actionContext);
|
||||
}
|
||||
|
||||
protected void initSession(ActionContext actionContext) {
|
||||
if (actionContext.getSession() == null) {
|
||||
actionContext.withSession(new HashMap<>());
|
||||
request.setSession(new MockHttpSession(servletContext));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds an ActionMapping for a given request
|
||||
*/
|
||||
protected ActionMapping getActionMapping(HttpServletRequest request) {
|
||||
return container.getInstance(ActionMapper.class).getMapping(request, configurationManager);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds an ActionMapping for a given url
|
||||
*/
|
||||
protected ActionMapping getActionMapping(String url) {
|
||||
MockHttpServletRequest req = new MockHttpServletRequest();
|
||||
req.setRequestURI(url);
|
||||
return getActionMapping(req);
|
||||
}
|
||||
|
||||
/**
|
||||
* Injects dependencies on an Object using Struts internal IoC container
|
||||
*/
|
||||
protected void injectStrutsDependencies(Object object) {
|
||||
container.inject(object);
|
||||
}
|
||||
|
||||
protected void setupBeforeInitDispatcher() throws Exception {
|
||||
}
|
||||
|
||||
protected void initServletMockObjects() {
|
||||
servletContext = new MockServletContext(resourceLoader);
|
||||
response = new MockHttpServletResponse();
|
||||
request = new MockHttpServletRequest();
|
||||
pageContext = new MockPageContext(servletContext, request, response);
|
||||
}
|
||||
|
||||
public void finishExecution() {
|
||||
HttpSession session = this.request.getSession();
|
||||
Enumeration attributeNames = session.getAttributeNames();
|
||||
|
||||
MockHttpServletRequest nextRequest = new MockHttpServletRequest();
|
||||
|
||||
while (attributeNames.hasMoreElements()) {
|
||||
String key = (String) attributeNames.nextElement();
|
||||
Object attribute = session.getAttribute(key);
|
||||
nextRequest.getSession().setAttribute(key, attribute);
|
||||
}
|
||||
|
||||
this.response = new MockHttpServletResponse();
|
||||
this.request = nextRequest;
|
||||
this.pageContext = new MockPageContext(servletContext, request, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the configuration settings, XWork configuration, and
|
||||
* message resources
|
||||
*/
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
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<String, String> params) {
|
||||
dispatcher = StrutsTestCaseHelper.initDispatcher(servletContext, params);
|
||||
configurationManager = dispatcher.getConfigurationManager();
|
||||
configuration = configurationManager.getConfiguration();
|
||||
container = configuration.getContainer();
|
||||
container.inject(dispatcher);
|
||||
return dispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override this method to return a comma separated list of paths to a configuration
|
||||
* file.
|
||||
* <p>The default implementation simply returns <code>null</code>.
|
||||
*
|
||||
* @return a comma separated list of config locations
|
||||
*/
|
||||
protected String getConfigPath() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
if (dispatcher != null && dispatcher.getConfigurationManager() != null) {
|
||||
dispatcher.cleanup();
|
||||
dispatcher = null;
|
||||
}
|
||||
StrutsTestCaseHelper.tearDown();
|
||||
}
|
||||
|
||||
}
|
||||
+7
-5
@@ -16,15 +16,13 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.struts2;
|
||||
package org.apache.struts2.junit;
|
||||
|
||||
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.portlet.PortletMode;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.struts2.StrutsStatics;
|
||||
import org.apache.struts2.mock.web.portlet.MockPortletContext;
|
||||
import org.apache.struts2.mock.web.portlet.MockPortletRequest;
|
||||
import org.apache.struts2.mock.web.portlet.MockPortletResponse;
|
||||
@@ -33,6 +31,10 @@ import org.apache.struts2.mock.web.portlet.MockStateAwareResponse;
|
||||
import org.apache.struts2.portlet.PortletConstants;
|
||||
import org.apache.struts2.portlet.PortletPhase;
|
||||
|
||||
import javax.portlet.PortletMode;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/*
|
||||
* Changes: This is a copy of org.apache.struts2.StrutsPortletTestCase from the Struts 2 portlet-plugin, moved
|
||||
* into the junit-plugin (same package org.apache.struts2).
|
||||
+2
-1
@@ -16,12 +16,13 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.struts2;
|
||||
package org.apache.struts2.junit;
|
||||
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.ActionProxy;
|
||||
import com.opensymphony.xwork2.ActionProxyFactory;
|
||||
import com.opensymphony.xwork2.config.Configuration;
|
||||
import org.apache.struts2.ServletActionContext;
|
||||
import org.apache.struts2.dispatcher.Dispatcher;
|
||||
import org.apache.struts2.dispatcher.HttpParameters;
|
||||
import org.apache.struts2.dispatcher.mapper.ActionMapping;
|
||||
+1
-1
@@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.struts2;
|
||||
package org.apache.struts2.junit;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
+47
-47
@@ -1,47 +1,47 @@
|
||||
/*
|
||||
* 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.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.support.GenericXmlContextLoader;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
/**
|
||||
* Base class for Spring JUnit actions
|
||||
*/
|
||||
public abstract class StrutsSpringTestCase extends StrutsTestCase {
|
||||
|
||||
private static final String DEFAULT_CONTEXT_LOCATION = "classpath*:applicationContext.xml";
|
||||
protected static ApplicationContext applicationContext;
|
||||
|
||||
protected void setupBeforeInitDispatcher() throws Exception {
|
||||
// only load beans from spring once
|
||||
if (applicationContext == null) {
|
||||
GenericXmlContextLoader xmlContextLoader = new GenericXmlContextLoader();
|
||||
applicationContext = xmlContextLoader.loadContext(getContextLocations());
|
||||
}
|
||||
|
||||
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);
|
||||
}
|
||||
|
||||
protected String[] getContextLocations() {
|
||||
return new String[] {DEFAULT_CONTEXT_LOCATION};
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* 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.junit;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.support.GenericXmlContextLoader;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
/**
|
||||
* Base class for Spring JUnit actions
|
||||
*/
|
||||
public abstract class StrutsSpringTestCase extends StrutsTestCase {
|
||||
|
||||
private static final String DEFAULT_CONTEXT_LOCATION = "classpath*:applicationContext.xml";
|
||||
protected static ApplicationContext applicationContext;
|
||||
|
||||
protected void setupBeforeInitDispatcher() throws Exception {
|
||||
// only load beans from spring once
|
||||
if (applicationContext == null) {
|
||||
GenericXmlContextLoader xmlContextLoader = new GenericXmlContextLoader();
|
||||
applicationContext = xmlContextLoader.loadContext(getContextLocations());
|
||||
}
|
||||
|
||||
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);
|
||||
}
|
||||
|
||||
protected String[] getContextLocations() {
|
||||
return new String[]{DEFAULT_CONTEXT_LOCATION};
|
||||
}
|
||||
|
||||
}
|
||||
+3
-2
@@ -16,13 +16,14 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.struts2;
|
||||
package org.apache.struts2.junit;
|
||||
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.ActionProxy;
|
||||
import com.opensymphony.xwork2.ActionProxyFactory;
|
||||
import com.opensymphony.xwork2.XWorkTestCase;
|
||||
import com.opensymphony.xwork2.config.Configuration;
|
||||
import org.apache.struts2.ServletActionContext;
|
||||
import org.apache.struts2.dispatcher.Dispatcher;
|
||||
import org.apache.struts2.dispatcher.HttpParameters;
|
||||
import org.apache.struts2.dispatcher.mapper.ActionMapper;
|
||||
@@ -94,7 +95,7 @@ public abstract class StrutsTestCase extends XWorkTestCase {
|
||||
|
||||
Configuration config = configurationManager.getConfiguration();
|
||||
ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
|
||||
namespace, name, method, new HashMap<String, Object>(), true, false);
|
||||
namespace, name, method, new HashMap<String, Object>(), true, false);
|
||||
|
||||
initActionContext(proxy.getInvocation().getInvocationContext());
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* 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.junit;
|
||||
|
||||
import com.opensymphony.xwork2.ActionProxyFactory;
|
||||
import com.opensymphony.xwork2.config.Configuration;
|
||||
import com.opensymphony.xwork2.config.ConfigurationException;
|
||||
import com.opensymphony.xwork2.config.ConfigurationManager;
|
||||
import com.opensymphony.xwork2.config.ConfigurationProvider;
|
||||
import com.opensymphony.xwork2.inject.Container;
|
||||
import com.opensymphony.xwork2.inject.ContainerBuilder;
|
||||
import com.opensymphony.xwork2.inject.Context;
|
||||
import com.opensymphony.xwork2.inject.Factory;
|
||||
import com.opensymphony.xwork2.inject.Scope;
|
||||
import com.opensymphony.xwork2.test.StubConfigurationProvider;
|
||||
import com.opensymphony.xwork2.util.XWorkTestCaseHelper;
|
||||
import com.opensymphony.xwork2.util.location.LocatableProperties;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
public abstract class XWorkJUnit4TestCase {
|
||||
|
||||
protected ConfigurationManager configurationManager;
|
||||
protected Configuration configuration;
|
||||
protected Container container;
|
||||
protected ActionProxyFactory actionProxyFactory;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
configurationManager = XWorkTestCaseHelper.setUp();
|
||||
configuration = configurationManager.getConfiguration();
|
||||
container = configuration.getContainer();
|
||||
actionProxyFactory = container.getInstance(ActionProxyFactory.class);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
XWorkTestCaseHelper.tearDown(configurationManager);
|
||||
configurationManager = null;
|
||||
configuration = null;
|
||||
container = null;
|
||||
actionProxyFactory = null;
|
||||
}
|
||||
|
||||
protected void loadConfigurationProviders(ConfigurationProvider... providers) {
|
||||
configurationManager = XWorkTestCaseHelper.loadConfigurationProviders(configurationManager, providers);
|
||||
configuration = configurationManager.getConfiguration();
|
||||
container = configuration.getContainer();
|
||||
actionProxyFactory = container.getInstance(ActionProxyFactory.class);
|
||||
}
|
||||
|
||||
protected void loadButAdd(final Class<?> type, final Object impl) {
|
||||
loadButAdd(type, Container.DEFAULT_NAME, impl);
|
||||
}
|
||||
|
||||
protected void loadButAdd(final Class<?> type, final String name, final Object impl) {
|
||||
loadConfigurationProviders(new StubConfigurationProvider() {
|
||||
@Override
|
||||
public void register(ContainerBuilder builder,
|
||||
LocatableProperties props) throws ConfigurationException {
|
||||
builder.factory(type, name, new Factory() {
|
||||
public Object create(Context context) throws Exception {
|
||||
return impl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class type() {
|
||||
return impl.getClass();
|
||||
}
|
||||
}, Scope.SINGLETON);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.struts2.util;
|
||||
package org.apache.struts2.junit.util;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
+39
-39
@@ -1,39 +1,39 @@
|
||||
/*
|
||||
* 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 com.opensymphony.xwork2.ActionSupport;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
public class JUnitTestAction extends ActionSupport {
|
||||
private static final long serialVersionUID = 1629266238339053546L;
|
||||
|
||||
private String name;
|
||||
|
||||
@Autowired
|
||||
private MySessionBean mySessionBean;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 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.junit;
|
||||
|
||||
import com.opensymphony.xwork2.ActionSupport;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
public class JUnitTestAction extends ActionSupport {
|
||||
private static final long serialVersionUID = 1629266238339053546L;
|
||||
|
||||
private String name;
|
||||
|
||||
@Autowired
|
||||
private MySessionBean mySessionBean;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.struts2;
|
||||
package org.apache.struts2.junit;
|
||||
|
||||
public class MySessionBean {
|
||||
|
||||
+3
-3
@@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.struts2;
|
||||
package org.apache.struts2.junit;
|
||||
|
||||
import com.opensymphony.xwork2.ActionProxy;
|
||||
import org.junit.Assert;
|
||||
@@ -27,7 +27,7 @@ import org.junit.Test;
|
||||
* Date: 8/15/11
|
||||
* Time: 7:04 PM
|
||||
*/
|
||||
public class StrutsJUnit4TestCaseTest extends StrutsJUnit4TestCase<JUnitTestAction>{
|
||||
public class StrutsJUnit4TestCaseTest extends StrutsJUnit4TestCase<JUnitTestAction> {
|
||||
@Test
|
||||
public void testExecuteActionAgainstCustomStrutsConfigFile() throws Exception {
|
||||
String output = executeAction("/test/testAction-2.action");
|
||||
@@ -38,7 +38,7 @@ public class StrutsJUnit4TestCaseTest extends StrutsJUnit4TestCase<JUnitTestActi
|
||||
public void testSessionInitialized() throws Exception {
|
||||
ActionProxy proxy = getActionProxy("/test/testAction-2.action");
|
||||
Assert.assertNotNull("invocation session should being initialized",
|
||||
proxy.getInvocation().getInvocationContext().getSession());
|
||||
proxy.getInvocation().getInvocationContext().getSession());
|
||||
}
|
||||
|
||||
@Override
|
||||
+8
-8
@@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.struts2;
|
||||
package org.apache.struts2.junit;
|
||||
|
||||
import com.opensymphony.xwork2.Action;
|
||||
import com.opensymphony.xwork2.ActionProxy;
|
||||
@@ -30,11 +30,11 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import javax.servlet.ServletException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={"classpath*:applicationContext.xml"})
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = {"classpath*:applicationContext.xml"})
|
||||
public class StrutsSpringJUnit4TestCaseTest extends StrutsSpringJUnit4TestCase<JUnitTestAction> {
|
||||
|
||||
@Test
|
||||
@Test
|
||||
public void getActionMapping() {
|
||||
ActionMapping mapping = getActionMapping("/test/testAction.action");
|
||||
Assert.assertNotNull(mapping);
|
||||
@@ -42,11 +42,11 @@ public class StrutsSpringJUnit4TestCaseTest extends StrutsSpringJUnit4TestCase<J
|
||||
Assert.assertEquals("testAction", mapping.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test
|
||||
public void getActionProxy() throws Exception {
|
||||
//set parameters before calling getActionProxy
|
||||
request.setParameter("name", "FD");
|
||||
|
||||
|
||||
ActionProxy proxy = getActionProxy("/test/testAction.action");
|
||||
Assert.assertNotNull(proxy);
|
||||
|
||||
@@ -58,13 +58,13 @@ public class StrutsSpringJUnit4TestCaseTest extends StrutsSpringJUnit4TestCase<J
|
||||
Assert.assertEquals("FD", action.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test
|
||||
public void executeAction() throws ServletException, UnsupportedEncodingException {
|
||||
String output = executeAction("/test/testAction.action");
|
||||
Assert.assertEquals("Hello", output);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test
|
||||
public void getValueFromStack() throws ServletException, UnsupportedEncodingException {
|
||||
request.setParameter("name", "FD");
|
||||
executeAction("/test/testAction.action");
|
||||
+27
-27
@@ -1,27 +1,27 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
public class StrutsSpringTestCaseTest extends StrutsSpringTestCase {
|
||||
public void testApplicationContext() {
|
||||
assertNotNull(applicationContext);
|
||||
JUnitTestAction action = (JUnitTestAction) applicationContext.getBean("testAction");
|
||||
assertNotNull(action);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 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.junit;
|
||||
|
||||
public class StrutsSpringTestCaseTest extends StrutsSpringTestCase {
|
||||
public void testApplicationContext() {
|
||||
assertNotNull(applicationContext);
|
||||
JUnitTestAction action = (JUnitTestAction) applicationContext.getBean("testAction");
|
||||
assertNotNull(action);
|
||||
}
|
||||
}
|
||||
+101
-100
@@ -1,100 +1,101 @@
|
||||
/*
|
||||
* 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 com.opensymphony.xwork2.Action;
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.ActionProxy;
|
||||
import org.apache.struts2.dispatcher.mapper.ActionMapping;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.portlet.PortletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
public class StrutsTestCaseTest extends StrutsSpringTestCase {
|
||||
|
||||
public void testGetActionMapping() {
|
||||
ActionMapping mapping = getActionMapping("/test/testAction.action");
|
||||
assertNotNull(mapping);
|
||||
assertEquals("/test", mapping.getNamespace());
|
||||
assertEquals("testAction", mapping.getName());
|
||||
}
|
||||
|
||||
public void testGetActionProxy() throws Exception {
|
||||
//set parameters before calling getActionProxy
|
||||
request.setParameter("name", "FD");
|
||||
|
||||
ActionProxy proxy = getActionProxy("/test/testAction.action");
|
||||
assertNotNull(proxy);
|
||||
|
||||
JUnitTestAction action = (JUnitTestAction) proxy.getAction();
|
||||
assertNotNull(action);
|
||||
|
||||
String result = proxy.execute();
|
||||
assertEquals(Action.SUCCESS, result);
|
||||
assertEquals("FD", action.getName());
|
||||
}
|
||||
|
||||
public void testExecuteAction() throws ServletException, UnsupportedEncodingException {
|
||||
String output = executeAction("/test/testAction.action");
|
||||
assertEquals("Hello", output);
|
||||
}
|
||||
|
||||
public void testGetValueFromStack() throws ServletException, UnsupportedEncodingException {
|
||||
request.setParameter("name", "FD");
|
||||
executeAction("/test/testAction.action");
|
||||
String name = (String) findValueAfterExecute("name");
|
||||
assertEquals("FD", name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPortletContextBeAvailable() throws Exception {
|
||||
// given
|
||||
assertNull(ActionContext.getContext().get(StrutsStatics.STRUTS_PORTLET_CONTEXT));
|
||||
|
||||
// when
|
||||
String output = executeAction("/test/testAction.action");
|
||||
assertEquals("Hello", output);
|
||||
|
||||
// then
|
||||
Object portletContext = ActionContext.getContext().get(StrutsStatics.STRUTS_PORTLET_CONTEXT);
|
||||
assertNotNull(portletContext);
|
||||
assertTrue(portletContext instanceof PortletContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldAdditionalContextParamsBeAvailable() throws Exception {
|
||||
// given
|
||||
String key = "my-param";
|
||||
assertNull(ActionContext.getContext().get(key));
|
||||
|
||||
// when
|
||||
String output = executeAction("/test/testAction.action");
|
||||
assertEquals("Hello", output);
|
||||
|
||||
// then
|
||||
assertNotNull(ActionContext.getContext().get(key));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void applyAdditionalParams(ActionContext context) {
|
||||
context.put("my-param", new Object());
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 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.junit;
|
||||
|
||||
import com.opensymphony.xwork2.Action;
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.ActionProxy;
|
||||
import org.apache.struts2.StrutsStatics;
|
||||
import org.apache.struts2.dispatcher.mapper.ActionMapping;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.portlet.PortletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
public class StrutsTestCaseTest extends StrutsSpringTestCase {
|
||||
|
||||
public void testGetActionMapping() {
|
||||
ActionMapping mapping = getActionMapping("/test/testAction.action");
|
||||
assertNotNull(mapping);
|
||||
assertEquals("/test", mapping.getNamespace());
|
||||
assertEquals("testAction", mapping.getName());
|
||||
}
|
||||
|
||||
public void testGetActionProxy() throws Exception {
|
||||
//set parameters before calling getActionProxy
|
||||
request.setParameter("name", "FD");
|
||||
|
||||
ActionProxy proxy = getActionProxy("/test/testAction.action");
|
||||
assertNotNull(proxy);
|
||||
|
||||
JUnitTestAction action = (JUnitTestAction) proxy.getAction();
|
||||
assertNotNull(action);
|
||||
|
||||
String result = proxy.execute();
|
||||
assertEquals(Action.SUCCESS, result);
|
||||
assertEquals("FD", action.getName());
|
||||
}
|
||||
|
||||
public void testExecuteAction() throws ServletException, UnsupportedEncodingException {
|
||||
String output = executeAction("/test/testAction.action");
|
||||
assertEquals("Hello", output);
|
||||
}
|
||||
|
||||
public void testGetValueFromStack() throws ServletException, UnsupportedEncodingException {
|
||||
request.setParameter("name", "FD");
|
||||
executeAction("/test/testAction.action");
|
||||
String name = (String) findValueAfterExecute("name");
|
||||
assertEquals("FD", name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPortletContextBeAvailable() throws Exception {
|
||||
// given
|
||||
assertNull(ActionContext.getContext().get(StrutsStatics.STRUTS_PORTLET_CONTEXT));
|
||||
|
||||
// when
|
||||
String output = executeAction("/test/testAction.action");
|
||||
assertEquals("Hello", output);
|
||||
|
||||
// then
|
||||
Object portletContext = ActionContext.getContext().get(StrutsStatics.STRUTS_PORTLET_CONTEXT);
|
||||
assertNotNull(portletContext);
|
||||
assertTrue(portletContext instanceof PortletContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldAdditionalContextParamsBeAvailable() throws Exception {
|
||||
// given
|
||||
String key = "my-param";
|
||||
assertNull(ActionContext.getContext().get(key));
|
||||
|
||||
// when
|
||||
String output = executeAction("/test/testAction.action");
|
||||
assertEquals("Hello", output);
|
||||
|
||||
// then
|
||||
assertNotNull(ActionContext.getContext().get(key));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void applyAdditionalParams(ActionContext context) {
|
||||
context.put("my-param", new Object());
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -16,10 +16,10 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.struts2.convention;
|
||||
package org.apache.struts2.junit.convention;
|
||||
|
||||
import actions.ViewAction;
|
||||
import org.apache.struts2.StrutsJUnit4TestCase;
|
||||
import org.apache.struts2.junit.StrutsJUnit4TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
+1
-1
@@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.struts2.session;
|
||||
package org.apache.struts2.junit.session;
|
||||
|
||||
import com.opensymphony.xwork2.ActionSupport;
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.struts2.session;
|
||||
package org.apache.struts2.junit.session;
|
||||
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.ActionSupport;
|
||||
+5
-5
@@ -16,22 +16,22 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.struts2.session;
|
||||
package org.apache.struts2.junit.session;
|
||||
|
||||
import org.apache.struts2.JUnitTestAction;
|
||||
import org.apache.struts2.StrutsJUnit4TestCase;
|
||||
import org.apache.struts2.junit.JUnitTestAction;
|
||||
import org.apache.struts2.junit.StrutsJUnit4TestCase;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Calls SessionSet which sets a value into the session and afterwards calls SessionGet, which
|
||||
* should return this session value.
|
||||
*
|
||||
* <p>
|
||||
* In prior versions only one executeAction() call could happen in a single test case, because
|
||||
* either the session values were deleted or the wrong result would be returned (always the result of
|
||||
* the first action execution).
|
||||
*/
|
||||
public class StrutsJUnit4SessionTestCaseTest extends StrutsJUnit4TestCase<JUnitTestAction>{
|
||||
public class StrutsJUnit4SessionTestCaseTest extends StrutsJUnit4TestCase<JUnitTestAction> {
|
||||
@Test
|
||||
public void testPersistingSessionValues() throws Exception {
|
||||
String output = executeAction("/sessiontest/sessionSet.action");
|
||||
@@ -33,6 +33,6 @@
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="testAction" class="org.apache.struts2.JUnitTestAction"/>
|
||||
<bean id="mySessionBean" class="org.apache.struts2.MySessionBean" scope="session" />
|
||||
</beans>
|
||||
<bean id="testAction" class="org.apache.struts2.junit.JUnitTestAction"/>
|
||||
<bean id="mySessionBean" class="org.apache.struts2.junit.MySessionBean" scope="session"/>
|
||||
</beans>
|
||||
|
||||
@@ -21,15 +21,15 @@
|
||||
-->
|
||||
|
||||
<!DOCTYPE struts PUBLIC
|
||||
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
|
||||
"http://struts.apache.org/dtds/struts-2.1.dtd">
|
||||
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
|
||||
"http://struts.apache.org/dtds/struts-2.1.dtd">
|
||||
|
||||
<struts>
|
||||
<package name="test" namespace="/sessiontest" extends="struts-default">
|
||||
<action name="sessionSet" class="org.apache.struts2.session.SessionSetAction">
|
||||
<action name="sessionSet" class="org.apache.struts2.junit.session.SessionSetAction">
|
||||
<result type="freemarker">/template-session.ftl</result>
|
||||
</action>
|
||||
<action name="sessionGet" class="org.apache.struts2.session.SessionGetAction">
|
||||
<action name="sessionGet" class="org.apache.struts2.junit.session.SessionGetAction">
|
||||
<result type="freemarker">/template-session.ftl</result>
|
||||
</action>
|
||||
</package>
|
||||
|
||||
@@ -20,12 +20,12 @@
|
||||
*/
|
||||
-->
|
||||
<!DOCTYPE struts PUBLIC
|
||||
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
|
||||
"http://struts.apache.org/dtds/struts-2.1.dtd">
|
||||
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
|
||||
"http://struts.apache.org/dtds/struts-2.1.dtd">
|
||||
|
||||
<struts>
|
||||
<package name="test" namespace="/test" extends="struts-default">
|
||||
<action name="testAction-2" class="org.apache.struts2.JUnitTestAction">
|
||||
<action name="testAction-2" class="org.apache.struts2.junit.JUnitTestAction">
|
||||
<result type="freemarker">/template-2.ftl</result>
|
||||
</action>
|
||||
</package>
|
||||
|
||||
@@ -20,14 +20,14 @@
|
||||
*/
|
||||
-->
|
||||
<!DOCTYPE struts PUBLIC
|
||||
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
|
||||
"http://struts.apache.org/dtds/struts-2.1.dtd">
|
||||
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
|
||||
"http://struts.apache.org/dtds/struts-2.1.dtd">
|
||||
|
||||
<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.junit.JUnitTestAction">
|
||||
<result type="freemarker">/template-1.ftl</result>
|
||||
</action>
|
||||
</package>
|
||||
</struts>
|
||||
</struts>
|
||||
|
||||
Reference in New Issue
Block a user