001: package com.meterware.servletunit;
002:
003: /********************************************************************************************************************
004: * $Id: ServletRunner.java,v 1.30 2004/09/29 17:15:26 russgold Exp $
005: *
006: * Copyright (c) 2000-2004, Russell Gold
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
009: * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
010: * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
011: * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included in all copies or substantial portions
014: * of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
017: * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
019: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020: * DEALINGS IN THE SOFTWARE.
021: *
022: *******************************************************************************************************************/
023: import java.io.File;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.net.MalformedURLException;
027: import java.util.Dictionary;
028: import java.util.Hashtable;
029:
030: import com.meterware.httpunit.HttpUnitUtils;
031: import com.meterware.httpunit.WebRequest;
032: import com.meterware.httpunit.WebResponse;
033: import com.meterware.httpunit.FrameSelector;
034:
035: import org.xml.sax.InputSource;
036: import org.xml.sax.SAXException;
037:
038: import javax.servlet.http.HttpSession;
039:
040: /**
041: * This class acts as a test environment for servlets.
042: *
043: * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a>
044: **/
045: public class ServletRunner {
046:
047: /**
048: * Default constructor, which defines no servlets.
049: */
050: public ServletRunner() {
051: _application = new WebApplication();
052: completeInitialization(null);
053: }
054:
055: /**
056: * Constructor which expects the full path to the web.xml for the
057: * application.
058: * @deprecated as of 1.6, use {@link #ServletRunner(File)}
059: *
060: * @param webXMLFileSpec the full path to the web.xml file
061: */
062: public ServletRunner(String webXMLFileSpec) throws IOException,
063: SAXException {
064: _application = new WebApplication(HttpUnitUtils.newParser()
065: .parse(webXMLFileSpec));
066: completeInitialization(null);
067: }
068:
069: /**
070: * Constructor which expects the full path to the web.xml for the
071: * application and a context path under which to mount it.
072: * @deprecated as of 1.6, use {@link #ServletRunner(File,String)}
073: *
074: * @param webXMLFileSpec the full path to the web.xml file
075: * @param contextPath the context path
076: */
077: public ServletRunner(String webXMLFileSpec, String contextPath)
078: throws IOException, SAXException {
079: this (new File(webXMLFileSpec), contextPath);
080: }
081:
082: /**
083: * Constructor which expects a File object representing the web.xml for the
084: * application.
085: *
086: * @param webXml the web.xml file
087: * @since 1.6
088: */
089: public ServletRunner(File webXml) throws IOException, SAXException {
090: _application = new WebApplication(HttpUnitUtils.newParser()
091: .parse(webXml));
092: completeInitialization(null);
093: }
094:
095: /**
096: * Constructor which expects a File object representing the web.xml for the
097: * application and a context path under which to mount it.
098: *
099: * @param webXml the web.xml file
100: * @param contextPath the context path
101: * @since 1.6
102: */
103: public ServletRunner(File webXml, String contextPath)
104: throws IOException, SAXException {
105: _application = new WebApplication(HttpUnitUtils.newParser()
106: .parse(webXml), webXml.getParentFile().getParentFile(),
107: contextPath);
108: completeInitialization(contextPath);
109: }
110:
111: /**
112: * Constructor which expects an input stream containing the web.xml for the application.
113: **/
114: public ServletRunner(InputStream webXML) throws IOException,
115: SAXException {
116: this (webXML, null);
117: }
118:
119: /**
120: * Constructor which expects an input stream containing the web.xml for the application.
121: **/
122: public ServletRunner(InputStream webXML, String contextPath)
123: throws IOException, SAXException {
124: _application = new WebApplication(HttpUnitUtils.newParser()
125: .parse(new InputSource(webXML)), contextPath);
126: completeInitialization(contextPath);
127: }
128:
129: /**
130: * Registers a servlet class to be run.
131: **/
132: public void registerServlet(String resourceName,
133: String servletClassName) {
134: _application.registerServlet(resourceName, servletClassName,
135: null);
136: }
137:
138: private void completeInitialization(String contextPath) {
139: _context = new ServletUnitContext(contextPath, _application
140: .getServletContext(), _application);
141: _application.registerServlet("*.jsp", _jspServletDescriptor
142: .getClassName(), _jspServletDescriptor
143: .getInitializationParameters(null, null));
144: }
145:
146: /**
147: * Registers a servlet class to be run, specifying initialization parameters.
148: **/
149: public void registerServlet(String resourceName,
150: String servletClassName, Hashtable initParameters) {
151: _application.registerServlet(resourceName, servletClassName,
152: initParameters);
153: }
154:
155: /**
156: * Returns the response from the specified servlet.
157: * @exception SAXException thrown if there is an error parsing the response
158: **/
159: public WebResponse getResponse(WebRequest request)
160: throws MalformedURLException, IOException, SAXException {
161: return getClient().getResponse(request);
162: }
163:
164: /**
165: * Returns the response from the specified servlet using GET.
166: * @exception SAXException thrown if there is an error parsing the response
167: **/
168: public WebResponse getResponse(String url)
169: throws MalformedURLException, IOException, SAXException {
170: return getClient().getResponse(url);
171: }
172:
173: /**
174: * Returns the session to be used by the next request.
175: * @param create if true, will create a new session if no valid session is defined.
176: * @since 1.6
177: */
178: public HttpSession getSession(boolean create) {
179: return getClient().getSession(create);
180: }
181:
182: /**
183: * Returns the value of the named context parameter found in the application definition.
184: */
185: public String getContextParameter(String name) {
186: Object value = _application.getContextParameters().get(name);
187: return value == null ? null : value.toString();
188: }
189:
190: /**
191: * Shuts down the servlet container, returning any resources held by it.
192: * Calls the destroy method of each active servlet, then notifies
193: * ContextListeners of server shutdown.
194: */
195: public void shutDown() {
196: _application.shutDown();
197: }
198:
199: /**
200: * Creates and returns a new web client that communicates with this servlet runner.
201: **/
202: public ServletUnitClient newClient() {
203: return ServletUnitClient.newClient(_factory);
204: }
205:
206: public static class JasperJSPServletDescriptor implements
207: JSPServletDescriptor {
208:
209: public String getClassName() {
210: return "org.apache.jasper.servlet.JspServlet";
211: }
212:
213: public Hashtable getInitializationParameters(String classPath,
214: String workingDirectory) {
215: Hashtable params = new Hashtable();
216: if (classPath != null)
217: params.put("classpath", classPath);
218: if (workingDirectory != null)
219: params.put("scratchdir", workingDirectory);
220: return params;
221: }
222: }
223:
224: public final static JSPServletDescriptor JASPER_DESCRIPTOR = new JasperJSPServletDescriptor();
225:
226: //-------------------------------------------- package methods ---------------------------------------------------------
227:
228: ServletUnitContext getContext() {
229: return _context;
230: }
231:
232: WebApplication getApplication() {
233: return _application;
234: }
235:
236: //---------------------------- private members ------------------------------------
237:
238: private static JSPServletDescriptor _jspServletDescriptor = JASPER_DESCRIPTOR;
239:
240: private WebApplication _application;
241:
242: private ServletUnitClient _client;
243:
244: private ServletUnitContext _context;
245:
246: private InvocationContextFactory _factory = new InvocationContextFactory() {
247: public InvocationContext newInvocation(
248: ServletUnitClient client, FrameSelector targetFrame,
249: WebRequest request, Dictionary clientHeaders,
250: byte[] messageBody) throws IOException,
251: MalformedURLException {
252: return new InvocationContextImpl(client,
253: ServletRunner.this , targetFrame, request,
254: clientHeaders, messageBody);
255: }
256:
257: public HttpSession getSession(String sessionId, boolean create) {
258: return _context.getValidSession(sessionId, null, create);
259: }
260: };
261:
262: private ServletUnitClient getClient() {
263: if (_client == null)
264: _client = newClient();
265: return _client;
266: }
267:
268: }
|