Search in sources :

Example 41 with MockActionProxy

use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.

the class ServletActionRedirectResultTest method testIncludeParameterInResultWithConditionParseOnWithNoNamespace.

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();
}
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 42 with MockActionProxy

use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.

the class ServletRedirectResultTest method testIncludeCollectionParameterInResult.

public void testIncludeCollectionParameterInResult() throws Exception {
    List<String> paramValues = new ArrayList<>();
    paramValues.add("value 1");
    paramValues.add("");
    paramValues.add("value 2");
    paramValues.add(null);
    ResultConfig resultConfig = new ResultConfig.Builder("", "").addParam("namespace", "someNamespace").addParam("param", "${list}").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();
    ServletRedirectResult result = new ServletRedirectResult();
    result.setLocation("/myNamespace/myAction.action");
    result.setParse(true);
    result.setEncode(false);
    result.setPrependServletContext(false);
    result.setUrlHelper(new DefaultUrlHelper());
    result.setSuppressEmptyParameters(true);
    IMocksControl control = createControl();
    ActionProxy mockActionProxy = control.createMock(ActionProxy.class);
    ActionInvocation mockInvocation = control.createMock(ActionInvocation.class);
    ValueStack mockValueStack = control.createMock(ValueStack.class);
    ActionContext actionContext = ActionContext.of(new HashMap<>()).withContainer(container);
    expect(mockInvocation.getStack()).andReturn(mockValueStack);
    expect(mockValueStack.getActionContext()).andReturn(actionContext);
    expect(mockInvocation.getStack()).andReturn(mockValueStack);
    // no asType !!!
    expect(mockValueStack.findValue("list")).andReturn(paramValues);
    expect(mockInvocation.getProxy()).andReturn(mockActionProxy);
    expect(mockInvocation.getResultCode()).andReturn("myResult");
    expect(mockActionProxy.getConfig()).andReturn(actionConfig);
    expect(mockInvocation.getInvocationContext()).andReturn(context);
    expect(mockValueStack.getActionContext()).andReturn(actionContext);
    control.replay();
    result.setActionMapper(container.getInstance(ActionMapper.class));
    result.execute(mockInvocation);
    assertEquals("/myNamespace/myAction.action?param=value+1&param=value+2", 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) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) ArrayList(java.util.ArrayList) ActionContext(com.opensymphony.xwork2.ActionContext) ServletActionContext(org.apache.struts2.ServletActionContext) 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 43 with MockActionProxy

use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.

the class ServletRedirectResultTest method testIncludeParameterInResult.

public void testIncludeParameterInResult() throws Exception {
    ResultConfig resultConfig = new ResultConfig.Builder("", "").addParam("namespace", "someNamespace").addParam("encode", "true").addParam("parse", "true").addParam("location", "someLocation").addParam("prependServletContext", "true").addParam("method", "someMethod").addParam("statusCode", "333").addParam("param1", "value 1").addParam("param2", "value 2").addParam("param3", "value 3").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();
    ServletRedirectResult result = new ServletRedirectResult();
    result.setLocation("/myNamespace/myAction.action");
    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) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) ActionContext(com.opensymphony.xwork2.ActionContext) ServletActionContext(org.apache.struts2.ServletActionContext) 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 44 with MockActionProxy

use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.

the class PortletUrlTagTest method setUp.

