KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > model > TestRunHandler


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.junit.model;
13
14 import java.util.Stack JavaDoc;
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 JavaDoc;
25 import org.xml.sax.Locator JavaDoc;
26 import org.xml.sax.SAXException JavaDoc;
27 import org.xml.sax.SAXParseException JavaDoc;
28 import org.xml.sax.helpers.DefaultHandler JavaDoc;
29
30 public class TestRunHandler extends DefaultHandler JavaDoc {
31     
32     /*
33      * TODO: validate (currently assumes correct XML)
34      */

35     
36     private int fId;
37     
38     private TestRunSession fTestRunSession;
39     private TestSuiteElement fTestSuite;
40     private TestCaseElement fTestCase;
41     private Stack JavaDoc/*<Boolean>*/ fNotRun= new Stack JavaDoc();
42
43     private StringBuffer JavaDoc fFailureBuffer;
44     private boolean fInExpected;
45     private boolean fInActual;
46     private StringBuffer JavaDoc fExpectedBuffer;
47     private StringBuffer JavaDoc fActualBuffer;
48
49     private Locator JavaDoc 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 JavaDoc locator) {
62         fLocator= locator;
63     }
64     
65     public void startDocument() throws SAXException JavaDoc {
66     }
67     
68     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
69         if (qName.equals(IXMLTags.NODE_TESTRUN)) {
70             if (fTestRunSession == null) {
71                 String JavaDoc name= attributes.getValue(IXMLTags.ATTR_NAME);
72                 String JavaDoc 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                 //TODO: read counts?
82

83             } else {
84                 fTestRunSession.reset();
85             }
86             fTestSuite= fTestRunSession.getTestRoot();
87             
88         } else if (qName.equals(IXMLTags.NODE_TESTSUITES)) {
89             // support Ant's 'junitreport' task; create suite from NODE_TESTSUITE
90

91         } else if (qName.equals(IXMLTags.NODE_TESTSUITE)) {
92             String JavaDoc name= attributes.getValue(IXMLTags.ATTR_NAME);
93             
94             if (fTestRunSession == null) {
95                 // support standalone suites and Ant's 'junitreport' task:
96
fTestRunSession= new TestRunSession(name, null);
97                 fTestSuite= fTestRunSession.getTestRoot();
98             }
99             
100             String JavaDoc pack= attributes.getValue(IXMLTags.ATTR_PACKAGE);
101             String JavaDoc suiteName= pack == null ? name : pack + "." + name; //$NON-NLS-1$
102
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             // not interested
107

108         } else if (qName.equals(IXMLTags.NODE_TESTCASE)) {
109             String JavaDoc name= attributes.getValue(IXMLTags.ATTR_NAME);
110             String JavaDoc 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             //TODO: multiple failures: https://bugs.eclipse.org/bugs/show_bug.cgi?id=125296
117
fStatus= Status.ERROR;
118             fFailureBuffer= new StringBuffer JavaDoc();
119             
120         } else if (qName.equals(IXMLTags.NODE_FAILURE)) {
121             //TODO: multiple failures: https://bugs.eclipse.org/bugs/show_bug.cgi?id=125296
122
fStatus= Status.FAILURE;
123             fFailureBuffer= new StringBuffer JavaDoc();
124             
125         } else if (qName.equals(IXMLTags.NODE_EXPECTED)) {
126             fInExpected= true;
127             fExpectedBuffer= new StringBuffer JavaDoc();
128             
129         } else if (qName.equals(IXMLTags.NODE_ACTUAL)) {
130             fInActual= true;
131             fActualBuffer= new StringBuffer JavaDoc();
132             
133         } else if (qName.equals(IXMLTags.NODE_SYSTEM_OUT) || qName.equals(IXMLTags.NODE_SYSTEM_ERR)) {
134             // not interested
135

136         } else {
137             throw new SAXParseException JavaDoc("unknown node '" + qName + "'", fLocator); //$NON-NLS-1$//$NON-NLS-2$
138
}
139     }
140     
141     public void characters(char[] ch, int start, int length) throws SAXException JavaDoc {
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 JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
154         if (qName.equals(IXMLTags.NODE_TESTRUN)) {
155             // OK
156

157         } else if (qName.equals(IXMLTags.NODE_TESTSUITES)) {
158             // OK
159

160         } else if (qName.equals(IXMLTags.NODE_TESTSUITE)) {
161             handleTestElementEnd(fTestSuite);
162             fTestSuite= fTestSuite.getParent();
163             //TODO: end suite: compare counters?
164

165         } else if (qName.equals(IXMLTags.NODE_PROPERTIES) || qName.equals(IXMLTags.NODE_PROPERTY)) {
166             // OK
167

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             // OK
186

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 JavaDoc toString(StringBuffer JavaDoc buffer) {
209         return buffer != null ? buffer.toString() : null;
210     }
211
212     private void handleUnknownNode(String JavaDoc qName) throws SAXException JavaDoc {
213         //TODO: just log if debug option is enabled?
214
String JavaDoc msg= "unknown node '" + qName + "'"; //$NON-NLS-1$//$NON-NLS-2$
215
if (fLocator != null) {
216             msg += " at line " + fLocator.getLineNumber() + ", column " + fLocator.getColumnNumber(); //$NON-NLS-1$//$NON-NLS-2$
217
}
218         throw new SAXException JavaDoc(msg);
219     }
220     
221     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
222         throw e;
223     }
224
225     public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
226         throw e;
227     }
228     
229     private String JavaDoc getNextId() {
230         return Integer.toString(fId++);
231     }
232     
233     /**
234      * @return the parsed test run session, or <code>null</code>
235      */

236     public TestRunSession getTestRunSession() {
237         return fTestRunSession;
238     }
239 }
240
Popular Tags