001: /*
002: * Copyright (c) 2002-2007 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.interceptor;
006:
007: import java.util.ArrayList;
008: import java.util.HashMap;
009: import java.util.LinkedHashMap;
010: import java.util.List;
011: import java.util.Map;
012:
013: import org.easymock.MockControl;
014:
015: import com.opensymphony.webwork.WebWorkTestCase;
016: import com.opensymphony.xwork.Action;
017: import com.opensymphony.xwork.ActionContext;
018: import com.opensymphony.xwork.ActionInvocation;
019: import com.opensymphony.xwork.ActionSupport;
020:
021: /**
022: * Test case for MessageStoreInterceptor.
023: *
024: * @author tmjee
025: *
026: * @version $Date$ $Id$
027: */
028: public class MessageStoreInterceptorTest extends WebWorkTestCase {
029:
030: public void testStoreMessage() throws Exception {
031: MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
032: interceptor.setAllowRequestParameterSwitch(true);
033: interceptor
034: .setOperationMode(MessageStoreInterceptor.STORE_MODE);
035:
036: Map paramMap = new LinkedHashMap();
037: Map sessionMap = new LinkedHashMap();
038:
039: ActionSupport action = new ActionSupport();
040: action.addActionError("some action error 1");
041: action.addActionError("some action error 2");
042: action.addActionMessage("some action message 1");
043: action.addActionMessage("some action message 2");
044: action.addFieldError("field1", "some field error 1");
045: action.addFieldError("field2", "some field error 2");
046:
047: ActionContext actionContext = new ActionContext(new HashMap());
048: actionContext.put(ActionContext.PARAMETERS, paramMap);
049: actionContext.put(ActionContext.SESSION, sessionMap);
050:
051: // Mock (ActionInvocation)
052: MockControl mockControlActionInvocation = MockControl
053: .createControl(ActionInvocation.class);
054: ActionInvocation mockActionInvocation = (ActionInvocation) mockControlActionInvocation
055: .getMock();
056: mockControlActionInvocation.expectAndDefaultReturn(
057: mockActionInvocation.getInvocationContext(),
058: actionContext);
059:
060: mockControlActionInvocation.expectAndDefaultReturn(
061: mockActionInvocation.invoke(), Action.SUCCESS);
062:
063: mockControlActionInvocation.expectAndDefaultReturn(
064: mockActionInvocation.getAction(), action);
065:
066: mockControlActionInvocation.replay();
067:
068: interceptor.init();
069: interceptor.intercept(mockActionInvocation);
070: interceptor.destroy();
071:
072: assertEquals(sessionMap.size(), 3);
073: assertTrue(sessionMap
074: .containsKey(MessageStoreInterceptor.actionErrorsSessionKey));
075: assertTrue(sessionMap
076: .containsKey(MessageStoreInterceptor.actionMessagesSessionKey));
077: assertTrue(sessionMap
078: .containsKey(MessageStoreInterceptor.fieldErrorsSessionKey));
079:
080: List actionErrors = (List) sessionMap
081: .get(MessageStoreInterceptor.actionErrorsSessionKey);
082: List actionMessages = (List) sessionMap
083: .get(MessageStoreInterceptor.actionMessagesSessionKey);
084: Map fieldErrors = (Map) sessionMap
085: .get(MessageStoreInterceptor.fieldErrorsSessionKey);
086:
087: assertEquals(actionErrors.size(), 2);
088: assertEquals(actionMessages.size(), 2);
089: assertEquals(fieldErrors.size(), 2);
090:
091: assertTrue(actionErrors.contains("some action error 1"));
092: assertTrue(actionErrors.contains("some action error 2"));
093: assertTrue(actionMessages.contains("some action message 1"));
094: assertTrue(actionMessages.contains("some action message 2"));
095: assertTrue(fieldErrors.containsKey("field1"));
096: assertTrue(fieldErrors.containsKey("field2"));
097: assertEquals(((List) fieldErrors.get("field1")).size(), 1);
098: assertEquals(((List) fieldErrors.get("field2")).size(), 1);
099: assertEquals(((List) fieldErrors.get("field1")).get(0),
100: "some field error 1");
101: assertEquals(((List) fieldErrors.get("field2")).get(0),
102: "some field error 2");
103:
104: mockControlActionInvocation.verify();
105: }
106:
107: public void testRetrieveMessage() throws Exception {
108: MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
109: interceptor
110: .setOperationMode(MessageStoreInterceptor.RETRIEVE_MODE);
111: interceptor.setAllowRequestParameterSwitch(true);
112:
113: ActionSupport action = new ActionSupport();
114:
115: MockControl mockControlActionInvocation = MockControl
116: .createControl(ActionInvocation.class);
117: ActionInvocation mockActionInvocation = (ActionInvocation) mockControlActionInvocation
118: .getMock();
119: mockControlActionInvocation.expectAndDefaultReturn(
120: mockActionInvocation.invoke(), Action.SUCCESS);
121:
122: Map paramsMap = new LinkedHashMap();
123: Map sessionMap = new LinkedHashMap();
124:
125: List actionErrors = new ArrayList();
126: List actionMessages = new ArrayList();
127: Map fieldErrors = new LinkedHashMap();
128:
129: actionErrors.add("some action error 1");
130: actionErrors.add("some action error 2");
131: actionMessages.add("some action messages 1");
132: actionMessages.add("some action messages 2");
133: List field1Errors = new ArrayList();
134: field1Errors.add("some field error 1");
135: List field2Errors = new ArrayList();
136: field2Errors.add("some field error 2");
137: fieldErrors.put("field1", field1Errors);
138: fieldErrors.put("field2", field2Errors);
139:
140: sessionMap.put(MessageStoreInterceptor.actionErrorsSessionKey,
141: actionErrors);
142: sessionMap.put(
143: MessageStoreInterceptor.actionMessagesSessionKey,
144: actionMessages);
145: sessionMap.put(MessageStoreInterceptor.fieldErrorsSessionKey,
146: fieldErrors);
147:
148: ActionContext actionContext = new ActionContext(new HashMap());
149: actionContext.put(ActionContext.PARAMETERS, paramsMap);
150: actionContext.put(ActionContext.SESSION, sessionMap);
151:
152: mockControlActionInvocation.expectAndDefaultReturn(
153: mockActionInvocation.getInvocationContext(),
154: actionContext);
155:
156: mockControlActionInvocation.expectAndDefaultReturn(
157: mockActionInvocation.getAction(), action);
158:
159: mockControlActionInvocation.replay();
160:
161: interceptor.init();
162: interceptor.intercept(mockActionInvocation);
163: interceptor.destroy();
164:
165: assertEquals(action.getActionErrors().size(), 2);
166: assertEquals(action.getActionMessages().size(), 2);
167: assertEquals(action.getFieldErrors().size(), 2);
168: assertTrue(action.getActionErrors().contains(
169: "some action error 1"));
170: assertTrue(action.getActionErrors().contains(
171: "some action error 2"));
172: assertTrue(action.getActionMessages().contains(
173: "some action messages 1"));
174: assertTrue(action.getActionMessages().contains(
175: "some action messages 2"));
176: assertEquals(((List) action.getFieldErrors().get("field1"))
177: .size(), 1);
178: assertEquals(((List) action.getFieldErrors().get("field2"))
179: .size(), 1);
180: assertEquals(((List) action.getFieldErrors().get("field1"))
181: .get(0), "some field error 1");
182: assertEquals(((List) action.getFieldErrors().get("field2"))
183: .get(0), "some field error 2");
184:
185: mockControlActionInvocation.verify();
186: }
187:
188: public void testRequestOperationMode1() throws Exception {
189:
190: Map paramMap = new LinkedHashMap();
191: paramMap.put("operationMode",
192: new String[] { MessageStoreInterceptor.RETRIEVE_MODE });
193:
194: ActionContext actionContext = new ActionContext(new HashMap());
195: actionContext.put(ActionContext.PARAMETERS, paramMap);
196:
197: MockControl mockControlActionInvocation = MockControl
198: .createControl(ActionInvocation.class);
199: ActionInvocation mockActionInvocation = (ActionInvocation) mockControlActionInvocation
200: .getMock();
201: mockControlActionInvocation.expectAndDefaultReturn(
202: mockActionInvocation.getInvocationContext(),
203: actionContext);
204:
205: mockControlActionInvocation.replay();
206:
207: MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
208: String operationMode = interceptor
209: .getRequestOperationMode(mockActionInvocation);
210:
211: assertEquals(operationMode,
212: MessageStoreInterceptor.RETRIEVE_MODE);
213:
214: mockControlActionInvocation.verify();
215: }
216:
217: public void testRequestOperationMode2() throws Exception {
218:
219: Map paramMap = new LinkedHashMap();
220: paramMap.put("operationMode",
221: new String[] { MessageStoreInterceptor.STORE_MODE });
222:
223: ActionContext actionContext = new ActionContext(new HashMap());
224: actionContext.put(ActionContext.PARAMETERS, paramMap);
225:
226: MockControl mockControlActionInvocation = MockControl
227: .createControl(ActionInvocation.class);
228: ActionInvocation mockActionInvocation = (ActionInvocation) mockControlActionInvocation
229: .getMock();
230: mockControlActionInvocation.expectAndDefaultReturn(
231: mockActionInvocation.getInvocationContext(),
232: actionContext);
233:
234: mockControlActionInvocation.replay();
235:
236: MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
237: String operationMode = interceptor
238: .getRequestOperationMode(mockActionInvocation);
239:
240: assertEquals(operationMode, MessageStoreInterceptor.STORE_MODE);
241:
242: mockControlActionInvocation.verify();
243: }
244:
245: public void testRequestOperationMode3() throws Exception {
246:
247: Map paramMap = new LinkedHashMap();
248:
249: ActionContext actionContext = new ActionContext(new HashMap());
250: actionContext.put(ActionContext.PARAMETERS, paramMap);
251:
252: MockControl mockControlActionInvocation = MockControl
253: .createControl(ActionInvocation.class);
254: ActionInvocation mockActionInvocation = (ActionInvocation) mockControlActionInvocation
255: .getMock();
256: mockControlActionInvocation.expectAndDefaultReturn(
257: mockActionInvocation.getInvocationContext(),
258: actionContext);
259:
260: mockControlActionInvocation.replay();
261:
262: MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
263: String operationMode = interceptor
264: .getRequestOperationMode(mockActionInvocation);
265:
266: assertEquals(operationMode, MessageStoreInterceptor.NONE);
267:
268: mockControlActionInvocation.verify();
269: }
270: }
|