001: /*
002: * $Id: MockPageContext.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.mock;
022:
023: import javax.servlet.Servlet;
024: import javax.servlet.ServletConfig;
025: import javax.servlet.ServletContext;
026: import javax.servlet.ServletRequest;
027: import javax.servlet.ServletResponse;
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpSession;
030: import javax.servlet.jsp.JspWriter;
031: import javax.servlet.jsp.PageContext;
032: import javax.servlet.jsp.tagext.BodyContent;
033:
034: import java.io.IOException;
035: import java.io.Reader;
036: import java.io.Writer;
037:
038: import java.util.Collections;
039: import java.util.Enumeration;
040: import java.util.HashMap;
041:
042: /**
043: * <p>Mock <strong>ServletContext</strong> object for low-level unit tests of
044: * Struts controller components. Coarser grained tests should be implemented
045: * in terms of the Cactus framework, instead of the mock object classes.</p>
046: *
047: * <p><strong>WARNING</strong> - Only the minimal set of methods needed to
048: * create unit tests is provided, plus additional methods to configure this
049: * object as necessary. Methods for unsupported operations will throw
050: * <code>UnsupportedOperationException</code>.</p>
051: *
052: * <p><strong>WARNING</strong> - Because unit tests operate in a single
053: * threaded environment, no synchronization is performed.</p>
054: *
055: * @version $Rev: 471754 $ $Date: 2005-05-07 12:45:39 -0400 (Sat, 07 May 2005)
056: * $
057: */
058: public class MockPageContext extends PageContext {
059: // ----------------------------------------------------- Instance Variables
060: protected ServletContext application = null;
061: protected HashMap attributes = new HashMap(); // Page scope attributes
062: protected ServletConfig config = null;
063: protected ServletRequest request = null;
064: protected ServletResponse response = null;
065: protected HttpSession session = null;
066: private boolean throwIOException;
067: private boolean returnBodyContent;
068:
069: // ----------------------------------------------------------- Constructors
070: public MockPageContext() {
071: super ();
072: }
073:
074: public MockPageContext(ServletConfig config,
075: ServletRequest request, ServletResponse response) {
076: super ();
077: setValues(config, request, response);
078: }
079:
080: /**
081: * <p> Construct a new PageContext impl. </p>
082: *
083: * @param throwIOException Determines if the returned JspWriter should
084: * throw an IOException on any method call.
085: * @param returnBody Determines if getOut() should return a new
086: * <code>JspWriter</code> or a <code>BodyContent</code>.
087: */
088: public MockPageContext(boolean throwIOException, boolean returnBody) {
089: this .throwIOException = throwIOException;
090: this .returnBodyContent = returnBody;
091: }
092:
093: private void checkAndThrow() throws IOException {
094: if (throwIOException) {
095: throw new IOException();
096: }
097: }
098:
099: // --------------------------------------------------------- Public Methods
100: public void setValues(ServletConfig config, ServletRequest request,
101: ServletResponse response) {
102: this .config = config;
103:
104: if (config != null) {
105: this .application = config.getServletContext();
106: } else {
107: this .application = null;
108: }
109:
110: this .request = request;
111: this .response = response;
112:
113: if (request != null) {
114: session = ((HttpServletRequest) request).getSession(false);
115: } else {
116: this .session = null;
117: }
118: }
119:
120: // ---------------------------------------------------- PageContext Methods
121: public Object findAttribute(String name) {
122: Object value = getAttribute(name, PageContext.PAGE_SCOPE);
123:
124: if (value == null) {
125: value = getAttribute(name, PageContext.REQUEST_SCOPE);
126: }
127:
128: if (value == null) {
129: value = getAttribute(name, PageContext.SESSION_SCOPE);
130: }
131:
132: if (value == null) {
133: value = getAttribute(name, PageContext.APPLICATION_SCOPE);
134: }
135:
136: return (value);
137: }
138:
139: public void forward(String path) {
140: throw new UnsupportedOperationException();
141: }
142:
143: public Object getAttribute(String name) {
144: return (getAttribute(name, PageContext.PAGE_SCOPE));
145: }
146:
147: public Object getAttribute(String name, int scope) {
148: if (scope == PageContext.PAGE_SCOPE) {
149: return (attributes.get(name));
150: } else if (scope == PageContext.REQUEST_SCOPE) {
151: if (request != null) {
152: return (request.getAttribute(name));
153: }
154:
155: return (null);
156: } else if (scope == PageContext.SESSION_SCOPE) {
157: if (session != null) {
158: return (session.getAttribute(name));
159: }
160:
161: return (null);
162: } else if (scope == PageContext.APPLICATION_SCOPE) {
163: if (application != null) {
164: return (application.getAttribute(name));
165: }
166:
167: return (null);
168: } else {
169: throw new IllegalArgumentException("Invalid scope " + scope);
170: }
171: }
172:
173: public Enumeration getAttributeNamesInScope(int scope) {
174: if (scope == PageContext.PAGE_SCOPE) {
175: return (new MockEnumeration(attributes.keySet().iterator()));
176: } else if (scope == PageContext.REQUEST_SCOPE) {
177: if (request != null) {
178: return (request.getAttributeNames());
179: }
180:
181: return (new MockEnumeration(Collections.EMPTY_LIST
182: .iterator()));
183: } else if (scope == PageContext.SESSION_SCOPE) {
184: if (session != null) {
185: return (session.getAttributeNames());
186: }
187:
188: return (new MockEnumeration(Collections.EMPTY_LIST
189: .iterator()));
190: } else if (scope == PageContext.APPLICATION_SCOPE) {
191: if (application != null) {
192: return (application.getAttributeNames());
193: }
194:
195: return new MockEnumeration(Collections.EMPTY_LIST
196: .iterator());
197: } else {
198: throw new IllegalArgumentException("Invalid scope " + scope);
199: }
200: }
201:
202: public int getAttributesScope(String name) {
203: if (attributes.get(name) != null) {
204: return (PageContext.PAGE_SCOPE);
205: } else if ((request != null)
206: && (request.getAttribute(name) != null)) {
207: return (PageContext.REQUEST_SCOPE);
208: } else if ((session != null)
209: && (session.getAttribute(name) != null)) {
210: return (PageContext.SESSION_SCOPE);
211: } else if ((application != null)
212: && (application.getAttribute(name) != null)) {
213: return (PageContext.APPLICATION_SCOPE);
214: } else {
215: return (0);
216: }
217: }
218:
219: public Exception getException() {
220: throw new UnsupportedOperationException();
221: }
222:
223: /**
224: * <p> Custom JspWriter that throws the specified exception (supplied on
225: * the constructor...if any), else it simply returns. </p>
226: */
227: public JspWriter getOut() {
228: JspWriter jspWriter = new JspWriter(0, false) {
229: public void print(String s) throws IOException {
230: checkAndThrow();
231: }
232:
233: public void newLine() throws IOException {
234: checkAndThrow();
235: }
236:
237: public void print(boolean b) throws IOException {
238: checkAndThrow();
239: }
240:
241: public void print(char c) throws IOException {
242: checkAndThrow();
243: }
244:
245: public void print(int i) throws IOException {
246: checkAndThrow();
247: }
248:
249: public void print(long l) throws IOException {
250: checkAndThrow();
251: }
252:
253: public void print(float f) throws IOException {
254: checkAndThrow();
255: }
256:
257: public void print(double d) throws IOException {
258: checkAndThrow();
259: }
260:
261: public void print(char[] s) throws IOException {
262: checkAndThrow();
263: }
264:
265: public void print(Object obj) throws IOException {
266: checkAndThrow();
267: }
268:
269: public void println() throws IOException {
270: checkAndThrow();
271: }
272:
273: public void println(boolean x) throws IOException {
274: checkAndThrow();
275: }
276:
277: public void println(char x) throws IOException {
278: checkAndThrow();
279: }
280:
281: public void println(int x) throws IOException {
282: checkAndThrow();
283: }
284:
285: public void println(long x) throws IOException {
286: checkAndThrow();
287: }
288:
289: public void println(float x) throws IOException {
290: checkAndThrow();
291: }
292:
293: public void println(double x) throws IOException {
294: checkAndThrow();
295: }
296:
297: public void println(char[] x) throws IOException {
298: checkAndThrow();
299: }
300:
301: public void println(String x) throws IOException {
302: checkAndThrow();
303: }
304:
305: public void println(Object x) throws IOException {
306: checkAndThrow();
307: }
308:
309: public void clear() throws IOException {
310: checkAndThrow();
311: }
312:
313: public void clearBuffer() throws IOException {
314: checkAndThrow();
315: }
316:
317: public void flush() throws IOException {
318: checkAndThrow();
319: }
320:
321: public void close() throws IOException {
322: checkAndThrow();
323: }
324:
325: public int getRemaining() {
326: return 0;
327: }
328:
329: public void write(char[] cbuf, int off, int len)
330: throws IOException {
331: checkAndThrow();
332: }
333: };
334:
335: if (returnBodyContent) {
336: return new BodyContent(jspWriter) {
337: public Reader getReader() {
338: return null;
339: }
340:
341: public String getString() {
342: return null;
343: }
344:
345: public void writeOut(Writer out) throws IOException {
346: checkAndThrow();
347: }
348:
349: public void newLine() throws IOException {
350: checkAndThrow();
351: }
352:
353: public void print(boolean b) throws IOException {
354: checkAndThrow();
355: }
356:
357: public void print(char c) throws IOException {
358: checkAndThrow();
359: }
360:
361: public void print(int i) throws IOException {
362: checkAndThrow();
363: }
364:
365: public void print(long l) throws IOException {
366: checkAndThrow();
367: }
368:
369: public void print(float f) throws IOException {
370: checkAndThrow();
371: }
372:
373: public void print(double d) throws IOException {
374: checkAndThrow();
375: }
376:
377: public void print(char[] s) throws IOException {
378: checkAndThrow();
379: }
380:
381: public void print(String s) throws IOException {
382: checkAndThrow();
383: }
384:
385: public void print(Object obj) throws IOException {
386: checkAndThrow();
387: }
388:
389: public void println() throws IOException {
390: checkAndThrow();
391: }
392:
393: public void println(boolean x) throws IOException {
394: checkAndThrow();
395: }
396:
397: public void println(char x) throws IOException {
398: checkAndThrow();
399: }
400:
401: public void println(int x) throws IOException {
402: checkAndThrow();
403: }
404:
405: public void println(long x) throws IOException {
406: checkAndThrow();
407: }
408:
409: public void println(float x) throws IOException {
410: checkAndThrow();
411: }
412:
413: public void println(double x) throws IOException {
414: checkAndThrow();
415: }
416:
417: public void println(char[] x) throws IOException {
418: checkAndThrow();
419: }
420:
421: public void println(String x) throws IOException {
422: checkAndThrow();
423: }
424:
425: public void println(Object x) throws IOException {
426: checkAndThrow();
427: }
428:
429: public void clear() throws IOException {
430: checkAndThrow();
431: }
432:
433: public void clearBuffer() throws IOException {
434: checkAndThrow();
435: }
436:
437: public void close() throws IOException {
438: checkAndThrow();
439: }
440:
441: public int getRemaining() {
442: return 0;
443: }
444:
445: public void write(char[] cbuf, int off, int len)
446: throws IOException {
447: checkAndThrow();
448: }
449: };
450: }
451:
452: return jspWriter;
453: }
454:
455: public Object getPage() {
456: throw new UnsupportedOperationException();
457: }
458:
459: public ServletRequest getRequest() {
460: return (this .request);
461: }
462:
463: public ServletResponse getResponse() {
464: return (this .response);
465: }
466:
467: public ServletConfig getServletConfig() {
468: return (this .config);
469: }
470:
471: public ServletContext getServletContext() {
472: return (this .application);
473: }
474:
475: public HttpSession getSession() {
476: return (this .session);
477: }
478:
479: public void handlePageException(Exception e) {
480: throw new UnsupportedOperationException();
481: }
482:
483: public void handlePageException(Throwable t) {
484: throw new UnsupportedOperationException();
485: }
486:
487: public void include(String path) {
488: throw new UnsupportedOperationException();
489: }
490:
491: public void initialize(Servlet servlet, ServletRequest request,
492: ServletResponse response, String errorPageURL,
493: boolean needsSession, int bufferSize, boolean autoFlush) {
494: throw new UnsupportedOperationException();
495: }
496:
497: public JspWriter popBody() {
498: throw new UnsupportedOperationException();
499: }
500:
501: public BodyContent pushBody() {
502: throw new UnsupportedOperationException();
503: }
504:
505: public void release() {
506: throw new UnsupportedOperationException();
507: }
508:
509: public void removeAttribute(String name) {
510: int scope = getAttributesScope(name);
511:
512: if (scope != 0) {
513: removeAttribute(name, scope);
514: }
515: }
516:
517: public void removeAttribute(String name, int scope) {
518: if (scope == PageContext.PAGE_SCOPE) {
519: attributes.remove(name);
520: } else if (scope == PageContext.REQUEST_SCOPE) {
521: if (request != null) {
522: request.removeAttribute(name);
523: }
524: } else if (scope == PageContext.SESSION_SCOPE) {
525: if (session != null) {
526: session.removeAttribute(name);
527: }
528: } else if (scope == PageContext.APPLICATION_SCOPE) {
529: if (application != null) {
530: application.removeAttribute(name);
531: }
532: } else {
533: throw new IllegalArgumentException("Invalid scope " + scope);
534: }
535: }
536:
537: public void setAttribute(String name, Object value) {
538: setAttribute(name, value, PageContext.PAGE_SCOPE);
539: }
540:
541: public void setAttribute(String name, Object value, int scope) {
542: if (scope == PageContext.PAGE_SCOPE) {
543: attributes.put(name, value);
544: } else if (scope == PageContext.REQUEST_SCOPE) {
545: if (request != null) {
546: request.setAttribute(name, value);
547: }
548: } else if (scope == PageContext.SESSION_SCOPE) {
549: if (session != null) {
550: session.setAttribute(name, value);
551: }
552: } else if (scope == PageContext.APPLICATION_SCOPE) {
553: if (application != null) {
554: application.setAttribute(name, value);
555: }
556: } else {
557: throw new IllegalArgumentException("Invalid scope " + scope);
558: }
559: }
560: }
|