1 11 12 package org.eclipse.jdt.internal.junit.model; 13 14 import java.util.Stack ; 15 16 import org.eclipse.core.resources.ResourcesPlugin; 17 18 import org.eclipse.jdt.core.IJavaModel; 19 import org.eclipse.jdt.core.IJavaProject; 20 import org.eclipse.jdt.core.JavaCore; 21 22 import org.eclipse.jdt.internal.junit.model.TestElement.Status; 23 24 import org.xml.sax.Attributes ; 25 import org.xml.sax.Locator ; 26 import org.xml.sax.SAXException ; 27 import org.xml.sax.SAXParseException ; 28 import org.xml.sax.helpers.DefaultHandler ; 29 30 public class TestRunHandler extends DefaultHandler { 31 32 35 36 private int fId; 37 38 private TestRunSession fTestRunSession; 39 private TestSuiteElement fTestSuite; 40 private TestCaseElement fTestCase; 41 private Stack fNotRun= new Stack (); 42 43 private StringBuffer fFailureBuffer; 44 private boolean fInExpected; 45 private boolean fInActual; 46 private StringBuffer fExpectedBuffer; 47 private StringBuffer fActualBuffer; 48 49 private Locator fLocator; 50 51 private Status fStatus; 52 53 public TestRunHandler() { 54 55 } 56 57 public TestRunHandler(TestRunSession testRunSession) { 58 fTestRunSession= testRunSession; 59 } 60 61 public void setDocumentLocator(Locator locator) { 62 fLocator= locator; 63 } 64 65 public void startDocument() throws SAXException { 66 } 67 68 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 69 if (qName.equals(IXMLTags.NODE_TESTRUN)) { 70 if (fTestRunSession == null) { 71 String name= attributes.getValue(IXMLTags.ATTR_NAME); 72 String project= attributes.getValue(IXMLTags.ATTR_PROJECT); 73 IJavaProject javaProject= null; 74 if (project != null) { 75 IJavaModel javaModel= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()); 76 javaProject= javaModel.getJavaProject(project); 77 if (! javaProject.exists()) 78 javaProject= null; 79 } 80 fTestRunSession= new TestRunSession(name, javaProject); 81 83 } else { 84 fTestRunSession.reset(); 85 } 86 fTestSuite= fTestRunSession.getTestRoot(); 87 88 } else if (qName.equals(IXMLTags.NODE_TESTSUITES)) { 89 91 } else if (qName.equals(IXMLTags.NODE_TESTSUITE)) { 92 String name= attributes.getValue(IXMLTags.ATTR_NAME); 93 94 if (fTestRunSession == null) { 95 fTestRunSession= new TestRunSession(name, null); 97 fTestSuite= fTestRunSession.getTestRoot(); 98 } 99 100 String pack= attributes.getValue(IXMLTags.ATTR_PACKAGE); 101 String suiteName= pack == null ? name : pack + "." + name; fTestSuite= (TestSuiteElement) fTestRunSession.createTestElement(fTestSuite, getNextId(), suiteName, true, 0); 103 fNotRun.push(Boolean.valueOf(attributes.getValue(IXMLTags.ATTR_INCOMPLETE))); 104 105 } else if (qName.equals(IXMLTags.NODE_PROPERTIES) || qName.equals(IXMLTags.NODE_PROPERTY)) { 106 108 } else if (qName.equals(IXMLTags.NODE_TESTCASE)) { 109 String name= attributes.getValue(IXMLTags.ATTR_NAME); 110 String classname= attributes.getValue(IXMLTags.ATTR_CLASSNAME); 111 fTestCase= (TestCaseElement) fTestRunSession.createTestElement(fTestSuite, getNextId(), name + '(' + classname + ')', false, 0); 112 fNotRun.push(Boolean.valueOf(attributes.getValue(IXMLTags.ATTR_INCOMPLETE))); 113 fTestCase.setIgnored(Boolean.valueOf(attributes.getValue(IXMLTags.ATTR_IGNORED)).booleanValue()); 114 115 } else if (qName.equals(IXMLTags.NODE_ERROR)) { 116 fStatus= Status.ERROR; 118 fFailureBuffer= new StringBuffer (); 119 120 } else if (qName.equals(IXMLTags.NODE_FAILURE)) { 121 fStatus= Status.FAILURE; 123 fFailureBuffer= new StringBuffer (); 124 125 } else if (qName.equals(IXMLTags.NODE_EXPECTED)) { 126 fInExpected= true; 127 fExpectedBuffer= new StringBuffer (); 128 129 } else if (qName.equals(IXMLTags.NODE_ACTUAL)) { 130 fInActual= true; 131 fActualBuffer= new StringBuffer (); 132 133 } else if (qName.equals(IXMLTags.NODE_SYSTEM_OUT) || qName.equals(IXMLTags.NODE_SYSTEM_ERR)) { 134 136 } else { 137 throw new SAXParseException ("unknown node '" + qName + "'", fLocator); } 139 } 140 141 public void characters(char[] ch, int start, int length) throws SAXException { 142 if (fInExpected) { 143 fExpectedBuffer.append(ch, start, length); 144 145 } else if (fInActual) { 146 fActualBuffer.append(ch, start, length); 147 148 } else if (fFailureBuffer != null) { 149 fFailureBuffer.append(ch, start, length); 150 } 151 } 152 153 public void endElement(String uri, String localName, String qName) throws SAXException { 154 if (qName.equals(IXMLTags.NODE_TESTRUN)) { 155 157 } else if (qName.equals(IXMLTags.NODE_TESTSUITES)) { 158 160 } else if (qName.equals(IXMLTags.NODE_TESTSUITE)) { 161 handleTestElementEnd(fTestSuite); 162 fTestSuite= fTestSuite.getParent(); 163 165 } else if (qName.equals(IXMLTags.NODE_PROPERTIES) || qName.equals(IXMLTags.NODE_PROPERTY)) { 166 168 } else if (qName.equals(IXMLTags.NODE_TESTCASE)) { 169 handleTestElementEnd(fTestCase); 170 fTestCase= null; 171 172 } else if (qName.equals(IXMLTags.NODE_FAILURE) || qName.equals(IXMLTags.NODE_ERROR)) { 173 TestElement testElement= fTestCase; 174 if (testElement == null) 175 testElement= fTestSuite; 176 handleFailure(testElement); 177 178 } else if (qName.equals(IXMLTags.NODE_EXPECTED)) { 179 fInExpected= false; 180 181 } else if (qName.equals(IXMLTags.NODE_ACTUAL)) { 182 fInActual= false; 183 184 } else if (qName.equals(IXMLTags.NODE_SYSTEM_OUT) || qName.equals(IXMLTags.NODE_SYSTEM_ERR)) { 185 187 } else { 188 189 handleUnknownNode(qName); 190 } 191 } 192 193 private void handleTestElementEnd(TestElement testElement) { 194 boolean completed= fNotRun.pop() != Boolean.TRUE; 195 fTestRunSession.registerTestEnded(testElement, completed); 196 } 197 198 private void handleFailure(TestElement testElement) { 199 if (fFailureBuffer != null) { 200 fTestRunSession.registerTestFailed(testElement, fStatus, fFailureBuffer.toString(), toString(fExpectedBuffer), toString(fActualBuffer)); 201 fFailureBuffer= null; 202 fExpectedBuffer= null; 203 fActualBuffer= null; 204 fStatus= null; 205 } 206 } 207 208 private String toString(StringBuffer buffer) { 209 return buffer != null ? buffer.toString() : null; 210 } 211 212 private void handleUnknownNode(String qName) throws SAXException { 213 String msg= "unknown node '" + qName + "'"; if (fLocator != null) { 216 msg += " at line " + fLocator.getLineNumber() + ", column " + fLocator.getColumnNumber(); } 218 throw new SAXException (msg); 219 } 220 221 public void error(SAXParseException e) throws SAXException { 222 throw e; 223 } 224 225 public void warning(SAXParseException e) throws SAXException { 226 throw e; 227 } 228 229 private String getNextId() { 230 return Integer.toString(fId++); 231 } 232 233 236 public TestRunSession getTestRunSession() { 237 return fTestRunSession; 238 } 239 } 240 | Popular Tags |