You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by cu...@apache.org on 2001/02/21 20:38:01 UTC

cvs commit: xml-xalan/test/java/src/org/apache/qetest/trax LoggingErrorListener.java

curcuru     01/02/21 11:38:01

  Modified:    test/java/src/org/apache/qetest/trax
                        LoggingErrorListener.java
  Log:
  Use new base class; added setExpected() functionality to call check*() when expected errors handled
  
  Revision  Changes    Path
  1.2       +202 -131  xml-xalan/test/java/src/org/apache/qetest/trax/LoggingErrorListener.java
  
  Index: LoggingErrorListener.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/test/java/src/org/apache/qetest/trax/LoggingErrorListener.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- LoggingErrorListener.java	2000/11/13 19:53:58	1.1
  +++ LoggingErrorListener.java	2001/02/21 19:37:59	1.2
  @@ -2,7 +2,7 @@
    * The Apache Software License, Version 1.1
    *
    *
  - * Copyright (c) 2000 The Apache Software Foundation.  All rights 
  + * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights 
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -67,66 +67,41 @@
   import javax.xml.transform.ErrorListener;
   import javax.xml.transform.TransformerException;
   
  -//-------------------------------------------------------------------------
  -
   /**
    * Cheap-o ErrorListener for use by API tests.
    * <p>Implements javax.xml.transform.ErrorListener and dumps 
  - * everything to a Reporter; is separately settable as 
  - * to when it will throw an exception.</p>
  - * @todo try calling getLocator() and asking it for info directly
  + * everything to a Logger; is separately settable as 
  + * to when it will throw an exception; also separately settable 
  + * as to when we should validate specific events that we handle.</p>
  + * //@todo try calling getLocator() and asking it for info directly
    * @author shane_curcuru@lotus.com
  - * @version $Id: LoggingErrorListener.java,v 1.1 2000/11/13 19:53:58 curcuru Exp $
  + * @version $Id: LoggingErrorListener.java,v 1.2 2001/02/21 19:37:59 curcuru Exp $
    */
  -public class LoggingErrorListener implements ErrorListener
  +public class LoggingErrorListener extends LoggingHandler implements ErrorListener
   {
   
       /** No-op ctor seems useful. */
  -    public LoggingErrorListener(){}
  -
  -    /**
  -     * Ctor that calls setReporter automatically.  
  -     *
  -     * @param r Reporter we should log to
  -     * @todo this should really be a Logger, not Reporter
  -     */
  -    public LoggingErrorListener(Reporter r)
  +    public LoggingErrorListener()
       {
  -        setReporter(r);
  +        setLogger(getDefaultLogger());
       }
   
  -    /** Our Reporter, who we tell all our secrets to. */
  -    private Reporter reporter;
  -
       /**
  -     * Accesor methods for our Reporter.  
  +     * Ctor that calls setLogger automatically.  
        *
  -     * NEEDSDOC @param r
  +     * @param l Logger we should log to
        */
  -    public void setReporter(Reporter r)
  +    public LoggingErrorListener(Logger l)
       {
  -        if (r != null)
  -            reporter = r;
  +        setLogger(l);
       }
   
  -    /**
  -     * Accesor methods for our Reporter.  
  -     *
  -     * NEEDSDOC ($objectName$) @return
  -     */
  -    public Reporter getReporter()
  -    {
  -        return (reporter);
  -    }
  -
  -    /** Prefixed to all reporter msg output. */
  -    private final String prefix = "EH:";
   
       /** 
        * Constants determining when we should throw exceptions.
        * <ul>Flags are combineable like a bitfield.
        * <li>THROW_NEVER - never ever (always continue - note this 
  -     * have have unexpected effects when fatalErrors happen, see 
  +     * may have unexpected effects when fatalErrors happen, see 
        * {@link javax.xml.transform.ErrorListener#fatalError(javax.xml.transform.TransformerException)}</li>
        * <li>THROW_ON_WARNING - throw only on warnings</li>
        * <li>THROW_ON_ERROR - throw only on errors</li>
  @@ -136,26 +111,27 @@
        */
       public static final int THROW_NEVER = 0;
   
  -    /** NEEDSDOC Field THROW_ON_WARNING          */
  +    /** THROW_ON_WARNING - throw only on warnings.  */
       public static final int THROW_ON_WARNING = 1;
   
  -    /** NEEDSDOC Field THROW_ON_ERROR          */
  +    /** THROW_ON_ERROR - throw only on errors.  */
       public static final int THROW_ON_ERROR = 2;
   
  -    /** NEEDSDOC Field THROW_ON_FATAL          */
  +    /** THROW_ON_FATAL - throw only on fatalErrors - default.  */
       public static final int THROW_ON_FATAL = 4;
   
  -    /** NEEDSDOC Field THROW_ALWAYS          */
  +    /** THROW_ALWAYS - always throw exceptions.   */
       public static final int THROW_ALWAYS = THROW_ON_WARNING & THROW_ON_ERROR
                                              & THROW_ON_FATAL;
   
       /** If we should throw an exception for each message type. */
  -    private int throwWhen = THROW_ON_FATAL;
  +    protected int throwWhen = THROW_ON_FATAL;
   
       /**
  -     * Accesor methods; don't think it needs to be synchronized.  
  +     * Tells us when we should re-throw exceptions.  
        *
  -     * NEEDSDOC @param t
  +     * @param t THROW_WHEN_* constant as to when we should re-throw 
  +     * an exception when we are called
        */
       public void setThrowWhen(int t)
       {
  @@ -163,112 +139,169 @@
       }
   
       /**
  -     * Accesor methods; don't think it needs to be synchronized.  
  +     * Tells us when we should re-throw exceptions.  
        *
  -     * NEEDSDOC ($objectName$) @return
  +     * @return THROW_WHEN_* constant as to when we should re-throw 
  +     * an exception when we are called
        */
       public int getThrowWhen()
       {
           return throwWhen;
       }
   
  -    /** Counters for how many problems or messages we've processed. */
  -    private int warningCtr = 0;
  +    /** Constant for items returned in getCounters: messages.  */
  +    public static final int TYPE_WARNING = 0;
   
  -    /** NEEDSDOC Field errorCtr          */
  -    private int errorCtr = 0;
  +    /** Constant for items returned in getCounters: errors.  */
  +    public static final int TYPE_ERROR = 1;
   
  -    /** NEEDSDOC Field fatalCtr          */
  -    private int fatalCtr = 0;
  +    /** Constant for items returned in getCounters: fatalErrors.  */
  +    public static final int TYPE_FATALERROR = 2;
   
  -    /**
  -     * Accesor methods for counters.  
  -     *
  -     * NEEDSDOC ($objectName$) @return
  +    /** 
  +     * Counters for how many events we've handled.  
  +     * Index into array are the TYPE_* constants.
        */
  -    public int getWarningCtr()
  +    protected int[] counters = 
       {
  -        return warningCtr;
  -    }
  +        0, /* warning */
  +        0, /* error */
  +        0  /* fatalError */
  +    };
   
  -    /**
  -     * Accesor methods for counters.  
  -     *
  -     * NEEDSDOC ($objectName$) @return
  -     */
  -    public int getErrorCtr()
  -    {
  -        return errorCtr;
  -    }
   
       /**
  -     * Accesor methods for counters.  
  +     * Get a list of counters of all items we've logged.
  +     * Returned as warnings, errors, fatalErrors
  +     * Index into array are the TYPE_* constants.
        *
  -     * NEEDSDOC ($objectName$) @return
  +     * @return array of int counters for each item we log
        */
  -    public int getFatalCtr()
  +    public int[] getCounters()
       {
  -        return fatalCtr;
  +        return counters;
       }
   
  +    /** Prefixed to all logger msg output. */
  +    public static final String prefix = "LEL:";
  +
  +
       /**
  -     * Cheap-o string representation of our state.  
  +     * Really Cheap-o string representation of our state.  
        *
  -     * NEEDSDOC ($objectName$) @return
  +     * @return String of getCounters() rolled up in minimal space
        */
  -    public String getCounterString()
  +    public String getQuickCounters()
       {
  -        return (prefix + " Warnings: " + getWarningCtr() + ", Errors: "
  -                + getErrorCtr() + ", FatalErrors: " + getFatalCtr());
  +        return (prefix + "(" + counters[TYPE_WARNING] + ", "
  +                + counters[TYPE_ERROR] + ", " + counters[TYPE_FATALERROR] + ")");
       }
   
  +
       /** Cheap-o string representation of last warn/error/fatal we got. */
  -    private String lastError = null;
  +    protected String lastItem = NOTHING_HANDLED;
   
       /**
  -     * NEEDSDOC Method setLastError 
  -     *
  +     * Sets a String representation of last item we handled. 
        *
  -     * NEEDSDOC @param s
  +     * @param s set into lastItem for retrieval with getLast()
        */
  -    protected void setLastError(String s)
  +    protected void setLastItem(String s)
       {
  -        lastError = s;
  +        lastItem = s;
       }
   
       /**
  -     * Accessor for string representation of last warn/error/fatal we got.  
  +     * Get a string representation of last item we logged.  
        *
  -     * NEEDSDOC ($objectName$) @return
  +     * @return String of the last item handled
        */
  -    public String getLastError()
  +    public String getLast()
       {
  -        return lastError;
  +        return lastItem;
       }
   
  -    /** What loggingLevel to use for reporter.logMsg(). */
  -    private int level = Reporter.DEFAULT_LOGGINGLEVEL;
   
  +    /** Expected values for events we may handle, default=ITEM_DONT_CARE. */
  +    protected String[] expected = 
  +    {
  +        ITEM_DONT_CARE, /* warning */
  +        ITEM_DONT_CARE, /* error */
  +        ITEM_DONT_CARE  /* fatalError */
  +    };
  +
  +
       /**
  -     * Accesor methods; don't think it needs to be synchronized.  
  +     * Ask us to report checkPass/Fail for certain events we handle.
  +     * Since we may have to handle many events between when a test 
  +     * will be able to call us, testers can set this to have us 
  +     * automatically call checkPass when we see an item that matches, 
  +     * or to call checkFail when we get an unexpected item.
  +     * Generally, we only call check* methods when:
  +     * <ul>
  +     * <li>containsString is not set, reset, or is ITEM_DONT_CARE, 
  +     * we do nothing (i.e. never call check* for this item)</li>
  +     * <li>containsString is ITEM_CHECKFAIL, we will always call 
  +     * checkFail with the contents of any item if it occours</li>
  +     * <li>containsString is anything else, we will grab a String 
  +     * representation of every item of that type that comes along, 
  +     * and if the containsString is found, case-sensitive, within 
  +     * the handled item's string, call checkPass, otherwise 
  +     * call checkFail</li>
  +     * <ul>
  +     * Note that any time we handle a particular event that was 
  +     * expected, we un-set the expected value for that item.  This 
  +     * means that you can only ask us to validate one occourence 
  +     * of any particular event; all events after that one will 
  +     * be treated as ITEM_DONT_CARE.  Callers can of course call 
  +     * setExpected again, of course, but this covers the case where 
  +     * we handle multiple events in a single block, perhaps out of 
  +     * the caller's direct control. 
  +     * Note that we first store the event via setLast(), then we 
  +     * validate the event as above, and then we potentially 
  +     * re-throw the exception as by setThrowWhen().
        *
  -     * NEEDSDOC @param l
  +     * @param itemType which of the various types of items we might 
  +     * handle; should be defined as a constant by subclasses
  +     * @param containsString a string to look for within whatever 
  +     * item we handle - usually checked for by seeing if the actual 
  +     * item we handle contains the containsString
        */
  -    public void setLoggingLevel(int l)
  +    public void setExpected(int itemType, String containsString)
       {
  -        level = l;
  +        // Default to don't care on null
  +        if (null == containsString)
  +            containsString = ITEM_DONT_CARE;
  +
  +        try
  +        {
  +            expected[itemType] = containsString;
  +        }
  +        catch (ArrayIndexOutOfBoundsException aioobe)
  +        {
  +            // Just log it for callers reference and continue anyway
  +            logger.logMsg(level, prefix + " setExpected called with illegal type:" + itemType);
  +        }
       }
   
  +
       /**
  -     * Accesor methods; don't think it needs to be synchronized.  
  -     *
  -     * NEEDSDOC ($objectName$) @return
  +     * Reset all items or counters we've handled.  
        */
  -    public int getLoggingLevel()
  +    public void reset()
       {
  -        return level;
  +        setLastItem(NOTHING_HANDLED);
  +        for (int i = 0; i < counters.length; i++)
  +        {
  +            counters[i] = 0;
  +        }
  +        for (int j = 0; j < expected.length; j++)
  +        {
  +            expected[j] = ITEM_DONT_CARE;
  +        }
       }
   
  +
       /**
        * Grab basic info out of a TransformerException.
        *
  @@ -279,33 +312,32 @@
       {
   
           if (exception == null)
  -            return null;
  +            return "";  // Don't return null, just to make other code here simpler
   
           return exception.getMessageAndLocation();
       }
   
  +
       /**
        * Implementation of warning; calls logMsg with info contained in exception.
        *
  -     * NEEDSDOC @param exception
  -     * @exception TransformerException thrown only if asked to or if reporters are bad
  +     * @param exception provided by Transformer
  +     * @exception TransformerException thrown only if asked to or if loggers are bad
        */
       public void warning(TransformerException exception) throws TransformerException
       {
   
  -        // Increment counter, save the exception, and log what we got
  -        warningCtr++;
  +        // Increment counter and save the exception
  +        counters[TYPE_WARNING]++;
   
           String exInfo = getTransformerExceptionInfo(exception);
   
  -        setLastError(exInfo);
  +        setLastItem(exInfo);
   
  -        if (reporter != null)
  -        {
  -            reporter.logMsg(level, prefix + " warning threw: " + exInfo);
  -            reporter.logMsg(level, getCounterString());
  -        }
  +        // Log or validate the exception
  +        logOrCheck(TYPE_WARNING, "warning", exInfo);
   
  +        // Also re-throw the exception if asked to
           if ((throwWhen & THROW_ON_WARNING) == THROW_ON_WARNING)
           {
               throw new TransformerException(exception);
  @@ -314,27 +346,25 @@
   
       /**
        * Implementation of error; calls logMsg with info contained in exception.
  -     * Only ever throws an exception itself if asked to or if reporters are bad.
  +     * Only ever throws an exception itself if asked to or if loggers are bad.
        *
  -     * NEEDSDOC @param exception
  -     * @exception TransformerException thrown only if asked to or if reporters are bad
  +     * @param exception provided by Transformer
  +     * @exception TransformerException thrown only if asked to or if loggers are bad
        */
       public void error(TransformerException exception) throws TransformerException
       {
   
           // Increment counter, save the exception, and log what we got
  -        errorCtr++;
  +        counters[TYPE_ERROR]++;
   
           String exInfo = getTransformerExceptionInfo(exception);
   
  -        setLastError(exInfo);
  +        setLastItem(exInfo);
   
  -        if (reporter != null)
  -        {
  -            reporter.logMsg(level, prefix + " error threw: " + exInfo);
  -            reporter.logMsg(level, getCounterString());
  -        }
  +        // Log or validate the exception
  +        logOrCheck(TYPE_ERROR, "error", exInfo);
   
  +        // Also re-throw the exception if asked to
           if ((throwWhen & THROW_ON_ERROR) == THROW_ON_ERROR)
           {
               throw new TransformerException(exception);
  @@ -343,32 +373,73 @@
   
       /**
        * Implementation of error; calls logMsg with info contained in exception.
  -     * Only ever throws an exception itself if asked to or if reporters are bad.
  +     * Only ever throws an exception itself if asked to or if loggers are bad.
        * Note that this may cause unusual behavior since we may not actually
        * re-throw the exception, even though it was 'fatal'.
        *
  -     * NEEDSDOC @param exception
  -     * @exception TransformerException thrown only if asked to or if reporters are bad
  +     * @param exception provided by Transformer
  +     * @exception TransformerException thrown only if asked to or if loggers are bad
        */
       public void fatalError(TransformerException exception) throws TransformerException
       {
   
           // Increment counter, save the exception, and log what we got
  -        fatalCtr++;
  +        counters[TYPE_FATALERROR]++;
   
           String exInfo = getTransformerExceptionInfo(exception);
   
  -        setLastError(exInfo);
  +        setLastItem(exInfo);
   
  -        if (reporter != null)
  -        {
  -            reporter.logMsg(level, prefix + " fatal threw: " + exInfo);
  -            reporter.logMsg(level, getCounterString());
  -        }
  +        // Log or validate the exception
  +        logOrCheck(TYPE_FATALERROR, "fatalError", exInfo);
   
  +        // Also re-throw the exception if asked to
           if ((throwWhen & THROW_ON_FATAL) == THROW_ON_FATAL)
           {
               throw new TransformerException(exception);
  +        }
  +    }
  +
  +
  +    /**
  +     * Worker method to either log or call check* for this event.  
  +     * A simple way to validate for any kind of event.
  +     *
  +     * @param type of message (warning/error/fatalerror)
  +     * @param desc description of this kind of message
  +     * @param exInfo String representation of current exception
  +     */
  +    protected void logOrCheck(int type, String desc, String exInfo)
  +    {
  +        String tmp = getQuickCounters() + " " + desc;
  +        // Either log the exception or call checkPass/checkFail 
  +        //  as requested by setExpected for this type
  +        if (ITEM_DONT_CARE == expected[type])
  +        {
  +            // We don't care about this, just log it
  +            logger.logMsg(level, desc + " threw: " + exInfo);
  +        }
  +        else if (ITEM_CHECKFAIL == expected[type])
  +        {
  +            // We shouldn't have been called here, so fail
  +            logger.checkFail(desc + " threw-unexpected: " + exInfo);
  +        }
  +        else if (exInfo.indexOf(expected[type]) > -1)
  +        {   
  +            // We got a warning the user expected, so pass
  +            logger.checkPass(desc + " threw-matching: " + exInfo);
  +            // Also reset this counter
  +            //@todo needswork: this is very state-dependent, and 
  +            //  might not be what the user expects, but at least it 
  +            //  won't give lots of extra false fails or passes
  +            expected[type] = ITEM_DONT_CARE;
  +        }
  +        else
  +        {
  +            // We got a warning the user didn't expect, so fail
  +            logger.checkFail(desc + " threw-notmatching: " + exInfo);
  +            // Also reset this counter
  +            expected[type] = ITEM_DONT_CARE;
           }
       }
   }