jstrachan 2002/11/28 05:16:09
Modified: jelly/src/test/org/apache/commons/jelly/junit suite.jelly
jelly/src/java/org/apache/commons/jelly/tags/junit
JUnitTagLibrary.java
Added: jelly/src/java/org/apache/commons/jelly/tags/junit
AssertThrownTag.java
Log:
Applied a slight modificaiton of Knut Wannheden's patch that adds a
<test:assertThrown> tag that expects the body to throw an exception.
This makes JellyUnit testing of things which are expected to throw an exception much
easier.
Revision Changes Path
1.8 +68 -22
jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/junit/suite.jelly
Index: suite.jelly
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/junit/suite.jelly,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- suite.jelly 27 Nov 2002 19:13:25 -0000 1.7
+++ suite.jelly 28 Nov 2002 13:16:08 -0000 1.8
@@ -6,33 +6,33 @@
<test:assert test="${2 == 2}">This should never fail</test:assert>
<j:catch var="ex">
- <test:assert test="${2 != 2}">This should always fail</test:assert>
- </j:catch>
+ <test:assert test="${2 != 2}">This should always
fail</test:assert>
+ </j:catch>
- <test:assert test="${ex != null}">We should have created an
exception</test:assert>
-
- <log:info>The exception was: ${ex.message}</log:info>
+ <test:assert test="${ex != null}">We should have created an
exception</test:assert>
+
+ <log:info>The exception was: ${ex.message}</log:info>
</test:case>
<test:case name="failTests">
- <j:catch var="ex">
- <test:fail>This should always fail</test:fail>
- </j:catch>
-
- <test:assert test="${ex != null}">We should have created an
exception</test:assert>
-
- <log:info>The exception was: ${ex.message}</log:info>
+ <j:catch var="ex">
+ <test:fail>This should always fail</test:fail>
+ </j:catch>
+
+ <test:assert test="${ex != null}">We should have created an
exception</test:assert>
+
+ <log:info>The exception was: ${ex.message}</log:info>
<j:catch var="ex">
- <test:fail message="This should always fail"/>
- </j:catch>
-
- <test:assert test="${ex != null}">We should have created an
exception</test:assert>
+ <test:fail message="This should always fail"/>
+ </j:catch>
- <log:info>The exception was: ${ex.message}</log:info>
+ <test:assert test="${ex != null}">We should have created an
exception</test:assert>
+
+ <log:info>The exception was: ${ex.message}</log:info>
</test:case>
@@ -44,11 +44,57 @@
<j:catch var="ex">
<test:assertEquals actual="${foo}" expected="def">This should always
fail</test:assertEquals>
- </j:catch>
-
- <test:assert test="${ex != null}">We should have created an
exception</test:assert>
-
- <log:info>The exception was: ${ex.message}</log:info>
+ </j:catch>
+
+ <test:assert test="${ex != null}">We should have created an
exception</test:assert>
+
+ <log:info>The exception was: ${ex.message}</log:info>
+ </test:case>
+
+
+ <test:case name="assertThrownTests">
+
+ <!-- test using var attribute -->
+ <test:assertThrown var="ex">
+ <test:fail message="This exeption should be exported"/>
+ </test:assertThrown>
+ <test:assert test="${ex != null}">No exception exported</test:assert>
+
+ <!-- test using superclass of expected throwable -->
+ <test:assertThrown expected="java.lang.Exception">
+ <test:fail message="This should always fail"/>
+ </test:assertThrown>
+
+ <!-- test using non exception class -->
+ <test:assertThrown expected="java.lang.Class">
+ <test:fail message="This should always fail"/>
+ </test:assertThrown>
+
+ <!-- test using undefined class -->
+ <test:assertThrown expected="java.lang.ClassNotFoundException">
+ <test:assertThrown expected="foo.bar.Baz">
+ <test:fail message="This should always fail"/>
+ </test:assertThrown>
+ </test:assertThrown>
+
+ <!-- test using other exception class -->
+ <j:catch var="ex">
+ <test:assertThrown expected="java.io.IOException">
+ <test:fail message="This should always fail"/>
+ </test:assertThrown>
+ </j:catch>
+
+ <test:assert test="${ex != null}">We should have created an
exception</test:assert>
+
+ <!-- test with no exception from body -->
+ <j:catch var="ex">
+ <test:assertThrown>
+ </test:assertThrown>
+ </j:catch>
+
+ <test:assert test="${ex != null}">We should have created an
exception</test:assert>
+ <test:assert test="${ex.message.startsWith('No exception was thrown.')}"/>
+
</test:case>
<!--
1.7 +1 -0
jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/junit/JUnitTagLibrary.java
Index: JUnitTagLibrary.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/junit/JUnitTagLibrary.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- JUnitTagLibrary.java 27 Nov 2002 12:43:19 -0000 1.6
+++ JUnitTagLibrary.java 28 Nov 2002 13:16:08 -0000 1.7
@@ -90,6 +90,7 @@
public JUnitTagLibrary() {
registerTag("assert", AssertTag.class);
registerTag("assertEquals", AssertEqualsTag.class);
+ registerTag("assertThrown", AssertThrownTag.class);
registerTag("fail", FailTag.class);
registerTag("run", RunTag.class );
registerTag("case", CaseTag.class );
1.1
jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/junit/AssertThrownTag.java
Index: AssertThrownTag.java
===================================================================
/*
* ====================================================================
*
* 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 [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.commons.jelly.tags.junit;
import org.apache.commons.jelly.JellyException;
import org.apache.commons.jelly.XMLOutput;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Runs its body and asserts that an exception is thrown by it. If no
* exception is thrown the tag fails. By default all exceptions are caught.
* If however <code>expected</code> was specified the body must throw
* an exception of the given class, otherwise the assertion fails. The
* exception thrown by the body can also be of any subtype of the specified
* exception class. The optional <code>var</code> attribute can be specified if
* the caught exception is to be exported to a variable.
*/
public class AssertThrownTag extends AssertTagSupport {
/** The Log to which logging calls will be made. */
private static final Log log = LogFactory.getLog(AssertThrownTag.class);
/**
* The variable name to export the caught exception to.
*/
private String var;
/**
* The class name (fully qualified) of the exception expected to be thrown
* by the body. Also a superclass of the expected exception can be given.
*/
private String expected;
/**
* Sets the ClassLoader to be used when loading an exception class
*/
private ClassLoader classLoader;
// Tag interface
//-------------------------------------------------------------------------
public void doTag(XMLOutput output) throws Exception {
Class throwableClass = getThrowableClass();
try {
invokeBody(output);
}
catch (Throwable t) {
if (t instanceof JellyException) {
// unwrap Jelly exceptions which wrap other exceptions
JellyException je = (JellyException) t;
if (je.getCause() != null) {
t = je.getCause();
}
}
if (var != null) {
context.setVariable(var, t);
}
if (throwableClass != null &&
!throwableClass.isAssignableFrom(t.getClass())) {
fail("Unexpected exception: " + t);
}
else {
return;
}
}
fail("No exception was thrown.");
}
// Properties
//-------------------------------------------------------------------------
/**
* Sets the class name of exception expected to be thrown by the body. The
* class name must be fully qualified and can either be the expected
* exception class itself or any supertype of it, but must be a subtype of
* <code>java.lang.Throwable</code>.
*/
public void setExpected(String expected) {
this.expected = expected;
}
/**
* Sets the variable name to define for this expression.
*/
public void setVar(String var) {
this.var = var;
}
/**
* Sets the class loader to be used to load the exception type
*/
public void setClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}
public ClassLoader getClassLoader() {
if (classLoader == null) {
return getClass().getClassLoader();
}
return classLoader;
}
// Implementation methods
//-------------------------------------------------------------------------
/**
* Returns the <code>Class</code> corresponding to the class
* specified by <code>expected</code>. If
* <code>expected</code> was either not specified then <code>java. lang.
* Throwable</code> is returned.
* Otherwise if the class couldn't be
* found or doesn't denote an exception class then an exception is thrown.
*
* @return Class The class of the exception to expect
*/
protected Class getThrowableClass() throws ClassNotFoundException {
if (expected == null) {
return Throwable.class;
}
Class throwableClass = null;
try {
throwableClass = getClassLoader().loadClass(expected);
}
catch (ClassNotFoundException e) {
try {
throwableClass =
Thread.currentThread().getContextClassLoader().loadClass(expected);
}
catch (ClassNotFoundException e2) {
log.warn( "Could not find exception class: " + expected );
throw e;
}
}
if (!Throwable.class.isAssignableFrom(throwableClass)) {
log.warn( "The class: " + expected + " is not an Exception
class.");
return null;
}
return throwableClass;
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>