public void setUp() throws Exception {
    super.setUp();
    ServletContext servletContext = new MockServletContext();
    dispatcher = new Dispatcher(servletContext, new HashMap<>());
    dispatcher.init();
    Dispatcher.setInstance(dispatcher);
    stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();
    mockActionInvocation = mock(ActionInvocation.class);
    mockActionProxy = mock(ActionProxy.class);
    mockHttpReq = mock(HttpServletRequest.class);
    mockHttpRes = mock(HttpServletResponse.class);
    mockPortletReq = mock(RenderRequest.class);
    mockPortletRes = mock(RenderResponse.class);
    mockPageCtx = mock(PageContext.class);
    mockPortletUrl = mock(PortletURL.class);
    mockJspWriter = new MockJspWriter();
    mockCtx = mock(PortletContext.class);
    mockActionProxy.stubs().method("getNamespace").will(returnValue("/view"));
    mockActionInvocation.stubs().method("getProxy").will(returnValue(mockActionProxy.proxy()));
    mockPageCtx.stubs().method("getRequest").will(returnValue(mockHttpReq.proxy()));
    mockPageCtx.stubs().method("getResponse").will(returnValue(mockHttpRes.proxy()));
    mockPageCtx.stubs().method("getOut").will(returnValue(mockJspWriter));
    mockHttpReq.stubs().method("getScheme").will(returnValue("http"));
    mockHttpReq.stubs().method("getAttribute").with(eq("struts.valueStack")).will(returnValue(stack));
    mockHttpReq.stubs().method("getAttribute").with(eq("javax.portlet.response")).will(returnValue(mockPortletRes.proxy()));
    mockHttpReq.stubs().method("getAttribute").with(eq("javax.portlet.request")).will(returnValue(mockPortletReq.proxy()));
    mockHttpReq.stubs().method("getAttribute").with(eq("javax.servlet.include.servlet_path")).will(returnValue("/servletPath"));
    mockHttpReq.stubs().method("getParameterMap").will(returnValue(Collections.emptyMap()));
    mockPortletReq.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
    mockPortletReq.stubs().method("getWindowState").will(returnValue(WindowState.NORMAL));
    mockPortletReq.stubs().method("getContextPath").will(returnValue("/contextPath"));
    tag.setPageContext((PageContext) mockPageCtx.proxy());
    Map<PortletMode, String> modeMap = new HashMap<>();
    modeMap.put(PortletMode.VIEW, "/view");
    modeMap.put(PortletMode.HELP, "/help");
    modeMap.put(PortletMode.EDIT, "/edit");
    Map<PortletMode, ActionMapping> actionMap = new HashMap<>();
    actionMap.put(PortletMode.VIEW, new ActionMapping("defaultView", "/view", "execute", new HashMap<>()));
    actionMap.put(PortletMode.HELP, new ActionMapping("defaultHelp", "/help", "execute", new HashMap<>()));
    actionMap.put(PortletMode.EDIT, new ActionMapping("defaultEdit", "/edit", "execute", new HashMap<>()));
    Map<String, Object> contextMap = stack.getActionContext().withSession(new HashMap<>()).with(PortletConstants.REQUEST, mockPortletReq.proxy()).with(PortletConstants.RESPONSE, mockPortletRes.proxy()).with(PortletConstants.PHASE, PortletPhase.RENDER_PHASE).with(PortletConstants.MODE_NAMESPACE_MAP, modeMap).with(PortletConstants.DEFAULT_ACTION_MAP, actionMap).with(STRUTS_PORTLET_CONTEXT, mockCtx.proxy()).getContextMap();
    ActionInvocation ai = (ActionInvocation) mockActionInvocation.proxy();
    ActionContext.of(contextMap).withValueStack(stack).withContainer(dispatcher.getContainer()).withActionInvocation(ai).bind();
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) HttpServletResponse(javax.servlet.http.HttpServletResponse) RenderRequest(javax.portlet.RenderRequest) Dispatcher(org.apache.struts2.dispatcher.Dispatcher) MockServletContext(org.springframework.mock.web.MockServletContext) PortletMode(javax.portlet.PortletMode) HttpServletRequest(javax.servlet.http.HttpServletRequest) ActionMapping(org.apache.struts2.dispatcher.mapper.ActionMapping) MockServletContext(org.springframework.mock.web.MockServletContext) ServletContext(javax.servlet.ServletContext) MockJspWriter(com.mockobjects.servlet.MockJspWriter) PageContext(javax.servlet.jsp.PageContext) PortletContext(javax.portlet.PortletContext) RenderResponse(javax.portlet.RenderResponse) PortletURL(javax.portlet.PortletURL)

Example 45 with MockActionProxy

use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.

the class MessageStorePreResultListenerTest method testAutomatic.

