001: /*
002: * Copyright (c) 2002-2007 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.interceptor;
006:
007: import java.util.Collections;
008: import java.util.Map;
009:
010: import javax.servlet.http.Cookie;
011:
012: import org.easymock.MockControl;
013: import org.springframework.mock.web.MockHttpServletRequest;
014:
015: import com.opensymphony.webwork.ServletActionContext;
016: import com.opensymphony.webwork.WebWorkTestCase;
017: import com.opensymphony.xwork.Action;
018: import com.opensymphony.xwork.ActionContext;
019: import com.opensymphony.xwork.ActionInvocation;
020: import com.opensymphony.xwork.ActionSupport;
021:
022: /**
023: * @author tmjee
024: * @version $Date: 2007-01-09 18:04:09 +0100 (Tue, 09 Jan 2007) $ $Id: CookieInterceptorTest.java 2798 2007-01-09 17:04:09Z tmjee $
025: */
026: public class CookieInterceptorTest extends WebWorkTestCase {
027:
028: public void testIntercepDefault() throws Exception {
029: MockHttpServletRequest request = new MockHttpServletRequest();
030: request.setCookies(new Cookie[] {
031: new Cookie("cookie1", "cookie1value"),
032: new Cookie("cookie2", "cookie2value"),
033: new Cookie("cookie3", "cookie3value") });
034: ServletActionContext.setRequest(request);
035:
036: MockActionWithCookieAware action = new MockActionWithCookieAware();
037:
038: ActionContext.getContext().getValueStack().push(action);
039:
040: MockControl actionInvocationControl = MockControl
041: .createControl(ActionInvocation.class);
042: ActionInvocation invocation = (ActionInvocation) actionInvocationControl
043: .getMock();
044: actionInvocationControl.expectAndDefaultReturn(invocation
045: .getAction(), action);
046: actionInvocationControl.expectAndDefaultReturn(invocation
047: .invoke(), Action.SUCCESS);
048:
049: actionInvocationControl.replay();
050:
051: // by default the interceptor doesn't accept any cookies
052: CookieInterceptor interceptor = new CookieInterceptor();
053: interceptor.intercept(invocation);
054:
055: assertTrue(action.getCookiesMap().isEmpty());
056: assertNull(action.getCookie1(), null);
057: assertNull(action.getCookie2(), null);
058: assertNull(action.getCookie3(), null);
059: assertNull(ActionContext.getContext().getValueStack()
060: .findValue("cookie1"));
061: assertNull(ActionContext.getContext().getValueStack()
062: .findValue("cookie2"));
063: assertNull(ActionContext.getContext().getValueStack()
064: .findValue("cookie3"));
065: }
066:
067: public void testInterceptAll1() throws Exception {
068: MockHttpServletRequest request = new MockHttpServletRequest();
069: request.setCookies(new Cookie[] {
070: new Cookie("cookie1", "cookie1value"),
071: new Cookie("cookie2", "cookie2value"),
072: new Cookie("cookie3", "cookie3value") });
073: ServletActionContext.setRequest(request);
074:
075: MockActionWithCookieAware action = new MockActionWithCookieAware();
076:
077: ActionContext.getContext().getValueStack().push(action);
078:
079: MockControl actionInvocationControl = MockControl
080: .createControl(ActionInvocation.class);
081: ActionInvocation invocation = (ActionInvocation) actionInvocationControl
082: .getMock();
083: actionInvocationControl.expectAndDefaultReturn(invocation
084: .getAction(), action);
085: actionInvocationControl.expectAndDefaultReturn(invocation
086: .invoke(), Action.SUCCESS);
087:
088: actionInvocationControl.replay();
089:
090: CookieInterceptor interceptor = new CookieInterceptor();
091: interceptor.setCookiesName("*");
092: interceptor.setCookiesValue("*");
093: interceptor.intercept(invocation);
094:
095: assertFalse(action.getCookiesMap().isEmpty());
096: assertEquals(action.getCookiesMap().size(), 3);
097: assertEquals(action.getCookiesMap().get("cookie1"),
098: "cookie1value");
099: assertEquals(action.getCookiesMap().get("cookie2"),
100: "cookie2value");
101: assertEquals(action.getCookiesMap().get("cookie3"),
102: "cookie3value");
103: assertEquals(action.getCookie1(), "cookie1value");
104: assertEquals(action.getCookie2(), "cookie2value");
105: assertEquals(action.getCookie3(), "cookie3value");
106: assertEquals(ActionContext.getContext().getValueStack()
107: .findValue("cookie1"), "cookie1value");
108: assertEquals(ActionContext.getContext().getValueStack()
109: .findValue("cookie2"), "cookie2value");
110: assertEquals(ActionContext.getContext().getValueStack()
111: .findValue("cookie3"), "cookie3value");
112: }
113:
114: public void testInterceptAll2() throws Exception {
115: MockHttpServletRequest request = new MockHttpServletRequest();
116: request.setCookies(new Cookie[] {
117: new Cookie("cookie1", "cookie1value"),
118: new Cookie("cookie2", "cookie2value"),
119: new Cookie("cookie3", "cookie3value") });
120: ServletActionContext.setRequest(request);
121:
122: MockActionWithCookieAware action = new MockActionWithCookieAware();
123:
124: ActionContext.getContext().getValueStack().push(action);
125:
126: MockControl actionInvocationControl = MockControl
127: .createControl(ActionInvocation.class);
128: ActionInvocation invocation = (ActionInvocation) actionInvocationControl
129: .getMock();
130: actionInvocationControl.expectAndDefaultReturn(invocation
131: .getAction(), action);
132: actionInvocationControl.expectAndDefaultReturn(invocation
133: .invoke(), Action.SUCCESS);
134:
135: actionInvocationControl.replay();
136:
137: CookieInterceptor interceptor = new CookieInterceptor();
138: interceptor.setCookiesName("cookie1, cookie2, cookie3");
139: interceptor
140: .setCookiesValue("cookie1value, cookie2value, cookie3value");
141: interceptor.intercept(invocation);
142:
143: assertFalse(action.getCookiesMap().isEmpty());
144: assertEquals(action.getCookiesMap().size(), 3);
145: assertEquals(action.getCookiesMap().get("cookie1"),
146: "cookie1value");
147: assertEquals(action.getCookiesMap().get("cookie2"),
148: "cookie2value");
149: assertEquals(action.getCookiesMap().get("cookie3"),
150: "cookie3value");
151: assertEquals(action.getCookie1(), "cookie1value");
152: assertEquals(action.getCookie2(), "cookie2value");
153: assertEquals(action.getCookie3(), "cookie3value");
154: assertEquals(ActionContext.getContext().getValueStack()
155: .findValue("cookie1"), "cookie1value");
156: assertEquals(ActionContext.getContext().getValueStack()
157: .findValue("cookie2"), "cookie2value");
158: assertEquals(ActionContext.getContext().getValueStack()
159: .findValue("cookie3"), "cookie3value");
160: }
161:
162: public void testInterceptSelectedCookiesNameOnly1()
163: throws Exception {
164: MockHttpServletRequest request = new MockHttpServletRequest();
165: request.setCookies(new Cookie[] {
166: new Cookie("cookie1", "cookie1value"),
167: new Cookie("cookie2", "cookie2value"),
168: new Cookie("cookie3", "cookie3value") });
169: ServletActionContext.setRequest(request);
170:
171: MockActionWithCookieAware action = new MockActionWithCookieAware();
172:
173: ActionContext.getContext().getValueStack().push(action);
174:
175: MockControl actionInvocationControl = MockControl
176: .createControl(ActionInvocation.class);
177: ActionInvocation invocation = (ActionInvocation) actionInvocationControl
178: .getMock();
179: actionInvocationControl.expectAndDefaultReturn(invocation
180: .getAction(), action);
181: actionInvocationControl.expectAndDefaultReturn(invocation
182: .invoke(), Action.SUCCESS);
183:
184: actionInvocationControl.replay();
185:
186: CookieInterceptor interceptor = new CookieInterceptor();
187: interceptor.setCookiesName("cookie1, cookie3");
188: interceptor
189: .setCookiesValue("cookie1value, cookie2value, cookie3value");
190: interceptor.intercept(invocation);
191:
192: assertFalse(action.getCookiesMap().isEmpty());
193: assertEquals(action.getCookiesMap().size(), 2);
194: assertEquals(action.getCookiesMap().get("cookie1"),
195: "cookie1value");
196: assertEquals(action.getCookiesMap().get("cookie2"), null);
197: assertEquals(action.getCookiesMap().get("cookie3"),
198: "cookie3value");
199: assertEquals(action.getCookie1(), "cookie1value");
200: assertEquals(action.getCookie2(), null);
201: assertEquals(action.getCookie3(), "cookie3value");
202: assertEquals(ActionContext.getContext().getValueStack()
203: .findValue("cookie1"), "cookie1value");
204: assertEquals(ActionContext.getContext().getValueStack()
205: .findValue("cookie2"), null);
206: assertEquals(ActionContext.getContext().getValueStack()
207: .findValue("cookie3"), "cookie3value");
208: }
209:
210: public void testInterceptSelectedCookiesNameOnly2()
211: throws Exception {
212: MockHttpServletRequest request = new MockHttpServletRequest();
213: request.setCookies(new Cookie[] {
214: new Cookie("cookie1", "cookie1value"),
215: new Cookie("cookie2", "cookie2value"),
216: new Cookie("cookie3", "cookie3value") });
217: ServletActionContext.setRequest(request);
218:
219: MockActionWithCookieAware action = new MockActionWithCookieAware();
220:
221: ActionContext.getContext().getValueStack().push(action);
222:
223: MockControl actionInvocationControl = MockControl
224: .createControl(ActionInvocation.class);
225: ActionInvocation invocation = (ActionInvocation) actionInvocationControl
226: .getMock();
227: actionInvocationControl.expectAndDefaultReturn(invocation
228: .getAction(), action);
229: actionInvocationControl.expectAndDefaultReturn(invocation
230: .invoke(), Action.SUCCESS);
231:
232: actionInvocationControl.replay();
233:
234: CookieInterceptor interceptor = new CookieInterceptor();
235: interceptor.setCookiesName("cookie1, cookie3");
236: interceptor.setCookiesValue("*");
237: interceptor.intercept(invocation);
238:
239: assertFalse(action.getCookiesMap().isEmpty());
240: assertEquals(action.getCookiesMap().size(), 2);
241: assertEquals(action.getCookiesMap().get("cookie1"),
242: "cookie1value");
243: assertEquals(action.getCookiesMap().get("cookie2"), null);
244: assertEquals(action.getCookiesMap().get("cookie3"),
245: "cookie3value");
246: assertEquals(action.getCookie1(), "cookie1value");
247: assertEquals(action.getCookie2(), null);
248: assertEquals(action.getCookie3(), "cookie3value");
249: assertEquals(ActionContext.getContext().getValueStack()
250: .findValue("cookie1"), "cookie1value");
251: assertEquals(ActionContext.getContext().getValueStack()
252: .findValue("cookie2"), null);
253: assertEquals(ActionContext.getContext().getValueStack()
254: .findValue("cookie3"), "cookie3value");
255: }
256:
257: public void testInterceptSelectedCookiesNameOnly3()
258: throws Exception {
259: MockHttpServletRequest request = new MockHttpServletRequest();
260: request.setCookies(new Cookie[] {
261: new Cookie("cookie1", "cookie1value"),
262: new Cookie("cookie2", "cookie2value"),
263: new Cookie("cookie3", "cookie3value") });
264: ServletActionContext.setRequest(request);
265:
266: MockActionWithCookieAware action = new MockActionWithCookieAware();
267:
268: ActionContext.getContext().getValueStack().push(action);
269:
270: MockControl actionInvocationControl = MockControl
271: .createControl(ActionInvocation.class);
272: ActionInvocation invocation = (ActionInvocation) actionInvocationControl
273: .getMock();
274: actionInvocationControl.expectAndDefaultReturn(invocation
275: .getAction(), action);
276: actionInvocationControl.expectAndDefaultReturn(invocation
277: .invoke(), Action.SUCCESS);
278:
279: actionInvocationControl.replay();
280:
281: CookieInterceptor interceptor = new CookieInterceptor();
282: interceptor.setCookiesName("cookie1, cookie3");
283: interceptor.setCookiesValue("");
284: interceptor.intercept(invocation);
285:
286: assertFalse(action.getCookiesMap().isEmpty());
287: assertEquals(action.getCookiesMap().size(), 2);
288: assertEquals(action.getCookiesMap().get("cookie1"),
289: "cookie1value");
290: assertEquals(action.getCookiesMap().get("cookie2"), null);
291: assertEquals(action.getCookiesMap().get("cookie3"),
292: "cookie3value");
293: assertEquals(action.getCookie1(), "cookie1value");
294: assertEquals(action.getCookie2(), null);
295: assertEquals(action.getCookie3(), "cookie3value");
296: assertEquals(ActionContext.getContext().getValueStack()
297: .findValue("cookie1"), "cookie1value");
298: assertEquals(ActionContext.getContext().getValueStack()
299: .findValue("cookie2"), null);
300: assertEquals(ActionContext.getContext().getValueStack()
301: .findValue("cookie3"), "cookie3value");
302: }
303:
304: public void testInterceptSelectedCookiesNameAndValue()
305: throws Exception {
306: MockHttpServletRequest request = new MockHttpServletRequest();
307: request.setCookies(new Cookie[] {
308: new Cookie("cookie1", "cookie1value"),
309: new Cookie("cookie2", "cookie2value"),
310: new Cookie("cookie3", "cookie3value") });
311: ServletActionContext.setRequest(request);
312:
313: MockActionWithCookieAware action = new MockActionWithCookieAware();
314:
315: ActionContext.getContext().getValueStack().push(action);
316:
317: MockControl actionInvocationControl = MockControl
318: .createControl(ActionInvocation.class);
319: ActionInvocation invocation = (ActionInvocation) actionInvocationControl
320: .getMock();
321: actionInvocationControl.expectAndDefaultReturn(invocation
322: .getAction(), action);
323: actionInvocationControl.expectAndDefaultReturn(invocation
324: .invoke(), Action.SUCCESS);
325:
326: actionInvocationControl.replay();
327:
328: CookieInterceptor interceptor = new CookieInterceptor();
329: interceptor.setCookiesName("cookie1, cookie3");
330: interceptor.setCookiesValue("cookie1value");
331: interceptor.intercept(invocation);
332:
333: assertFalse(action.getCookiesMap().isEmpty());
334: assertEquals(action.getCookiesMap().size(), 1);
335: assertEquals(action.getCookiesMap().get("cookie1"),
336: "cookie1value");
337: assertEquals(action.getCookiesMap().get("cookie2"), null);
338: assertEquals(action.getCookiesMap().get("cookie3"), null);
339: assertEquals(action.getCookie1(), "cookie1value");
340: assertEquals(action.getCookie2(), null);
341: assertEquals(action.getCookie3(), null);
342: assertEquals(ActionContext.getContext().getValueStack()
343: .findValue("cookie1"), "cookie1value");
344: assertEquals(ActionContext.getContext().getValueStack()
345: .findValue("cookie2"), null);
346: assertEquals(ActionContext.getContext().getValueStack()
347: .findValue("cookie3"), null);
348: }
349:
350: /**
351: * @author tmjee
352: * @version $Date: 2007-01-09 18:04:09 +0100 (Tue, 09 Jan 2007) $ $Id: CookieInterceptorTest.java 2798 2007-01-09 17:04:09Z tmjee $
353: */
354: public static class MockActionWithCookieAware extends ActionSupport
355: implements CookiesAware {
356:
357: private static final long serialVersionUID = -6202290616812813386L;
358:
359: private Map cookies = Collections.EMPTY_MAP;
360: private String cookie1;
361: private String cookie2;
362: private String cookie3;
363:
364: public void setCookiesMap(Map cookies) {
365: this .cookies = cookies;
366: }
367:
368: public Map getCookiesMap() {
369: return this .cookies;
370: }
371:
372: public String getCookie1() {
373: return cookie1;
374: }
375:
376: public void setCookie1(String cookie1) {
377: this .cookie1 = cookie1;
378: }
379:
380: public String getCookie2() {
381: return cookie2;
382: }
383:
384: public void setCookie2(String cookie2) {
385: this .cookie2 = cookie2;
386: }
387:
388: public String getCookie3() {
389: return cookie3;
390: }
391:
392: public void setCookie3(String cookie3) {
393: this.cookie3 = cookie3;
394: }
395: }
396:
397: }
|