alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*   
 *  Copyright 1999-2004 The Apache Sofware Foundation.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.apache.tomcat.util.test;

import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Hashtable;

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("");
        if( result )
            out.println( "OK " );
        else
            out.println("FAIL ");
        if( uri!=null )
            out.println("");

        String msg=null;
        if(  "".equals( httpClient.getComment() )) 
            msg=" (" + httpRequest.getRequestLine() + ")";
        else
            msg=httpClient.getComment() + " (" + httpRequest.getRequestLine() + ")";

        out.println( msg );

        if( ! result )
            out.println("");
        
        out.println("
"); if( ! result ) { out.println("Message:
");
            out.println( httpClient.getFailureMessage() );
            out.println("
"); } if( ! result && httpClient.getDebug() > 0 ) { out.println("Request:
" + httpRequest.getFullRequest());
            out.println("
Response: " + httpRequest.getHttpResponse().getResponseLine()); out.println("
Response headers:
"); Hashtable headerH=httpRequest.getHttpResponse().getHeaders(); Enumeration hE=headerH.elements(); while( hE.hasMoreElements() ) { Header h=(Header) hE.nextElement(); out.println("" + h.getName() + ":" + h.getValue() + "
"); } out.println("Response body:
 ");
            out.println(httpRequest.getHttpResponse().getResponseBody());
            out.println("
"); } Throwable ex=httpRequest.getHttpResponse().getThrowable(); if( ex!=null) { out.println("Exception
");
            ex.printStackTrace(out);
            out.println("

"); } out.flush(); } }
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.