intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / net.sourceforge.phpeclipse.phpunit / src / net / sourceforge / phpeclipse / phpunit / PHPUnitView.java
1 package net.sourceforge.phpeclipse.phpunit;
2
3 import java.io.IOException;
4
5 import org.eclipse.jface.action.Action;
6 import org.eclipse.jface.action.IToolBarManager;
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.events.MouseEvent;
9 import org.eclipse.swt.events.MouseListener;
10 import org.eclipse.swt.layout.FillLayout;
11 import org.eclipse.swt.widgets.Button;
12 import org.eclipse.swt.widgets.Composite;
13 import org.eclipse.swt.widgets.Group;
14 import org.eclipse.ui.IActionBars;
15 import org.eclipse.ui.ISharedImages;
16 import org.eclipse.ui.PlatformUI;
17 import org.eclipse.ui.part.ViewPart;
18
19 /**
20  * @author Ali Echihabi
21  *
22  * To change the template for this generated type comment go to
23  * Window>Preferences>Java>Code Generation>Code and Comments
24  */
25 /*
26  * Created on May 22, 2004
27  *
28  * To change the template for this generated file go to
29  * Window>Preferences>Java>Code Generation>Code and Comments
30  */
31
32 /**
33  * @author Ali Echihabi (ali_echihabi@ieee.org)
34  *
35  * Plugin for PHP unit Testing.
36  * www.phpeclipse.de
37  * 
38  * This the main view showing the progress and reports.
39  * 
40  */
41
42 public class PHPUnitView extends ViewPart {
43
44         /*
45          * like J Unit
46          * a tree.
47          * The first level nodes are the test suites.
48          * children are nested test suites.
49          * leafs: test functions.
50          * hierarchy: package->testsuite1->testcase->test_function
51          */
52
53         private int numTests; // total number of tests
54         private int numTestsRun; // number of tests run so far
55         private int numFailures; // number of failures so far
56         private int numErrors; // number of errors so far
57         private int numPasses; // number of passes so far (they should add up)   
58
59         private XMLReportHandler handler;
60
61
62
63         private TestPool testPool;
64
65         private Button startButton;
66
67         private ProgressInfoComposite progressInfoComposite;
68         private ResultsInfoComposite resultsInfoComposite;
69         private Group settingsInfoComposite; //TODO: move somewhere else, launcher, wizard or preferences.
70         
71
72
73         public PHPUnitView() {
74                 handler = new XMLReportHandler();
75                 testPool = new TestPool();
76                 
77
78         }
79
80         public void createPartControl(Composite parent) {
81
82                 parent.setLayout(new FillLayout(SWT.VERTICAL));
83
84                 //Launch ToolBar:
85                 setActions();
86
87                 //Build the progress info Composite             s               
88                 progressInfoComposite = new ProgressInfoComposite(parent);
89                 
90
91                 //Build the result info composite
92                 resultsInfoComposite = new ResultsInfoComposite(parent);
93                 
94                 //build the settings composite
95                 buildSettingsComposite(parent);
96
97                 startButton = new Button(parent, SWT.CENTER);
98                 startButton.setText("Start Tests");
99                 startButton.addMouseListener(new MouseListener() {
100
101                         public void mouseDoubleClick(MouseEvent arg0) {
102
103                         }
104
105                         public void mouseDown(MouseEvent arg0) {
106
107                                 startTests();
108
109                         }
110
111                         public void mouseUp(MouseEvent arg0) {
112
113                         }
114
115                 }); // end add action listener.
116
117         }
118
119         /**
120          * @param parent
121          */
122         private void buildSettingsComposite(Composite parent) {
123                 
124                 
125                 settingsInfoComposite = new Group(parent, SWT.NONE);
126 //              settingsInfoComposite.setText("Settings");
127 //              settingsInfoComposite.setLayout(new GridLayout(2,false));
128 //              
129 //              
130 //              //the test suite to launch
131 //              Label testSuiteLabel = new Label(settingsInfoComposite, SWT.NONE);
132 //              testSuiteLabel.setText("Test suite to run:");
133 //              //testSuiteLabel.setLayoutData(new GridData())
134 //              Text testSuiteText = new Text(settingsInfoComposite, SWT.NONE);
135 //              
136 //              //the path to php
137 //              Label phpPathLabel = new Label(settingsInfoComposite, SWT.NONE);
138 //              phpPathLabel.setText("php Path:");
139 //              //testSuiteLabel.setLayoutData(new GridData())
140 //              Text phpPathText = new Text(settingsInfoComposite, SWT.NONE);
141                 
142
143                 
144         }
145
146         private void setActions() {
147                 final IActionBars actionBars = getViewSite().getActionBars();
148                 IToolBarManager toolBarManager = actionBars.getToolBarManager();
149                 Action action1 = new Action() {};
150                 action1.setText("Action 1");
151                 action1.setToolTipText("Start the testing");
152                 //final URL installUrl = PaintPlugin.getDefault().getDescriptor().getInstallURL();
153                 //final URL imageUrl = new URL(installUrl, PaintPlugin.getResourceString(id + ".image"));
154 //              URL imageUrl = null;
155 //              try {
156 //                      imageUrl =
157 //                              new URL("C:\\sample.gif");
158 //              } catch (MalformedURLException e) {
159 //                      // TODO Auto-generated catch block
160 //                      e.printStackTrace();
161 //              }
162 //              action1.setImageDescriptor(ImageDescriptor.createFromURL(imageUrl));
163 //              
164                 action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
165                         getImageDescriptor(ISharedImages.IMG_OBJS_TASK_TSK));
166                 toolBarManager.add(action1);
167         }
168
169
170
171         /* (non-Javadoc)
172          * @see org.eclipse.ui.IWorkbenchPart#setFocus()
173          */
174         public void setFocus() {
175                 
176         }
177
178         /**
179          * mark the given test as passed in the GUI.
180          * 
181          * @param testID
182          */
183         public void markTestPassed(String testID) {
184
185                 // testid, use it in hashmap to retrieve tree item of test and
186                 // change icon color, increment pass counter, etc...
187
188                 testPool.getTest(testID).setVerdict(Test.PASS);
189                 
190
191         }
192
193         public void markTestStarted(String testID) {
194
195                 
196
197         }
198
199         public void createNewTest(String testName, String testID) {
200
201                 testPool.addTest(new Test(testName, testID));
202         }
203
204         public void markTestFail(String testID) {
205                 numFailures++;
206                 testPool.getTest(testID).setVerdict(Test.FAIL);
207                 
208         }
209
210         public void markTestingStarted(int numTestsToBeRun) {
211                 
212                 this.numTests = numTestsToBeRun;
213                 //reportArea.append("Tests started expecting: " + numTests + " \n");
214                 
215         }
216
217         public void markTestingFinished() {
218
219                 
220                 //reportArea.append("end all tests \n");
221
222         }
223
224         // action to start tests:
225         private void startTests() {
226
227                 // preparation:
228                 // take the full test suite (could containt other test suites).
229                 // create temp php file that starts that suite and uses socketTestReport 
230                 // as a test result reporter.
231                 // add listener: localhost , port 13579
232                 // start listening at port.
233
234                 
235                 listenForReports();
236                 
237                 try {
238                         Runtime.getRuntime().exec("php.exe \"C:/Program Files/Apache Group/Apache2/htdocs/phpUnit/suite.php\"");
239                 } catch (IOException e) {
240                         // TODO Auto-generated catch block
241                         e.printStackTrace();
242                 }
243
244         }
245
246         /**
247          * 
248          */
249         private void listenForReports() {
250
251                 ConnectionListener conListener = new ConnectionListener();
252                 conListener.start(this);
253
254         } //end of method
255
256         /**
257          * handle this report: test passed, faile, end of all.
258          * @param report
259          */
260         public void handleReport(String report) {
261
262                 //delegate to the XML report handler.           
263                 handler.handle(report, this);
264
265         }
266
267
268         /**
269          * @param command
270          * @param testCount
271          * @param testID
272          */
273         public void handleCommand(
274                 String command,
275                 String testCount,
276                 String testID) {
277
278
279                 if (command.equals("startAll")) {
280                 
281                          markTestingStarted(new Integer(testCount).intValue());
282                          
283                         
284                 }else if (command.equals("testStarted")) {
285
286                         createNewTest("testName", testID);
287                         markTestStarted(testID);
288
289                 } else if (command.equals("testFINISHED")) {
290
291                         markTestFinished();
292                         
293                 } else if (command.equals("endAll")) {
294
295                         markTestingFinished();
296                 }
297
298
299                 progressInfoComposite.updateInfo(numTests, numTestsRun, numFailures, numErrors);
300                 resultsInfoComposite.updateInfo(testPool);
301
302         }
303
304         /**
305          * 
306          */
307         private void markTestFinished() {
308                         
309                 numTestsRun++;
310                 
311         }
312
313         /**
314          * 
315          */
316         private void updateResultsInfo() {
317                 // TODO Auto-generated method stub
318                 
319         }
320
321         /**
322          * @param currentTestID
323          * @param verdict
324          */
325         public void setTestVerdict(String currentTestID, String verdict) {
326
327                 if (verdict.equals("passed"))
328                         markTestPassed(currentTestID);
329                 else
330                         markTestFail(currentTestID);
331
332         }
333
334         /**
335          * @param currentTestID
336          * @param exception
337          */
338         public void addTestException(String currentTestID, String exception) {
339
340                 //TODO: decide how to show exceptions. don't show them for now.
341                 //reportArea.append("   test " + currentTestID + " exception: " + exception + "\n");
342
343         }
344
345 } //end of class