01: // StrutsTestCase - a JUnit extension for testing Struts actions
02: // within the context of the ActionServlet.
03: // Copyright (C) 2002 Deryl Seale
04: //
05: // This library is free software; you can redistribute it and/or
06: // modify it under the terms of the Apache Software License as
07: // published by the Apache Software Foundation; either version 1.1
08: // of the License, or (at your option) any later version.
09: //
10: // This library is distributed in the hope that it will be useful,
11: // but WITHOUT ANY WARRANTY; without even the implied warranty of
12: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: // Apache Software Foundation Licens for more details.
14: //
15: // You may view the full text here: http://www.apache.org/LICENSE.txt
16:
17: package servletunit.struts;
18:
19: import org.apache.cactus.server.ServletContextWrapper;
20: import servletunit.RequestDispatcherSimulator;
21:
22: import javax.servlet.RequestDispatcher;
23: import javax.servlet.ServletContext;
24: import java.net.URL;
25: import java.net.MalformedURLException;
26: import java.io.File;
27: import java.io.InputStream;
28: import java.io.FileInputStream;
29: import java.io.FileNotFoundException;
30:
31: /**
32: * A wrapper for the ServletContext class. This is used in
33: * CactusStrutsTestCase so that we can retrieve the forward
34: * processed by the ActionServlet and use absolute paths
35: * for Struts resources. This allows us to to use
36: * the ActionServlet as a black box, rather than mimic its
37: * behavior as was previously the case.
38: */
39: public class StrutsServletContextWrapper extends ServletContextWrapper {
40:
41: boolean processRequest = false;
42: private String dispatchedResource;
43:
44: public StrutsServletContextWrapper(ServletContext context) {
45: super (context);
46: }
47:
48: public void setProcessRequest(boolean flag) {
49: this .processRequest = flag;
50: }
51:
52: public RequestDispatcher getRequestDispatcher(String path) {
53: dispatchedResource = path;
54: if (!processRequest)
55: return new RequestDispatcherSimulator(path);
56: else
57: return super .getRequestDispatcher(path);
58: }
59:
60: public String getForward() {
61: return dispatchedResource;
62: }
63:
64: /**
65: * Override the getResource method to look for resources in the file system, allowing
66: * the use of absolute paths for Struts configuration files. If the resource path exists
67: * in the file system, this method will return a URL based on the supplied path; otherwise,
68: * it defers to the ServletContext loader.
69: */
70: public URL getResource(String pathname)
71: throws MalformedURLException {
72: File file = new File(pathname);
73: if (file.exists())
74: return file.toURL();
75: else
76: return super .getResource(pathname);
77: }
78:
79: /**
80: * Override the getResourceAsStream method to look for resources in the file system, allowing
81: * the use of absolute paths for Struts configuration files. If the resource path exists
82: * in the file system, this method will return a URL based on the supplied path; otherwise,
83: * it defers to the ServletContext loader.
84: */
85: public InputStream getResourceAsStream(String pathname) {
86: File file = new File(pathname);
87: if (file.exists())
88: try {
89: return new FileInputStream(file);
90: } catch (FileNotFoundException e) {
91: return super.getResourceAsStream(pathname);
92: }
93: else
94: return super.getResourceAsStream(pathname);
95: }
96:
97: }
|