001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jelly.tags.junit;
017:
018: import java.io.PrintWriter;
019: import java.io.StringWriter;
020:
021: import junit.framework.AssertionFailedError;
022: import junit.framework.Test;
023: import junit.framework.TestListener;
024: import junit.framework.TestResult;
025:
026: import org.apache.commons.jelly.JellyTagException;
027: import org.apache.commons.jelly.MissingAttributeException;
028: import org.apache.commons.jelly.TagSupport;
029: import org.apache.commons.jelly.XMLOutput;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.xml.sax.SAXException;
033: import org.xml.sax.helpers.AttributesImpl;
034:
035: /**
036: * This tag will run the given Test which could be an individual TestCase or a TestSuite.
037: * The TestResult can be specified to capture the output, otherwise the results are output
038: * as XML so that they can be formatted in some custom manner.
039: *
040: * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
041: * @version $Revision: 155420 $
042: */
043: public class RunTag extends TagSupport {
044:
045: /** The Log to which logging calls will be made. */
046: private static final Log log = LogFactory.getLog(RunTag.class);
047:
048: private Test test;
049: private TestResult result;
050: private TestListener listener;
051:
052: // Tag interface
053: //-------------------------------------------------------------------------
054: public void doTag(XMLOutput output) throws JellyTagException {
055: Test test = getTest();
056: if (test == null) {
057: test = (Test) context
058: .getVariable("org.apache.commons.jelly.junit.suite");
059: }
060: if (test == null) {
061: throw new MissingAttributeException("test");
062: }
063: TestResult result = getResult();
064: if (result == null) {
065: result = createResult(output);
066: }
067: TestListener listener = getListener();
068: if (listener == null) {
069: listener = createTestListener(output);
070: }
071: result.addListener(listener);
072: test.run(result);
073: }
074:
075: // Properties
076: //-------------------------------------------------------------------------
077:
078: /**
079: * Returns the TestResult used to capture the output of the test.
080: * @return TestResult
081: */
082: public TestResult getResult() {
083: return result;
084: }
085:
086: /**
087: * Returns the Test to be ran.
088: * @return Test
089: */
090: public Test getTest() {
091: return test;
092: }
093:
094: /**
095: * Sets the JUnit TestResult used to capture the results of the tst
096: * @param result The TestResult to use
097: */
098: public void setResult(TestResult result) {
099: this .result = result;
100: }
101:
102: /**
103: * Sets the JUnit Test to run which could be an individual test or a TestSuite
104: * @param test The test to run
105: */
106: public void setTest(Test test) {
107: this .test = test;
108: }
109:
110: /**
111: * Returns the listener.
112: * @return TestListener
113: */
114: public TestListener getListener() {
115: return listener;
116: }
117:
118: /**
119: * Sets the TestListener.to be used to format the output of running the unit test cases
120: * @param listener The listener to set
121: */
122: public void setListener(TestListener listener) {
123: this .listener = listener;
124: }
125:
126: // Implementation methods
127: //-------------------------------------------------------------------------
128:
129: /**
130: * Factory method to create a new TestResult to capture the output of
131: * the test cases
132: */
133: protected TestResult createResult(XMLOutput output) {
134: return new TestResult();
135: }
136:
137: /**
138: * Factory method to create a new TestListener to capture the output of
139: * the test cases
140: */
141: protected TestListener createTestListener(final XMLOutput output) {
142: return new TestListener() {
143: public void addError(Test test, Throwable t) {
144: try {
145: output.startElement("error");
146:
147: output.startElement("message");
148: output.write(t.getMessage());
149: output.endElement("message");
150:
151: output.startElement("stack");
152: output.write(stackTraceToString(t));
153: output.endElement("stack");
154:
155: output.endElement("error");
156: } catch (SAXException e) {
157: handleSAXException(e);
158: }
159: }
160:
161: public void addFailure(Test test, AssertionFailedError t) {
162: try {
163: output.startElement("failure");
164:
165: output.startElement("message");
166: output.write(t.getMessage());
167: output.endElement("message");
168:
169: output.startElement("stack");
170: output.write(stackTraceToString(t));
171: output.endElement("stack");
172:
173: output.endElement("failure");
174: } catch (SAXException e) {
175: handleSAXException(e);
176: }
177: }
178:
179: public void endTest(Test test) {
180: try {
181: output.endElement("test");
182: } catch (SAXException e) {
183: handleSAXException(e);
184: }
185: }
186:
187: public void startTest(Test test) {
188: try {
189: String name = test.toString();
190: AttributesImpl attributes = new AttributesImpl();
191: attributes.addAttribute("", "name", "name",
192: "CDATA", name);
193:
194: output.startElement("test", attributes);
195: } catch (SAXException e) {
196: handleSAXException(e);
197: }
198: }
199: };
200: }
201:
202: /**
203: * @return the stack trace as a String
204: */
205: protected String stackTraceToString(Throwable t) {
206: StringWriter writer = new StringWriter();
207: t.printStackTrace(new PrintWriter(writer));
208: return writer.toString();
209: }
210:
211: /**
212: * Handles SAX Exceptions
213: */
214: protected void handleSAXException(SAXException e) {
215: log.error("Caught: " + e, e);
216: }
217: }
|