cmlenz      2002/12/26 12:07:01

  Modified:    framework/src/java/share/org/apache/cactus/util
                        StringUtil.java
               framework/src/test/share/org/apache/cactus TestAll.java
  Added:       framework/src/test/share/org/apache/cactus/util
                        TestStringUtil.java
  Log:
  Add stack trace filtering functionality, mostly copied from the Ant test 
  runner.
  
  Revision  Changes    Path
  1.1                  
jakarta-cactus/framework/src/test/share/org/apache/cactus/util/TestStringUtil.java
  
  Index: TestStringUtil.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-2002 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", "Cactus" 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/>.
   *
   */
  package org.apache.cactus.util;
  
  import junit.framework.TestCase;
  
  /**
   * Unit tests for the <code>StringUtil</code> class.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Christopher Lenz</a>
   *
   * @version $Id: TestStringUtil.java,v 1.1 2002/12/26 20:07:00 cmlenz Exp $
   */
  public class TestStringUtil extends TestCase
  {
      // Make sure logging is disabled
      static
      {
          System.setProperty("org.apache.commons.logging.Log", 
              "org.apache.commons.logging.impl.NoOpLog");
      }
  
      /**
       * Defines the testcase name for JUnit.
       *
       * @param theName the testcase's name.
       */
      public TestStringUtil(String theName)
      {
          super(theName);
      }
  
      //-------------------------------------------------------------------------
  
      /**
       * Verify package-based stack-trace filtering.
       */
      public void testFilterLinePackageTrue()
      {
          String[] filterPatterns = new String[] {"my.package" };
          assertTrue(StringUtil.filterLine(
              "    at my.package.MyClass.method(MyClass.java:100)",
              filterPatterns));
      }
      
      /**
       * Verify package-based stack-trace filtering.
       */
      public void testFilterLinePackageFalse()
      {
          String[] filterPatterns = new String[] {"my.package" };
          assertFalse(StringUtil.filterLine(
              "    at other.package.MyClass.method(MyClass.java:100)",
              filterPatterns));
      }
  
      /**
       * Verify class-based stack-trace filtering.
       */
      public void testFilterLineClassTrue()
      {
          String[] filterPatterns = new String[] {"my.package.MyClass" };
          assertTrue(StringUtil.filterLine(
              "    at my.package.MyClass.method(MyClass.java:100)",
              filterPatterns));
      }
  
      /**
       * Verify class-based stack-trace filtering.
       */
      public void testFilterLineClassFalse1()
      {
          String[] filterPatterns = new String[] {"my.package.MyClass" };
          assertFalse(StringUtil.filterLine(
              "    at my.package.OtherClass.method(MyClass.java:100)",
              filterPatterns));
      }
  
      /**
       * Verify class-based stack-trace filtering.
       */
      public void testFilterLineClassFalse2()
      {
          String[] filterPatterns = new String[] {"my.package.MyClass" };
          assertFalse(StringUtil.filterLine(
              "    at other.package.MyClass.method(MyClass.java:100)",
              filterPatterns));
      }
  
  }
  
  
  
  1.3       +85 -3     
jakarta-cactus/framework/src/java/share/org/apache/cactus/util/StringUtil.java
  
  Index: StringUtil.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/util/StringUtil.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- StringUtil.java   28 Aug 2002 19:57:15 -0000      1.2
  +++ StringUtil.java   26 Dec 2002 20:07:01 -0000      1.3
  @@ -56,30 +56,112 @@
    */
   package org.apache.cactus.util;
   
  +import java.io.BufferedReader;
  +import java.io.IOException;
   import java.io.PrintWriter;
  +import java.io.StringReader;
   import java.io.StringWriter;
   
   /**
    * Various utility methods for string manipulation.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Vincent Massol</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Christopher Lenz</a>
    *
    * @version $Id$
    */
   public class StringUtil
   {
       /**
  +     * Returns the stack trace of an exception as String.
  +     * 
        * @param theThrowable the exception from which to extract the stack trace
        *        as a String
        * @return the exception stack trace as a String
        */
       public static String exceptionToString(Throwable theThrowable)
       {
  +        return exceptionToString(theThrowable, null);
  +    }
  +
  +    /**
  +     * Returns the stack trace of an exception as String, optionally filtering
  +     * out line from the stack trac
  +     * 
  +     * @param theThrowable the exception from which to extract the stack trace
  +     *        as a String
  +     * @param theFilterPatterns Array containing a list of patterns to filter 
  +     *        out from the stack trace
  +     * @return the exception stack trace as a String
  +     */
  +    public static String exceptionToString(Throwable theThrowable,
  +                                           String[] theFilterPatterns)
  +    {
           StringWriter sw = new StringWriter();
           PrintWriter pw = new PrintWriter(sw);
   
           theThrowable.printStackTrace(pw);
  +        String stackTrace = sw.toString();
  +        return filterStackTrace(stackTrace, theFilterPatterns);
  +    }
   
  -        return sw.toString();
  +    /**
  +     * 
  +     * 
  +     * @param theStackTrace The original, unfiltered stack trace
  +     * @param theFilterPatterns The patterns to filter out
  +     * @return The filtered stack trace
  +     */
  +    static String filterStackTrace(String theStackTrace,
  +                                   String[] theFilterPatterns)
  +    { 
  +        if ((theFilterPatterns == null) || (theFilterPatterns.length == 0) ||
  +            (theStackTrace == null)) 
  +        {
  +            return theStackTrace;
  +        }
  +
  +        StringWriter stringWriter = new StringWriter();
  +        PrintWriter printWriter = new PrintWriter(stringWriter);
  +        StringReader stringReader = new StringReader(theStackTrace);
  +        BufferedReader bufferedReader = new BufferedReader(stringReader);    
  +
  +        String line;
  +        try
  +        {   
  +            while ((line = bufferedReader.readLine()) != null)
  +            {
  +                if (!filterLine(line, theFilterPatterns))
  +                {
  +                    printWriter.println(line);
  +                }
  +            }
  +        }
  +        catch (IOException e)
  +        {
  +            return theStackTrace;
  +        }
  +        return stringWriter.toString();
       }
  -}
  \ No newline at end of file
  +
  +    /**
  +     * 
  +     * 
  +     * @param theLine The line to check
  +     * @param theFilterPatterns The patterns to filter out
  +     * @return boolean Whether the specified line should be filtered from the
  +     *         stack trace
  +     */
  +    static boolean filterLine(String theLine, String[] theFilterPatterns)
  +    {
  +        for (int i = 0; i < theFilterPatterns.length; i++)
  +        {
  +            if (theLine.indexOf(theFilterPatterns[i]) > 0)
  +            {
  +                return true;
  +            }
  +        }
  +        return false;
  +    }
  +
  +}
  
  
  
  1.7       +3 -1      
jakarta-cactus/framework/src/test/share/org/apache/cactus/TestAll.java
  
  Index: TestAll.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-cactus/framework/src/test/share/org/apache/cactus/TestAll.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TestAll.java      24 Nov 2002 17:12:17 -0000      1.6
  +++ TestAll.java      26 Dec 2002 20:07:01 -0000      1.7
  @@ -107,6 +107,8 @@
           suite.addTestSuite(org.apache.cactus.TestWebRequest.class);
           suite.addTestSuite(
               org.apache.cactus.client.TestWebTestResultParser.class);
  +        suite.addTestSuite(
  +            org.apache.cactus.util.TestStringUtil.class);
   
           return suite;
       }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to