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