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