costin 01/01/20 22:39:22
Modified: src/admin/WEB-INF admin.tld
src/admin/WEB-INF/classes/tadm GTestTag.java
src/admin/test test.jsp
src/share/org/apache/tomcat/util/test DefaultMatcher.java
GTest.java Header.java HttpClient.java
Response.java
src/tests/webpages/WEB-INF test-tomcat.xml
Log:
Second round of changes in the sanity-test running.
You should be able to run the tests by starting tomcat and accessing
http://localhost:8080/admin/test/test.jsp
The old GTest is deprecated, it has been split into HttpClient and
DefaultMatcher ( plus a wrapper that allows 'old-style' calling ).
The goal is to make it easy to run the tests, and to make it easy and
clean to add tests.
I'll probably add a page that allow to run watchdog from the web, but
right now it's more important to get "writing new tests" and
adding regression tests for the bugs we fix as easy and simple as possible.
( long term we should use a dedicated test program - ant is getting
very hard to use embeded without enough API control ).
Changes:
- added a form to test.jsp, allow to run a subset of the tests
- fixes to deal with IntrospectionHelper
- various fixes in the util.test
- more debugging
- dispay the <a> on GET tests, so we can re-run individual tests
manually
- added a sample test using the cleaner ( IMHO ) syntax ( i.e. httpClient
followed by a matcher )
Missing:
- code documentation
- documentation on how to write tests
- fixes, etc.
( it's a test application, should make things simpler - if it doesn't we
should go back to the old one )
Revision Changes Path
1.5 +5 -0 jakarta-tomcat/src/admin/WEB-INF/admin.tld
Index: admin.tld
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/admin/WEB-INF/admin.tld,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- admin.tld 2001/01/20 21:50:23 1.4
+++ admin.tld 2001/01/21 06:39:21 1.5
@@ -102,6 +102,11 @@
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
+ <attribute>
+ <name>debug</name>
+ <required>false</required>
+ <rtexprvalue>true</rtexprvalue>
+ </attribute>
</tag>
1.4 +9 -1 jakarta-tomcat/src/admin/WEB-INF/classes/tadm/GTestTag.java
Index: GTestTag.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/admin/WEB-INF/classes/tadm/GTestTag.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- GTestTag.java 2001/01/20 23:25:44 1.3
+++ GTestTag.java 2001/01/21 06:39:21 1.4
@@ -63,6 +63,7 @@
String testFileName;
String target;
String testApp;
+ String debug;
/** Set the name of the test.xml, relative to the base dir.
* For example, /WEB-INF/test-tomcat.xml
@@ -84,6 +85,10 @@
public void setTestApp( String s ) {
testApp=s;
}
+
+ public void setDebug( String s ) {
+ debug=s;
+ }
// -------------------- Implementation methods --------------------
@@ -102,6 +107,8 @@
// new one
GTest.setDefaultWriter(out);
GTest.setDefaultOutput("html");
+ if(debug!=null)
+ GTest.setDefaultDebug(Integer.valueOf( debug ).intValue());
Project project=new Project();
@@ -127,7 +134,8 @@
if( ex instanceof BuildException ) {
Throwable ex1=((BuildException)ex).getException();
System.out.println("Root cause: " );
- ex1.printStackTrace(out);
+ if( ex1!=null)
+ ex1.printStackTrace(out);
}
}
}
1.3 +28 -4 jakarta-tomcat/src/admin/test/test.jsp
Index: test.jsp
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/admin/test/test.jsp,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- test.jsp 2001/01/20 23:25:44 1.2
+++ test.jsp 2001/01/21 06:39:21 1.3
@@ -6,11 +6,37 @@
This page will show the result of executing the sanity test suite.
You can see the context log <a href="/test/context_log.txt">here</a>
+<form method="GET" action="test.jsp" >
+Target:
+<select name="target" >
+ <option>new-style</option>
+ <option>file-tomcat</option>
+ <option>dispatch-tomcat</option>
+ <option>get-tomcat</option>
+ <option>requestMap</option>
+ <option>post</option>
+ <option>jsp-tomcat</option>
+ <option>wrong_request</option>
+ <option>unavailable</option>
+ <option>restricted</option>
+ <option selected>client</option>
+</select>
+<br>
+
+Debug: <input type="checkbox" name="debug" value="10"><br>
+
+<input type="submit">
+</form>
+
<% // This is an ugly hack to make the logs easily accessible.
// Keep in mind this is just a way to jump-start testing, not a
// production-quality test runner.
%>
+<% out.flush();
+ if( request.getParameter("target") == null ) return;
+%>
+
<adm:admin ctxPath="/test"
action="setLogger"
value="webapps/test/context_log.txt" />
@@ -18,8 +44,6 @@
<adm:gtest testFile="WEB-INF/test-tomcat.xml"
testApp="/test"
- target='<%= request.getParameter("target") %>' />
-
-
+ target='<%= request.getParameter("target") %>'
+ debug='<%= request.getParameter("debug") %>' />
-</pre>
\ No newline at end of file
1.2 +23 -12
jakarta-tomcat/src/share/org/apache/tomcat/util/test/DefaultMatcher.java
Index: DefaultMatcher.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/test/DefaultMatcher.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DefaultMatcher.java 2001/01/20 19:42:09 1.1
+++ DefaultMatcher.java 2001/01/21 06:39:22 1.2
@@ -74,12 +74,14 @@
// Expected response
boolean magnitude=true;
boolean exactMatch=false;
+
// Match the body against a golden file
String goldenFile;
// Match the body against a string
String responseMatch;
// the response should include the following headers
- Hashtable expectHeaders;
+ Vector headerVector=new Vector(); // workaround for introspection problems
+ Hashtable expectHeaders=new Hashtable();
// Match request line
String returnCode="";
String description;
@@ -89,7 +91,7 @@
// Results of matching
boolean result=false;
- StringBuffer messageSB;
+ StringBuffer messageSB=new StringBuffer();
public DefaultMatcher() {
}
@@ -130,8 +132,9 @@
/** Display debug info
*/
- public void setDebug( String debugS ) {
- debug=Integer.valueOf( debugS).intValue();
+ public void setDebug( int d ) {
+ // debug=Integer.valueOf( debugS).intValue();
+ debug=d;
}
/** True if this is a positive test, false for negative
@@ -148,14 +151,13 @@
public void addHeader( Header rh ) {
- expectHeaders.put( rh.getName(), rh );
+ headerVector.addElement( rh );
}
-
+
/** Verify that response includes the expected headers.
* The value is a "|" separated list of headers to expect.
*/
public void setExpectHeaders( String s ) {
- expectHeaders=new Hashtable();
Header.parseHeadersAsString( s, expectHeaders );
}
@@ -177,6 +179,7 @@
try {
result=checkResponse( magnitude );
} catch(Exception ex ) {
+ ex.printStackTrace();
result=false;
}
}
@@ -199,13 +202,18 @@
responseLine.indexOf(returnCode) > -1);
if( match != testCondition ) {
responseStatus = false;
- log("ERROR");
log(" Expecting: " + returnCode );
log(" Got : " + responseLine);
}
}
+
+ Enumeration en=headerVector.elements();
+ while( en.hasMoreElements()) {
+ Header rh=(Header)en.nextElement();
+ expectHeaders.put( rh.getName(), rh );
+ }
- if( expectHeaders != null ) {
+ if( expectHeaders.size() > 0 ) {
// Check if we got the expected headers
if(headers==null) {
log("ERROR no response header, expecting header");
@@ -213,11 +221,14 @@
Enumeration e=expectHeaders.keys();
while( e.hasMoreElements()) {
String key=(String)e.nextElement();
- String value=(String)expectHeaders.get(key);
- String respValue=(String)headers.get(key);
+ Header h=(Header)expectHeaders.get(key);
+ String value=h.getValue();
+ h=(Header)headers.get(key);
+ String respValue=h.getValue();
if( respValue==null || respValue.indexOf( value ) <0 ) {
log("ERROR expecting header " + key + ":" +
- value + " GOT: " + respValue+ " HEADERS(" +
headers + ")");
+ value + " GOT: " + respValue+ " HEADERS(" +
+ headers + ")");
return false;
}
1.3 +65 -20 jakarta-tomcat/src/share/org/apache/tomcat/util/test/GTest.java
Index: GTest.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/test/GTest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- GTest.java 2001/01/20 21:44:39 1.2
+++ GTest.java 2001/01/21 06:39:22 1.3
@@ -75,23 +75,26 @@
HttpClient httpClient=new HttpClient();
DefaultMatcher matcher=new DefaultMatcher();
- int debug=0;
String description="No description";
static PrintWriter defaultOutput=new PrintWriter(System.out);
static String defaultOutType="text";
+ static int defaultDebug=0;
PrintWriter out=defaultOutput;
String outType=defaultOutType;
+ int debug=defaultDebug;
boolean failureOnly=false;
public GTest() {
+ matcher.setDebug( debug );
+ httpClient.setDebug( debug );
}
- // -------------------- GTest behavior --------------------
+ // -------------------- Defaults --------------------
- public void setWriter( PrintWriter pw ) {
- out=pw;
+ public static void setDefaultDebug( int d ) {
+ defaultDebug=d;
}
public static void setDefaultWriter( PrintWriter pw ) {
@@ -102,6 +105,11 @@
defaultOutType=s;
}
+ // -------------------- GTest behavior --------------------
+ public void setWriter( PrintWriter pw ) {
+ out=pw;
+ }
+
/** text, xml, html
*/
public void setOutput( String t ) {
@@ -143,8 +151,8 @@
*/
public void setDebug( String debugS ) {
debug=Integer.valueOf( debugS).intValue();
- matcher.setDebug( debugS );
- httpClient.setDebug( debugS );
+ matcher.setDebug( debug );
+ httpClient.setDebug( debug );
}
// -------------------- Client properties --------------------
@@ -234,40 +242,47 @@
if( result && failureOnly ) return;
if( "text".equals(outType) )
- textReport( result , null );
+ textReport();
if( "html".equals(outType) )
- htmlReport( result , null );
+ htmlReport();
if( "xml".equals(outType) )
- xmlReport( result , null );
+ xmlReport();
} catch(Exception ex ) {
- textReport( false, ex );
+ // no exception should be thrown in normal operation
+ ex.printStackTrace();
}
}
// -------------------- Internal methods --------------------
- private void textReport( boolean result, Exception ex ) {
+ private void textReport() {
String msg=null;
if( "No description".equals( description ))
msg=" (" + httpClient.getRequestLine() + ")";
else
msg=description + " (" + httpClient.getRequestLine() + ")";
- if(result)
- out.println("OK " + msg );
- else
+ if(matcher.getResult())
+ out.println("OK " + msg );
+ else {
out.println("FAIL " + msg );
+ out.println("Message: " + matcher.getMessage());
+ }
- if( ex!=null)
- ex.printStackTrace(out);
}
- private void htmlReport( boolean result, Exception ex ) {
+ private void htmlReport() {
+ boolean result=matcher.getResult();
+ String uri=httpClient.getURI();
+ if( uri!=null )
+ out.println("<a href='" + uri + "'>");
if( result )
out.println( "OK " );
else
out.println("<font color='red'>FAIL ");
+ if( uri!=null )
+ out.println("</a>");
String msg=null;
if( "No description".equals( description ))
@@ -279,9 +294,33 @@
if( ! result )
out.println("</font>");
-
+
out.println("<br>");
+ if( ! result ) {
+ out.println("<b>Message:</b><pre>");
+ out.println( matcher.getMessage());
+ out.println("</pre>");
+ }
+
+ if( ! result && debug > 0 ) {
+ out.println("<b>Request: </b><pre>" + httpClient.getFullRequest());
+ out.println("</pre><b>Response:</b> " +
+ httpClient.getResponse().getResponseLine());
+ out.println("<br><b>Response headers:</b><br>");
+ Hashtable headerH=httpClient.getResponse().getHeaders();
+ Enumeration hE=headerH.elements();
+ while( hE.hasMoreElements() ) {
+ Header h=(Header) hE.nextElement();
+ out.println("<b>" + h.getName() + ":</b>" +
+ h.getValue() + "<br>");
+ }
+ out.println("<b>Response body:</b><pre> ");
+ out.println(httpClient.getResponse().getResponseBody());
+ out.println("</pre>");
+ }
+
+ Throwable ex=httpClient.getResponse().getThrowable();
if( ex!=null) {
out.println("<b>Exception</b><pre>");
ex.printStackTrace(out);
@@ -289,7 +328,8 @@
}
}
- private void xmlReport( boolean result, Exception ex ) {
+ private void xmlReport() {
+ boolean result=matcher.getResult();
String msg=null;
if( "No description".equals( description ))
msg=" (" + httpClient.getRequestLine() + ")";
@@ -301,8 +341,13 @@
else
out.println("FAIL " + msg );
- if( ex!=null)
+ Throwable ex=httpClient.getResponse().getThrowable();
+ if( ex!=null) {
+ out.println("<b>Exception</b><pre>");
ex.printStackTrace(out);
+ out.println("</pre><br>");
+ }
+
}
1.2 +2 -2 jakarta-tomcat/src/share/org/apache/tomcat/util/test/Header.java
Index: Header.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/test/Header.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Header.java 2001/01/20 19:42:09 1.1
+++ Header.java 2001/01/21 06:39:22 1.2
@@ -136,8 +136,8 @@
if (colon < 0) {
return;
}
- String name = line.substring(0, colon).trim();
- String value = line.substring(colon + 1).trim();
+ name = line.substring(0, colon).trim();
+ value = line.substring(colon + 1).trim();
}
1.2 +81 -21
jakarta-tomcat/src/share/org/apache/tomcat/util/test/HttpClient.java
Index: HttpClient.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/test/HttpClient.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- HttpClient.java 2001/01/20 19:42:09 1.1
+++ HttpClient.java 2001/01/21 06:39:22 1.2
@@ -79,14 +79,19 @@
int debug=0;
+ String method="GET";
+ String protocol="HTTP/1.0";
+ String path;
+
String requestLine;
- Hashtable requestHeaders;
+ Hashtable requestHeaders=new Hashtable();
+ Vector headerVector=new Vector();// alternate
Body body;
- String requestBody;
+ String fullRequest;
// Response resulted from this request
- Response response;
+ Response response=new Response();
public HttpClient() {
}
@@ -120,7 +125,14 @@
public void addBody( Body b ) {
body=b;
}
+
+ public void setProtocol( String s ) {
+ protocol=s;
+ }
+ public void setPath( String s ) {
+ path=s;
+ }
public void addHeader( String n, String v ) {
requestHeaders.put(n, new Header( n, v) );
@@ -129,7 +141,7 @@
/** Add a header to the request
*/
public void addHeader( Header rh ) {
- requestHeaders.put( rh.getName(), rh );
+ headerVector.addElement( rh );
}
/** Add headers - string representation, will be parsed
@@ -150,8 +162,8 @@
/** Display debug info
*/
- public void setDebug( String debugS ) {
- debug=Integer.valueOf( debugS).intValue();
+ public void setDebug( int d ) {
+ debug=d;
}
/** Verbose request line - including method and protocol
@@ -166,14 +178,18 @@
/** Allow sending a verbose request
*/
- public void setVerboseRequest( String s ) {
- requestBody=s;
+ public void setFullRequest( String s ) {
+ fullRequest=s;
}
+ public String getFullRequest() {
+ return fullRequest;
+ }
+
/** Alternate method for sending a verbose request
*/
public void addText(String s ) {
- requestBody=s;
+ fullRequest=s;
}
// -------------------- Access the response --------------------
@@ -198,48 +214,65 @@
throws Exception
{
// explicitely set
- if( requestBody != null ) return;
+ if( fullRequest != null ) return;
// use the existing info to compose what will be sent to the
// server
StringBuffer sb=new StringBuffer();
- sb.append(requestLine).append(CRLF);
+ if( requestLine != null )
+ sb.append(requestLine);
+ else {
+ sb.append( method ).append(" ").append(path).append(" ");
+ sb.append(protocol);
+ requestLine=sb.toString();
+ }
+ sb.append(CRLF);
+
// We may test HTTP0.9 behavior. If it's post 1.0, it needs
// a LF
if( requestLine.indexOf( "HTTP/1." ) <0 ) {
- requestBody=sb.toString();
+ fullRequest=sb.toString();
return; // nothing to add
}
String contentL=null;
-
+
+ Enumeration en=headerVector.elements();
+ while( en.hasMoreElements()) {
+ Header rh=(Header)en.nextElement();
+ requestHeaders.put( rh.getName(), rh );
+ }
+
// headers
Enumeration headersE=requestHeaders.elements();
while( headersE.hasMoreElements() ) {
Header h=(Header)headersE.nextElement();
- sb.append(h.toString()).append( CRLF );
+ sb.append(h.getName()).append(": ");
+ sb.append(h.getValue()).append( CRLF );
if( "Content-Length".equals( h.getName() )) {
contentL=h.getValue();
}
}
-
+
// If we have a body
if( body != null) {
// If set explicitely ( maybe we're testing bad POSTs )
if( contentL==null ) {
sb.append("Content-Length: ").append( body.getBody().length());
- sb.append(CRLF);
+ sb.append(CRLF).append( CRLF);
}
sb.append(body.getBody());
// no /n at the end -see HTTP specs!
// If we want to test bad POST - set Content-Length
// explicitely.
+ } else {
+ sb.append( CRLF );
}
- // set the requestBody
- requestBody=sb.toString();
+ // set the fullRequest
+ fullRequest=sb.toString();
}
/** Invoke a request, set headers, responseLine, body
@@ -258,9 +291,15 @@
OutputStreamWriter out=new OutputStreamWriter(os);
PrintWriter pw = new PrintWriter(out);
+ prepareRequest();
+ if( debug > 5 ) {
+ System.out.println("--------------------Sending " );
+ System.out.println(fullRequest);
+ System.out.println("----------" );
+ }
// Write the request
try {
- os.write(requestBody.getBytes()); // XXX encoding !
+ os.write(fullRequest.getBytes()); // XXX encoding !
os.flush();
} catch (Exception ex1 ) {
response.setThrowable( ex1 );
@@ -269,17 +308,25 @@
try {
// http 1.0 +
- if( requestBody.indexOf( "HTTP/1." ) > -1) {
+ if( fullRequest.indexOf( "HTTP/1." ) > -1) {
+ if( debug > 5 )
+ System.out.println("Reading response " );
String responseLine = read( is );
+ if( debug > 5 )
+ System.out.println("Got: " + responseLine );
response.setResponseLine( responseLine );
Hashtable headers=Header.parseHeaders( is );
+ if( debug > 5 )
+ System.out.println("Got headers: " + headers );
response.setHeaders( headers );
}
StringBuffer result = readBody( is );
if(result!=null)
response.setResponseBody( result.toString() );
+ if( debug > 5 )
+ System.out.println("Got body: " + result );
} catch( SocketException ex ) {
response.setThrowable( ex );
@@ -328,7 +375,7 @@
while (true) {
try {
int ch = input.read();
- // log("XXX " + (char)ch );
+ // System.out.println("XXX " + (char)ch );
if (ch < 0) {
if (sb.length() == 0) {
// if(debug>0) log("Error reading line " + ch + " " +
@@ -360,4 +407,17 @@
return (sb.toString());
}
+ /** Return a URI (guessed) from the requestLine/fullRequest
+ */
+ public String getURI() {
+ String toExtract=fullRequest;
+ if( fullRequest==null ) toExtract=requestLine;
+ if( toExtract==null ) return null;
+
+ if( ! toExtract.startsWith("GET")) return null;
+ StringTokenizer st=new StringTokenizer( toExtract," " );
+ st.nextToken(); // GET
+ return st.nextToken();
+ }
+
}
1.2 +1 -1
jakarta-tomcat/src/share/org/apache/tomcat/util/test/Response.java
Index: Response.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/test/Response.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Response.java 2001/01/20 19:42:09 1.1
+++ Response.java 2001/01/21 06:39:22 1.2
@@ -72,7 +72,7 @@
String responseLine;
String responseBody;
- Hashtable responseHeaders;
+ Hashtable responseHeaders=new Hashtable();
Throwable exception;
1.4 +23 -1 jakarta-tomcat/src/tests/webpages/WEB-INF/test-tomcat.xml
Index: test-tomcat.xml
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/tests/webpages/WEB-INF/test-tomcat.xml,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- test-tomcat.xml 2001/01/20 20:11:04 1.3
+++ test-tomcat.xml 2001/01/21 06:39:22 1.4
@@ -22,7 +22,29 @@
<property name="gdir" value="../Golden" />
<property name="http.protocol" value="HTTP/1.0" />
- <taskdef name="gtest" classname="org.apache.tomcat.task.GTest" />
+ <taskdef name="gtest" classname="org.apache.tomcat.util.test.GTest" />
+ <taskdef name="httpClient"
+ classname="org.apache.tomcat.util.test.HttpClient" />
+ <taskdef name="header"
+ classname="org.apache.tomcat.util.test.Header" />
+ <taskdef name="body"
+ classname="org.apache.tomcat.util.test.Body" />
+ <taskdef name="defaultMatcher"
+ classname="org.apache.tomcat.util.test.DefaultMatcher" />
+
+ <!-- ==================== File tests ==================== -->
+ <target name="new-style" >
+ <gtest host="${host}" port="${port}" >
+ <httpClient path="/test/index.html" debug="0">
+ <header name="foo" value="bar"/>
+ <body>RequestBody</body>
+ </httpClient>
+ <defaultMatcher returnCode="200">
+ <header name="Content-Type" value="text/html"/>
+ </defaultMatcher>
+ </gtest>
+ </target>
+
<!-- ==================== File tests ==================== -->
<target name="file">
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]