View Javadoc

1   /*
2    * $Id: CookieInterceptorTest.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2.interceptor;
23  
24  import java.util.Collections;
25  import java.util.Map;
26  
27  import javax.servlet.http.Cookie;
28  
29  import org.easymock.MockControl;
30  import org.springframework.mock.web.MockHttpServletRequest;
31  
32  import org.apache.struts2.ServletActionContext;
33  import org.apache.struts2.StrutsTestCase;
34  import com.opensymphony.xwork2.Action;
35  import com.opensymphony.xwork2.ActionContext;
36  import com.opensymphony.xwork2.ActionInvocation;
37  import com.opensymphony.xwork2.ActionSupport;
38  
39  public class CookieInterceptorTest extends StrutsTestCase {
40  
41  
42      public void testIntercepDefault() throws Exception {
43          MockHttpServletRequest request = new MockHttpServletRequest();
44          request.setCookies(new Cookie[] {
45                  new Cookie("cookie1", "cookie1value"),
46                  new Cookie("cookie2", "cookie2value"),
47                  new Cookie("cookie3", "cookie3value")
48              });
49          ServletActionContext.setRequest(request);
50  
51          MockActionWithCookieAware action = new MockActionWithCookieAware();
52  
53          ActionContext.getContext().getValueStack().push(action);
54  
55          MockControl actionInvocationControl = MockControl.createControl(ActionInvocation.class);
56          ActionInvocation invocation = (ActionInvocation) actionInvocationControl.getMock();
57          actionInvocationControl.expectAndDefaultReturn(
58                                                         invocation.getAction(), action);
59          actionInvocationControl.expectAndDefaultReturn(
60                                                         invocation.invoke(), Action.SUCCESS);
61  
62          actionInvocationControl.replay();
63  
64          // by default the interceptor doesn't accept any cookies
65          CookieInterceptor interceptor = new CookieInterceptor();
66          interceptor.intercept(invocation);
67  
68          assertTrue(action.getCookiesMap().isEmpty());
69          assertNull(action.getCookie1(), null);
70          assertNull(action.getCookie2(), null);
71          assertNull(action.getCookie3(), null);
72          assertNull(ActionContext.getContext().getValueStack().findValue("cookie1"));
73          assertNull(ActionContext.getContext().getValueStack().findValue("cookie2"));
74          assertNull(ActionContext.getContext().getValueStack().findValue("cookie3"));
75      }
76  
77      public void testInterceptAll1() throws Exception {
78          MockHttpServletRequest request = new MockHttpServletRequest();
79          request.setCookies(new Cookie[] {
80                  new Cookie("cookie1", "cookie1value"),
81                  new Cookie("cookie2", "cookie2value"),
82                  new Cookie("cookie3", "cookie3value")
83              });
84          ServletActionContext.setRequest(request);
85  
86          MockActionWithCookieAware action = new MockActionWithCookieAware();
87  
88          ActionContext.getContext().getValueStack().push(action);
89  
90          MockControl actionInvocationControl = MockControl.createControl(ActionInvocation.class);
91          ActionInvocation invocation = (ActionInvocation) actionInvocationControl.getMock();
92          actionInvocationControl.expectAndDefaultReturn(
93                                                         invocation.getAction(), action);
94          actionInvocationControl.expectAndDefaultReturn(
95                                                         invocation.invoke(), Action.SUCCESS);
96  
97          actionInvocationControl.replay();
98  
99          CookieInterceptor interceptor = new CookieInterceptor();
100         interceptor.setCookiesName("*");
101         interceptor.setCookiesValue("*");
102         interceptor.intercept(invocation);
103 
104         assertFalse(action.getCookiesMap().isEmpty());
105         assertEquals(action.getCookiesMap().size(), 3);
106         assertEquals(action.getCookiesMap().get("cookie1"), "cookie1value");
107         assertEquals(action.getCookiesMap().get("cookie2"), "cookie2value");
108         assertEquals(action.getCookiesMap().get("cookie3"), "cookie3value");
109         assertEquals(action.getCookie1(), "cookie1value");
110         assertEquals(action.getCookie2(), "cookie2value");
111         assertEquals(action.getCookie3(), "cookie3value");
112         assertEquals(ActionContext.getContext().getValueStack().findValue("cookie1"), "cookie1value");
113         assertEquals(ActionContext.getContext().getValueStack().findValue("cookie2"), "cookie2value");
114         assertEquals(ActionContext.getContext().getValueStack().findValue("cookie3"), "cookie3value");
115     }
116 
117 
118     public void testInterceptAll2() throws Exception {
119         MockHttpServletRequest request = new MockHttpServletRequest();
120         request.setCookies(new Cookie[] {
121                 new Cookie("cookie1", "cookie1value"),
122                 new Cookie("cookie2", "cookie2value"),
123                 new Cookie("cookie3", "cookie3value")
124             });
125         ServletActionContext.setRequest(request);
126 
127         MockActionWithCookieAware action = new MockActionWithCookieAware();
128 
129         ActionContext.getContext().getValueStack().push(action);
130 
131         MockControl actionInvocationControl = MockControl.createControl(ActionInvocation.class);
132         ActionInvocation invocation = (ActionInvocation) actionInvocationControl.getMock();
133         actionInvocationControl.expectAndDefaultReturn(
134                                                        invocation.getAction(), action);
135         actionInvocationControl.expectAndDefaultReturn(
136                                                        invocation.invoke(), Action.SUCCESS);
137 
138         actionInvocationControl.replay();
139 
140         CookieInterceptor interceptor = new CookieInterceptor();
141         interceptor.setCookiesName("cookie1, cookie2, cookie3");
142         interceptor.setCookiesValue("cookie1value, cookie2value, cookie3value");
143         interceptor.intercept(invocation);
144 
145         assertFalse(action.getCookiesMap().isEmpty());
146         assertEquals(action.getCookiesMap().size(), 3);
147         assertEquals(action.getCookiesMap().get("cookie1"), "cookie1value");
148         assertEquals(action.getCookiesMap().get("cookie2"), "cookie2value");
149         assertEquals(action.getCookiesMap().get("cookie3"), "cookie3value");
150         assertEquals(action.getCookie1(), "cookie1value");
151         assertEquals(action.getCookie2(), "cookie2value");
152         assertEquals(action.getCookie3(), "cookie3value");
153         assertEquals(ActionContext.getContext().getValueStack().findValue("cookie1"), "cookie1value");
154         assertEquals(ActionContext.getContext().getValueStack().findValue("cookie2"), "cookie2value");
155         assertEquals(ActionContext.getContext().getValueStack().findValue("cookie3"), "cookie3value");
156     }
157 
158     public void testInterceptSelectedCookiesNameOnly1() throws Exception {
159         MockHttpServletRequest request = new MockHttpServletRequest();
160         request.setCookies(new Cookie[] {
161                 new Cookie("cookie1", "cookie1value"),
162                 new Cookie("cookie2", "cookie2value"),
163                 new Cookie("cookie3", "cookie3value")
164             });
165         ServletActionContext.setRequest(request);
166 
167         MockActionWithCookieAware action = new MockActionWithCookieAware();
168 
169         ActionContext.getContext().getValueStack().push(action);
170 
171         MockControl actionInvocationControl = MockControl.createControl(ActionInvocation.class);
172         ActionInvocation invocation = (ActionInvocation) actionInvocationControl.getMock();
173         actionInvocationControl.expectAndDefaultReturn(
174                                                        invocation.getAction(), action);
175         actionInvocationControl.expectAndDefaultReturn(
176                                                        invocation.invoke(), Action.SUCCESS);
177 
178         actionInvocationControl.replay();
179 
180         CookieInterceptor interceptor = new CookieInterceptor();
181         interceptor.setCookiesName("cookie1, cookie3");
182         interceptor.setCookiesValue("cookie1value, cookie2value, cookie3value");
183         interceptor.intercept(invocation);
184 
185         assertFalse(action.getCookiesMap().isEmpty());
186         assertEquals(action.getCookiesMap().size(), 2);
187         assertEquals(action.getCookiesMap().get("cookie1"), "cookie1value");
188         assertEquals(action.getCookiesMap().get("cookie2"), null);
189         assertEquals(action.getCookiesMap().get("cookie3"), "cookie3value");
190         assertEquals(action.getCookie1(), "cookie1value");
191         assertEquals(action.getCookie2(), null);
192         assertEquals(action.getCookie3(), "cookie3value");
193         assertEquals(ActionContext.getContext().getValueStack().findValue("cookie1"), "cookie1value");
194         assertEquals(ActionContext.getContext().getValueStack().findValue("cookie2"), null);
195         assertEquals(ActionContext.getContext().getValueStack().findValue("cookie3"), "cookie3value");
196     }
197 
198     public void testInterceptSelectedCookiesNameOnly2() throws Exception {
199         MockHttpServletRequest request = new MockHttpServletRequest();
200         request.setCookies(new Cookie[] {
201                 new Cookie("cookie1", "cookie1value"),
202                 new Cookie("cookie2", "cookie2value"),
203                 new Cookie("cookie3", "cookie3value")
204             });
205         ServletActionContext.setRequest(request);
206 
207         MockActionWithCookieAware action = new MockActionWithCookieAware();
208 
209         ActionContext.getContext().getValueStack().push(action);
210 
211         MockControl actionInvocationControl = MockControl.createControl(ActionInvocation.class);
212         ActionInvocation invocation = (ActionInvocation) actionInvocationControl.getMock();
213         actionInvocationControl.expectAndDefaultReturn(
214                                                        invocation.getAction(), action);
215         actionInvocationControl.expectAndDefaultReturn(
216                                                        invocation.invoke(), Action.SUCCESS);
217 
218         actionInvocationControl.replay();
219 
220         CookieInterceptor interceptor = new CookieInterceptor();
221         interceptor.setCookiesName("cookie1, cookie3");
222         interceptor.setCookiesValue("*");
223         interceptor.intercept(invocation);
224 
225         assertFalse(action.getCookiesMap().isEmpty());
226         assertEquals(action.getCookiesMap().size(), 2);
227         assertEquals(action.getCookiesMap().get("cookie1"), "cookie1value");
228         assertEquals(action.getCookiesMap().get("cookie2"), null);
229         assertEquals(action.getCookiesMap().get("cookie3"), "cookie3value");
230         assertEquals(action.getCookie1(), "cookie1value");
231         assertEquals(action.getCookie2(), null);
232         assertEquals(action.getCookie3(), "cookie3value");
233         assertEquals(ActionContext.getContext().getValueStack().findValue("cookie1"), "cookie1value");
234         assertEquals(ActionContext.getContext().getValueStack().findValue("cookie2"), null);
235         assertEquals(ActionContext.getContext().getValueStack().findValue("cookie3"), "cookie3value");
236     }
237 
238     public void testInterceptSelectedCookiesNameOnly3() throws Exception {
239         MockHttpServletRequest request = new MockHttpServletRequest();
240         request.setCookies(new Cookie[] {
241                 new Cookie("cookie1", "cookie1value"),
242                 new Cookie("cookie2", "cookie2value"),
243                 new Cookie("cookie3", "cookie3value")
244             });
245         ServletActionContext.setRequest(request);
246 
247         MockActionWithCookieAware action = new MockActionWithCookieAware();
248 
249         ActionContext.getContext().getValueStack().push(action);
250 
251         MockControl actionInvocationControl = MockControl.createControl(ActionInvocation.class);
252         ActionInvocation invocation = (ActionInvocation) actionInvocationControl.getMock();
253         actionInvocationControl.expectAndDefaultReturn(
254                                                        invocation.getAction(), action);
255         actionInvocationControl.expectAndDefaultReturn(
256                                                        invocation.invoke(), Action.SUCCESS);
257 
258         actionInvocationControl.replay();
259 
260         CookieInterceptor interceptor = new CookieInterceptor();
261         interceptor.setCookiesName("cookie1, cookie3");
262         interceptor.setCookiesValue("");
263         interceptor.intercept(invocation);
264 
265         assertFalse(action.getCookiesMap().isEmpty());
266         assertEquals(action.getCookiesMap().size(), 2);
267         assertEquals(action.getCookiesMap().get("cookie1"), "cookie1value");
268         assertEquals(action.getCookiesMap().get("cookie2"), null);
269         assertEquals(action.getCookiesMap().get("cookie3"), "cookie3value");
270         assertEquals(action.getCookie1(), "cookie1value");
271         assertEquals(action.getCookie2(), null);
272         assertEquals(action.getCookie3(), "cookie3value");
273         assertEquals(ActionContext.getContext().getValueStack().findValue("cookie1"), "cookie1value");
274         assertEquals(ActionContext.getContext().getValueStack().findValue("cookie2"), null);
275         assertEquals(ActionContext.getContext().getValueStack().findValue("cookie3"), "cookie3value");
276     }
277 
278 
279     public void testInterceptSelectedCookiesNameAndValue() throws Exception {
280         MockHttpServletRequest request = new MockHttpServletRequest();
281         request.setCookies(new Cookie[] {
282                 new Cookie("cookie1", "cookie1value"),
283                 new Cookie("cookie2", "cookie2value"),
284                 new Cookie("cookie3", "cookie3value")
285             });
286         ServletActionContext.setRequest(request);
287 
288         MockActionWithCookieAware action = new MockActionWithCookieAware();
289 
290         ActionContext.getContext().getValueStack().push(action);
291 
292         MockControl actionInvocationControl = MockControl.createControl(ActionInvocation.class);
293         ActionInvocation invocation = (ActionInvocation) actionInvocationControl.getMock();
294         actionInvocationControl.expectAndDefaultReturn(
295                                                        invocation.getAction(), action);
296         actionInvocationControl.expectAndDefaultReturn(
297                                                        invocation.invoke(), Action.SUCCESS);
298 
299         actionInvocationControl.replay();
300 
301         CookieInterceptor interceptor = new CookieInterceptor();
302         interceptor.setCookiesName("cookie1, cookie3");
303         interceptor.setCookiesValue("cookie1value");
304         interceptor.intercept(invocation);
305 
306         assertFalse(action.getCookiesMap().isEmpty());
307         assertEquals(action.getCookiesMap().size(), 1);
308         assertEquals(action.getCookiesMap().get("cookie1"), "cookie1value");
309         assertEquals(action.getCookiesMap().get("cookie2"), null);
310         assertEquals(action.getCookiesMap().get("cookie3"), null);
311         assertEquals(action.getCookie1(), "cookie1value");
312         assertEquals(action.getCookie2(), null);
313         assertEquals(action.getCookie3(), null);
314         assertEquals(ActionContext.getContext().getValueStack().findValue("cookie1"), "cookie1value");
315         assertEquals(ActionContext.getContext().getValueStack().findValue("cookie2"), null);
316         assertEquals(ActionContext.getContext().getValueStack().findValue("cookie3"), null);
317     }
318 
319 
320     public static class MockActionWithCookieAware extends ActionSupport implements CookiesAware {
321 
322         private static final long serialVersionUID = -6202290616812813386L;
323 
324         private Map cookies = Collections.EMPTY_MAP;
325         private String cookie1;
326         private String cookie2;
327         private String cookie3;
328 
329         public void setCookiesMap(Map cookies) {
330             this.cookies = cookies;
331         }
332         public Map getCookiesMap() {
333             return this.cookies;
334         }
335 
336         public String getCookie1() { return cookie1; }
337         public void setCookie1(String cookie1) { this.cookie1 = cookie1; }
338 
339         public String getCookie2() { return cookie2; }
340         public void setCookie2(String cookie2) { this.cookie2 = cookie2; }
341 
342         public String getCookie3() { return cookie3; }
343         public void setCookie3(String cookie3) { this.cookie3 = cookie3; }
344     }
345 
346 }