001: package com.meterware.servletunit;
002:
003: /********************************************************************************************************************
004: * $Id: ServletUnitClient.java,v 1.16 2004/10/27 00:57:33 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 com.meterware.httpunit.*;
024:
025: import java.io.ByteArrayOutputStream;
026: import java.io.IOException;
027: import java.net.MalformedURLException;
028:
029: import javax.servlet.ServletException;
030: import javax.servlet.http.HttpSession;
031:
032: import org.xml.sax.SAXException;
033:
034: /**
035: * A client for use with the servlet runner class, allowing the testing of servlets
036: * without an actual servlet container. Testing can be done in one of two ways.
037: * End-to-end testing works much like the HttpUnit package, except that only servlets
038: * actually registered with the ServletRunner will be invoked. It is also possible
039: * to test servlets 'from the inside' by creating a ServletInvocationContext and then
040: * calling any servlet methods which may be desired. Even in this latter mode, end-to-end
041: * testing is supported, but requires a call to this class's getResponse method to update
042: * its cookies and frames.
043: *
044: * @author <a href="russgold@httpunit.org">Russell Gold</a>
045: **/
046: public class ServletUnitClient extends WebClient {
047:
048: /**
049: * Creates and returns a new servlet unit client instance.
050: **/
051: public static ServletUnitClient newClient(
052: InvocationContextFactory factory) {
053: return new ServletUnitClient(factory);
054: }
055:
056: /**
057: * Creates and returns a new invocation context from a GET request.
058: **/
059: public InvocationContext newInvocation(String requestString)
060: throws IOException, MalformedURLException {
061: return newInvocation(new GetMethodWebRequest(requestString));
062: }
063:
064: /**
065: * Creates and returns a new invocation context to test calling of servlet methods.
066: **/
067: public InvocationContext newInvocation(WebRequest request)
068: throws IOException, MalformedURLException {
069: return newInvocation(request, FrameSelector.TOP_FRAME);
070: }
071:
072: InvocationContext newInvocation(WebRequest request,
073: FrameSelector frame) throws IOException,
074: MalformedURLException {
075: ByteArrayOutputStream baos = getMessageBody(request);
076: return _invocationContextFactory.newInvocation(this , frame,
077: request, getHeaderFields(request.getURL()), baos
078: .toByteArray());
079: }
080:
081: ByteArrayOutputStream getMessageBody(WebRequest request)
082: throws IOException {
083: ByteArrayOutputStream baos = new ByteArrayOutputStream();
084: writeMessageBody(request, baos);
085: return baos;
086: }
087:
088: /**
089: * Updates this client and returns the response which would be displayed by the
090: * user agent. Note that this will typically be the same as that returned by the
091: * servlet invocation unless that invocation results in a redirect request.
092: **/
093: public WebResponse getResponse(InvocationContext invocation)
094: throws MalformedURLException, IOException, SAXException {
095: updateMainWindow(invocation.getFrame(), invocation
096: .getServletResponse());
097: return getFrameContents(invocation.getFrame());
098: }
099:
100: /**
101: * Returns the session that would be used by the next request (if it asks for one).
102: * @param create if true, will create a new session if no valid session is defined.
103: * @since 1.6
104: */
105: public HttpSession getSession(boolean create) {
106: HttpSession session = _invocationContextFactory
107: .getSession(
108: getCookieValue(ServletUnitHttpSession.SESSION_COOKIE_NAME),
109: create);
110: if (session != null)
111: putCookie(ServletUnitHttpSession.SESSION_COOKIE_NAME,
112: session.getId());
113: return session;
114: }
115:
116: //-------------------------------- WebClient methods --------------------------------------
117:
118: /**
119: * Creates a web response object which represents the response to the specified web request.
120: **/
121: protected WebResponse newResponse(WebRequest request,
122: FrameSelector targetFrame) throws MalformedURLException,
123: IOException {
124:
125: try {
126: InvocationContext invocation = newInvocation(request,
127: targetFrame);
128: invocation.service();
129: return invocation.getServletResponse();
130: } catch (ServletException e) {
131: throw new HttpInternalErrorException(request.getURL(), e);
132: }
133:
134: }
135:
136: //-------------------------- private members -----------------------------------
137:
138: private InvocationContextFactory _invocationContextFactory;
139:
140: //--------------------------------- package methods ---------------------------------------
141:
142: private ServletUnitClient(InvocationContextFactory factory) {
143: _invocationContextFactory = factory;
144: }
145: }
|