You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by js...@apache.org on 2002/11/28 09:35:48 UTC

cvs commit: jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/validate AssertValidTag.java

jstrachan    2002/11/28 00:35:48

  Modified:    jelly/src/java/org/apache/commons/jelly JellyException.java
               jelly/src/java/org/apache/commons/jelly/impl TagScript.java
               jelly/src/java/org/apache/commons/jelly/tags/xmlunit
                        XMLUnitTagSupport.java
               jelly/src/java/org/apache/commons/jelly/tags/junit
                        AssertTagSupport.java
               jelly/src/java/org/apache/commons/jelly/tags/validate
                        AssertValidTag.java
  Added:       jelly/src/java/org/apache/commons/jelly LocationAware.java
               jelly/src/java/org/apache/commons/jelly/tags/junit
                        JellyAssertionFailedError.java
  Log:
  Added support for LocationAware tags and exceptions. This allows tags or exceptions to be aware of where in a Jelly script they are invoked or thrown.
  
  This is particularly useful in JellyUnit. Now all the JellyUnit assertion failures throw JellyAssertionFailedErrrors which include details of where in the Jelly script the failure occurred.
  
  Revision  Changes    Path
  1.12      +6 -6      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/JellyException.java
  
  Index: JellyException.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/JellyException.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- JellyException.java	30 Oct 2002 19:10:48 -0000	1.11
  +++ JellyException.java	28 Nov 2002 08:35:48 -0000	1.12
  @@ -72,7 +72,7 @@
    * @version $Revision$
    */
   
  -public class JellyException extends Exception {
  +public class JellyException extends Exception implements LocationAware {
       
       /** the underlying cause of the exception */
       private Throwable cause;
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/LocationAware.java
  
  Index: LocationAware.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/LocationAware.java,v 1.11 2002/10/30 19:10:48 jstrachan Exp $
   * $Revision: 1.11 $
   * $Date: 2002/10/30 19:10:48 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 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", "Commons", 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 apache@apache.org.
   *
   * 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/>.
   * 
   * $Id: LocationAware.java,v 1.11 2002/10/30 19:10:48 jstrachan Exp $
   */
  
  package org.apache.commons.jelly;
  
  /** 
   * <p><code>LocationAware</code> represents a Tag or Exception which is location aware.
   * That is to say it is capable of recording where in a Jelly script a tag or exception
   * is used which can aid debugging and tracing.</p>
   *
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.11 $
   */
  
  public interface LocationAware {
      
      /** 
       * @return the line number of the tag 
       */
      public int getLineNumber();
      
      /** 
       * Sets the line number of the tag 
       */
      public void setLineNumber(int lineNumber);
  
      /** 
       * @return the column number of the tag 
       */
      public int getColumnNumber();
      
      /** 
       * Sets the column number of the tag 
       */
      public void setColumnNumber(int columnNumber);
  
      /** 
       * @return the Jelly file which caused the problem 
       */
      public String getFileName();
      /** 
       * Sets the Jelly file which caused the problem 
       */
      public void setFileName(String fileName);
      
  
      /** 
       * @return the element name which caused the problem
       */
      public String getElementName();
  
      /** 
       * Sets the element name which caused the problem
       */
      public void setElementName(String elementName);
  }
  
  
  
  1.29      +31 -18    jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/impl/TagScript.java
  
  Index: TagScript.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/impl/TagScript.java,v
  retrieving revision 1.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- TagScript.java	19 Nov 2002 13:28:42 -0000	1.28
  +++ TagScript.java	28 Nov 2002 08:35:48 -0000	1.29
  @@ -75,6 +75,7 @@
   import org.apache.commons.jelly.JellyContext;
   import org.apache.commons.jelly.JellyException;
   import org.apache.commons.jelly.DynaTag;
  +import org.apache.commons.jelly.LocationAware;
   import org.apache.commons.jelly.NamespaceAwareTag;
   import org.apache.commons.jelly.Script;
   import org.apache.commons.jelly.Tag;
  @@ -500,8 +501,11 @@
           tag.setBody( tagBody );
           
           if (tag instanceof NamespaceAwareTag) {
  -        	NamespaceAwareTag naTag = (NamespaceAwareTag) tag;
  -        	naTag.setNamespaceContext(getNamespaceContext());
  +            NamespaceAwareTag naTag = (NamespaceAwareTag) tag;
  +            naTag.setNamespaceContext(getNamespaceContext());
  +        }
  +        if (tag instanceof LocationAware) {
  +            applyLocation((LocationAware) tag);
           }
       }
        
  @@ -600,21 +604,26 @@
        * such as adding line number information etc.
        */
       protected void handleException(JellyException e) throws Exception {
  -    	if (log.isTraceEnabled()) {
  -        	log.trace( "Caught exception: " + e, e );
  -    	}
  +        if (log.isTraceEnabled()) {
  +            log.trace( "Caught exception: " + e, e );
  +        }
   
  -        if (e.getLineNumber() == -1) {
  -            e.setColumnNumber(columnNumber);
  -            e.setLineNumber(lineNumber);
  +        applyLocation(e);
  +        
  +        throw e;
  +    }
  +    
  +    protected void applyLocation(LocationAware locationAware) {
  +        if (locationAware.getLineNumber() == -1) {
  +            locationAware.setColumnNumber(columnNumber);
  +            locationAware.setLineNumber(lineNumber);
           }
  -        if ( e.getFileName() == null ) {
  -            e.setFileName( fileName );
  +        if ( locationAware.getFileName() == null ) {
  +            locationAware.setFileName( fileName );
           }
  -        if ( e.getElementName() == null ) {
  -            e.setElementName( elementName );
  +        if ( locationAware.getElementName() == null ) {
  +            locationAware.setElementName( elementName );
           }
  -        throw e;
       }
       
       /**
  @@ -627,6 +636,10 @@
           	log.trace( "Caught exception: " + e, e );
       	}
   
  +        if (e instanceof LocationAware) {
  +            applyLocation((LocationAware) e);
  +        }
  +        
           if ( e instanceof JellyException ) {
               e.fillInStackTrace();
               throw e;
  
  
  
  1.2       +4 -11     jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xmlunit/XMLUnitTagSupport.java
  
  Index: XMLUnitTagSupport.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xmlunit/XMLUnitTagSupport.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- XMLUnitTagSupport.java	27 Nov 2002 16:46:09 -0000	1.1
  +++ XMLUnitTagSupport.java	28 Nov 2002 08:35:48 -0000	1.2
  @@ -61,15 +61,16 @@
   import java.io.Reader;
   import java.net.URL;
   
  -import junit.framework.AssertionFailedError;
  -
   import org.apache.commons.jelly.TagSupport;
   import org.apache.commons.jelly.XMLOutput;
  +import org.apache.commons.jelly.tags.junit.AssertTagSupport;
  +
  +
   import org.dom4j.Document;
   import org.dom4j.io.SAXContentHandler;
   import org.dom4j.io.SAXReader;
   
  -public abstract class XMLUnitTagSupport extends TagSupport {
  +public abstract class XMLUnitTagSupport extends AssertTagSupport {
   
   	/** The SAXReader used to parser the document */
   	private SAXReader saxReader;
  @@ -129,12 +130,4 @@
   					+ source);
   		}
   	}
  -
  -	/**
  -	 * Produces a failure assertion with the given message
  -	 */
  -	protected void fail(String message) throws AssertionFailedError {
  -		throw new AssertionFailedError(message);
  -	}
  -
   }
  
  
  
  1.7       +4 -6      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/junit/AssertTagSupport.java
  
  Index: AssertTagSupport.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/junit/AssertTagSupport.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- AssertTagSupport.java	30 Oct 2002 19:16:30 -0000	1.6
  +++ AssertTagSupport.java	28 Nov 2002 08:35:48 -0000	1.7
  @@ -61,8 +61,6 @@
    */
   package org.apache.commons.jelly.tags.junit;
   
  -import junit.framework.AssertionFailedError;
  -
   import org.apache.commons.jelly.XMLOutput;
   import org.apache.commons.jelly.expression.Expression;
   import org.apache.commons.jelly.tags.xml.XPathTagSupport;
  @@ -85,14 +83,14 @@
       /**
        * Produces a failure assertion with the given message 
        */
  -    protected void fail(String message) throws AssertionFailedError {
  -        throw new AssertionFailedError(message);
  +    protected void fail(String message) throws JellyAssertionFailedError {
  +        throw new JellyAssertionFailedError(message);
       }
       
       /**
        * Produces a failure assertion with the given message and added detail.
        */
  -    protected void fail(String message, String detail) throws AssertionFailedError {
  +    protected void fail(String message, String detail) throws JellyAssertionFailedError {
           if (message == null || message.length() == 0) {
               fail(detail);
           }
  @@ -104,7 +102,7 @@
       /**
        * Produces a failure if the actual value was not equal to the expected value
        */
  -    protected void failNotEquals(String message, Object expected, Object actual, String expressions) throws AssertionFailedError {
  +    protected void failNotEquals(String message, Object expected, Object actual, String expressions) throws JellyAssertionFailedError {
           String formatted= "";
           if (message != null) {
               formatted = message +" ";
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/junit/JellyAssertionFailedError.java
  
  Index: JellyAssertionFailedError.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/JellyAssertionFailedError.java,v 1.11 2002/10/30 19:10:48 jstrachan Exp $
   * $Revision: 1.11 $
   * $Date: 2002/10/30 19:10:48 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 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", "Commons", 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 apache@apache.org.
   *
   * 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/>.
   * 
   * $Id: JellyAssertionFailedError.java,v 1.11 2002/10/30 19:10:48 jstrachan Exp $
   */
  
  package org.apache.commons.jelly.tags.junit;
  
  import java.io.PrintStream;
  import java.io.PrintWriter;
  
  import junit.framework.AssertionFailedError;
  
  import org.apache.commons.jelly.LocationAware;
  
  /** 
   * <p><code>JellyAssertionFailedError</code> is 
   * a JUnit AssertionFailedError which is LocationAware so that it can include
   * details of where in the JellyUnit test case that the failure occurred.</p>
   *
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.11 $
   */
  
  public class JellyAssertionFailedError extends Exception implements LocationAware {
      
      /** the underlying cause of the exception */
      private Throwable cause;
  
      /** the Jelly file which caused the problem */
      private String fileName;
  
      /** the tag name which caused the problem */
      private String elementName;
  
      /** the line number in the script of the error */
      private int lineNumber = -1;
      
      /** the column number in the script of the error */
      private int columnNumber = -1;
      
      public JellyAssertionFailedError() {
      }
  
      public JellyAssertionFailedError(String message) {
          super(message);
      }
  
      public JellyAssertionFailedError(String message, Throwable cause) {
          super(message);
          this.cause = cause;
      }
      
      public JellyAssertionFailedError(Throwable cause) {
          super(cause.getLocalizedMessage());
          this.cause = cause;
      }
      
      public Throwable getCause() {
          return cause;
      }
  
      
      /** 
       * @return the line number of the tag 
       */
      public int getLineNumber() {
          return lineNumber;
      }
      
      /** 
       * Sets the line number of the tag 
       */
      public void setLineNumber(int lineNumber) {
          this.lineNumber = lineNumber;
      }
  
      /** 
       * @return the column number of the tag 
       */
      public int getColumnNumber() {
          return columnNumber;
      }
      
      /** 
       * Sets the column number of the tag 
       */
      public void setColumnNumber(int columnNumber) {
          this.columnNumber = columnNumber;
      }
  
      /** 
       * @return the Jelly file which caused the problem 
       */
      public String getFileName() {
          return fileName;
      }
  
      /** 
       * Sets the Jelly file which caused the problem 
       */
      public void setFileName(String fileName) {
          this.fileName = fileName;
      }
      
  
      /** 
       * @return the element name which caused the problem
       */
      public String getElementName() {
          return elementName;
      }
  
      /** 
       * Sets the element name which caused the problem
       */
      public void setElementName(String elementName) {
          this.elementName = elementName;
      }
      
      
      public String getMessage() {
          return super.getMessage() + " File: " + fileName + " At tag <" + elementName + ">: line: " 
              + lineNumber + " column: " + columnNumber;
      }
  
      public String getReason() {
          return super.getMessage();
      }
  
      // #### overload the printStackTrace methods...
      public void printStackTrace(PrintWriter s) { 
          synchronized (s) {
              super.printStackTrace(s);
              if  (cause != null) {
                  s.println("Root cause");
                  cause.printStackTrace(s);
              }
          }
      }
          
      public void printStackTrace(PrintStream s) {
          synchronized (s) {
              super.printStackTrace(s);
              if  (cause != null) {
                  s.println("Root cause");
                  cause.printStackTrace(s);
              }
          }
      }
  
  	public void printStackTrace() {
  		super.printStackTrace();
  		if (cause != null) {
  			System.out.println("Root cause");
  			cause.printStackTrace();
  		}
  	}
  }
  
  
  
  1.3       +2 -3      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/validate/AssertValidTag.java
  
  Index: AssertValidTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/validate/AssertValidTag.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AssertValidTag.java	30 Oct 2002 19:16:31 -0000	1.2
  +++ AssertValidTag.java	28 Nov 2002 08:35:48 -0000	1.3
  @@ -61,9 +61,8 @@
    */
   package org.apache.commons.jelly.tags.validate;
   
  -import junit.framework.AssertionFailedError;
  -
   import org.apache.commons.jelly.XMLOutput;
  +import org.apache.commons.jelly.tags.junit.JellyAssertionFailedError;
   
   import org.xml.sax.ErrorHandler;
   import org.xml.sax.SAXException;
  @@ -122,7 +121,7 @@
           
           if ( ! valid ) {
               String message = buffer.toString();
  -            throw new AssertionFailedError( "The XML is not valid according to the schema: " + message );
  +            throw new JellyAssertionFailedError( "The XML is not valid according to the schema: " + message );
           }
       }
       
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>