KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > mock > MockPageContext


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

18
19
20 package org.apache.struts.mock;
21
22
23 import java.util.Collections JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.HashMap JavaDoc;
26
27 import javax.servlet.Servlet JavaDoc;
28 import javax.servlet.ServletConfig JavaDoc;
29 import javax.servlet.ServletContext JavaDoc;
30 import javax.servlet.ServletRequest JavaDoc;
31 import javax.servlet.ServletResponse JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpSession JavaDoc;
34 import javax.servlet.jsp.JspWriter JavaDoc;
35 import javax.servlet.jsp.PageContext JavaDoc;
36 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
37
38
39 /**
40  * <p>Mock <strong>ServletContext</strong> object for low-level unit tests
41  * of Struts controller components. Coarser grained tests should be
42  * implemented in terms of the Cactus framework, instead of the mock
43  * object classes.</p>
44  *
45  * <p><strong>WARNING</strong> - Only the minimal set of methods needed to
46  * create unit tests is provided, plus additional methods to configure this
47  * object as necessary. Methods for unsupported operations will throw
48  * <code>UnsupportedOperationException</code>.</p>
49  *
50  * <p><strong>WARNING</strong> - Because unit tests operate in a single
51  * threaded environment, no synchronization is performed.</p>
52  *
53  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
54  */

