larryi      01/07/17 06:34:04

  Modified:    src/share/org/apache/tomcat/util/test GTest.java
                        HttpClient.java TestDefaults.java
  Added:       src/share/org/apache/tomcat/util/test Report.java
  Log:
  My attempt a fixing the problem where HttpClient based tests only store
  their output in the static "result" vectors.  Thus, their results are only visible if
  the "driver" uses these vectors to display the output, such as test.jsp.
  If the test is run from a batch file, these tests have been silent.
  
  I have moved the handling which outputs to the PrintWriter to a Report class,
  which is used by HttpClient.  GTest now uses the Report class indirectly
  through HttpClient.  Output should now appear for HttpClient based tests
  when run from a batch file.
  
  Revision  Changes    Path
  1.12      +5 -88     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.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- GTest.java        2001/02/16 21:54:25     1.11
  +++ GTest.java        2001/07/17 13:34:00     1.12
  @@ -75,8 +75,6 @@
   */
   public class GTest  {
       // Defaults
  -    static PrintWriter defaultOutput=new PrintWriter(System.out);
  -    static String defaultOutType="text";
       static int defaultDebug=0;
       static boolean failureOnly=false;
   
  @@ -116,7 +114,7 @@
       }
   
       public static void setDefaultWriter( PrintWriter pw ) {
  -     defaultOutput=pw;
  +     Report.setDefaultWriter(pw);
       }
   
       /** @deprecated. Output will be text or none, with external
  @@ -124,7 +122,7 @@
        still a simple test runner )
       */
       public static void setDefaultOutput( String s ) {
  -     defaultOutType=s;
  +     Report.setDefaultOutput(s);
       }
   
       /** Vector of GTest elements, containing all test instances
  @@ -312,21 +310,13 @@
       public void execute() {
        try {
            //   System.out.println("XXX " + outType + " " + defaultOutType);
  -         if( out==null) out=defaultOutput;
  -         if( outType==null) outType=defaultOutType;
            if( debug==-1) debug=defaultDebug;
   
            initMatchers();
  +            httpClient.setWriter(out);
  +            httpClient.setOutput(outType);
  +            httpClient.setDebug(debug);
            httpClient.execute();
  -         HttpResponse resp=httpRequest.getHttpResponse();
  -
  -         result=httpClient.getResult();
  -         failMessage=httpClient.getFailureMessage();
  -
  -         if( "text".equals(outType) )
  -             textReport();
  -         if( "html".equals(outType) )
  -             htmlReport();
        } catch(Exception ex ) {
            // no exception should be thrown in normal operation
            ex.printStackTrace();
  @@ -372,78 +362,5 @@
            httpClient.addMatcher(sm );
        }
       }
  -    
  -    private void textReport() {
  -     String msg=null;
  -     if(  "".equals( getDescription() )) 
  -         msg=" (" + httpRequest.getRequestLine() + ")";
  -     else
  -         msg=getDescription() + " (" + httpRequest.getRequestLine() + ")";
  -
  -     if( result ) 
  -         out.println("OK " +  msg );
  -     else {
  -         out.println("FAIL " + msg );
  -         out.println("Message: " + failMessage);
  -     }
  -     out.flush();
  -    }
  -
  -    private void htmlReport() {
  -     String uri=httpRequest.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(  "".equals( getDescription() )) 
  -         msg=" (" + httpRequest.getRequestLine() + ")";
  -     else
  -         msg=getDescription() + " (" + httpRequest.getRequestLine() + ")";
  -
  -     out.println( msg );
  -     
  -     if( ! result )
  -         out.println("</font>");
  -     
  -     out.println("<br>");
  -
  -     if( ! result ) {
  -         out.println("<b>Message:</b><pre>");
  -         out.println( failMessage);
  -         out.println("</pre>");
  -     }
  -
  -     if( ! result && debug > 0 ) {
  -         out.println("<b>Request: </b><pre>" + httpRequest.getFullRequest());
  -         out.println("</pre><b>Response:</b> " +
  -                     httpRequest.getHttpResponse().getResponseLine());
  -         out.println("<br><b>Response headers:</b><br>");
  -         Hashtable headerH=httpRequest.getHttpResponse().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(httpRequest.getHttpResponse().getResponseBody());
  -         out.println("</pre>");
  -     }
  -
  -     Throwable ex=httpRequest.getHttpResponse().getThrowable();
  -     if( ex!=null) {
  -         out.println("<b>Exception</b><pre>");
  -         ex.printStackTrace(out);
  -         out.println("</pre><br>");
  -     }
  -     out.flush();
  -    }
  -
       
   }
  
  
  
  1.8       +25 -1     
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.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- HttpClient.java   2001/02/09 03:48:15     1.7
  +++ HttpClient.java   2001/07/17 13:34:01     1.8
  @@ -78,12 +78,17 @@
    *  Part of GTest - send a Http request. 
    */
   public class HttpClient {
  +    static Report defaultReport=new Report();
  +
       HttpRequest firstRequest=null;
       Vector actions=new Vector();
       String id;
       int debug=0;
       Body comment=null;
       boolean success=true;
  +
  +    PrintWriter out=null;
  +    String outType=null;
       
       public HttpClient() {
       }
  @@ -101,6 +106,10 @@
       public void setDebug( int d ) {
        debug=d;
       }
  +
  +    public int getDebug() {
  +        return debug;
  +    }
       
       /** Add a request that will be executed.
        */
  @@ -123,6 +132,14 @@
       public void setDescription( String s ) {
        comment=new Body( s );
       }
  +
  +    public void setWriter( PrintWriter pw ) {
  +        out=pw;
  +    }
  +
  +    public void setOutput( String t ) {
  +        outType=t;
  +    }
       
       // -------------------- Various matchers --------------------
   
  @@ -178,9 +195,9 @@
       // -------------------- Execute the request --------------------
   
       public void execute() {
  +        HttpRequest lastRequest=null;
        try {
            Enumeration aE=actions.elements();
  -         HttpRequest lastRequest=null;
            while( aE.hasMoreElements() ) {
                Object action=aE.nextElement();
                if( action instanceof HttpRequest ) {
  @@ -212,6 +229,13 @@
        else
            testSuccess.addElement( this );
   
  +        if( lastRequest != null) {
  +            defaultReport.setHttpClient(this);
  +            defaultReport.setHttpRequest(lastRequest);
  +            defaultReport.setWriter(out);
  +            defaultReport.setOutput(outType);
  +            defaultReport.writeReport();
  +        }
       }
   
       /** Invoke a request, set headers, responseLine, body
  
  
  
  1.3       +1 -1      
jakarta-tomcat/src/share/org/apache/tomcat/util/test/TestDefaults.java
  
  Index: TestDefaults.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/test/TestDefaults.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestDefaults.java 2001/02/09 03:48:15     1.2
  +++ TestDefaults.java 2001/07/17 13:34:01     1.3
  @@ -89,7 +89,7 @@
       }
   
       public void setOutputType(String t  ) {
  -     GTest.setDefaultOutput(t);
  +     Report.setDefaultOutput(t);
       }
   
       public void execute() {
  
  
  
  1.1                  jakarta-tomcat/src/share/org/apache/tomcat/util/test/Report.java
  
  Index: Report.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */ 
  package org.apache.tomcat.util.test;
  
  import java.io.*;
  import java.util.*;
  
  public class Report  {
      // Defaults
      static PrintWriter defaultOutput=new PrintWriter(System.out);
      static String defaultOutType="text";
  
      PrintWriter out=null;
      String outType=null;
      HttpClient httpClient;
      HttpRequest httpRequest;
      String description;
      String failueMessage;
  
      public static void setDefaultWriter( PrintWriter pw ) {
          defaultOutput=pw;
      }
  
      public static void setDefaultOutput( String s ) {
        defaultOutType=s;
      }
  
      public void setWriter( PrintWriter pw ) {
          out=pw;
      }
  
      /** text, xml, html
       */
      public void setOutput( String t ) {
          outType=t;
      }
  
      public void setDescription( String desc ) {
          description = desc;
      }
  
      public void setHttpClient( HttpClient client ) {
          httpClient = client;
      }
  
      public void setHttpRequest( HttpRequest req ) {
          httpRequest = req;
      }
  
      public void setFailureMsg( String msg ) {
          failueMessage = msg;
      }
  
      public void writeReport() {
          if( out==null) out=defaultOutput;
          if( outType==null) outType=defaultOutType;
  
          if( "text".equals(outType) )
              textReport();
          if( "html".equals(outType) )
              htmlReport();
      }
  
      private void textReport() {
          String msg=null;
          if(  "".equals( httpClient.getComment() )) 
              msg=" (" + httpRequest.getRequestLine() + ")";
          else
              msg=httpClient.getComment() + " (" + httpRequest.getRequestLine() + ")";
  
          if( httpClient.getResult() ) 
              out.println("OK " +  msg );
          else {
              out.println("FAIL " + msg );
              out.println("Message: " + httpClient.getFailureMessage());
          }
          out.flush();
      }
  
      private void htmlReport() {
          String uri=httpRequest.getURI();
          boolean result=httpClient.getResult();
          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(  "".equals( httpClient.getComment() )) 
              msg=" (" + httpRequest.getRequestLine() + ")";
          else
              msg=httpClient.getComment() + " (" + httpRequest.getRequestLine() + ")";
  
          out.println( msg );
  
          if( ! result )
              out.println("</font>");
          
          out.println("<br>");
  
          if( ! result ) {
              out.println("<b>Message:</b><pre>");
              out.println( httpClient.getFailureMessage() );
              out.println("</pre>");
          }
  
          if( ! result && httpClient.getDebug() > 0 ) {
              out.println("<b>Request: </b><pre>" + httpRequest.getFullRequest());
              out.println("</pre><b>Response:</b> " +
                          httpRequest.getHttpResponse().getResponseLine());
              out.println("<br><b>Response headers:</b><br>");
              Hashtable headerH=httpRequest.getHttpResponse().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(httpRequest.getHttpResponse().getResponseBody());
              out.println("</pre>");
          }
  
          Throwable ex=httpRequest.getHttpResponse().getThrowable();
          if( ex!=null) {
              out.println("<b>Exception</b><pre>");
              ex.printStackTrace(out);
              out.println("</pre><br>");
          }
          out.flush();
      }
  }
  
  

Reply via email to