KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > junit > internal > http > MockPageContext


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.junit.internal.http;
8
9
10 import java.io.IOException JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import javax.servlet.Servlet JavaDoc;
18 import javax.servlet.ServletConfig JavaDoc;
19 import javax.servlet.ServletContext JavaDoc;
20 import javax.servlet.ServletException JavaDoc;
21 import javax.servlet.ServletRequest JavaDoc;
22 import javax.servlet.ServletResponse JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpSession JavaDoc;
25 import javax.servlet.jsp.JspWriter JavaDoc;
26 import javax.servlet.jsp.PageContext JavaDoc;
27
28
29 /**
30  * <p>
31  * This class
32  * </p>
33  *
34  * @author Brian Pontarelli
35  * @since 2.0
36  * @version 2.0
37  */

38 public class MockPageContext extends PageContext JavaDoc {
39
40     private ServletConfig JavaDoc config;
41     private ServletRequest JavaDoc request;
42     private ServletResponse JavaDoc response;
43     private Map JavaDoc attributes;
44     private Throwable JavaDoc t;
45     private JspWriter JavaDoc out = new MockJspWriter(MockJspWriter.NO_BUFFER, true);
46     
47
48     /**
49      * Constructor for MockPageContext.
50      */

51     public MockPageContext(ServletConfig JavaDoc config, ServletRequest JavaDoc request,
52             ServletResponse JavaDoc response) {
53         super();
54         this.config = config;
55         this.request = request;
56         this.response = response;
57         this.attributes = new HashMap JavaDoc();
58     }
59
60
61     /**
62      */

63     public void initialize(Servlet JavaDoc servlet, ServletRequest JavaDoc request,
64             ServletResponse JavaDoc response, String JavaDoc errorPageURL, boolean session,
65             int bufferSize, boolean autoFlush)
66     throws IOException JavaDoc, IllegalStateException JavaDoc, IllegalArgumentException JavaDoc {
67         this.config = servlet.getServletConfig();
68         this.request = request;
69         this.response = response;
70         //this.errorPageURL = errorPageURL;
71
//this.needsSession = session;
72
//this.bufferSize = bufferSize;
73
//this.autoFlush = autoFlush;
74
}
75
76     /**
77      * no op
78      */

79     public void release() {
80     }
81
82     /**
83      * Sets the attribute to the given value
84      */

85     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
86         attributes.put(name, value);
87     }
88
89     /**
90      * Sets the attribute to the given value
91      */

92     public void setAttribute(String JavaDoc name, Object JavaDoc value, int scope) {
93         switch (scope) {
94             case PAGE_SCOPE:
95                 setAttribute(name, value);
96                 break;
97             case REQUEST_SCOPE:
98                 request.setAttribute(name, value);
99                 break;
100             case SESSION_SCOPE:
101                 ((HttpServletRequest JavaDoc) request).getSession().setAttribute(name, value);
102                 break;
103             case APPLICATION_SCOPE:
104                 ((HttpServletRequest JavaDoc) request).getSession().getServletContext().setAttribute(name, value);
105                 break;
106             default:
107                 throw new IllegalArgumentException JavaDoc("Invalid scope");
108         }
109     }
110
111     /**
112      * Returns the attribute from the attributes Map
113      */

114     public Object JavaDoc getAttribute(String JavaDoc name) {
115         return attributes.get(name);
116     }
117
118     /**
119      * Returns the attribute from the scope
120      */

121     public Object JavaDoc getAttribute(String JavaDoc name, int scope) {
122         Object JavaDoc value = null;
123         switch (scope) {
124             case PAGE_SCOPE:
125                 value = getAttribute(name);
126                 break;
127             case REQUEST_SCOPE:
128                 value = request.getAttribute(name);
129                 break;
130             case SESSION_SCOPE:
131                 value = ((HttpServletRequest JavaDoc) request).getSession().getAttribute(name);
132                 break;
133             case APPLICATION_SCOPE:
134                 value = ((HttpServletRequest JavaDoc) request).getSession().getServletContext().getAttribute(name);
135                 break;
136             default:
137                 throw new IllegalArgumentException JavaDoc("Invalid scope");
138         }
139         return value;
140     }
141
142     /**
143      * Finds the attribute in all the scopes
144      */

145     public Object JavaDoc findAttribute(String JavaDoc name) {
146         Object JavaDoc value = getAttribute(name);
147         if (value == null) {
148             value = request.getAttribute(name);
149         }
150         
151         if (value == null) {
152             value = ((HttpServletRequest JavaDoc) request).getSession().getAttribute(name);
153         }
154         
155         if (value == null) {
156             value = ((HttpServletRequest JavaDoc) request).getSession().getServletContext().getAttribute(name);
157         }
158         return value;
159     }
160
161     /**
162      * Removes the attribute
163      */

164     public void removeAttribute(String JavaDoc name) {
165         attributes.remove(name);
166     }
167
168     /**
169      * Removes the attribute from the scope
170      */