55
56 public class MockPageContext extends PageContext JavaDoc {
57
58
59
60     // ----------------------------------------------------------- Constructors
61

62
63     public MockPageContext() {
64         super();
65     }
66
67
68     public MockPageContext(ServletConfig JavaDoc config,
69                            ServletRequest JavaDoc request,
70                            ServletResponse JavaDoc response) {
71         super();
72         setValues(config, request, response);
73     }
74
75
76     // ----------------------------------------------------- Instance Variables
77

78
79     protected ServletContext JavaDoc application = null;
80     protected HashMap JavaDoc attributes = new HashMap JavaDoc(); // Page scope attributes
81
protected ServletConfig JavaDoc config = null;
82     protected ServletRequest JavaDoc request = null;
83     protected ServletResponse JavaDoc response = null;
84     protected HttpSession JavaDoc session = null;
85
86
87     // --------------------------------------------------------- Public Methods
88

89
90     public void setValues(ServletConfig JavaDoc config,
91                           ServletRequest JavaDoc request,
92                           ServletResponse JavaDoc response) {
93         this.config = config;
94         if (config != null) {
95             this.application = config.getServletContext();
96         } else {
97             this.application = null;
98         }
99         this.request = request;
100         this.response = response;
101         if (request != null) {
102             session = ((HttpServletRequest JavaDoc) request).getSession(false);
103         } else {
104             this.session = null;
105         }
106     }
107
108
109
110     // ---------------------------------------------------- PageContext Methods
111

112
113     public Object JavaDoc findAttribute(String JavaDoc name) {
114         Object JavaDoc value = getAttribute(name, PageContext.PAGE_SCOPE);
115         if (value == null) {
116             value = getAttribute(name, PageContext.REQUEST_SCOPE);
117         }
118         if (value == null) {
119             value = getAttribute(name, PageContext.SESSION_SCOPE);
120         }
121         if (value == null) {
122             value = getAttribute(name, PageContext.APPLICATION_SCOPE);
123         }
124         return (value);
125     }
126
127
128     public void forward(String JavaDoc path) {
129         throw new UnsupportedOperationException JavaDoc();
130     }
131
132
133     public Object JavaDoc getAttribute(String JavaDoc name) {
134         return (getAttribute(name, PageContext.PAGE_SCOPE));
135     }
136
137
138     public Object JavaDoc getAttribute(String JavaDoc name, int scope) {
139         if (scope == PageContext.PAGE_SCOPE) {
140             return (attributes.get(name));
141         } else if (scope == PageContext.REQUEST_SCOPE) {
142             if (request != null) {
143                 return (request.getAttribute(name));
144             } else {
145                 return (null);
146             }
147         } else if (scope == PageContext.SESSION_SCOPE) {
148             if (session != null) {
149                 return (session.getAttribute(name));
150             } else {
151                 return (null);
152             }
153         } else if (scope == PageContext.APPLICATION_SCOPE) {
154             if (application != null) {
155                 return (application.getAttribute(name));
156             } else {
157                 return (null);
158             }
159         } else {
160             throw new IllegalArgumentException JavaDoc("Invalid scope " + scope);
161         }
162     }
163
164
165     public Enumeration JavaDoc getAttributeNamesInScope(int scope) {
166         if (scope == PageContext.PAGE_SCOPE) {
167             return (new MockEnumeration(attributes.keySet().iterator()));
168         } else if (scope == PageContext.REQUEST_SCOPE) {
169             if (request != null) {
170                 return (request.getAttributeNames());
171             } else {
172                 return
173                     (new MockEnumeration(Collections.EMPTY_LIST.iterator()));
174             }
175         } else if (scope == PageContext.SESSION_SCOPE) {
176             if (session != null) {
177                 return (session.getAttributeNames());
178             } else {
179                 return
180                     (new MockEnumeration(Collections.EMPTY_LIST.iterator()));
181             }
182         } else if (scope == PageContext.APPLICATION_SCOPE) {
183             if (application != null) {
184                 return (application.getAttributeNames());
185             } else {
186                 return
187                     (new MockEnumeration(Collections.EMPTY_LIST.iterator()));
188             }
189         } else {
190             throw new IllegalArgumentException JavaDoc("Invalid scope " + scope);
191         }
192     }
193
194
195     public int getAttributesScope(String JavaDoc name) {
196         if (attributes.get(name) != null) {
197             return (PageContext.PAGE_SCOPE);
198         } else if ((request != null) &&
199                    (request.getAttribute(name) != null)) {
200             return (PageContext.REQUEST_SCOPE);
201         } else if ((session != null) &&
202                    (session.getAttribute(name) != null)) {
203             return (PageContext.SESSION_SCOPE);
204         } else if ((application != null) &&
205                    (application.getAttribute(name) != null)) {
206             return (PageContext.APPLICATION_SCOPE);
207         } else {
208             return (0);
209         }
210     }
211
212
213     public Exception JavaDoc getException() {
214         throw new UnsupportedOperationException JavaDoc();
215     }
216
217
218     public JspWriter JavaDoc getOut() {
219         throw new UnsupportedOperationException JavaDoc();
220     }
221
222
223     public Object JavaDoc getPage() {
224         throw new UnsupportedOperationException JavaDoc();
225     }
226
227
228     public ServletRequest JavaDoc getRequest() {
229         return (this.request);
230     }
231
232
233     public ServletResponse JavaDoc getResponse() {
234         return (this.response);
235     }
236
237
238     public ServletConfig JavaDoc getServletConfig() {
239         return (this.config);
240     }
241
242
243     public ServletContext JavaDoc getServletContext() {
244         return (this.application);
245     }
246
247
248     public HttpSession JavaDoc getSession() {
249         return (this.session);
250     }
251
252
253     public void handlePageException(Exception JavaDoc e) {
254         throw new UnsupportedOperationException JavaDoc();
255     }
256
257
258     public void handlePageException(Throwable JavaDoc t) {
259         throw new UnsupportedOperationException JavaDoc();
260     }
261
262
263     public void include(String JavaDoc path) {
264         throw new UnsupportedOperationException JavaDoc();
265     }
266
267
268     public void initialize(Servlet JavaDoc servlet, ServletRequest JavaDoc request,
269                            ServletResponse JavaDoc response, String JavaDoc errorPageURL,
270                            boolean needsSession, int bufferSize,
271                            boolean autoFlush) {
272         throw new UnsupportedOperationException JavaDoc();
273     }
274
275
276     public JspWriter JavaDoc popBody() {
277         throw new UnsupportedOperationException JavaDoc();
278     }
279
280
281     public BodyContent JavaDoc pushBody() {
282         throw new UnsupportedOperationException JavaDoc();
283     }
284
285
286     public void release() {
287         throw new UnsupportedOperationException JavaDoc();
288     }
289
290
291     public void removeAttribute(String JavaDoc name) {
292         int scope = getAttributesScope(name);
293         if (scope != 0) {
294             removeAttribute(name, scope);
295         }
296     }
297
298
299     public void removeAttribute(String JavaDoc name, int scope) {
300         if (scope == PageContext.PAGE_SCOPE) {
301             attributes.remove(name);
302         } else if (scope == PageContext.REQUEST_SCOPE) {
303             if (request != null) {
304                 request.removeAttribute(name);
305             }
306         } else if (scope == PageContext.SESSION_SCOPE) {
307             if (session != null) {
308                 session.removeAttribute(name);
309             }
310         } else if (scope == PageContext.APPLICATION_SCOPE) {
311             if (application != null) {
312                 application.removeAttribute(name);
313             }
314         } else {
315             throw new IllegalArgumentException JavaDoc("Invalid scope " + scope);
316         }
317     }
318
319
320     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
321         setAttribute(name, value, PageContext.PAGE_SCOPE);
322     }
323
324
325     public void setAttribute(String JavaDoc name, Object JavaDoc value, int scope) {
326         if (scope == PageContext.PAGE_SCOPE) {
327             attributes.put(name, value);
328         } else if (scope == PageContext.REQUEST_SCOPE) {
329             if (request != null) {
330                 request.setAttribute(name, value);
331             }
332         } else if (scope == PageContext.SESSION_SCOPE) {
333             if (session != null) {
334                 session.setAttribute(name, value);
335             }
336         } else if (scope == PageContext.APPLICATION_SCOPE) {
337             if (application != null) {
338                 application.setAttribute(name, value);
339             }
340         } else {
341             throw new IllegalArgumentException JavaDoc("Invalid scope " + scope);
342         }
343     }
344
345
346 }
347
Popular Tags