use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.
the class RestActionInvocationTest method testDefaultErrorResult.
/**
* Test the default error result.
* @throws Exception
*/
public void testDefaultErrorResult() throws Exception {
// Exception
Exception e = new Exception();
restActionInvocation.getStack().set("exception", e);
request.setMethod("GET");
RestAction restAction = (RestAction) restActionInvocation.getAction();
List<String> model = new ArrayList<String>();
model.add("Item");
restAction.model = model;
restActionInvocation.setDefaultErrorResultName("default-error");
ResultConfig resultConfig = new ResultConfig.Builder("default-error", "org.apache.struts2.result.HttpHeaderResult").addParam("status", "123").build();
ActionConfig actionConfig = new ActionConfig.Builder("org.apache.rest", "RestAction", "org.apache.rest.RestAction").addResultConfig(resultConfig).build();
((MockActionProxy) restActionInvocation.getProxy()).setConfig(actionConfig);
restActionInvocation.processResult();
assertEquals(123, response.getStatus());
}
use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.
the class FreeMarkerResultTest method setUp.
protected void setUp() throws Exception {
super.setUp();
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();
response.setWriter(writer);
request = new MockHttpServletRequest();
servletContext = new StrutsMockServletContext();
stack = ActionContext.getContext().getValueStack();
context = ActionContext.of(stack.getContext()).withServletResponse(response).withServletRequest(request).withServletContext(servletContext).bind();
servletContext.setAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY, null);
invocation = new MockActionInvocation();
invocation.setStack(stack);
invocation.setInvocationContext(context);
invocation.setProxy(new MockActionProxy());
servletContext.setRealPath(new File(FreeMarkerResultTest.class.getResource("someFreeMarkerFile.ftl").toURI()).toURL().getFile());
}
use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.
the class PostbackResultTest method testWithNoNamespace.
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();
}
use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.
the class ServletActionRedirectResultTest method testIncludeParameterInResultWithConditionParseOn.
public void testIncludeParameterInResultWithConditionParseOn() 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.setNamespace("/myNamespace${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);
expect(mockInvocation.getResultCode()).andReturn("myResult");
expect(mockActionProxy.getConfig()).andReturn(actionConfig);
expect(mockInvocation.getInvocationContext()).andReturn(context);
expect(mockInvocation.getStack()).andReturn(stack).anyTimes();
control.replay();
result.setActionMapper(container.getInstance(ActionMapper.class));
result.execute(mockInvocation);
assertEquals("/myNamespace0/myAction0.action?param1=value+1¶m2=value+2¶m3=value+3#fragment", res.getRedirectedUrl());
control.verify();
}
use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.
the class ServletActionRedirectResultTest method testIncludeParameterInResult.
public void testIncludeParameterInResult() 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("param1", "value 1").addParam("param2", "value 2").addParam("param3", "value 3").addParam("anchor", "fragment").build();
ActionContext context = ActionContext.getContext();
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");
result.setNamespace("/myNamespace");
result.setParse(false);
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);
expect(mockInvocation.getResultCode()).andReturn("myResult");
expect(mockActionProxy.getConfig()).andReturn(actionConfig);
expect(mockInvocation.getInvocationContext()).andReturn(context);
control.replay();
result.setActionMapper(container.getInstance(ActionMapper.class));
result.execute(mockInvocation);
assertEquals("/myNamespace/myAction.action?param1=value+1¶m2=value+2¶m3=value+3#fragment", res.getRedirectedUrl());
control.verify();
}
Aggregations