[Httpunit-commit] CVS: httpunit/src/com/meterware/servletunit ServletRunner.java,1.5,1.6 ServletUnit
Brought to you by:
russgold
Update of /cvsroot/httpunit/httpunit/src/com/meterware/servletunit
In directory usw-pr-cvs1:/tmp/cvs-serv5733/src/com/meterware/servletunit
Modified Files:
ServletRunner.java ServletUnitClient.java
ServletUnitHttpRequest.java ServletUnitHttpResponse.java
ServletUnitWebResponse.java
Log Message:
Added support for web.xml, response messages, and readonly controls
Index: ServletRunner.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletRunner.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ServletRunner.java 2001/06/18 20:21:30 1.5
+++ ServletRunner.java 2001/11/08 22:07:53 1.6
@@ -21,6 +21,8 @@
*******************************************************************************************************************/
import java.io.IOException;
+import java.io.InputStream;
+import java.io.ByteArrayInputStream;
import java.net.MalformedURLException;
import java.net.URL;
@@ -28,6 +30,7 @@
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Vector;
+import java.util.Dictionary;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
@@ -40,6 +43,12 @@
import com.meterware.httpunit.*;
import org.xml.sax.SAXException;
+import org.xml.sax.InputSource;
+import org.apache.xerces.parsers.DOMParser;
+import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
/**
@@ -47,6 +56,63 @@
**/
public class ServletRunner {
+ /**
+ * Default constructor, which defines no servlets.
+ */
+ public ServletRunner() {}
+
+
+ /**
+ * Constructor which expects the full path to the web.xml for the application.
+ **/
+ public ServletRunner( String webXMLFileSpec ) throws IOException, SAXException {
+ DOMParser parser = new DOMParser();
+ parser.parse( webXMLFileSpec );
+
+ registerServlets( parser.getDocument() );
+ }
+
+
+ /**
+ * Constructor which expects an input stream containing the web.xml for the application.
+ **/
+ public ServletRunner( InputStream webXML ) throws IOException, SAXException {
+ DOMParser parser = new DOMParser();
+ parser.parse( new InputSource( webXML ) );
+
+ registerServlets( parser.getDocument() );
+ }
+
+
+ private void registerServlets( Document document ) throws SAXException {
+ Hashtable nameToClass = new Hashtable();
+ NodeList nl = document.getElementsByTagName( "servlet" );
+ for (int i = 0; i < nl.getLength(); i++) registerServletClass( nameToClass, (Element) nl.item(i) );
+ nl = document.getElementsByTagName( "servlet-mapping" );
+ for (int i = 0; i < nl.getLength(); i++) registerServlet( nameToClass, (Element) nl.item(i) );
+ }
+
+
+ private void registerServletClass( Dictionary mapping, Element servletElement ) throws SAXException {
+ mapping.put( getChildNodeValue( servletElement, "servlet-name" ),
+ getChildNodeValue( servletElement, "servlet-class" ) );
+ }
+
+
+ private void registerServlet( Dictionary mapping, Element servletElement ) throws SAXException {
+ registerServlet( getChildNodeValue( servletElement, "url-pattern" ),
+ (String) mapping.get( getChildNodeValue( servletElement, "servlet-name" ) ) );
+ }
+
+
+ private String getChildNodeValue( Element root, String childNodeName ) throws SAXException {
+ NodeList nl = root.getElementsByTagName( childNodeName );
+ if (nl.getLength() != 1) throw new SAXException( "Node <" + root.getNodeName() + "> has no child named <" + childNodeName + ">" );
+ Node childNode = nl.item(0).getFirstChild();
+ if (childNode == null) throw new SAXException( "No value specified for <" + childNodeName + "> node" );
+ if (childNode.getNodeType() != Node.TEXT_NODE) throw new SAXException( "No text value found for <" + childNodeName + "> node" );
+ return childNode.getNodeValue();
+ }
/**
* Registers a servlet class to be run.
@@ -78,20 +144,20 @@
Servlet getServlet( URL url ) {
String className = (String) _servlets.get( getServletName( url.getFile() ) );
- if (className == null) throw new HttpNotFoundException( url.toExternalForm() );
+ if (className == null) throw new HttpNotFoundException( url );
try {
Class servletClass = Class.forName( className );
if (!Servlet.class.isAssignableFrom( servletClass )) {
- throw new HttpInternalErrorException( url.toExternalForm() );
+ throw new HttpInternalErrorException( url );
}
return (Servlet) servletClass.newInstance();
} catch (ClassNotFoundException e) {
- throw new HttpNotFoundException( url.toExternalForm() );
+ throw new HttpNotFoundException( url, e );
} catch (IllegalAccessException e) {
- throw new HttpInternalErrorException( url.toExternalForm() );
+ throw new HttpInternalErrorException( url, e );
} catch (InstantiationException e) {
- throw new HttpInternalErrorException( url.toExternalForm() );
+ throw new HttpInternalErrorException( url, e );
}
}
Index: ServletUnitClient.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletUnitClient.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ServletUnitClient.java 2001/06/06 13:25:04 1.1
+++ ServletUnitClient.java 2001/11/08 22:07:53 1.2
@@ -24,6 +24,7 @@
import com.meterware.httpunit.WebClient;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
+import com.meterware.httpunit.GetMethodWebRequest;
import java.io.IOException;
@@ -52,6 +53,14 @@
/**
+ * Creates and returns a new invocation context from a GET request.
+ **/
+ public InvocationContext newInvocation( String requestString ) throws MalformedURLException {
+ return newInvocation( new GetMethodWebRequest( requestString ) );
+ }
+
+
+ /**
* Creates and returns a new invocation context to test calling of servlet methods.
**/
public InvocationContext newInvocation( WebRequest request ) throws MalformedURLException {
@@ -90,7 +99,7 @@
try {
invocation.getServlet().service( invocation.getRequest(), invocation.getResponse() );
} catch (ServletException e) {
- throw new HttpInternalErrorException( request.getURL().toExternalForm() );
+ throw new HttpInternalErrorException( request.getURL(), e );
}
return invocation.getServletResponse();
Index: ServletUnitHttpRequest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletUnitHttpRequest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ServletUnitHttpRequest.java 2001/10/25 19:23:17 1.3
+++ ServletUnitHttpRequest.java 2001/11/08 22:07:53 1.4
@@ -33,6 +33,7 @@
import javax.servlet.http.*;
import com.meterware.httpunit.WebRequest;
+import com.meterware.httpunit.HttpUnitUtils;
/**
@@ -338,8 +339,7 @@
* been read.
**/
public Enumeration getParameterNames() {
- throwNotImplementedYet();
- return null;
+ return _parameters.keys();
}
@@ -647,15 +647,10 @@
private void addParameter( String name, String encodedValue ) {
String[] values = (String[]) _parameters.get( name );
if (values == null) {
- _parameters.put( name, new String[] { decode( encodedValue ) } );
+ _parameters.put( name, new String[] { HttpUnitUtils.decode( encodedValue ) } );
} else {
- _parameters.put( name, extendedArray( values, decode( encodedValue ) ) );
+ _parameters.put( name, extendedArray( values, HttpUnitUtils.decode( encodedValue ) ) );
}
- }
-
-
- private String decode( String encodedValue ) {
- return encodedValue; // FIXME
}
Index: ServletUnitHttpResponse.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletUnitHttpResponse.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ServletUnitHttpResponse.java 2001/07/19 17:43:50 1.5
+++ ServletUnitHttpResponse.java 2001/11/08 22:07:53 1.6
@@ -2,7 +2,7 @@
/********************************************************************************************************************
* $Id$
*
-* Copyright (c) 2000, Russell Gold
+* Copyright (c) 2000-2001, Russell Gold
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
@@ -120,7 +120,7 @@
* (<body></body>).
**/
public void sendError( int sc ) throws IOException {
- throw new RuntimeException( "sendError not implemented" );
+ sendError( sc, "" );
}
@@ -134,7 +134,14 @@
* (<body></body>).
**/
public void sendError(int sc, String msg) throws IOException {
- throw new RuntimeException( "sendError not implemented" );
+ setStatus( sc );
+ _statusMessage = msg;
+
+ _writer = null;
+ _servletStream = null;
+
+ setContentType( "text/html" );
+ getWriter().println( "<html><head><title>" + msg + "</title></head><body>" + msg + "</body></html>" );
}
@@ -145,8 +152,8 @@
* is an error, the <code>sendError</code> method should be used
* instead.
**/
- public void setStatus(int sc) {
- throw new RuntimeException( "setStatus not implemented" );
+ public void setStatus( int sc ) {
+ _status = sc;
}
@@ -156,7 +163,7 @@
* use sendError(int, String). Sets the status code and message for this response.
**/
public void setStatus( int sc, String msg ) {
- throw new RuntimeException( "setStatus not implemented" );
+ setStatus( sc );
}
@@ -411,6 +418,14 @@
}
+ /**
+ * Returns the message associated with this response's status.
+ **/
+ String getMessage() {
+ return _statusMessage;
+ }
+
+
public String[] getHeaderFieldNames() {
if (!_headersComplete) completeHeaders();
Vector names = new Vector();
@@ -446,6 +461,8 @@
private ByteArrayOutputStream _outputStream;
private int _status = SC_OK;
+
+ private String _statusMessage = "OK";
private Hashtable _headers = new Hashtable();
Index: ServletUnitWebResponse.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/servletunit/ServletUnitWebResponse.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- ServletUnitWebResponse.java 2001/07/19 17:43:50 1.6
+++ ServletUnitWebResponse.java 2001/11/08 22:07:53 1.7
@@ -67,6 +67,14 @@
}
+ /**
+ * Returns the response message associated with this response.
+ **/
+ public String getResponseMessage() {
+ return _response.getMessage();
+ }
+
+
public String[] getHeaderFieldNames() {
return _response.getHeaderFieldNames();
}
|