1 /*************************************************************************
2 * @author Ali Echihabi (ali_echihabi@ieee.org, ali.echihabi@souss.ca)
4 * Plugin for PHP unit Testing.
7 *************************************************************************/
10 package net.sourceforge.phpeclipse.phpunit.reporthandling;
13 import java.io.FileInputStream;
14 import java.io.FileNotFoundException;
15 import java.io.FileOutputStream;
16 import java.io.IOException;
18 import javax.xml.parsers.FactoryConfigurationError;
19 import javax.xml.parsers.ParserConfigurationException;
20 import javax.xml.parsers.SAXParser;
21 import javax.xml.parsers.SAXParserFactory;
23 import net.sourceforge.phpeclipse.phpunit.PHPUnitView;
25 import org.xml.sax.Attributes;
26 import org.xml.sax.SAXException;
27 import org.xml.sax.helpers.DefaultHandler;
29 public class XMLReportHandler extends DefaultHandler {
31 private String currentVerdict;
32 private PHPUnitView view;
33 private String currentCommand;
34 private String currentTestCount;
35 private String currentTestID;
36 private String currentTestName;
37 private String currentTestParentTestSuiteName;
39 private void doAsyncRunnable(Runnable runnable) {
41 view.getSite().getShell().getDisplay().asyncExec(runnable);
44 public void handle(String report, PHPUnitView view) {
46 //TODO : how to parse directly a string?
47 // now doing it with a stream.
51 System.out.println("handling: " + report);
55 File file = new File("tmp3.xml");
56 FileOutputStream out = null;
57 FileInputStream in = null;
58 out = new FileOutputStream(file);
59 //OutputStreamWriter outS = new OutputStreamWriter(out, UTF8);
61 out.write(report.getBytes("UTF8"));
63 in = new FileInputStream(file);
64 parser = SAXParserFactory.newInstance().newSAXParser();
65 parser.parse(in, this);
69 } catch (ParserConfigurationException e) {
70 // TODO Auto-generated catch block
72 } catch (SAXException e) {
73 // TODO Auto-generated catch block
75 } catch (FactoryConfigurationError e) {
76 // TODO Auto-generated catch block
78 } catch (FileNotFoundException e1) {
79 // TODO Auto-generated catch block
81 } catch (IOException e) {
82 // TODO Auto-generated catch block
91 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
93 public void endElement(String arg0, String arg1, String elementName)
96 // send this current command to view
101 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
103 public void startElement(
107 Attributes attributes)
108 throws SAXException {
110 System.out.println(arg0 + " - " + arg1 + " - " + elementName);
113 if(elementName.equals("report")) {
115 currentCommand = attributes.getValue("command");
116 currentTestCount = attributes.getValue("testCount");
117 currentTestID = attributes.getValue("testID");
118 currentTestName = attributes.getValue("testName");
119 currentTestParentTestSuiteName = attributes.getValue("parentTestSuiteName");
121 doAsyncRunnable(new Runnable() {
125 //view.handleCommand(currentCommand, currentTestCount, currentTestID, );
126 view.handleCommand(currentCommand, new String[] {currentTestID, currentTestCount, currentTestName, currentTestParentTestSuiteName});
131 } else if (elementName.equals("verdict")) {
133 currentVerdict = attributes.getValue("desc");
134 //view.setTestVerdict(currentTestID, currentVerdict);
136 doAsyncRunnable(new Runnable() {
140 view.setTestVerdict(currentTestID, currentVerdict);
145 } else if (elementName.equals("exceptions")) {
149 } else if (elementName.equals("exception")) {
151 final String exception = attributes.getValue("desc");
153 doAsyncRunnable(new Runnable() {
157 view.addTestException(currentTestID, exception);
167 public static void main(String[] args) {
169 XMLReportHandler handler = new XMLReportHandler();
171 xml = "<report id='2' command='testFINISHED' testCount='1' testID='manyfailingtests_testpass1'> <verdict desc='passed'> <exceptions></exceptions></verdict></report> ";
172 handler.handle(xml, null);
174 xml = "<report id='2' command='testFINISHED' testCount='1' testID='manyfailingtests_testpass1'> <verdict desc='passed'> <exceptions></exceptions></verdict></report> ";
175 handler.handle(xml, null);
177 xml = "<report id='3' command='testStarted' testCount='2' testID='manyfailingtests_testpass2'> </report> ";
178 handler.handle(xml, null);
180 xml = "<report id='4' command='testFINISHED' testCount='2' testID='manyfailingtests_testpass2'> <verdict desc='passed'> <exceptions></exceptions></verdict></report>";
181 handler.handle(xml, null);