Added Run Tests Action in the popup menu for .php file
[phpeclipse.git] / net.sourceforge.phpeclipse.phpunit / src / net / sourceforge / phpeclipse / phpunit / PHPUnitView.java
1 package net.sourceforge.phpeclipse.phpunit;
2
3
4 import java.io.BufferedReader;
5 import java.io.InputStreamReader;
6 import java.net.ServerSocket;
7 import java.net.Socket;
8
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.events.ControlEvent;
11 import org.eclipse.swt.events.ControlListener;
12 import org.eclipse.swt.events.MouseEvent;
13 import org.eclipse.swt.events.MouseListener;
14 import org.eclipse.swt.widgets.Button;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Label;
17 import org.eclipse.swt.widgets.Text;
18 import org.eclipse.ui.part.ViewPart;
19
20 /**
21  * @author Ali Echihabi
22  *
23  * To change the template for this generated type comment go to
24  * Window>Preferences>Java>Code Generation>Code and Comments
25  */
26 /*
27  * Created on May 22, 2004
28  *
29  * To change the template for this generated file go to
30  * Window>Preferences>Java>Code Generation>Code and Comments
31  */
32
33 /**
34  * @author Ali Echihabi (ali_echihabi@ieee.org)
35  *
36  * Plugin for PHP unit Testing.
37  * www.phpeclipse.de
38  * 
39  * This the main view showing the progress and reports.
40  * 
41  */
42
43
44 public class PHPUnitView extends ViewPart {
45
46         /*
47          * like J Unit
48          * a tree.
49          * The first level nodes are the test suites.
50          * children are nested test suites.
51          * leafs: test functions.
52          */
53         
54         
55         private int numTests; // total number of tests
56         private int numTestsRun; // number of tests run so far
57         private int numFailures; // number of failures so far
58         private int numErrors; // number of errors so far
59         private int numPasses; // number of passes so far (they should add up)   
60
61         Label labelRuns, labelRunsVal; // Runs: 12
62         Label labelErrors, labelErrorsVal;
63         Label labelFailures, labelFailuresVal;
64         
65         Text reportArea; // TODO: replace with Tree display like JUnit
66
67         Button startButton;
68
69         public PHPUnitView() {
70                 
71         }
72         
73         public void createPartControl(Composite parent) {
74                 
75                 //viewer = new TreeViewer(parent);
76                 labelRuns = new Label(parent, SWT.WRAP);
77                 labelRuns.setText("Runs: ");
78                 labelRunsVal = new Label(parent, SWT.WRAP);
79                 labelRunsVal.setText("0 / 0");
80                 
81                 labelFailures = new Label(parent, SWT.WRAP);
82                 labelFailures.setText("Failures: ");
83                 labelFailuresVal = new Label(parent, SWT.WRAP);
84                 labelFailuresVal.setText("0");
85                 
86                 labelErrors = new Label(parent, SWT.WRAP);
87                 labelErrors.setText("Errors: ");
88                 labelErrorsVal = new Label(parent, SWT.WRAP);
89                 labelErrorsVal.setText("0");
90
91                 reportArea = new Text(parent, SWT.MULTI | SWT.BORDER |
92                 SWT.WRAP | SWT.V_SCROLL | SWT.READ_ONLY);
93                 
94                 startButton = new Button(parent, SWT.CENTER);
95                 startButton.setText("Start Tests");
96                 startButton.addMouseListener( new MouseListener() {
97
98                         public void mouseDoubleClick(MouseEvent arg0) {
99                                 // TODO Auto-generated method stub
100                                 
101                         }
102
103                         public void mouseDown(MouseEvent arg0) {
104                                 // TODO Auto-generated method stub
105                                 startTests();
106                         }
107
108                         public void mouseUp(MouseEvent arg0) {
109                                 // TODO Auto-generated method stub
110                                 
111                         }
112
113
114                         
115                         
116                         }); // end add action listener.
117                 
118                 // TODO layout!
119         }
120
121         /* (non-Javadoc)
122          * @see org.eclipse.ui.IWorkbenchPart#setFocus()
123          */
124         public void setFocus() {
125                 //markTestPass("hello");
126         }
127
128         /**
129          * mark the given test as passed in the GUI.
130          * 
131          * @param testID
132          */
133         private void markTestPass(String testID) {
134                 
135                 // testid, use it in hashmap to retrieve tree item of test and
136                 // change icon color, increment pass counter, etc...
137                 
138                 
139                 //for now:
140                 reportArea.append("test passed");
141         }
142
143         
144         
145         
146         // action to start tests:
147         private void startTests() {
148                 
149                 // preparation:
150                 // take the full test suite (could containt other test suites).
151                 // create temp php file that starts that suite and uses socketTestReport 
152                 // as a test result reporter.
153                 // add listener: localhost , port 13579
154                 // start listening at port.
155                 
156                 reportArea.append("Tests started \n");
157                 listenForReports();
158                 
159                 
160                 
161         }
162         
163         private void listenForReports() {
164                 
165                 ServerSocket sSocket = null;
166                 Socket serviceSocket = null;    
167                 
168                 try {
169                         
170                         reportArea.append("listening at port 12345");
171                         
172                         sSocket = new ServerSocket(12345);
173                         
174                         // accept connection from test reporter.
175                         serviceSocket = sSocket.accept();
176         
177                 
178                         InputStreamReader reader = new InputStreamReader(serviceSocket.getInputStream());                       
179                         BufferedReader in = new BufferedReader(reader);
180                         String report = null;
181                         
182                         // keep listening until the 
183                         while ( (report = in.readLine()) != null && (report != "end_all_tests") ) {                     
184                                 
185                                 handleReport(report);                   
186                         }                       
187                         
188                         reportArea.append("Finished!");
189                         
190                         sSocket.close();
191                         serviceSocket.close();          
192                         
193                 } catch (Exception e) {
194                         
195                         e.printStackTrace();
196                         
197                 } 
198                 
199                 
200                                 
201                 
202         }
203         
204         /**
205          * handle this report: test passed, faile, end of all.
206          * @param report
207          */
208         private void handleReport(String report) {
209                 
210                 reportArea.append("msg: " + report + "\n");
211                 
212         }
213         
214
215 }