001: /*
002: * $Id: MessageStoreInterceptorTest.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.interceptor;
022:
023: import java.util.ArrayList;
024: import java.util.HashMap;
025: import java.util.LinkedHashMap;
026: import java.util.List;
027: import java.util.Map;
028:
029: import org.apache.struts2.StrutsTestCase;
030: import org.easymock.EasyMock;
031:
032: import com.opensymphony.xwork2.Action;
033: import com.opensymphony.xwork2.ActionContext;
034: import com.opensymphony.xwork2.ActionInvocation;
035: import com.opensymphony.xwork2.ActionSupport;
036:
037: /**
038: * Test case for MessageStoreInterceptor.
039: *
040: * @version $Date: 2006-11-06 10:01:43 -0500 (Mon, 06 Nov 2006) $ $Id: MessageStoreInterceptorTest.java 471756 2006-11-06 15:01:43Z husted $
041: */
042: public class MessageStoreInterceptorTest extends StrutsTestCase {
043:
044: public void testStoreMessage() throws Exception {
045: MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
046: interceptor.setAllowRequestParameterSwitch(true);
047: interceptor
048: .setOperationMode(MessageStoreInterceptor.STORE_MODE);
049:
050: Map paramMap = new LinkedHashMap();
051: Map sessionMap = new LinkedHashMap();
052:
053: ActionSupport action = new ActionSupport();
054: action.addActionError("some action error 1");
055: action.addActionError("some action error 2");
056: action.addActionMessage("some action message 1");
057: action.addActionMessage("some action message 2");
058: action.addFieldError("field1", "some field error 1");
059: action.addFieldError("field2", "some field error 2");
060:
061: ActionContext actionContext = new ActionContext(new HashMap());
062: actionContext.put(ActionContext.PARAMETERS, paramMap);
063: actionContext.put(ActionContext.SESSION, sessionMap);
064:
065: // Mock (ActionInvocation)
066: ActionInvocation mockActionInvocation = EasyMock
067: .createControl().createMock(ActionInvocation.class);
068: mockActionInvocation.getInvocationContext();
069: EasyMock.expectLastCall().andReturn(actionContext);
070: EasyMock.expectLastCall().anyTimes();
071:
072: mockActionInvocation.invoke();
073: EasyMock.expectLastCall().andReturn(Action.SUCCESS);
074:
075: mockActionInvocation.getAction();
076: EasyMock.expectLastCall().andReturn(action);
077:
078: EasyMock.replay(mockActionInvocation);
079:
080: interceptor.init();
081: interceptor.intercept(mockActionInvocation);
082: interceptor.destroy();
083:
084: assertEquals(sessionMap.size(), 3);
085: assertTrue(sessionMap
086: .containsKey(MessageStoreInterceptor.actionErrorsSessionKey));
087: assertTrue(sessionMap
088: .containsKey(MessageStoreInterceptor.actionMessagesSessionKey));
089: assertTrue(sessionMap
090: .containsKey(MessageStoreInterceptor.fieldErrorsSessionKey));
091:
092: List actionErrors = (List) sessionMap
093: .get(MessageStoreInterceptor.actionErrorsSessionKey);
094: List actionMessages = (List) sessionMap
095: .get(MessageStoreInterceptor.actionMessagesSessionKey);
096: Map fieldErrors = (Map) sessionMap
097: .get(MessageStoreInterceptor.fieldErrorsSessionKey);
098:
099: assertEquals(actionErrors.size(), 2);
100: assertEquals(actionMessages.size(), 2);
101: assertEquals(fieldErrors.size(), 2);
102:
103: assertTrue(actionErrors.contains("some action error 1"));
104: assertTrue(actionErrors.contains("some action error 2"));
105: assertTrue(actionMessages.contains("some action message 1"));
106: assertTrue(actionMessages.contains("some action message 2"));
107: assertTrue(fieldErrors.containsKey("field1"));
108: assertTrue(fieldErrors.containsKey("field2"));
109: assertEquals(((List) fieldErrors.get("field1")).size(), 1);
110: assertEquals(((List) fieldErrors.get("field2")).size(), 1);
111: assertEquals(((List) fieldErrors.get("field1")).get(0),
112: "some field error 1");
113: assertEquals(((List) fieldErrors.get("field2")).get(0),
114: "some field error 2");
115:
116: EasyMock.verify(mockActionInvocation);
117: }
118:
119: public void testRetrieveMessage() throws Exception {
120: MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
121: interceptor
122: .setOperationMode(MessageStoreInterceptor.RETRIEVE_MODE);
123: interceptor.setAllowRequestParameterSwitch(true);
124:
125: ActionSupport action = new ActionSupport();
126:
127: ActionInvocation mockActionInvocation = EasyMock
128: .createControl().createMock(ActionInvocation.class);
129: mockActionInvocation.invoke();
130: EasyMock.expectLastCall().andReturn(Action.SUCCESS);
131:
132: Map paramsMap = new LinkedHashMap();
133: Map sessionMap = new LinkedHashMap();
134:
135: List actionErrors = new ArrayList();
136: List actionMessages = new ArrayList();
137: Map fieldErrors = new LinkedHashMap();
138:
139: actionErrors.add("some action error 1");
140: actionErrors.add("some action error 2");
141: actionMessages.add("some action messages 1");
142: actionMessages.add("some action messages 2");
143: List field1Errors = new ArrayList();
144: field1Errors.add("some field error 1");
145: List field2Errors = new ArrayList();
146: field2Errors.add("some field error 2");
147: fieldErrors.put("field1", field1Errors);
148: fieldErrors.put("field2", field2Errors);
149:
150: sessionMap.put(MessageStoreInterceptor.actionErrorsSessionKey,
151: actionErrors);
152: sessionMap.put(
153: MessageStoreInterceptor.actionMessagesSessionKey,
154: actionMessages);
155: sessionMap.put(MessageStoreInterceptor.fieldErrorsSessionKey,
156: fieldErrors);
157:
158: ActionContext actionContext = new ActionContext(new HashMap());
159: actionContext.put(ActionContext.PARAMETERS, paramsMap);
160: actionContext.put(ActionContext.SESSION, sessionMap);
161:
162: mockActionInvocation.getInvocationContext();
163: EasyMock.expectLastCall().andReturn(actionContext);
164: EasyMock.expectLastCall().anyTimes();
165:
166: mockActionInvocation.getAction();
167: EasyMock.expectLastCall().andReturn(action);
168:
169: EasyMock.replay(mockActionInvocation);
170:
171: interceptor.init();
172: interceptor.intercept(mockActionInvocation);
173: interceptor.destroy();
174:
175: assertEquals(action.getActionErrors().size(), 2);
176: assertEquals(action.getActionMessages().size(), 2);
177: assertEquals(action.getFieldErrors().size(), 2);
178: assertTrue(action.getActionErrors().contains(
179: "some action error 1"));
180: assertTrue(action.getActionErrors().contains(
181: "some action error 2"));
182: assertTrue(action.getActionMessages().contains(
183: "some action messages 1"));
184: assertTrue(action.getActionMessages().contains(
185: "some action messages 2"));
186: assertEquals(((List) action.getFieldErrors().get("field1"))
187: .size(), 1);
188: assertEquals(((List) action.getFieldErrors().get("field2"))
189: .size(), 1);
190: assertEquals(((List) action.getFieldErrors().get("field1"))
191: .get(0), "some field error 1");
192: assertEquals(((List) action.getFieldErrors().get("field2"))
193: .get(0), "some field error 2");
194:
195: EasyMock.verify(mockActionInvocation);
196: }
197:
198: public void testRequestOperationMode1() throws Exception {
199:
200: Map paramMap = new LinkedHashMap();
201: paramMap.put("operationMode",
202: new String[] { MessageStoreInterceptor.RETRIEVE_MODE });
203:
204: ActionContext actionContext = new ActionContext(new HashMap());
205: actionContext.put(ActionContext.PARAMETERS, paramMap);
206:
207: ActionInvocation mockActionInvocation = EasyMock
208: .createControl().createMock(ActionInvocation.class);
209: mockActionInvocation.getInvocationContext();
210: EasyMock.expectLastCall().andReturn(actionContext);
211: EasyMock.expectLastCall().anyTimes();
212:
213: EasyMock.replay(mockActionInvocation);
214:
215: MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
216: String operationMode = interceptor
217: .getRequestOperationMode(mockActionInvocation);
218:
219: assertEquals(operationMode,
220: MessageStoreInterceptor.RETRIEVE_MODE);
221:
222: EasyMock.verify(mockActionInvocation);
223: }
224:
225: public void testRequestOperationMode2() throws Exception {
226:
227: Map paramMap = new LinkedHashMap();
228: paramMap.put("operationMode",
229: new String[] { MessageStoreInterceptor.STORE_MODE });
230:
231: ActionContext actionContext = new ActionContext(new HashMap());
232: actionContext.put(ActionContext.PARAMETERS, paramMap);
233:
234: ActionInvocation mockActionInvocation = EasyMock
235: .createControl().createMock(ActionInvocation.class);
236: mockActionInvocation.getInvocationContext();
237: EasyMock.expectLastCall().andReturn(actionContext);
238: EasyMock.expectLastCall().anyTimes();
239:
240: EasyMock.replay(mockActionInvocation);
241:
242: MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
243: String operationMode = interceptor
244: .getRequestOperationMode(mockActionInvocation);
245:
246: assertEquals(operationMode, MessageStoreInterceptor.STORE_MODE);
247:
248: EasyMock.verify(mockActionInvocation);
249: }
250:
251: public void testRequestOperationMode3() throws Exception {
252:
253: Map paramMap = new LinkedHashMap();
254:
255: ActionContext actionContext = new ActionContext(new HashMap());
256: actionContext.put(ActionContext.PARAMETERS, paramMap);
257:
258: ActionInvocation mockActionInvocation = EasyMock
259: .createControl().createMock(ActionInvocation.class);
260: mockActionInvocation.getInvocationContext();
261: EasyMock.expectLastCall().andReturn(actionContext);
262: EasyMock.expectLastCall().anyTimes();
263:
264: EasyMock.replay(mockActionInvocation);
265:
266: MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
267: String operationMode = interceptor
268: .getRequestOperationMode(mockActionInvocation);
269:
270: assertEquals(operationMode, MessageStoreInterceptor.NONE);
271:
272: EasyMock.verify(mockActionInvocation);
273:
274: }
275: }
|