1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.mock;
19
20 import javax.servlet.Servlet;
21 import javax.servlet.ServletConfig;
22 import javax.servlet.ServletContext;
23 import javax.servlet.ServletRequest;
24 import javax.servlet.ServletResponse;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpSession;
27 import javax.servlet.jsp.JspWriter;
28 import javax.servlet.jsp.PageContext;
29 import javax.servlet.jsp.tagext.BodyContent;
30
31 import java.io.IOException;
32 import java.io.Reader;
33 import java.io.Writer;
34
35 import java.util.Collections;
36 import java.util.Enumeration;
37 import java.util.HashMap;
38
39 /***
40 * <p>Mock <strong>ServletContext</strong> object for low-level unit tests of
41 * Struts controller components. Coarser grained tests should be implemented
42 * in terms of the Cactus framework, instead of the mock object classes.</p>
43 *
44 * <p><strong>WARNING</strong> - Only the minimal set of methods needed to
45 * create unit tests is provided, plus additional methods to configure this
46 * object as necessary. Methods for unsupported operations will throw
47 * <code>UnsupportedOperationException</code>.</p>
48 *
49 * <p><strong>WARNING</strong> - Because unit tests operate in a single
50 * threaded environment, no synchronization is performed.</p>
51 *
52 * @version $Rev: 421119 $ $Date: 2005-05-07 12:45:39 -0400 (Sat, 07 May 2005)
53 * $
54 */
55 public class MockPageContext extends PageContext {
56
57 protected ServletContext application = null;
58 protected HashMap attributes = new HashMap();
59 protected ServletConfig config = null;
60 protected ServletRequest request = null;
61 protected ServletResponse response = null;
62 protected HttpSession session = null;
63 private boolean throwIOException;
64 private boolean returnBodyContent;
65
66
67 public MockPageContext() {
68 super();
69 }
70
71 public MockPageContext(ServletConfig config, ServletRequest request,
72 ServletResponse response) {
73 super();
74 setValues(config, request, response);
75 }
76
77 /***
78 * <p> Construct a new PageContext impl. </p>
79 *
80 * @param throwIOException Determines if the returned JspWriter should
81 * throw an IOException on any method call.
82 * @param returnBody Determines if getOut() should return a new
83 * <code>JspWriter</code> or a <code>BodyContent</code>.
84 */
85 public MockPageContext(boolean throwIOException, boolean returnBody) {
86 this.throwIOException = throwIOException;
87 this.returnBodyContent = returnBody;
88 }
89
90 private void checkAndThrow()
91 throws IOException {
92 if (throwIOException) {
93 throw new IOException();
94 }
95 }
96
97
98 public void setValues(ServletConfig config, ServletRequest request,
99 ServletResponse response) {
100 this.config = config;
101
102 if (config != null) {
103 this.application = config.getServletContext();
104 } else {
105 this.application = null;
106 }
107
108 this.request = request;
109 this.response = response;
110
111 if (request != null) {
112 session = ((HttpServletRequest) request).getSession(false);
113 } else {
114 this.session = null;
115 }
116 }
117
118
119 public Object findAttribute(String name) {
120 Object value = getAttribute(name, PageContext.PAGE_SCOPE);
121
122 if (value == null) {
123 value = getAttribute(name, PageContext.REQUEST_SCOPE);
124 }
125
126 if (value == null) {
127 value = getAttribute(name, PageContext.SESSION_SCOPE);
128 }
129
130 if (value == null) {
131 value = getAttribute(name, PageContext.APPLICATION_SCOPE);
132 }
133
134 return (value);
135 }
136
137 public void forward(String path) {
138 throw new UnsupportedOperationException();
139 }
140
141 public Object getAttribute(String name) {
142 return (getAttribute(name, PageContext.PAGE_SCOPE));
143 }
144
145 public Object getAttribute(String name, int scope) {
146 if (scope == PageContext.PAGE_SCOPE) {
147 return (attributes.get(name));
148 } else if (scope == PageContext.REQUEST_SCOPE) {
149 if (request != null) {
150 return (request.getAttribute(name));
151 }
152
153 return (null);
154 } else if (scope == PageContext.SESSION_SCOPE) {
155 if (session != null) {
156 return (session.getAttribute(name));
157 }
158
159 return (null);
160 } else if (scope == PageContext.APPLICATION_SCOPE) {
161 if (application != null) {
162 return (application.getAttribute(name));
163 }
164
165 return (null);
166 } else {
167 throw new IllegalArgumentException("Invalid scope " + scope);
168 }
169 }
170
171 public Enumeration getAttributeNamesInScope(int scope) {
172 if (scope == PageContext.PAGE_SCOPE) {
173 return (new MockEnumeration(attributes.keySet().iterator()));
174 } else if (scope == PageContext.REQUEST_SCOPE) {
175 if (request != null) {
176 return (request.getAttributeNames());
177 }
178
179 return (new MockEnumeration(Collections.EMPTY_LIST.iterator()));
180 } else if (scope == PageContext.SESSION_SCOPE) {
181 if (session != null) {
182 return (session.getAttributeNames());
183 }
184
185 return (new MockEnumeration(Collections.EMPTY_LIST.iterator()));
186 } else if (scope == PageContext.APPLICATION_SCOPE) {
187 if (application != null) {
188 return (application.getAttributeNames());
189 }
190
191 return new MockEnumeration(Collections.EMPTY_LIST.iterator());
192 } else {
193 throw new IllegalArgumentException("Invalid scope " + scope);
194 }
195 }
196
197 public int getAttributesScope(String name) {
198 if (attributes.get(name) != null) {
199 return (PageContext.PAGE_SCOPE);
200 } else if ((request != null) && (request.getAttribute(name) != null)) {
201 return (PageContext.REQUEST_SCOPE);
202 } else if ((session != null) && (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 public Exception getException() {
213 throw new UnsupportedOperationException();
214 }
215
216 /***
217 * <p> Custom JspWriter that throws the specified exception (supplied on
218 * the constructor...if any), else it simply returns. </p>
219 */
220 public JspWriter getOut() {
221 JspWriter jspWriter =
222 new JspWriter(0, false) {
223 public void print(String s)
224 throws IOException {
225 checkAndThrow();
226 }
227
228 public void newLine()
229 throws IOException {
230 checkAndThrow();
231 }
232
233 public void print(boolean b)
234 throws IOException {
235 checkAndThrow();
236 }
237
238 public void print(char c)
239 throws IOException {
240 checkAndThrow();
241 }
242
243 public void print(int i)
244 throws IOException {
245 checkAndThrow();
246 }
247
248 public void print(long l)
249 throws IOException {
250 checkAndThrow();
251 }
252
253 public void print(float f)
254 throws IOException {
255 checkAndThrow();
256 }
257
258 public void print(double d)
259 throws IOException {
260 checkAndThrow();
261 }
262
263 public void print(char[] s)
264 throws IOException {
265 checkAndThrow();
266 }
267
268 public void print(Object obj)
269 throws IOException {
270 checkAndThrow();
271 }
272
273 public void println()
274 throws IOException {
275 checkAndThrow();
276 }
277
278 public void println(boolean x)
279 throws IOException {
280 checkAndThrow();
281 }
282
283 public void println(char x)
284 throws IOException {
285 checkAndThrow();
286 }
287
288 public void println(int x)
289 throws IOException {
290 checkAndThrow();
291 }
292
293 public void println(long x)
294 throws IOException {
295 checkAndThrow();
296 }
297
298 public void println(float x)
299 throws IOException {
300 checkAndThrow();
301 }
302
303 public void println(double x)
304 throws IOException {
305 checkAndThrow();
306 }
307
308 public void println(char[] x)
309 throws IOException {
310 checkAndThrow();
311 }
312
313 public void println(String x)
314 throws IOException {
315 checkAndThrow();
316 }
317
318 public void println(Object x)
319 throws IOException {
320 checkAndThrow();
321 }
322
323 public void clear()
324 throws IOException {
325 checkAndThrow();
326 }
327
328 public void clearBuffer()
329 throws IOException {
330 checkAndThrow();
331 }
332
333 public void flush()
334 throws IOException {
335 checkAndThrow();
336 }
337
338 public void close()
339 throws IOException {
340 checkAndThrow();
341 }
342
343 public int getRemaining() {
344 return 0;
345 }
346
347 public void write(char[] cbuf, int off, int len)
348 throws IOException {
349 checkAndThrow();
350 }
351 };
352
353 if (returnBodyContent) {
354 return new BodyContent(jspWriter) {
355 public Reader getReader() {
356 return null;
357 }
358
359 public String getString() {
360 return null;
361 }
362
363 public void writeOut(Writer out)
364 throws IOException {
365 checkAndThrow();
366 }
367
368 public void newLine()
369 throws IOException {
370 checkAndThrow();
371 }
372
373 public void print(boolean b)
374 throws IOException {
375 checkAndThrow();
376 }
377
378 public void print(char c)
379 throws IOException {
380 checkAndThrow();
381 }
382
383 public void print(int i)
384 throws IOException {
385 checkAndThrow();
386 }
387
388 public void print(long l)
389 throws IOException {
390 checkAndThrow();
391 }
392
393 public void print(float f)
394 throws IOException {
395 checkAndThrow();
396 }
397
398 public void print(double d)
399 throws IOException {
400 checkAndThrow();
401 }
402
403 public void print(char[] s)
404 throws IOException {
405 checkAndThrow();
406 }
407
408 public void print(String s)
409 throws IOException {
410 checkAndThrow();
411 }
412
413 public void print(Object obj)
414 throws IOException {
415 checkAndThrow();
416 }
417
418 public void println()
419 throws IOException {
420 checkAndThrow();
421 }
422
423 public void println(boolean x)
424 throws IOException {
425 checkAndThrow();
426 }
427
428 public void println(char x)
429 throws IOException {
430 checkAndThrow();
431 }
432
433 public void println(int x)
434 throws IOException {
435 checkAndThrow();
436 }
437
438 public void println(long x)
439 throws IOException {
440 checkAndThrow();
441 }
442
443 public void println(float x)
444 throws IOException {
445 checkAndThrow();
446 }
447
448 public void println(double x)
449 throws IOException {
450 checkAndThrow();
451 }
452
453 public void println(char[] x)
454 throws IOException {
455 checkAndThrow();
456 }
457
458 public void println(String x)
459 throws IOException {
460 checkAndThrow();
461 }
462
463 public void println(Object x)
464 throws IOException {
465 checkAndThrow();
466 }
467
468 public void clear()
469 throws IOException {
470 checkAndThrow();
471 }
472
473 public void clearBuffer()
474 throws IOException {
475 checkAndThrow();
476 }
477
478 public void close()
479 throws IOException {
480 checkAndThrow();
481 }
482
483 public int getRemaining() {
484 return 0;
485 }
486
487 public void write(char[] cbuf, int off, int len)
488 throws IOException {
489 checkAndThrow();
490 }
491 };
492 }
493
494 return jspWriter;
495 }
496
497 public Object getPage() {
498 throw new UnsupportedOperationException();
499 }
500
501 public ServletRequest getRequest() {
502 return (this.request);
503 }
504
505 public ServletResponse getResponse() {
506 return (this.response);
507 }
508
509 public ServletConfig getServletConfig() {
510 return (this.config);
511 }
512
513 public ServletContext getServletContext() {
514 return (this.application);
515 }
516
517 public HttpSession getSession() {
518 return (this.session);
519 }
520
521 public void handlePageException(Exception e) {
522 throw new UnsupportedOperationException();
523 }
524
525 public void handlePageException(Throwable t) {
526 throw new UnsupportedOperationException();
527 }
528
529 public void include(String path) {
530 throw new UnsupportedOperationException();
531 }
532
533 public void initialize(Servlet servlet, ServletRequest request,
534 ServletResponse response, String errorPageURL, boolean needsSession,
535 int bufferSize, boolean autoFlush) {
536 throw new UnsupportedOperationException();
537 }
538
539 public JspWriter popBody() {
540 throw new UnsupportedOperationException();
541 }
542
543 public BodyContent pushBody() {
544 throw new UnsupportedOperationException();
545 }
546
547 public void release() {
548 throw new UnsupportedOperationException();
549 }
550
551 public void removeAttribute(String name) {
552 int scope = getAttributesScope(name);
553
554 if (scope != 0) {
555 removeAttribute(name, scope);
556 }
557 }
558
559 public void removeAttribute(String name, int scope) {
560 if (scope == PageContext.PAGE_SCOPE) {
561 attributes.remove(name);
562 } else if (scope == PageContext.REQUEST_SCOPE) {
563 if (request != null) {
564 request.removeAttribute(name);
565 }
566 } else if (scope == PageContext.SESSION_SCOPE) {
567 if (session != null) {
568 session.removeAttribute(name);
569 }
570 } else if (scope == PageContext.APPLICATION_SCOPE) {
571 if (application != null) {
572 application.removeAttribute(name);
573 }
574 } else {
575 throw new IllegalArgumentException("Invalid scope " + scope);
576 }
577 }
578
579 public void setAttribute(String name, Object value) {
580 setAttribute(name, value, PageContext.PAGE_SCOPE);
581 }
582
583 public void setAttribute(String name, Object value, int scope) {
584 if (scope == PageContext.PAGE_SCOPE) {
585 attributes.put(name, value);
586 } else if (scope == PageContext.REQUEST_SCOPE) {
587 if (request != null) {
588 request.setAttribute(name, value);
589 }
590 } else if (scope == PageContext.SESSION_SCOPE) {
591 if (session != null) {
592 session.setAttribute(name, value);
593 }
594 } else if (scope == PageContext.APPLICATION_SCOPE) {
595 if (application != null) {
596 application.setAttribute(name, value);
597 }
598 } else {
599 throw new IllegalArgumentException("Invalid scope " + scope);
600 }
601 }
602 }