use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.
the class MessageStorePreResultListenerTest method testStoreMessage.
public void testStoreMessage() {
MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
interceptor.setAllowRequestParameterSwitch(true);
interceptor.setOperationMode(MessageStoreInterceptor.STORE_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);
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);
assertEquals(sessionMap.size(), 3);
assertTrue(sessionMap.containsKey(MessageStoreInterceptor.actionErrorsSessionKey));
assertTrue(sessionMap.containsKey(MessageStoreInterceptor.actionMessagesSessionKey));
assertTrue(sessionMap.containsKey(MessageStoreInterceptor.fieldErrorsSessionKey));
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"));
assertTrue(fieldErrors.containsKey("field1"));
assertTrue(fieldErrors.containsKey("field2"));
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);
}
use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.
the class StrutsConversionErrorInterceptorTest method setUp.
protected void setUp() throws Exception {
super.setUp();
interceptor = new StrutsConversionErrorInterceptor();
mockInvocation = new Mock(ActionInvocation.class);
invocation = (ActionInvocation) mockInvocation.proxy();
stack = ActionContext.getContext().getValueStack();
conversionErrors = new HashMap<>();
context = ActionContext.of(stack.getContext()).withConversionErrors(conversionErrors).bind();
mockInvocation.matchAndReturn("getInvocationContext", context);
mockInvocation.expectAndReturn("invoke", Action.SUCCESS);
mockInvocation.expectAndReturn("getStack", stack);
mockInvocation.expect("addPreResultListener", C.ANY_ARGS);
}
use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.
the class ValidateAction method testDMIMethodsAreIgnored.
public void testDMIMethodsAreIgnored() throws Exception {
// given
ParametersInterceptor interceptor = createParametersInterceptor();
final Map<String, Object> actual = injectValueStackFactory(interceptor);
ValueStack stack = injectValueStack(actual);
final Map<String, Object> expected = new HashMap<String, Object>() {
{
put("ordinary.bean", "value");
}
};
Map<String, Object> parameters = new HashMap<String, Object>() {
{
put("ordinary.bean", "value");
put("action:", "myAction");
put("method:", "doExecute");
}
};
// when
interceptor.setParameters(new NoParametersAction(), stack, HttpParameters.create(parameters).build());
// then
assertEquals(expected, actual);
}
use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.
the class ValidateAction method testInternalParametersAreIgnored.
public void testInternalParametersAreIgnored() throws Exception {
// given
ParametersInterceptor interceptor = createParametersInterceptor();
final Map<String, Object> actual = injectValueStackFactory(interceptor);
ValueStack stack = injectValueStack(actual);
final Map<String, Object> expected = new HashMap<String, Object>() {
{
put("ordinary.bean", "value");
}
};
Map<String, Object> parameters = new HashMap<String, Object>() {
{
put("ordinary.bean", "value");
put("#some.internal.object", "true");
put("(bla)#some.internal.object", "true");
put("#some.internal.object(bla)#some.internal.object", "true");
put("#_some.internal.object", "true");
put("\u0023_some.internal.object", "true");
put("\u0023_some.internal.object,[dfd],bla(\u0023_some.internal.object)", "true");
put("\\u0023_some.internal.object", "true");
}
};
// when
interceptor.setParameters(new NoParametersAction(), stack, HttpParameters.create(parameters).build());
// then
assertEquals(expected, actual);
}
use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.
the class PrepareInterceptorTest method testPrefixInvocation2.
public void testPrefixInvocation2() throws Exception {
ActionProxy mockActionProxy = (ActionProxy) createMock(ActionProxy.class);
expect(mockActionProxy.getMethod()).andStubReturn("save");
ActionInvocation mockActionInvocation = (ActionInvocation) createMock(ActionInvocation.class);
expect(mockActionInvocation.getAction()).andStubReturn(mockAction);
expect(mockActionInvocation.invoke()).andStubReturn("okok");
expect(mockActionInvocation.getProxy()).andStubReturn(mockActionProxy);
mockAction.prepare();
expectLastCall().times(1);
replay(mockAction, mockActionProxy, mockActionInvocation);
PrepareInterceptor interceptor = new PrepareInterceptor();
String result = interceptor.intercept(mockActionInvocation);
assertEquals("okok", result);
verify(mockAction, mockActionProxy, mockActionInvocation);
}
Aggregations