KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
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 JavaDoc;
26 import org.xml.sax.ContentHandler JavaDoc;
27 import org.xml.sax.DTDHandler JavaDoc;
28 import org.xml.sax.EntityResolver JavaDoc;
29 import org.xml.sax.ErrorHandler JavaDoc;
30 import org.xml.sax.InputSource JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32 import org.xml.sax.XMLReader JavaDoc;
33 import org.xml.sax.helpers.AttributesImpl JavaDoc;
34
35 public class TestRunSessionSerializer implements XMLReader JavaDoc {
36
37     private static final String JavaDoc EMPTY= ""; //$NON-NLS-1$
38
private static final String JavaDoc CDATA= "CDATA"; //$NON-NLS-1$
39
private static final Attributes JavaDoc NO_ATTS= new AttributesImpl JavaDoc();
40
41
42     private final TestRunSession fTestRunSession;
43     private ContentHandler JavaDoc fHandler;
44     private ErrorHandler JavaDoc fErrorHandler;
45     
46     /**
47      * @param testRunSession the test run session to serialize
48      */

49     public TestRunSessionSerializer(TestRunSession testRunSession) {
50         Assert.isNotNull(testRunSession);
51         fTestRunSession= testRunSession;
52     }
53
54     public void parse(InputSource JavaDoc input) throws IOException JavaDoc, SAXException JavaDoc {
55         if (fHandler == null)
56             throw new SAXException JavaDoc("ContentHandler missing"); //$NON-NLS-1$
57

58         fHandler.startDocument();
59         handleTestRun(fTestRunSession);
60         fHandler.endDocument();
61     }
62
63     private void handleTestRun(TestRunSession testRunSession) throws SAXException JavaDoc {
64         AttributesImpl JavaDoc atts= new AttributesImpl JavaDoc();
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 JavaDoc {
86         if (testElement instanceof TestSuiteElement) {
87             TestSuiteElement testSuiteElement= (TestSuiteElement) testElement;
88             
89             AttributesImpl JavaDoc atts= new AttributesImpl JavaDoc();
90             addCDATA(atts, IXMLTags.ATTR_NAME, testSuiteElement.getSuiteTypeName());
91 // addCDATA(atts, IXMLTags.ATTR_TIME, Integer.toString(testCaseElement.getTime()));
92
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 JavaDoc atts= new AttributesImpl JavaDoc();
108             addCDATA(atts, IXMLTags.ATTR_NAME, testCaseElement.getTestMethodName());
109             addCDATA(atts, IXMLTags.ATTR_CLASSNAME, testCaseElement.getClassName());
110 // addCDATA(atts, IXMLTags.ATTR_TIME, Integer.toString(testCaseElement.getTime()));
111
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 JavaDoc(String.valueOf(testElement));
123         }
124         
125     }
126
127     private void addFailure(ITestElement testElement) throws SAXException JavaDoc {
128         FailureTrace failureTrace= testElement.getFailureTrace();
129         if (failureTrace != null) {
130             AttributesImpl JavaDoc failureAtts= new AttributesImpl JavaDoc();
131 // addCDATA(failureAtts, IXMLTags.ATTR_MESSAGE, xx);
132
// addCDATA(failureAtts, IXMLTags.ATTR_TYPE, xx);
133
String JavaDoc failureKind= testElement.getTestResult(false) == Result.ERROR ? IXMLTags.NODE_ERROR : IXMLTags.NODE_FAILURE;
134             startElement(failureKind, failureAtts);
135             String JavaDoc expected= failureTrace.getExpected();
136             String JavaDoc 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 JavaDoc trace= failureTrace.getTrace();
148             fHandler.characters(trace.toCharArray(), 0, trace.length());
149             endElement(failureKind);
150         }
151     }
152
153     private void startElement(String JavaDoc name, Attributes JavaDoc atts) throws SAXException JavaDoc {
154         fHandler.startElement(EMPTY, name, name, atts);
155     }
156
157     private void endElement(String JavaDoc name) throws SAXException JavaDoc {
158         fHandler.endElement(EMPTY, name, name);
159     }
160
161     private static void addCDATA(AttributesImpl JavaDoc atts, String JavaDoc name, int value) {
162         addCDATA(atts, name, Integer.toString(value));
163     }
164     
165     private static void addCDATA(AttributesImpl JavaDoc atts, String JavaDoc name, String JavaDoc value) {
166         atts.addAttribute(EMPTY, EMPTY, name, CDATA, value);
167     }
168
169     public void setContentHandler(ContentHandler JavaDoc handler) {
170         this.fHandler= handler;
171     }
172
173     public ContentHandler JavaDoc getContentHandler() {
174         return fHandler;
175     }
176
177     public void setErrorHandler(ErrorHandler JavaDoc handler) {
178         fErrorHandler= handler;
179     }
180
181     public ErrorHandler JavaDoc getErrorHandler() {
182         return fErrorHandler;
183     }
184
185     // ignored:
186

187     public void parse(String JavaDoc systemId) throws IOException JavaDoc, SAXException JavaDoc {
188     }
189
190     public void setDTDHandler(DTDHandler JavaDoc handler) {
191     }
192     
193     public DTDHandler JavaDoc getDTDHandler() {
194         return null;
195     }
196
197     public void setEntityResolver(EntityResolver JavaDoc resolver) {
198     }
199     
200     public EntityResolver JavaDoc getEntityResolver() {
201         return null;
202     }
203
204     public void setProperty(java.lang.String JavaDoc name, java.lang.Object JavaDoc value) {
205     }
206     
207     public Object JavaDoc getProperty(java.lang.String JavaDoc name) {
208         return null;
209     }
210
211     public void setFeature(java.lang.String JavaDoc name, boolean value) {
212     }
213     
214     public boolean getFeature(java.lang.String JavaDoc name) {
215         return false;
216     }
217 }
218
Popular Tags