KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > services > impl > TestCookieSource


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.services.impl;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.List JavaDoc;
19
20 import javax.servlet.http.Cookie JavaDoc;
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23
24 import org.apache.hivemind.test.HiveMindTestCase;
25 import org.easymock.MockControl;
26
27 /**
28  * Tests for {@link org.apache.tapestry.services.impl.CookieSourceImpl}.
29  *
30  * @author Howard Lewis Ship
31  * @since 4.0
32  */

33 public class TestCookieSource extends HiveMindTestCase
34 {
35     private static class ComparableCookie extends Cookie JavaDoc
36     {
37         public ComparableCookie(String JavaDoc name, String JavaDoc value)
38         {
39             super(name, value);
40         }
41
42         public boolean equals(Object JavaDoc obj)
43         {
44             Cookie JavaDoc c = (Cookie JavaDoc) obj;
45
46             return equals(getName(), c.getName()) && equals(getValue(), c.getValue())
47                     && equals(getPath(), c.getPath());
48         }
49
50         private boolean equals(Object JavaDoc value, Object JavaDoc other)
51         {
52             return value == other || (value != null && value.equals(other));
53         }
54     }
55
56     private HttpServletRequest JavaDoc setupRequest(String JavaDoc[] nameValues)
57     {
58         Cookie JavaDoc[] cookies = null;
59
60         if (nameValues != null)
61         {
62
63             List JavaDoc l = new ArrayList JavaDoc();
64
65             for (int i = 0; i < nameValues.length; i += 2)
66             {
67                 String JavaDoc name = nameValues[i];
68                 String JavaDoc value = nameValues[i + 1];
69
70                 Cookie JavaDoc c = new Cookie JavaDoc(name, value);
71
72                 l.add(c);
73             }
74
75             cookies = (Cookie JavaDoc[]) l.toArray(new Cookie JavaDoc[l.size()]);
76         }
77
78         MockControl control = newControl(HttpServletRequest JavaDoc.class);
79         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) control.getMock();
80
81         request.getCookies();
82         control.setReturnValue(cookies);
83
84         return request;
85     }
86
87     private void attempt(String JavaDoc name, String JavaDoc expected, String JavaDoc[] nameValues)
88     {
89         HttpServletRequest JavaDoc request = setupRequest(nameValues);
90
91         CookieSourceImpl cs = new CookieSourceImpl();
92
93         cs.setRequest(request);
94
95         replayControls();
96
97         String JavaDoc actual = cs.readCookieValue(name);
98
99         assertEquals(expected, actual);
100
101         verifyControls();
102     }
103
104     public void testNoCookies()
105     {
106         attempt("foo", null, null);
107     }
108
109     public void testMatch()
110     {
111         attempt("fred", "flintstone", new String JavaDoc[]
112         { "barney", "rubble", "fred", "flintstone" });
113     }
114
115     public void testNoMatch()
116     {
117         attempt("foo", null, new String JavaDoc[]
118         { "bar", "baz" });
119     }
120
121     public void testWriteCookie()
122     {
123         MockControl requestControl = newControl(HttpServletRequest JavaDoc.class);
124         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) requestControl.getMock();
125
126         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) newMock(HttpServletResponse JavaDoc.class);
127
128         // Training
129

130         request.getContextPath();
131         requestControl.setReturnValue("/context");
132
133         Cookie JavaDoc cookie = new ComparableCookie("foo", "bar");
134         cookie.setPath("/context");
135
136         response.addCookie(cookie);
137
138         replayControls();
139
140         CookieSourceImpl cs = new CookieSourceImpl();
141         cs.setRequest(request);
142         cs.setResponse(response);
143
144         cs.writeCookieValue("foo", "bar");
145
146         verifyControls();
147     }
148
149     public void testRemoveCookie()
150     {
151         MockControl requestControl = newControl(HttpServletRequest JavaDoc.class);
152         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) requestControl.getMock();
153
154         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) newMock(HttpServletResponse JavaDoc.class);
155
156         // Training
157

158         request.getContextPath();
159         requestControl.setReturnValue("/context");
160
161         Cookie JavaDoc cookie = new ComparableCookie("foo", null);
162         cookie.setPath("/context");
163
164         response.addCookie(cookie);
165
166         replayControls();
167
168         CookieSourceImpl cs = new CookieSourceImpl();
169         cs.setRequest(request);
170         cs.setResponse(response);
171
172         cs.removeCookieValue("foo");
173
174         verifyControls();
175     }
176 }
Popular Tags