1 16 package org.apache.commons.jelly.tags.junit; 17 18 import java.io.PrintWriter ; 19 import java.io.StringWriter ; 20 21 import junit.framework.AssertionFailedError; 22 import junit.framework.Test; 23 import junit.framework.TestListener; 24 import junit.framework.TestResult; 25 26 import org.apache.commons.jelly.JellyTagException; 27 import org.apache.commons.jelly.MissingAttributeException; 28 import org.apache.commons.jelly.TagSupport; 29 import org.apache.commons.jelly.XMLOutput; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.xml.sax.SAXException ; 33 import org.xml.sax.helpers.AttributesImpl ; 34 35 43 public class RunTag extends TagSupport { 44 45 46 private static final Log log = LogFactory.getLog(RunTag.class); 47 48 private Test test; 49 private TestResult result; 50 private TestListener listener; 51 52 public void doTag(XMLOutput output) throws JellyTagException { 55 Test test = getTest(); 56 if ( test == null ) { 57 test = (Test) context.getVariable("org.apache.commons.jelly.junit.suite"); 58 } 59 if ( test == null ) { 60 throw new MissingAttributeException( "test" ); 61 } 62 TestResult result = getResult(); 63 if ( result == null ) { 64 result = createResult(output); 65 } 66 TestListener listener = getListener(); 67 if ( listener == null ) { 68 listener = createTestListener(output); 69 } 70 result.addListener(listener); 71 test.run(result); 72 } 73 74 77 81 public TestResult getResult() { 82 return result; 83 } 84 85 89 public Test getTest() { 90 return test; 91 } 92 93 97 public void setResult(TestResult result) { 98 this.result = result; 99 } 100 101 105 public void setTest(Test test) { 106 this.test = test; 107 } 108 109 113 public TestListener getListener() { 114 return listener; 115 } 116 117 121 public void setListener(TestListener listener) { 122 this.listener = listener; 123 } 124 125 126 127 130 134 protected TestResult createResult(XMLOutput output) { 135 return new TestResult(); 136 } 137 138 142 protected TestListener createTestListener(final XMLOutput output) { 143 return new TestListener() { 144 public void addError(Test test, Throwable t) { 145 try { 146 output.startElement("error"); 147 148 output.startElement("message"); 149 output.write(t.getMessage()); 150 output.endElement("message"); 151 152 output.startElement("stack"); 153 output.write( stackTraceToString(t) ); 154 output.endElement("stack"); 155 156 output.endElement("error"); 157 } 158 catch (SAXException e) { 159 handleSAXException(e); 160 } 161 } 162 163 public void addFailure(Test test, AssertionFailedError t) { 164 try { 165 output.startElement("failure"); 166 167 output.startElement("message"); 168 output.write(t.getMessage()); 169 output.endElement("message"); 170 171 output.startElement("stack"); 172 output.write( stackTraceToString(t) ); 173 output.endElement("stack"); 174 175 output.endElement("failure"); 176 } 177 catch (SAXException e) { 178 handleSAXException(e); 179 } 180 } 181 182 public void endTest(Test test) { 183 try { 184 output.endElement("test"); 185 } 186 catch (SAXException e) { 187 handleSAXException(e); 188 } 189 } 190 191 public void startTest(Test test) { 192 try { 193 String name = test.toString(); 194 AttributesImpl attributes = new AttributesImpl (); 195 attributes.addAttribute("", "name", "name", "CDATA", name); 196 197 output.startElement("test", attributes); 198 } 199 catch (SAXException e) { 200 handleSAXException(e); 201 } 202 } 203 }; 204 } 205 206 209 protected String stackTraceToString(Throwable t) { 210 StringWriter writer = new StringWriter (); 211 t.printStackTrace(new PrintWriter (writer)); 212 return writer.toString(); 213 } 214 215 218 protected void handleSAXException(SAXException e) { 219 log.error( "Caught: " + e, e ); 220 } 221 } 222 | Popular Tags |