View Javadoc

    1   /*
    2    * $Id: MessageStoreInterceptorTest.java 471756 2006-11-06 15:01:43Z husted $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *  http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21  package org.apache.struts2.interceptor;
    22
    23  import java.util.ArrayList;
    24  import java.util.HashMap;
    25  import java.util.LinkedHashMap;
    26  import java.util.List;
    27  import java.util.Map;
    28
    29  import org.apache.struts2.StrutsTestCase;
    30  import org.easymock.EasyMock;
    31
    32  import com.opensymphony.xwork2.Action;
    33  import com.opensymphony.xwork2.ActionContext;
    34  import com.opensymphony.xwork2.ActionInvocation;
    35  import com.opensymphony.xwork2.ActionSupport;
    36
    37
    38  /***
    39   * Test case for MessageStoreInterceptor.
    40   *
    41   * @version $Date: 2006-11-06 10:01:43 -0500 (Mon, 06 Nov 2006) $ $Id: MessageStoreInterceptorTest.java 471756 2006-11-06 15:01:43Z husted $
    42   */
    43  public class MessageStoreInterceptorTest extends StrutsTestCase {
    44
    45      public void testStoreMessage() throws Exception {
    46          MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
    47          interceptor.setAllowRequestParameterSwitch(true);
    48          interceptor.setOperationMode(MessageStoreInterceptor.STORE_MODE);
    49
    50
    51          Map paramMap = new LinkedHashMap();
    52          Map sessionMap = new LinkedHashMap();
    53
    54          ActionSupport action = new ActionSupport();
    55          action.addActionError("some action error 1");
    56          action.addActionError("some action error 2");
    57          action.addActionMessage("some action message 1");
    58          action.addActionMessage("some action message 2");
    59          action.addFieldError("field1", "some field error 1");
    60          action.addFieldError("field2", "some field error 2");
    61
    62          ActionContext actionContext = new ActionContext(new HashMap());
    63          actionContext.put(ActionContext.PARAMETERS, paramMap);
    64          actionContext.put(ActionContext.SESSION, sessionMap);
    65
    66          // Mock (ActionInvocation)
    67          ActionInvocation mockActionInvocation = EasyMock.createControl().createMock(ActionInvocation.class);
    68          mockActionInvocation.getInvocationContext();
    69          EasyMock.expectLastCall().andReturn(actionContext);
    70          EasyMock.expectLastCall().anyTimes();
    71
    72          mockActionInvocation.invoke();
    73          EasyMock.expectLastCall().andReturn(Action.SUCCESS);
    74
    75          mockActionInvocation.getAction();
    76          EasyMock.expectLastCall().andReturn(action);
    77
    78
    79          EasyMock.replay(mockActionInvocation);
    80
    81          interceptor.init();
    82          interceptor.intercept(mockActionInvocation);
    83          interceptor.destroy();
    84
    85          assertEquals(sessionMap.size(), 3);
    86          assertTrue(sessionMap.containsKey(MessageStoreInterceptor.actionErrorsSessionKey));
    87          assertTrue(sessionMap.containsKey(MessageStoreInterceptor.actionMessagesSessionKey));
    88          assertTrue(sessionMap.containsKey(MessageStoreInterceptor.fieldErrorsSessionKey));
    89
    90          List actionErrors = (List) sessionMap.get(MessageStoreInterceptor.actionErrorsSessionKey);
    91          List actionMessages = (List) sessionMap.get(MessageStoreInterceptor.actionMessagesSessionKey);
    92          Map fieldErrors = (Map) sessionMap.get(MessageStoreInterceptor.fieldErrorsSessionKey);
    93
    94          assertEquals(actionErrors.size(), 2);
    95          assertEquals(actionMessages.size(), 2);
    96          assertEquals(fieldErrors.size(), 2);
    97
    98          assertTrue(actionErrors.contains("some action error 1"));
    99          assertTrue(actionErrors.contains("some action error 2"));
    100         assertTrue(actionMessages.contains("some action message 1"));
    101         assertTrue(actionMessages.contains("some action message 2"));
    102         assertTrue(fieldErrors.containsKey("field1"));
    103         assertTrue(fieldErrors.containsKey("field2"));
    104         assertEquals(((List)fieldErrors.get("field1")).size(), 1);
    105         assertEquals(((List)fieldErrors.get("field2")).size(), 1);
    106         assertEquals(((List)fieldErrors.get("field1")).get(0), "some field error 1");
    107         assertEquals(((List)fieldErrors.get("field2")).get(0), "some field error 2");
    108
    109         EasyMock.verify(mockActionInvocation);
    110     }
    111
    112     public void testRetrieveMessage() throws Exception {
    113         MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
    114         interceptor.setOperationMode(MessageStoreInterceptor.RETRIEVE_MODE);
    115         interceptor.setAllowRequestParameterSwitch(true);
    116
    117
    118         ActionSupport action = new ActionSupport();
    119
    120         ActionInvocation mockActionInvocation = EasyMock.createControl().createMock(ActionInvocation.class);
    121         mockActionInvocation.invoke();
    122         EasyMock.expectLastCall().andReturn(Action.SUCCESS);
    123
    124         Map paramsMap = new LinkedHashMap();
    125         Map sessionMap = new LinkedHashMap();
    126
    127         List actionErrors = new ArrayList();
    128         List actionMessages = new ArrayList();
    129         Map fieldErrors = new LinkedHashMap();
    130
    131         actionErrors.add("some action error 1");
    132         actionErrors.add("some action error 2");
    133         actionMessages.add("some action messages 1");
    134         actionMessages.add("some action messages 2");
    135         List field1Errors = new ArrayList();
    136         field1Errors.add("some field error 1");
    137         List field2Errors = new ArrayList();
    138         field2Errors.add("some field error 2");
    139         fieldErrors.put("field1", field1Errors);
    140         fieldErrors.put("field2", field2Errors);
    141
    142         sessionMap.put(MessageStoreInterceptor.actionErrorsSessionKey, actionErrors);
    143         sessionMap.put(MessageStoreInterceptor.actionMessagesSessionKey, actionMessages);
    144         sessionMap.put(MessageStoreInterceptor.fieldErrorsSessionKey, fieldErrors);
    145
    146
    147         ActionContext actionContext = new ActionContext(new HashMap());
    148         actionContext.put(ActionContext.PARAMETERS, paramsMap);
    149         actionContext.put(ActionContext.SESSION, sessionMap);
    150
    151         mockActionInvocation.getInvocationContext();
    152         EasyMock.expectLastCall().andReturn(actionContext);
    153         EasyMock.expectLastCall().anyTimes();
    154
    155         mockActionInvocation.getAction();
    156         EasyMock.expectLastCall().andReturn(action);
    157
    158         EasyMock.replay(mockActionInvocation);
    159
    160         interceptor.init();
    161         interceptor.intercept(mockActionInvocation);
    162         interceptor.destroy();
    163
    164         assertEquals(action.getActionErrors().size(), 2);
    165         assertEquals(action.getActionMessages().size(), 2);
    166         assertEquals(action.getFieldErrors().size(), 2);
    167         assertTrue(action.getActionErrors().contains("some action error 1"));
    168         assertTrue(action.getActionErrors().contains("some action error 2"));
    169         assertTrue(action.getActionMessages().contains("some action messages 1"));
    170         assertTrue(action.getActionMessages().contains("some action messages 2"));
    171         assertEquals(((List)action.getFieldErrors().get("field1")).size(), 1);
    172         assertEquals(((List)action.getFieldErrors().get("field2")).size(), 1);
    173         assertEquals(((List)action.getFieldErrors().get("field1")).get(0), "some field error 1");
    174         assertEquals(((List)action.getFieldErrors().get("field2")).get(0), "some field error 2");
    175
    176         EasyMock.verify(mockActionInvocation);
    177     }
    178
    179     public void testRequestOperationMode1() throws Exception {
    180
    181         Map paramMap = new LinkedHashMap();
    182         paramMap.put("operationMode", new String[] { MessageStoreInterceptor.RETRIEVE_MODE });
    183
    184         ActionContext actionContext = new ActionContext(new HashMap());
    185         actionContext.put(ActionContext.PARAMETERS, paramMap);
    186
    187         ActionInvocation mockActionInvocation = EasyMock.createControl().createMock(ActionInvocation.class);
    188         mockActionInvocation.getInvocationContext();
    189         EasyMock.expectLastCall().andReturn(actionContext);
    190         EasyMock.expectLastCall().anyTimes();
    191
    192         EasyMock.replay(mockActionInvocation);
    193
    194         MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
    195         String operationMode = interceptor.getRequestOperationMode(mockActionInvocation);
    196
    197         assertEquals(operationMode, MessageStoreInterceptor.RETRIEVE_MODE);
    198
    199         EasyMock.verify(mockActionInvocation);
    200     }
    201
    202     public void testRequestOperationMode2() throws Exception {
    203
    204         Map paramMap = new LinkedHashMap();
    205         paramMap.put("operationMode", new String[] { MessageStoreInterceptor.STORE_MODE });
    206
    207         ActionContext actionContext = new ActionContext(new HashMap());
    208         actionContext.put(ActionContext.PARAMETERS, paramMap);
    209
    210         ActionInvocation mockActionInvocation = EasyMock.createControl().createMock(ActionInvocation.class);
    211         mockActionInvocation.getInvocationContext();
    212         EasyMock.expectLastCall().andReturn(actionContext);
    213         EasyMock.expectLastCall().anyTimes();
    214
    215         EasyMock.replay(mockActionInvocation);
    216
    217         MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
    218         String operationMode = interceptor.getRequestOperationMode(mockActionInvocation);
    219
    220         assertEquals(operationMode, MessageStoreInterceptor.STORE_MODE);
    221
    222         EasyMock.verify(mockActionInvocation);
    223     }
    224
    225     public void testRequestOperationMode3() throws Exception {
    226
    227         Map paramMap = new LinkedHashMap();
    228
    229         ActionContext actionContext = new ActionContext(new HashMap());
    230         actionContext.put(ActionContext.PARAMETERS, paramMap);
    231
    232         ActionInvocation mockActionInvocation = EasyMock.createControl().createMock(ActionInvocation.class);
    233         mockActionInvocation.getInvocationContext();
    234         EasyMock.expectLastCall().andReturn(actionContext);
    235         EasyMock.expectLastCall().anyTimes();
    236
    237         EasyMock.replay(mockActionInvocation);
    238
    239         MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
    240         String operationMode = interceptor.getRequestOperationMode(mockActionInvocation);
    241
    242         assertEquals(operationMode, MessageStoreInterceptor.NONE);
    243
    244         EasyMock.verify(mockActionInvocation);
    245
    246     }
    247 }