1 11 12 package org.eclipse.jdt.internal.junit.model; 13 14 import java.io.IOException ; 15 16 import org.eclipse.core.runtime.Assert; 17 18 import org.eclipse.jdt.core.IJavaProject; 19 20 import org.eclipse.jdt.junit.model.ITestElement; 21 import org.eclipse.jdt.junit.model.ITestElement.FailureTrace; 22 import org.eclipse.jdt.junit.model.ITestElement.ProgressState; 23 import org.eclipse.jdt.junit.model.ITestElement.Result; 24 25 import org.xml.sax.Attributes ; 26 import org.xml.sax.ContentHandler ; 27 import org.xml.sax.DTDHandler ; 28 import org.xml.sax.EntityResolver ; 29 import org.xml.sax.ErrorHandler ; 30 import org.xml.sax.InputSource ; 31 import org.xml.sax.SAXException ; 32 import org.xml.sax.XMLReader ; 33 import org.xml.sax.helpers.AttributesImpl ; 34 35 public class TestRunSessionSerializer implements XMLReader { 36 37 private static final String EMPTY= ""; private static final String CDATA= "CDATA"; private static final Attributes NO_ATTS= new AttributesImpl (); 40 41 42 private final TestRunSession fTestRunSession; 43 private ContentHandler fHandler; 44 private ErrorHandler fErrorHandler; 45 46 49 public TestRunSessionSerializer(TestRunSession testRunSession) { 50 Assert.isNotNull(testRunSession); 51 fTestRunSession= testRunSession; 52 } 53 54 public void parse(InputSource input) throws IOException , SAXException { 55 if (fHandler == null) 56 throw new SAXException ("ContentHandler missing"); 58 fHandler.startDocument(); 59 handleTestRun(fTestRunSession); 60 fHandler.endDocument(); 61 } 62 63 private void handleTestRun(TestRunSession testRunSession) throws SAXException { 64 AttributesImpl atts= new AttributesImpl (); 65 addCDATA(atts, IXMLTags.ATTR_NAME, fTestRunSession.getTestRunName()); 66 IJavaProject project= fTestRunSession.getLaunchedProject(); 67 if (project != null) 68 addCDATA(atts, IXMLTags.ATTR_PROJECT, project.getElementName()); 69 addCDATA(atts, IXMLTags.ATTR_TESTS, fTestRunSession.getTotalCount()); 70 addCDATA(atts, IXMLTags.ATTR_STARTED, fTestRunSession.getStartedCount()); 71 addCDATA(atts, IXMLTags.ATTR_FAILURES, fTestRunSession.getFailureCount()); 72 addCDATA(atts, IXMLTags.ATTR_ERRORS, fTestRunSession.getErrorCount()); 73 addCDATA(atts, IXMLTags.ATTR_IGNORED, fTestRunSession.getIgnoredCount()); 74 startElement(IXMLTags.NODE_TESTRUN, atts); 75 76 TestRoot testRoot= fTestRunSession.getTestRoot(); 77 ITestElement[] topSuites= testRoot.getChildren(); 78 for (int i= 0; i < topSuites.length; i++) { 79 handleTestElement(topSuites[i]); 80 } 81 82 endElement(IXMLTags.NODE_TESTRUN); 83 } 84 85 private void handleTestElement(ITestElement testElement) throws SAXException { 86 if (testElement instanceof TestSuiteElement) { 87 TestSuiteElement testSuiteElement= (TestSuiteElement) testElement; 88 89 AttributesImpl atts= new AttributesImpl (); 90 addCDATA(atts, IXMLTags.ATTR_NAME, testSuiteElement.getSuiteTypeName()); 91 if (testElement.getProgressState() != ProgressState.COMPLETED || testElement.getTestResult(false) != Result.UNDEFINED) 93 addCDATA(atts, IXMLTags.ATTR_INCOMPLETE, Boolean.TRUE.toString()); 94 95 startElement(IXMLTags.NODE_TESTSUITE, atts); 96 addFailure(testElement); 97 98 ITestElement[] children= testSuiteElement.getChildren(); 99 for (int i= 0; i < children.length; i++) { 100 handleTestElement(children[i]); 101 } 102 endElement(IXMLTags.NODE_TESTSUITE); 103 104 } else if (testElement instanceof TestCaseElement) { 105 TestCaseElement testCaseElement= (TestCaseElement) testElement; 106 107 AttributesImpl atts= new AttributesImpl (); 108 addCDATA(atts, IXMLTags.ATTR_NAME, testCaseElement.getTestMethodName()); 109 addCDATA(atts, IXMLTags.ATTR_CLASSNAME, testCaseElement.getClassName()); 110 if (testElement.getProgressState() != ProgressState.COMPLETED) 112 addCDATA(atts, IXMLTags.ATTR_INCOMPLETE, Boolean.TRUE.toString()); 113 if (testCaseElement.isIgnored()) 114 addCDATA(atts, IXMLTags.ATTR_IGNORED, Boolean.TRUE.toString()); 115 116 startElement(IXMLTags.NODE_TESTCASE, atts); 117 addFailure(testElement); 118 119 endElement(IXMLTags.NODE_TESTCASE); 120 121 } else { 122 throw new IllegalStateException (String.valueOf(testElement)); 123 } 124 125 } 126 127 private void addFailure(ITestElement testElement) throws SAXException { 128 FailureTrace failureTrace= testElement.getFailureTrace(); 129 if (failureTrace != null) { 130 AttributesImpl failureAtts= new AttributesImpl (); 131 String failureKind= testElement.getTestResult(false) == Result.ERROR ? IXMLTags.NODE_ERROR : IXMLTags.NODE_FAILURE; 134 startElement(failureKind, failureAtts); 135 String expected= failureTrace.getExpected(); 136 String actual= failureTrace.getActual(); 137 if (expected != null) { 138 startElement(IXMLTags.NODE_EXPECTED, NO_ATTS); 139 fHandler.characters(expected.toCharArray(), 0, expected.length()); 140 endElement(IXMLTags.NODE_EXPECTED); 141 } 142 if (actual != null) { 143 startElement(IXMLTags.NODE_ACTUAL, NO_ATTS); 144 fHandler.characters(actual.toCharArray(), 0, actual.length()); 145 endElement(IXMLTags.NODE_ACTUAL); 146 } 147 String trace= failureTrace.getTrace(); 148 fHandler.characters(trace.toCharArray(), 0, trace.length()); 149 endElement(failureKind); 150 } 151 } 152 153 private void startElement(String name, Attributes atts) throws SAXException { 154 fHandler.startElement(EMPTY, name, name, atts); 155 } 156 157 private void endElement(String name) throws SAXException { 158 fHandler.endElement(EMPTY, name, name); 159 } 160 161 private static void addCDATA(AttributesImpl atts, String name, int value) { 162 addCDATA(atts, name, Integer.toString(value)); 163 } 164 165 private static void addCDATA(AttributesImpl atts, String name, String value) { 166 atts.addAttribute(EMPTY, EMPTY, name, CDATA, value); 167 } 168 169 public void setContentHandler(ContentHandler handler) { 170 this.fHandler= handler; 171 } 172 173 public ContentHandler getContentHandler() { 174 return fHandler; 175 } 176 177 public void setErrorHandler(ErrorHandler handler) { 178 fErrorHandler= handler; 179 } 180 181 public ErrorHandler getErrorHandler() { 182 return fErrorHandler; 183 } 184 185 187 public void parse(String systemId) throws IOException , SAXException { 188 } 189 190 public void setDTDHandler(DTDHandler handler) { 191 } 192 193 public DTDHandler getDTDHandler() { 194 return null; 195 } 196 197 public void setEntityResolver(EntityResolver resolver) { 198 } 199 200 public EntityResolver getEntityResolver() { 201 return null; 202 } 203 204 public void setProperty(java.lang.String name, java.lang.Object value) { 205 } 206 207 public Object getProperty(java.lang.String name) { 208 return null; 209 } 210 211 public void setFeature(java.lang.String name, boolean value) { 212 } 213 214 public boolean getFeature(java.lang.String name) { 215 return false; 216 } 217 } 218 | Popular Tags |