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