Clover coverage report - Cactus 1.5 for J2EE API 1.3
Coverage timestamp: Wed Feb 18 2004 09:09:13 EST
file stats: LOC: 265   Methods: 6
NCLOC: 116   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
WebTestResultParser.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  * ====================================================================
 3   
  *
 4   
  * The Apache Software License, Version 1.1
 5   
  *
 6   
  * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
 7   
  * reserved.
 8   
  *
 9   
  * Redistribution and use in source and binary forms, with or without
 10   
  * modification, are permitted provided that the following conditions
 11   
  * are met:
 12   
  *
 13   
  * 1. Redistributions of source code must retain the above copyright
 14   
  *    notice, this list of conditions and the following disclaimer.
 15   
  *
 16   
  * 2. Redistributions in binary form must reproduce the above copyright
 17   
  *    notice, this list of conditions and the following disclaimer in
 18   
  *    the documentation and/or other materials provided with the
 19   
  *    distribution.
 20   
  *
 21   
  * 3. The end-user documentation included with the redistribution, if
 22   
  *    any, must include the following acknowlegement:
 23   
  *       "This product includes software developed by the
 24   
  *        Apache Software Foundation (http://www.apache.org/)."
 25   
  *    Alternately, this acknowlegement may appear in the software itself,
 26   
  *    if and wherever such third-party acknowlegements normally appear.
 27   
  *
 28   
  * 4. The names "The Jakarta Project", "Cactus" and "Apache Software
 29   
  *    Foundation" must not be used to endorse or promote products
 30   
  *    derived from this software without prior written permission. For
 31   
  *    written permission, please contact apache@apache.org.
 32   
  *
 33   
  * 5. Products derived from this software may not be called "Apache"
 34   
  *    nor may "Apache" appear in their names without prior written
 35   
  *    permission of the Apache Group.
 36   
  *
 37   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 38   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 39   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 40   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 41   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 42   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 43   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 44   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 45   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 46   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 47   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 48   
  * SUCH DAMAGE.
 49   
  * ====================================================================
 50   
  *
 51   
  * This software consists of voluntary contributions made by many
 52   
  * individuals on behalf of the Apache Software Foundation.  For more
 53   
  * information on the Apache Software Foundation, please see
 54   
  * <http://www.apache.org/>.
 55   
  *
 56   
  */
 57   
 package org.apache.cactus.client;
 58   
 
 59   
 import org.apache.cactus.WebTestResult;
 60   
 
 61   
 /**
 62   
  * Parse a string representing a Test result and transform it into a
 63   
  * <code>WebTestResult</code> object.
 64   
  *
 65   
  * @see WebTestResult
 66   
  *
 67   
  * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
 68   
  *
 69   
  * @version $Id: WebTestResultParser.java,v 1.10 2003/05/26 11:45:25 cmlenz Exp $
 70   
  */
 71   
 public class WebTestResultParser
 72   
 {
 73   
     /**
 74   
      * Parsed exception class name
 75   
      */
 76   
     protected String exceptionClassname;
 77   
 
 78   
     /**
 79   
      * Parsed exception message
 80   
      */
 81   
     protected String exceptionMessage;
 82   
 
 83   
     /**
 84   
      * Parsed exception stack trace
 85   
      */
 86   
     protected String exceptionStacktrace;
 87   
 
 88   
     /**
 89   
      * Parse a string and transform it into a <code>WebTestResult</code> object.
 90   
      *
 91   
      * @param theData the string to parse
 92   
      * @return the <code>WebTestResult</code> object corresponding to the data
 93   
      *         string
 94   
      * @exception ParsingException if an error happens during parsing
 95   
      */
 96  0
     public WebTestResult parse(String theData) throws ParsingException
 97   
     {
 98  0
         String buffer;
 99  0
         WebTestResult result;
 100   
 
 101  0
         buffer = readRootElement(theData);
 102   
 
 103  0
         if (buffer.length() == 0)
 104   
         {
 105  0
             result = new WebTestResult();
 106   
         }
 107   
         else
 108   
         {
 109  0
             buffer = readExceptionClassname(buffer);
 110  0
             buffer = readExceptionMessage(buffer);
 111  0
             buffer = readExceptionStacktrace(buffer);
 112  0
             result = new WebTestResult(this.exceptionClassname, 
 113   
                 this.exceptionMessage, this.exceptionStacktrace);
 114   
         }
 115   
 
 116  0
         return result;
 117   
     }
 118   
 
 119   
     /**
 120   
      * Read the {@link WebTestResult#XML_ROOT_ELEMENT} portion.
 121   
      *
 122   
      * @param theData the string buffer to parse
 123   
      * @return the string buffer minus what has been read
 124   
      * @exception ParsingException if an error happens during parsing
 125   
      */
 126  0
     protected String readRootElement(String theData) throws ParsingException
 127   
     {
 128  0
         String startRootString = "<" + WebTestResult.XML_ROOT_ELEMENT + ">";
 129  0
         String endRootString = "</" + WebTestResult.XML_ROOT_ELEMENT + ">";
 130  0
         String buffer;
 131   
 
 132   
         // It is possible that some end of line character are inserted at the
 133   
         // end of the string. This is valid, which is why we trim the string
 134   
         // before perfoming the checks.
 135  0
         String trimmedData = theData.trim();
 136   
 
 137  0
         if (trimmedData.startsWith(startRootString)
 138   
             && trimmedData.endsWith(endRootString))
 139   
         {
 140  0
             buffer = trimmedData.substring(startRootString.length(), 
 141   
                 trimmedData.length() - endRootString.length());
 142   
         }
 143   
         else
 144   
         {
 145  0
             throw new ParsingException(formatError(theData));
 146   
         }
 147   
 
 148  0
         return buffer;
 149   
     }
 150   
 
 151   
     /**
 152   
      * Read the {@link WebTestResult#XML_EXCEPTION_CLASSNAME_ATTRIBUTE} portion
 153   
      * and extract the exception classname.
 154   
      *
 155   
      * @param theData the string buffer to parse
 156   
      * @return the string buffer minus what has been read
 157   
      * @exception ParsingException if an error happens during parsing
 158   
      */
 159  0
     protected String readExceptionClassname(String theData)
 160   
         throws ParsingException
 161   
     {
 162  0
         String startString = "<" + WebTestResult.XML_EXCEPTION_ELEMENT + " "
 163   
             + WebTestResult.XML_EXCEPTION_CLASSNAME_ATTRIBUTE + "=\"";
 164  0
         String endString = "</" + WebTestResult.XML_EXCEPTION_ELEMENT + ">";
 165  0
         String buffer;
 166   
 
 167  0
         if (theData.startsWith(startString) && theData.endsWith(endString))
 168   
         {
 169  0
             int pos = theData.indexOf('\"', startString.length());
 170   
 
 171  0
             this.exceptionClassname = theData.substring(startString.length(), 
 172   
                 pos);
 173  0
             buffer = theData.substring(startString.length()
 174   
                 + this.exceptionClassname.length() + 2, 
 175   
                 theData.length() - endString.length());
 176   
         }
 177   
         else
 178   
         {
 179  0
             throw new ParsingException(formatError(theData));
 180   
         }
 181   
 
 182  0
         return buffer;
 183   
     }
 184   
 
 185   
     /**
 186   
      * Read the {@link WebTestResult#XML_EXCEPTION_MESSAGE_ELEMENT} portion
 187   
      * and extract the exception message.
 188   
      *
 189   
      * @param theData the string buffer to parse
 190   
      * @return the string buffer minus what has been read
 191   
      * @exception ParsingException if an error happens during parsing
 192   
      */
 193  0
     protected String readExceptionMessage(String theData)
 194   
         throws ParsingException
 195   
     {
 196  0
         String startString = "<" + WebTestResult.XML_EXCEPTION_MESSAGE_ELEMENT
 197   
             + "><![CDATA[";
 198  0
         String endString = "]]></"
 199   
             + WebTestResult.XML_EXCEPTION_MESSAGE_ELEMENT + ">";
 200  0
         String buffer;
 201   
 
 202  0
         if (theData.startsWith(startString))
 203   
         {
 204  0
             int pos = theData.indexOf(endString, startString.length());
 205   
 
 206  0
             this.exceptionMessage = theData.substring(startString.length(), 
 207   
                 pos);
 208  0
             buffer = theData.substring(pos + endString.length());
 209   
         }
 210   
         else
 211   
         {
 212  0
             throw new ParsingException(formatError(theData));
 213   
         }
 214   
 
 215  0
         return buffer;
 216   
     }
 217   
 
 218   
     /**
 219   
      * Read the {@link WebTestResult#XML_EXCEPTION_STACKTRACE_ELEMENT} portion
 220   
      * and extract the exception stacktrace.
 221   
      *
 222   
      * @param theData the string buffer to parse
 223   
      * @return the string buffer minus what has been read
 224   
      * @exception ParsingException if an error happens during parsing
 225   
      */
 226  0
     protected String readExceptionStacktrace(String theData)
 227   
         throws ParsingException
 228   
     {
 229  0
         String startString = "<"
 230   
             + WebTestResult.XML_EXCEPTION_STACKTRACE_ELEMENT + "><![CDATA[";
 231  0
         String endString = "]]></"
 232   
             + WebTestResult.XML_EXCEPTION_STACKTRACE_ELEMENT + ">";
 233  0
         String buffer;
 234   
 
 235  0
         if (theData.startsWith(startString))
 236   
         {
 237  0
             int pos = theData.indexOf(endString, startString.length());
 238   
 
 239  0
             this.exceptionStacktrace = theData.substring(startString.length(), 
 240   
                 pos);
 241  0
             buffer = theData.substring(pos + endString.length());
 242   
         }
 243   
         else
 244   
         {
 245  0
             throw new ParsingException(formatError(theData));
 246   
         }
 247   
 
 248  0
         return buffer;
 249   
     }
 250   
 
 251   
     /**
 252   
      * @param theData the data to format
 253   
      * @return the first 100 characters (or less if the data has fewer
 254   
      *        characters) of the invalid data as it can be very big
 255   
      */
 256  0
     private String formatError(String theData)
 257   
     {
 258  0
         int nbChars = theData.length() > 100 ? 100 : theData.length();
 259   
 
 260  0
         return "Not a valid response. First " + nbChars 
 261   
             + " characters of the reponse: ["
 262   
             + theData.substring(0, nbChars) + "]";        
 263   
     }
 264   
 }
 265