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¶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 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¶m=value+2", res.getRedirectedUrl());
control.verify();
}
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¶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 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();
}
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);
}
Aggregations