use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.
the class FetchMetadataInterceptorTest method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
container.inject(interceptor);
interceptor.setExemptedPaths("/foo,/bar");
ServletActionContext.setRequest(request);
ServletActionContext.setResponse(response);
ActionContext context = ServletActionContext.getActionContext();
mai.setInvocationContext(context);
}
use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.
the class FileUploadInterceptorTest method testAcceptFileWithNoFile.
public void testAcceptFileWithNoFile() throws Exception {
FileUploadInterceptor interceptor = new FileUploadInterceptor();
interceptor.setAllowedTypes("text/plain");
// when file is not of allowed types
ValidationAwareSupport validation = new ValidationAwareSupport();
boolean notOk = interceptor.acceptFile(action, null, "filename.html", "text/html", "inputName", validation);
assertFalse(notOk);
assertFalse(validation.getFieldErrors().isEmpty());
assertTrue(validation.hasErrors());
List errors = (List) validation.getFieldErrors().get("inputName");
assertEquals(1, errors.size());
String msg = (String) errors.get(0);
assertTrue(msg.startsWith("Error uploading:"));
assertTrue(msg.indexOf("inputName") > 0);
}
use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.
the class I18nInterceptorTest method setUp.
public void setUp() throws Exception {
interceptor = new I18nInterceptor();
interceptor.setLocaleProviderFactory(new DefaultLocaleProviderFactory());
interceptor.init();
session = new HashMap<>();
ac = ActionContext.of(new HashMap<>()).bind().withSession(session).withParameters(HttpParameters.create().build());
request = new MockHttpServletRequest();
request.setSession(new MockHttpSession());
ServletActionContext.setRequest(request);
Action action = () -> Action.SUCCESS;
MockActionProxy proxy = new MockActionProxy();
proxy.setAction(action);
proxy.setNamespace("i18n");
proxy.setActionName("anAction");
mai = new MockActionInvocation();
((MockActionInvocation) mai).setAction(action);
((MockActionInvocation) mai).setInvocationContext(ac);
((MockActionInvocation) mai).setProxy(proxy);
}
use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.
the class MessageStoreInterceptorTest method testRequestOperationMode2.
public void testRequestOperationMode2() {
Map<String, Object> paramMap = new LinkedHashMap<>();
paramMap.put("operationMode", new String[] { MessageStoreInterceptor.STORE_MODE });
ActionContext actionContext = ActionContext.of(new HashMap<>()).bind();
actionContext.setParameters(HttpParameters.create(paramMap).build());
ActionInvocation mockActionInvocation = EasyMock.createControl().createMock(ActionInvocation.class);
mockActionInvocation.getInvocationContext();
EasyMock.expectLastCall().andReturn(actionContext);
EasyMock.expectLastCall().anyTimes();
EasyMock.replay(mockActionInvocation);
MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
String operationMode = interceptor.getRequestOperationMode(mockActionInvocation);
assertEquals(operationMode, MessageStoreInterceptor.STORE_MODE);
EasyMock.verify(mockActionInvocation);
}
use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.
the class MessageStoreInterceptorTest method testRetrieveMessage.
public void testRetrieveMessage() throws Exception {
MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
interceptor.setOperationMode(MessageStoreInterceptor.RETRIEVE_MODE);
interceptor.setAllowRequestParameterSwitch(true);
ActionSupport action = new ActionSupport();
ActionInvocation mockActionInvocation = EasyMock.createControl().createMock(ActionInvocation.class);
mockActionInvocation.invoke();
EasyMock.expectLastCall().andReturn(Action.SUCCESS);
Map<String, Object> sessionMap = new LinkedHashMap<>();
List<String> actionErrors = new ArrayList<>();
List<String> actionMessages = new ArrayList<>();
Map<String, List<String>> fieldErrors = new LinkedHashMap<>();
actionErrors.add("some action error 1");
actionErrors.add("some action error 2");
actionMessages.add("some action messages 1");
actionMessages.add("some action messages 2");
List<String> field1Errors = new ArrayList<>();
field1Errors.add("some field error 1");
List<String> field2Errors = new ArrayList<>();
field2Errors.add("some field error 2");
fieldErrors.put("field1", field1Errors);
fieldErrors.put("field2", field2Errors);
sessionMap.put(MessageStoreInterceptor.actionErrorsSessionKey, actionErrors);
sessionMap.put(MessageStoreInterceptor.actionMessagesSessionKey, actionMessages);
sessionMap.put(MessageStoreInterceptor.fieldErrorsSessionKey, fieldErrors);
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);
ActionContext actionContext = ActionContext.of(new HashMap<>()).bind();
actionContext.setParameters(HttpParameters.create().build());
actionContext.setSession(sessionMap);
mockActionInvocation.getInvocationContext();
EasyMock.expectLastCall().andReturn(actionContext);
EasyMock.expectLastCall().anyTimes();
mockActionInvocation.getAction();
EasyMock.expectLastCall().andReturn(action);
EasyMock.expectLastCall().anyTimes();
mockActionInvocation.addPreResultListener(EasyMock.anyObject());
EasyMock.expectLastCall();
EasyMock.replay(mockActionInvocation);
interceptor.init();
interceptor.intercept(mockActionInvocation);
interceptor.destroy();
assertEquals(action.getActionErrors().size(), 2);
assertEquals(action.getActionMessages().size(), 2);
assertEquals(action.getFieldErrors().size(), 2);
assertTrue(action.getActionErrors().contains("some action error 1"));
assertTrue(action.getActionErrors().contains("some action error 2"));
assertTrue(action.getActionMessages().contains("some action messages 1"));
assertTrue(action.getActionMessages().contains("some action messages 2"));
assertEquals(action.getFieldErrors().get("field1").size(), 1);
assertEquals(action.getFieldErrors().get("field2").size(), 1);
assertEquals(action.getFieldErrors().get("field1").get(0), "some field error 1");
assertEquals(action.getFieldErrors().get("field2").get(0), "some field error 2");
EasyMock.verify(mockActionInvocation);
}
Aggregations