Search in sources :

Example 36 with MockActionProxy

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());
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ArrayList(java.util.ArrayList) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) MockActionProxy(com.opensymphony.xwork2.mock.MockActionProxy)

Example 37 with MockActionProxy

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());
}
Also used : DefaultFileManagerFactory(com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory) StringWriter(java.io.StringWriter) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) StrutsMockHttpServletResponse(org.apache.struts2.views.jsp.StrutsMockHttpServletResponse) File(java.io.File) StrutsMockServletContext(org.apache.struts2.views.jsp.StrutsMockServletContext) MockActionProxy(com.opensymphony.xwork2.mock.MockActionProxy) PrintWriter(java.io.PrintWriter)

Example 38 with MockActionProxy

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();
}
Also used : IMocksControl(org.easymock.IMocksControl) ActionMapper(org.apache.struts2.dispatcher.mapper.ActionMapper) ActionProxy(com.opensymphony.xwork2.ActionProxy) ValueStack(com.opensymphony.xwork2.util.ValueStack) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ServletActionContext(org.apache.struts2.ServletActionContext) ActionContext(com.opensymphony.xwork2.ActionContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 39 with MockActionProxy

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&param2=value+2&param3=value+3#fragment", res.getRedirectedUrl());
    control.verify();
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ActionProxy(com.opensymphony.xwork2.ActionProxy) ValueStack(com.opensymphony.xwork2.util.ValueStack) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ServletActionContext(org.apache.struts2.ServletActionContext) ActionContext(com.opensymphony.xwork2.ActionContext) IMocksControl(org.easymock.IMocksControl) ActionMapper(org.apache.struts2.dispatcher.mapper.ActionMapper) DefaultUrlHelper(org.apache.struts2.views.util.DefaultUrlHelper) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 40 with MockActionProxy

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&param2=value+2&param3=value+3#fragment", res.getRedirectedUrl());
    control.verify();
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ServletActionContext(org.apache.struts2.ServletActionContext) ActionContext(com.opensymphony.xwork2.ActionContext) IMocksControl(org.easymock.IMocksControl) ActionMapper(org.apache.struts2.dispatcher.mapper.ActionMapper) DefaultUrlHelper(org.apache.struts2.views.util.DefaultUrlHelper) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Aggregations

MockActionProxy (com.opensymphony.xwork2.mock.MockActionProxy)43 MockActionInvocation (com.opensymphony.xwork2.mock.MockActionInvocation)34 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)29 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)19 ActionProxy (com.opensymphony.xwork2.ActionProxy)17 HashMap (java.util.HashMap)15 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)13 ActionContext (com.opensymphony.xwork2.ActionContext)12 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)11 SimpleFooAction (com.opensymphony.xwork2.SimpleFooAction)10 ServletActionContext (org.apache.struts2.ServletActionContext)10 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)10 OgnlUtil (com.opensymphony.xwork2.ognl.OgnlUtil)8 ActionMapper (org.apache.struts2.dispatcher.mapper.ActionMapper)7 IMocksControl (org.easymock.IMocksControl)7 ValueStack (com.opensymphony.xwork2.util.ValueStack)6 DefaultUrlHelper (org.apache.struts2.views.util.DefaultUrlHelper)6 Mock (com.mockobjects.dynamic.Mock)5 Action (com.opensymphony.xwork2.Action)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5