Search in sources :

Example 26 with Interceptor

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);
}
Also used : ActionContext(com.opensymphony.xwork2.ActionContext) ServletActionContext(org.apache.struts2.ServletActionContext)

Example 27 with Interceptor

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);
}
Also used : ValidationAwareSupport(com.opensymphony.xwork2.ValidationAwareSupport) List(java.util.List)

Example 28 with Interceptor

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);
}
Also used : Action(com.opensymphony.xwork2.Action) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) MockHttpSession(org.springframework.mock.web.MockHttpSession) DefaultLocaleProviderFactory(com.opensymphony.xwork2.DefaultLocaleProviderFactory) MockActionProxy(com.opensymphony.xwork2.mock.MockActionProxy)

Example 29 with Interceptor

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);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ServletActionContext(org.apache.struts2.ServletActionContext) ActionContext(com.opensymphony.xwork2.ActionContext) LinkedHashMap(java.util.LinkedHashMap)

Example 30 with Interceptor

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);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) HttpSession(javax.servlet.http.HttpSession) ArrayList(java.util.ArrayList) ActionSupport(com.opensymphony.xwork2.ActionSupport) ServletActionContext(org.apache.struts2.ServletActionContext) ActionContext(com.opensymphony.xwork2.ActionContext) LinkedHashMap(java.util.LinkedHashMap) HttpServletRequest(javax.servlet.http.HttpServletRequest) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ActionInvocation (com.opensymphony.xwork2.ActionInvocation)32 HashMap (java.util.HashMap)28 ActionContext (com.opensymphony.xwork2.ActionContext)20 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)19 MockActionInvocation (com.opensymphony.xwork2.mock.MockActionInvocation)19 ServletActionContext (org.apache.struts2.ServletActionContext)14 InterceptorStackConfig (com.opensymphony.xwork2.config.entities.InterceptorStackConfig)13 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)12 LinkedHashMap (java.util.LinkedHashMap)12 Map (java.util.Map)11 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)11 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)10 InterceptorConfig (com.opensymphony.xwork2.config.entities.InterceptorConfig)10 DefaultAcceptedPatternsChecker (com.opensymphony.xwork2.security.DefaultAcceptedPatternsChecker)10 DefaultExcludedPatternsChecker (com.opensymphony.xwork2.security.DefaultExcludedPatternsChecker)10 Cookie (javax.servlet.http.Cookie)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 Mock (com.mockobjects.dynamic.Mock)8 ActionSupport (com.opensymphony.xwork2.ActionSupport)8