A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse.phpunit / src / net / sourceforge / phpeclipse / phpunit / reporthandling / XMLReportHandler.java
1 /*************************************************************************
2  * @author Ali Echihabi (ali_echihabi@ieee.org, ali.echihabi@souss.ca)
3  *
4  * Plugin for PHP unit Testing.
5  * www.phpeclipse.de
6  * 
7  *************************************************************************/
8
9 package net.sourceforge.phpeclipse.phpunit.reporthandling;
10
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.FileNotFoundException;
14 import java.io.FileOutputStream;
15 import java.io.IOException;
16
17 import javax.xml.parsers.FactoryConfigurationError;
18 import javax.xml.parsers.ParserConfigurationException;
19 import javax.xml.parsers.SAXParser;
20 import javax.xml.parsers.SAXParserFactory;
21
22 import net.sourceforge.phpeclipse.phpunit.PHPUnitView;
23
24 import org.xml.sax.Attributes;
25 import org.xml.sax.SAXException;
26 import org.xml.sax.helpers.DefaultHandler;
27
28 public class XMLReportHandler extends DefaultHandler {
29
30         private String currentVerdict;
31
32         private PHPUnitView view;
33
34         private String currentCommand;
35
36         private String currentTestCount;
37
38         private String currentTestID;
39
40         private String currentTestName;
41
42         private String currentTestParentTestSuiteName;
43
44         private void doAsyncRunnable(Runnable runnable) {
45
46                 view.getSite().getShell().getDisplay().asyncExec(runnable);
47         }
48
49         public void handle(String report, PHPUnitView view) {
50
51                 // TODO : how to parse directly a string?
52                 // now doing it with a stream.
53                 this.view = view;
54                 SAXParser parser;
55
56                 System.out.println("handling: " + report);
57
58                 try {
59
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);
65                         report += "\n \r";
66                         out.write(report.getBytes("UTF8"));
67                         out.close();
68                         in = new FileInputStream(file);
69                         parser = SAXParserFactory.newInstance().newSAXParser();
70                         parser.parse(in, this);
71                         in.close();
72                         file.delete();
73
74                 } catch (ParserConfigurationException e) {
75                         // TODO Auto-generated catch block
76                         e.printStackTrace();
77                 } catch (SAXException e) {
78                         // TODO Auto-generated catch block
79                         e.printStackTrace();
80                 } catch (FactoryConfigurationError e) {
81                         // TODO Auto-generated catch block
82                         e.printStackTrace();
83                 } catch (FileNotFoundException e1) {
84                         // TODO Auto-generated catch block
85                         e1.printStackTrace();
86                 } catch (IOException e) {
87                         // TODO Auto-generated catch block
88                         e.printStackTrace();
89                 }
90
91         }
92
93         /*
94          * (non-Javadoc)
95          * 
96          * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
97          *      java.lang.String, java.lang.String)
98          */
99         public void endElement(String arg0, String arg1, String elementName)
100                         throws SAXException {
101
102                 // send this current command to view
103
104         }
105
106         /*
107          * (non-Javadoc)
108          * 
109          * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
110          *      java.lang.String, java.lang.String, org.xml.sax.Attributes)
111          */
112         public void startElement(String arg0, String arg1, String elementName,
113                         Attributes attributes) throws SAXException {
114
115                 System.out.println(arg0 + " - " + arg1 + " - " + elementName);
116
117                 if (elementName.equals("report")) {
118
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");
125
126                         doAsyncRunnable(new Runnable() {
127
128                                 public void run() {
129
130                                         // view.handleCommand(currentCommand, currentTestCount,
131                                         // currentTestID, );
132                                         view.handleCommand(currentCommand, new String[] {
133                                                         currentTestID, currentTestCount, currentTestName,
134                                                         currentTestParentTestSuiteName });
135
136                                 }
137                         });
138
139                 } else if (elementName.equals("verdict")) {
140
141                         currentVerdict = attributes.getValue("desc");
142                         // view.setTestVerdict(currentTestID, currentVerdict);
143
144                         doAsyncRunnable(new Runnable() {
145
146                                 public void run() {
147
148                                         view.setTestVerdict(currentTestID, currentVerdict);
149                                 }
150
151                         });
152
153                 } else if (elementName.equals("exceptions")) {
154
155                         // do nothing
156
157                 } else if (elementName.equals("exception")) {
158
159                         final String exception = attributes.getValue("desc");
160
161                         doAsyncRunnable(new Runnable() {
162
163                                 public void run() {
164
165                                         view.addTestException(currentTestID, exception);
166                                 }
167
168                         });
169
170                 }
171
172         }
173
174         public static void main(String[] args) {
175
176                 XMLReportHandler handler = new XMLReportHandler();
177                 String xml = "";
178                 xml = "<report id='2' command='testFINISHED' testCount='1' testID='manyfailingtests_testpass1'> <verdict desc='passed'> <exceptions></exceptions></verdict></report> ";
179                 handler.handle(xml, null);
180
181                 xml = "<report id='2' command='testFINISHED' testCount='1' testID='manyfailingtests_testpass1'> <verdict desc='passed'> <exceptions></exceptions></verdict></report> ";
182                 handler.handle(xml, null);
183
184                 xml = "<report id='3' command='testStarted' testCount='2' testID='manyfailingtests_testpass2'> </report> ";
185                 handler.handle(xml, null);
186
187                 xml = "<report id='4' command='testFINISHED' testCount='2' testID='manyfailingtests_testpass2'> <verdict desc='passed'> <exceptions></exceptions></verdict></report>";
188                 handler.handle(xml, null);
189         }
190
191 }