KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > request > TestRequestContext


1 // Copyright 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.request;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.Enumeration JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24 import javax.servlet.http.HttpSession JavaDoc;
25
26 import org.apache.hivemind.test.HiveMindTestCase;
27 import org.easymock.MockControl;
28
29 /**
30  * Tests for {@link org.apache.tapestry.request.RequestContext}.
31  *
32  * @author Howard M. Lewis Ship
33  * @since 4.0
34  */

35 public class TestRequestContext extends HiveMindTestCase
36 {
37     private HttpSession JavaDoc newSession()
38     {
39         return (HttpSession JavaDoc) newMock(HttpSession JavaDoc.class);
40     }
41
42     private HttpServletRequest JavaDoc newRequest(boolean create, HttpSession JavaDoc session)
43     {
44         MockControl control = newControl(HttpServletRequest JavaDoc.class);
45         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) control.getMock();
46
47         request.getSession(create);
48         control.setReturnValue(session);
49
50         return request;
51     }
52
53     private HttpServletRequest JavaDoc newRequest()
54     {
55         return (HttpServletRequest JavaDoc) newMock(HttpServletRequest JavaDoc.class);
56     }
57
58     private HttpServletResponse JavaDoc newResponse()
59     {
60         return (HttpServletResponse JavaDoc) newMock(HttpServletResponse JavaDoc.class);
61     }
62
63     public void testGetParameter()
64     {
65         MockControl control = newControl(HttpServletRequest JavaDoc.class);
66         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) control.getMock();
67         HttpServletResponse JavaDoc response = newResponse();
68
69         String JavaDoc value = "VALUE";
70
71         request.getParameter("myparam");
72         control.setReturnValue(value);
73
74         replayControls();
75
76         RequestContext rc = new RequestContext(request, response);
77
78         assertSame(value, rc.getParameter("myparam"));
79
80         verifyControls();
81     }
82
83     public void testGetParameters()
84     {
85         MockControl control = newControl(HttpServletRequest JavaDoc.class);
86         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) control.getMock();
87         HttpServletResponse JavaDoc response = newResponse();
88
89         String JavaDoc[] values =
90         { "this", "that" };
91
92         request.getParameterValues("myparam");
93         control.setReturnValue(values);
94
95         replayControls();
96
97         RequestContext rc = new RequestContext(request, response);
98
99         assertSame(values, rc.getParameters("myparam"));
100
101         verifyControls();
102     }
103
104     public void testGetParameterNames()
105     {
106         MockControl control = newControl(HttpServletRequest JavaDoc.class);
107         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) control.getMock();
108         HttpServletResponse JavaDoc response = newResponse();
109
110         List JavaDoc names = new ArrayList JavaDoc();
111         names.add("fred");
112         names.add("barney");
113
114         Enumeration JavaDoc e = Collections.enumeration(names);
115
116         request.getParameterNames();
117         control.setReturnValue(e);
118
119         replayControls();
120
121         RequestContext rc = new RequestContext(request, response);
122
123         String JavaDoc[] pnames = rc.getParameterNames();
124
125         assertListsEqual(new String JavaDoc[]
126         { "fred", "barney" }, pnames);
127
128         verifyControls();
129     }
130
131     public void testGetRequestAndResponse()
132     {
133         HttpServletRequest JavaDoc request = newRequest();
134         HttpServletResponse JavaDoc response = newResponse();
135
136         replayControls();
137
138         RequestContext rc = new RequestContext(request, response);
139
140         assertSame(request, rc.getRequest());
141         assertSame(response, rc.getResponse());
142
143         verifyControls();
144     }
145
146     public void testGetAttribute()
147     {
148         MockControl control = newControl(HttpServletRequest JavaDoc.class);
149         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) control.getMock();
150         HttpServletResponse JavaDoc response = newResponse();
151
152         Object JavaDoc attribute = new Object JavaDoc();
153
154         request.getAttribute("myattr");
155         control.setReturnValue(attribute);
156
157         replayControls();
158
159         RequestContext rc = new RequestContext(request, response);
160
161         assertSame(attribute, rc.getAttribute("myattr"));
162
163         verifyControls();
164     }
165
166     public void testGetSession()
167     {
168         HttpSession JavaDoc session = newSession();
169         HttpServletRequest JavaDoc request = newRequest(false, session);
170         HttpServletResponse JavaDoc response = newResponse();
171
172         replayControls();
173
174         RequestContext rc = new RequestContext(request, response);
175
176         assertSame(session, rc.getSession());
177
178         verifyControls();
179     }
180
181     public void testCreateSession()
182     {
183         HttpSession JavaDoc session = newSession();
184         HttpServletRequest JavaDoc request = newRequest(true, session);
185         HttpServletResponse JavaDoc response = newResponse();
186
187         replayControls();
188
189         RequestContext rc = new RequestContext(request, response);
190
191         assertSame(session, rc.createSession());
192
193         verifyControls();
194     }
195 }
Popular Tags