increase test coverage

This commit is contained in:
Yasser Zamani
2018-10-06 17:31:27 +03:30
parent 13d9053050
commit 53825df9fb
4 changed files with 229 additions and 20 deletions
@@ -72,6 +72,41 @@ public class ChainResultTest extends XWorkTestCase {
}
}
public void testWithNoNamespace() throws Exception {
ActionChainResult result = new ActionChainResult();
result.setActionName("${actionName}");
String expectedActionName = "testActionName";
String expectedNamespace = "${1-1}";
Map<String, Object> values = new HashMap<>();
values.put("actionName", expectedActionName);
ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(values);
Mock actionProxyMock = new Mock(ActionProxy.class);
actionProxyMock.expect("execute");
actionProxyMock.expectAndReturn("getNamespace", expectedNamespace);
actionProxyMock.expectAndReturn("getActionName", expectedActionName);
actionProxyMock.expectAndReturn("getMethod", null);
ActionProxy actionProxy = (ActionProxy) actionProxyMock.proxy();
ActionProxyFactory testActionProxyFactory = new NamespaceActionNameTestActionProxyFactory(expectedNamespace, expectedActionName, actionProxy);
result.setActionProxyFactory(testActionProxyFactory);
Mock invocationMock = new Mock(ActionInvocation.class);
invocationMock.matchAndReturn("getProxy", actionProxy);
try {
ActionContext testContext = new ActionContext(stack.getContext());
ActionContext.setContext(testContext);
result.execute((ActionInvocation) invocationMock.proxy());
actionProxyMock.verify();
} finally {
ActionContext.setContext(null);
}
}
public void testRecursiveChain() throws Exception {
ActionProxy proxy = actionProxyFactory.createActionProxy("", "InfiniteRecursionChain", null, null);
@@ -88,7 +123,7 @@ public class ChainResultTest extends XWorkTestCase {
private String expectedActionName;
private String expectedNamespace;
public NamespaceActionNameTestActionProxyFactory(String expectedNamespace, String expectedActionName, ActionProxy returnVal) {
NamespaceActionNameTestActionProxyFactory(String expectedNamespace, String expectedActionName, ActionProxy returnVal) {
this.expectedNamespace = expectedNamespace;
this.expectedActionName = expectedActionName;
this.returnVal = returnVal;
@@ -0,0 +1,96 @@
/*
* 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.result;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsInternalTestCase;
import org.apache.struts2.dispatcher.mapper.ActionMapper;
import org.easymock.IMocksControl;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import static org.easymock.EasyMock.createControl;
import static org.easymock.EasyMock.expect;
public class PostbackResultTest extends StrutsInternalTestCase {
public void testWithNoNamespace() throws Exception {
ActionContext context = ActionContext.getContext();
ValueStack stack = context.getValueStack();
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse res = new MockHttpServletResponse();
context.put(ServletActionContext.HTTP_REQUEST, req);
context.put(ServletActionContext.HTTP_RESPONSE, res);
PostbackResult result = new PostbackResult();
result.setActionName("myAction${1-1}");
result.setPrependServletContext(false);
IMocksControl control = createControl();
ActionProxy mockActionProxy = control.createMock(ActionProxy.class);
ActionInvocation mockInvocation = control.createMock(ActionInvocation.class);
expect(mockInvocation.getInvocationContext()).andReturn(context).anyTimes();
expect(mockInvocation.getStack()).andReturn(stack).anyTimes();
expect(mockInvocation.getProxy()).andReturn(mockActionProxy);
expect(mockActionProxy.getNamespace()).andReturn("${1-1}");
control.replay();
result.setActionMapper(container.getInstance(ActionMapper.class));
result.execute(mockInvocation);
assertEquals("<!DOCTYPE html><html><body><form action=\"${1-1}/myAction0.action\" method=\"POST\">" +
"<script>setTimeout(function(){document.forms[0].submit();},0);</script></html>", res.getContentAsString());
control.verify();
}
public void testWithNamespace() throws Exception {
ActionContext context = ActionContext.getContext();
ValueStack stack = context.getValueStack();
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse res = new MockHttpServletResponse();
context.put(ServletActionContext.HTTP_REQUEST, req);
context.put(ServletActionContext.HTTP_RESPONSE, res);
PostbackResult result = new PostbackResult();
result.setActionName("myAction${1-1}");
result.setNamespace("myNamespace${1-1}");
result.setPrependServletContext(false);
IMocksControl control = createControl();
ActionInvocation mockInvocation = control.createMock(ActionInvocation.class);
expect(mockInvocation.getInvocationContext()).andReturn(context).anyTimes();
expect(mockInvocation.getStack()).andReturn(stack).anyTimes();
control.replay();
result.setActionMapper(container.getInstance(ActionMapper.class));
result.execute(mockInvocation);
assertEquals("<!DOCTYPE html><html><body><form action=\"myNamespace0/myAction0.action\" method=\"POST\">" +
"<script>setTimeout(function(){document.forms[0].submit();},0);</script></html>", res.getContentAsString());
control.verify();
}
}
@@ -28,7 +28,6 @@ import com.opensymphony.xwork2.util.ValueStack;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsInternalTestCase;
import org.apache.struts2.dispatcher.mapper.ActionMapper;
import org.apache.struts2.result.ServletActionRedirectResult;
import org.apache.struts2.views.util.DefaultUrlHelper;
import org.easymock.IMocksControl;
import org.springframework.mock.web.MockHttpServletRequest;
@@ -41,9 +40,6 @@ import static org.easymock.EasyMock.createControl;
import static org.easymock.EasyMock.expect;
/**
* @version $Date$ $Id$
*/
public class ServletActionRedirectResultTest extends StrutsInternalTestCase {
public void testIncludeParameterInResultWithConditionParseOn() throws Exception {
@@ -76,15 +72,15 @@ public class ServletActionRedirectResultTest extends StrutsInternalTestCase {
context.put(ServletActionContext.HTTP_RESPONSE, res);
Map<String, ResultConfig> results= new HashMap<String, ResultConfig>();
Map<String, ResultConfig> results= new HashMap<>();
results.put("myResult", resultConfig);
ActionConfig actionConfig = new ActionConfig.Builder("", "", "")
.addResultConfigs(results).build();
ServletActionRedirectResult result = new ServletActionRedirectResult();
result.setActionName("myAction");
result.setNamespace("/myNamespace");
result.setActionName("myAction${1-1}");
result.setNamespace("/myNamespace${1-1}");
result.setParse(true);
result.setEncode(false);
result.setPrependServletContext(false);
@@ -103,7 +99,69 @@ public class ServletActionRedirectResultTest extends StrutsInternalTestCase {
control.replay();
result.setActionMapper(container.getInstance(ActionMapper.class));
result.execute(mockInvocation);
assertEquals("/myNamespace/myAction.action?param1=value+1&param2=value+2&param3=value+3#fragment", res.getRedirectedUrl());
assertEquals("/myNamespace0/myAction0.action?param1=value+1&param2=value+2&param3=value+3#fragment", res.getRedirectedUrl());
control.verify();
}
public void testIncludeParameterInResultWithConditionParseOnWithNoNamespace() throws Exception {
ResultConfig resultConfig = new ResultConfig.Builder("", "")
.addParam("actionName", "someActionName")
.addParam("namespace", "someNamespace")
.addParam("encode", "true")
.addParam("parse", "true")
.addParam("location", "someLocation")
.addParam("prependServletContext", "true")
.addParam("method", "someMethod")
.addParam("statusCode", "333")
.addParam("param1", "${#value1}")
.addParam("param2", "${#value2}")
.addParam("param3", "${#value3}")
.addParam("anchor", "${#fragment}")
.build();
ActionContext context = ActionContext.getContext();
ValueStack stack = context.getValueStack();
context.getContextMap().put("value1", "value 1");
context.getContextMap().put("value2", "value 2");
context.getContextMap().put("value3", "value 3");
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse res = new MockHttpServletResponse();
context.put(ServletActionContext.HTTP_REQUEST, req);
context.put(ServletActionContext.HTTP_RESPONSE, res);
Map<String, ResultConfig> results= new HashMap<>();
results.put("myResult", resultConfig);
ActionConfig actionConfig = new ActionConfig.Builder("", "", "")
.addResultConfigs(results).build();
ServletActionRedirectResult result = new ServletActionRedirectResult();
result.setActionName("myAction${1-1}");
result.setParse(true);
result.setEncode(false);
result.setPrependServletContext(false);
result.setAnchor("fragment");
result.setUrlHelper(new DefaultUrlHelper());
IMocksControl control = createControl();
ActionProxy mockActionProxy = control.createMock(ActionProxy.class);
ActionInvocation mockInvocation = control.createMock(ActionInvocation.class);
expect(mockInvocation.getProxy()).andReturn(mockActionProxy).times(2);
expect(mockInvocation.getResultCode()).andReturn("myResult");
expect(mockActionProxy.getConfig()).andReturn(actionConfig);
expect(mockActionProxy.getNamespace()).andReturn("${1-1}");
expect(mockInvocation.getInvocationContext()).andReturn(context);
expect(mockInvocation.getStack()).andReturn(stack).anyTimes();
control.replay();
result.setActionMapper(container.getInstance(ActionMapper.class));
result.execute(mockInvocation);
assertEquals("/${1-1}/myAction0.action?param1=value+1&param2=value+2&param3=value+3#fragment", res.getRedirectedUrl());
control.verify();
}
@@ -131,7 +189,7 @@ public class ServletActionRedirectResultTest extends StrutsInternalTestCase {
context.put(ServletActionContext.HTTP_RESPONSE, res);
Map<String, ResultConfig> results= new HashMap<String, ResultConfig>();
Map<String, ResultConfig> results= new HashMap<>();
results.put("myResult", resultConfig);
ActionConfig actionConfig = new ActionConfig.Builder("", "", "")
@@ -18,17 +18,16 @@
*/
package org.apache.struts2.result;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.struts2.StrutsInternalTestCase;
import org.apache.struts2.result.StrutsResultSupport;
import org.easymock.EasyMock;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.struts2.StrutsInternalTestCase;
import org.easymock.EasyMock;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Test case for StrutsResultSupport.
@@ -61,6 +60,27 @@ public class StrutsResultSupportTest extends StrutsInternalTestCase {
EasyMock.verify(mockActionInvocation);
}
public void testParseButNotParseLocation() throws Exception {
ValueStack stack = ActionContext.getContext().getValueStack();
ActionInvocation mockActionInvocation = EasyMock.createNiceMock(ActionInvocation.class);
mockActionInvocation.getStack();
EasyMock.expectLastCall().andReturn(stack).anyTimes();
EasyMock.replay(mockActionInvocation);
InternalStrutsResultSupport result = new InternalStrutsResultSupport();
result.setParse(true);
result.setEncode(false);
result.parseLocation = false;
result.setLocation("${1-1}");
result.execute(mockActionInvocation);
assertNotNull(result.getInternalLocation());
assertEquals("${1-1}", result.getInternalLocation());
EasyMock.verify(mockActionInvocation);
}
public void testParseAndEncode() throws Exception {
ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(new ActionSupport() {
@@ -110,7 +130,7 @@ public class StrutsResultSupportTest extends StrutsInternalTestCase {
EasyMock.verify(mockActionInvocation);
}
public void testConditionalParseCollection() throws Exception {
public void testConditionalParseCollection() {
ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(new ActionSupport() {
public List<String> getList() {
@@ -142,11 +162,11 @@ public class StrutsResultSupportTest extends StrutsInternalTestCase {
public static class InternalStrutsResultSupport extends StrutsResultSupport {
private String _internalLocation = null;
protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
protected void doExecute(String finalLocation, ActionInvocation invocation) {
_internalLocation = finalLocation;
}
public String getInternalLocation() {
String getInternalLocation() {
return _internalLocation;
}
}