public void testAutomatic() {
    MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
    interceptor.setOperationMode(MessageStoreInterceptor.AUTOMATIC_MODE);
    MessageStorePreResultListener listener = new MessageStorePreResultListener();
    listener.init(interceptor);
    Map<String, Object> sessionMap = new LinkedHashMap<>();
    ActionSupport action = new ActionSupport();
    action.addActionError("some action error 1");
    action.addActionError("some action error 2");
    action.addActionMessage("some action message 1");
    action.addActionMessage("some action message 2");
    action.addFieldError("field1", "some field error 1");
    action.addFieldError("field2", "some field error 2");
    ActionContext actionContext = ActionContext.of(new HashMap<>()).bind();
    actionContext.setParameters(HttpParameters.create().build());
    actionContext.setSession(sessionMap);
    HttpSession mockedSession = EasyMock.createControl().createMock(HttpSession.class);
    HttpServletRequest mockedRequest = EasyMock.createControl().createMock(HttpServletRequest.class);
    mockedRequest.getSession(false);
    EasyMock.expectLastCall().andReturn(mockedSession);
    EasyMock.expectLastCall().once();
    ServletActionContext.setRequest(mockedRequest);
    EasyMock.replay(mockedRequest);
    HttpServletResponse mockedResponse = EasyMock.createControl().createMock(HttpServletResponse.class);
    mockedResponse.isCommitted();
    EasyMock.expectLastCall().andReturn(false);
    EasyMock.expectLastCall().once();
    ServletActionContext.setResponse(mockedResponse);
    EasyMock.replay(mockedResponse);
    // Mock (ActionInvocation)
    ActionInvocation mockActionInvocation = EasyMock.createControl().createMock(ActionInvocation.class);
    mockActionInvocation.getInvocationContext();
    EasyMock.expectLastCall().andReturn(actionContext);
    EasyMock.expectLastCall().anyTimes();
    mockActionInvocation.getAction();
    EasyMock.expectLastCall().andReturn(action);
    EasyMock.expectLastCall().anyTimes();
    mockActionInvocation.getProxy();
    MockActionProxy actionProxy = new MockActionProxy();
    ResultConfig resultConfig = new ResultConfig.Builder(Action.SUCCESS, ServletRedirectResult.class.getName()).build();
    ActionConfig actionConfig = new ActionConfig.Builder("", "test", action.getClass().getName()).addResultConfig(resultConfig).build();
    actionProxy.setConfig(actionConfig);
    EasyMock.expectLastCall().andReturn(actionProxy);
    EasyMock.expectLastCall().anyTimes();
    EasyMock.replay(mockActionInvocation);
    interceptor.init();
    listener.beforeResult(mockActionInvocation, Action.SUCCESS);
    List actionErrors = (List) sessionMap.get(MessageStoreInterceptor.actionErrorsSessionKey);
    List actionMessages = (List) sessionMap.get(MessageStoreInterceptor.actionMessagesSessionKey);
    Map fieldErrors = (Map) sessionMap.get(MessageStoreInterceptor.fieldErrorsSessionKey);
    assertEquals(actionErrors.size(), 2);
    assertEquals(actionMessages.size(), 2);
    assertEquals(fieldErrors.size(), 2);
    assertTrue(actionErrors.contains("some action error 1"));
    assertTrue(actionErrors.contains("some action error 2"));
    assertTrue(actionMessages.contains("some action message 1"));
    assertTrue(actionMessages.contains("some action message 2"));
    assertEquals(((List) fieldErrors.get("field1")).size(), 1);
    assertEquals(((List) fieldErrors.get("field2")).size(), 1);
    assertEquals(((List) fieldErrors.get("field1")).get(0), "some field error 1");
    assertEquals(((List) fieldErrors.get("field2")).get(0), "some field error 2");
    EasyMock.verify(mockActionInvocation);
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpSession(javax.servlet.http.HttpSession) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ActionSupport(com.opensymphony.xwork2.ActionSupport) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletActionContext(org.apache.struts2.ServletActionContext) ActionContext(com.opensymphony.xwork2.ActionContext) LinkedHashMap(java.util.LinkedHashMap) HttpServletRequest(javax.servlet.http.HttpServletRequest) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MockActionProxy(com.opensymphony.xwork2.mock.MockActionProxy)

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