jstrachan 2002/09/17 08:28:25
Modified: jelly/src/java/org/apache/commons/jelly/tags/junit
RunTag.java
Log:
Allow the test name to be defaulted to the default TestSuite created by the
<test:suite> tag
Also allow a TestListener to be specified via the listener="" attribute.
The listener defaults to a simple listener which outputs the results as XML events,
which can be consumed and processed by the parent tag/script
Revision Changes Path
1.2 +127 -1
jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/junit/RunTag.java
Index: RunTag.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/junit/RunTag.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- RunTag.java 22 Jul 2002 07:58:49 -0000 1.1
+++ RunTag.java 17 Sep 2002 15:28:24 -0000 1.2
@@ -61,13 +61,25 @@
*/
package org.apache.commons.jelly.tags.junit;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import junit.framework.AssertionFailedError;
import junit.framework.Test;
+import junit.framework.TestListener;
import junit.framework.TestResult;
import org.apache.commons.jelly.MissingAttributeException;
import org.apache.commons.jelly.TagSupport;
import org.apache.commons.jelly.XMLOutput;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.AttributesImpl;
+
/**
* This tag will run the given Test which could be an individual TestCase or a
TestSuite.
* The TestResult can be specified to capture the output, otherwise the results are
output
@@ -78,13 +90,20 @@
*/
public class RunTag extends TagSupport {
+ /** The Log to which logging calls will be made. */
+ private static final Log log = LogFactory.getLog(RunTag.class);
+
private Test test;
private TestResult result;
-
+ private TestListener listener;
// Tag interface
//-------------------------------------------------------------------------
public void doTag(XMLOutput output) throws Exception {
+ Test test = getTest();
+ if ( test == null ) {
+ test = (Test)
context.getVariable("org.apache.commons.jelly.junit.suite");
+ }
if ( test == null ) {
throw new MissingAttributeException( "test" );
}
@@ -92,6 +111,11 @@
if ( result == null ) {
result = createResult(output);
}
+ TestListener listener = getListener();
+ if ( listener == null ) {
+ listener = createTestListener(output);
+ }
+ result.addListener(listener);
test.run(result);
}
@@ -130,6 +154,24 @@
this.test = test;
}
+ /**
+ * Returns the listener.
+ * @return TestListener
+ */
+ public TestListener getListener() {
+ return listener;
+ }
+
+ /**
+ * Sets the TestListener.to be used to format the output of running the unit
test cases
+ * @param listener The listener to set
+ */
+ public void setListener(TestListener listener) {
+ this.listener = listener;
+ }
+
+
+
// Implementation methods
//-------------------------------------------------------------------------
@@ -139,5 +181,89 @@
*/
protected TestResult createResult(XMLOutput output) {
return new TestResult();
+ }
+
+ /**
+ * Factory method to create a new TestListener to capture the output of
+ * the test cases
+ */
+ protected TestListener createTestListener(final XMLOutput output) {
+ return new TestListener() {
+ public void addError(Test test, Throwable t) {
+ try {
+ output.startElement("error");
+
+ output.startElement("message");
+ output.write(t.getMessage());
+ output.endElement("message");
+
+ output.startElement("stack");
+ output.write( stackTraceToString(t) );
+ output.endElement("stack");
+
+ output.endElement("error");
+ }
+ catch (SAXException e) {
+ handleSAXException(e);
+ }
+ }
+
+ public void addFailure(Test test, AssertionFailedError t) {
+ try {
+ output.startElement("failure");
+
+ output.startElement("message");
+ output.write(t.getMessage());
+ output.endElement("message");
+
+ output.startElement("stack");
+ output.write( stackTraceToString(t) );
+ output.endElement("stack");
+
+ output.endElement("failure");
+ }
+ catch (SAXException e) {
+ handleSAXException(e);
+ }
+ }
+
+ public void endTest(Test test) {
+ try {
+ output.endElement("test");
+ }
+ catch (SAXException e) {
+ handleSAXException(e);
+ }
+ }
+
+ public void startTest(Test test) {
+ try {
+ String name = test.toString();
+ AttributesImpl attributes = new AttributesImpl();
+ attributes.addAttribute("", "name", "name", "CDATA", name);
+
+ output.startElement("test", attributes);
+ }
+ catch (SAXException e) {
+ handleSAXException(e);
+ }
+ }
+ };
+ }
+
+ /**
+ * @return the stack trace as a String
+ */
+ protected String stackTraceToString(Throwable t) {
+ StringWriter writer = new StringWriter();
+ t.printStackTrace(new PrintWriter(writer));
+ return writer.toString();
+ }
+
+ /**
+ * Handles SAX Exceptions
+ */
+ protected void handleSAXException(SAXException e) {
+ log.error( "Caught: " + e, e );
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>