1 /*************************************************************************
2 * @author Ali Echihabi (ali_echihabi@ieee.org, ali.echihabi@souss.ca)
4 * Plugin for PHP unit Testing.
7 *************************************************************************/
9 package net.sourceforge.phpeclipse.phpunit.reporthandling;
12 import java.io.FileInputStream;
13 import java.io.FileNotFoundException;
14 import java.io.FileOutputStream;
15 import java.io.IOException;
17 import javax.xml.parsers.FactoryConfigurationError;
18 import javax.xml.parsers.ParserConfigurationException;
19 import javax.xml.parsers.SAXParser;
20 import javax.xml.parsers.SAXParserFactory;
22 import net.sourceforge.phpeclipse.phpunit.PHPUnitView;
24 import org.xml.sax.Attributes;
25 import org.xml.sax.SAXException;
26 import org.xml.sax.helpers.DefaultHandler;
28 public class XMLReportHandler extends DefaultHandler {
30 private String currentVerdict;
32 private PHPUnitView view;
34 private String currentCommand;
36 private String currentTestCount;
38 private String currentTestID;
40 private String currentTestName;
42 private String currentTestParentTestSuiteName;
44 private void doAsyncRunnable(Runnable runnable) {
46 view.getSite().getShell().getDisplay().asyncExec(runnable);
49 public void handle(String report, PHPUnitView view) {
51 // TODO : how to parse directly a string?
52 // now doing it with a stream.
56 System.out.println("handling: " + report);
60 File file = new File("tmp3.xml");
61 FileOutputStream out = null;
62 FileInputStream in = null;
63 out = new FileOutputStream(file);
64 // OutputStreamWriter outS = new OutputStreamWriter(out, UTF8);
66 out.write(report.getBytes("UTF8"));
68 in = new FileInputStream(file);
69 parser = SAXParserFactory.newInstance().newSAXParser();
70 parser.parse(in, this);
74 } catch (ParserConfigurationException e) {
75 // TODO Auto-generated catch block
77 } catch (SAXException e) {
78 // TODO Auto-generated catch block
80 } catch (FactoryConfigurationError e) {
81 // TODO Auto-generated catch block
83 } catch (FileNotFoundException e1) {
84 // TODO Auto-generated catch block
86 } catch (IOException e) {
87 // TODO Auto-generated catch block
96 * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
97 * java.lang.String, java.lang.String)
99 public void endElement(String arg0, String arg1, String elementName)
100 throws SAXException {
102 // send this current command to view
109 * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
110 * java.lang.String, java.lang.String, org.xml.sax.Attributes)
112 public void startElement(String arg0, String arg1, String elementName,
113 Attributes attributes) throws SAXException {
115 System.out.println(arg0 + " - " + arg1 + " - " + elementName);
117 if (elementName.equals("report")) {
119 currentCommand = attributes.getValue("command");
120 currentTestCount = attributes.getValue("testCount");
121 currentTestID = attributes.getValue("testID");
122 currentTestName = attributes.getValue("testName");
123 currentTestParentTestSuiteName = attributes
124 .getValue("parentTestSuiteName");
126 doAsyncRunnable(new Runnable() {
130 // view.handleCommand(currentCommand, currentTestCount,
132 view.handleCommand(currentCommand, new String[] {
133 currentTestID, currentTestCount, currentTestName,
134 currentTestParentTestSuiteName });
139 } else if (elementName.equals("verdict")) {
141 currentVerdict = attributes.getValue("desc");
142 // view.setTestVerdict(currentTestID, currentVerdict);
144 doAsyncRunnable(new Runnable() {
148 view.setTestVerdict(currentTestID, currentVerdict);
153 } else if (elementName.equals("exceptions")) {
157 } else if (elementName.equals("exception")) {
159 final String exception = attributes.getValue("desc");
161 doAsyncRunnable(new Runnable() {
165 view.addTestException(currentTestID, exception);
174 public static void main(String[] args) {
176 XMLReportHandler handler = new XMLReportHandler();
178 xml = "<report id='2' command='testFINISHED' testCount='1' testID='manyfailingtests_testpass1'> <verdict desc='passed'> <exceptions></exceptions></verdict></report> ";
179 handler.handle(xml, null);
181 xml = "<report id='2' command='testFINISHED' testCount='1' testID='manyfailingtests_testpass1'> <verdict desc='passed'> <exceptions></exceptions></verdict></report> ";
182 handler.handle(xml, null);
184 xml = "<report id='3' command='testStarted' testCount='2' testID='manyfailingtests_testpass2'> </report> ";
185 handler.handle(xml, null);
187 xml = "<report id='4' command='testFINISHED' testCount='2' testID='manyfailingtests_testpass2'> <verdict desc='passed'> <exceptions></exceptions></verdict></report>";
188 handler.handle(xml, null);