171     public void removeAttribute(String JavaDoc name, int scope) {
172         switch (scope) {
173             case PAGE_SCOPE:
174                 removeAttribute(name);
175             case REQUEST_SCOPE:
176                 request.removeAttribute(name);
177                 break;
178             case SESSION_SCOPE:
179                 ((HttpServletRequest JavaDoc) request).getSession().removeAttribute(name);
180                 break;
181             case APPLICATION_SCOPE:
182                 ((HttpServletRequest JavaDoc) request).getSession().getServletContext().removeAttribute(name);
183                 break;
184             default:
185                 throw new IllegalArgumentException JavaDoc("Invalid scope");
186         }
187     }
188
189     /**
190      * Returns the scope of the attribute
191      */

192     public int getAttributesScope(String JavaDoc name) {
193         Object JavaDoc value = getAttribute(name);
194         if (value == null) {
195             value = request.getAttribute(name);
196         } else {
197             return PAGE_SCOPE;
198         }
199
200         if (value == null) {
201             value = ((HttpServletRequest JavaDoc) request).getSession().getAttribute(name);
202         } else {
203             return REQUEST_SCOPE;
204         }
205
206         if (value == null) {
207             value = ((HttpServletRequest JavaDoc) request).getSession().getServletContext().getAttribute(name);
208         } else {
209             return SESSION_SCOPE;
210         }
211
212         if (value != null) {
213             return APPLICATION_SCOPE;
214         }
215         return 0;
216     }
217
218     /**
219      * Returns the attribute names from the scope
220      */

221     public Enumeration JavaDoc getAttributeNamesInScope(int scope) {
222         switch (scope) {
223             case PAGE_SCOPE:
224                 Set JavaDoc keys = attributes.keySet();
225                 final Iterator JavaDoc iter = keys.iterator();
226     
227                 return new Enumeration JavaDoc() {
228                     public boolean hasMoreElements() {
229                         return iter.hasNext();
230                     }
231                     public Object JavaDoc nextElement() {
232                         return iter.next();
233                     }
234                 };
235             case REQUEST_SCOPE:
236                 return request.getAttributeNames();
237             case SESSION_SCOPE:
238                 return ((HttpServletRequest JavaDoc) request).getSession().getAttributeNames();
239             case APPLICATION_SCOPE:
240                 return ((HttpServletRequest JavaDoc) request).getSession().getServletContext().getAttributeNames();
241             default:
242                 throw new IllegalArgumentException JavaDoc("Invalid scope");
243         }
244     }
245
246     /**
247      */

248     public JspWriter JavaDoc getOut() {
249         return out;
250     }
251
252     /**
253      * Returns the session
254      */

255     public HttpSession JavaDoc getSession() {
256         return ((HttpServletRequest JavaDoc) request).getSession();
257     }
258
259     /**
260      * Returns null
261      */

262     public Object JavaDoc getPage() {
263         return null;
264     }
265
266     /**
267      * Returns the request
268      */

269     public ServletRequest JavaDoc getRequest() {
270         return request;
271     }
272
273     /**
274      * Returns the response
275      */

276     public ServletResponse JavaDoc getResponse() {
277         return response;
278     }
279
280     /**
281      * Returns the exception
282      */

283     public Exception JavaDoc getException() {
284         return (Exception JavaDoc) t;
285     }
286
287     /**
288      * Returns the servlet config
289      */

290     public ServletConfig JavaDoc getServletConfig() {
291         return config;
292     }
293
294     /**
295      * Returns the context
296      */

297     public ServletContext JavaDoc getServletContext() {
298         return ((HttpServletRequest JavaDoc) request).getSession().getServletContext();
299     }
300
301     /**
302      * No op
303      */

304     public void forward(String JavaDoc url) throws ServletException JavaDoc, IOException JavaDoc {
305     }
306
307     /**
308      * No op
309      */

310     public void include(String JavaDoc url) throws ServletException JavaDoc, IOException JavaDoc {
311     }
312
313     /**
314      * Stores the exception
315      */

316     public void handlePageException(Exception JavaDoc e)
317     throws ServletException JavaDoc, IOException JavaDoc {
318         this.t = e;
319     }
320
321     /**
322      * Stores the exception
323      */

324     public void handlePageException(Throwable JavaDoc t)
325     throws ServletException JavaDoc, IOException JavaDoc {
326         this.t = t;
327     }
328
329
330     //-------------------------------------------------------------------------
331
// Helper methods
332
//-------------------------------------------------------------------------
333

334     /**
335      * Returns the mock jsp writer
336      */

337     public MockJspWriter getMockOut() {
338         return (MockJspWriter) out;
339     }
340
341     /**
342      * Clears the contents of the mock jsp writer
343      */

344     public void clearMockOut() {
345         ((MockJspWriter) out).clear();
346     }
347 }
Popular Tags