corrected the number of tests expected in progress view. Added toolbar actions to...
[phpeclipse.git] / net.sourceforge.phpeclipse.phpunit / src / net / sourceforge / phpeclipse / phpunit / PHPUnitView.java
1 package net.sourceforge.phpeclipse.phpunit;
2
3
4
5 import java.io.BufferedWriter;
6 import java.io.File;
7 import java.io.FileWriter;
8 import java.io.IOException;
9 import java.net.MalformedURLException;
10 import java.net.URL;
11
12 import net.sourceforge.phpeclipse.phpunit.testpool.TestCase;
13 import net.sourceforge.phpeclipse.phpunit.testpool.TestPool;
14 import net.sourceforge.phpeclipse.phpunit.testpool.TestSuite;
15
16 import org.eclipse.jface.action.Action;
17 import org.eclipse.jface.action.IToolBarManager;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.MouseEvent;
21 import org.eclipse.swt.events.MouseListener;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Button;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.ui.IActionBars;
27 import org.eclipse.ui.ISharedImages;
28 import org.eclipse.ui.IViewSite;
29 import org.eclipse.ui.PartInitException;
30 import org.eclipse.ui.PlatformUI;
31 import org.eclipse.ui.part.ViewPart;
32
33 /**
34  * @author Ali Echihabi
35  *
36  * To change the template for this generated type comment go to
37  * Window>Preferences>Java>Code Generation>Code and Comments
38  */
39 /*
40  * Created on May 22, 2004
41  *
42  * To change the template for this generated file go to
43  * Window>Preferences>Java>Code Generation>Code and Comments
44  */
45
46 /**
47  * @author Ali Echihabi (ali_echihabi@ieee.org)
48  *
49  * Plugin for PHP unit Testing.
50  * www.phpeclipse.de
51  * 
52  * This the main view showing the progress and reports.
53  * 
54  */
55
56 public class PHPUnitView extends ViewPart {
57
58
59
60
61         /*
62          * like J Unit
63          * a tree.
64          * The first level nodes are the test suites.
65          * children are nested test suites.
66          * leafs: test functions.
67          * hierarchy: package->testsuite1->testcase->test_function
68          */
69
70         private static PHPUnitView view = null;
71
72         private XMLReportHandler handler;
73         
74         private TestPool testPool;
75
76         //private Button startButton;
77
78         private ProgressInfoComposite progressInfoComposite;
79         private ResultsInfoComposite resultsInfoComposite;
80         private SettingsInfoComposite settingsInfoComposite; //TODO: move somewhere else, launcher, wizard or preferences.
81         
82
83
84         public PHPUnitView() {
85                 
86                 if(view == null)
87                         view = this;
88
89         }
90         
91         public static PHPUnitView getDefault() {
92                 
93                 
94                 return view; 
95         }
96
97         public void createPartControl(Composite parent) {
98
99                 //parent.setLayout(new FillLayout(SWT.VERTICAL));
100
101                 GridLayout gridLayout = new GridLayout();
102                 gridLayout.numColumns = 1;
103                 
104                 // set title and layout
105                 parent.setLayout(gridLayout);           
106                 
107
108                 //Launch ToolBar:
109                 setActions();
110
111                 //Build the progress info Composites            
112                 progressInfoComposite = new ProgressInfoComposite(parent);
113                 progressInfoComposite.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
114                 
115                 
116         
117                 //Build the result info composite
118                 resultsInfoComposite = new ResultsInfoComposite(parent);
119                 resultsInfoComposite.setLayoutData(new GridData(GridData.GRAB_VERTICAL | GridData.FILL_BOTH));
120                 
121                 //build the settings composite
122                 //buildSettingsComposite(parent);
123                 
124                 settingsInfoComposite = new SettingsInfoComposite(parent, SWT.NONE);
125
126
127 //              startButton = new Button(parent, SWT.CENTER);
128 //              startButton.setText("Start Tests");
129 //              startButton.addMouseListener(new MouseListener() {
130 //
131 //                      public void mouseDoubleClick(MouseEvent arg0) {
132 //
133 //                      }
134 //
135 //                      public void mouseDown(MouseEvent arg0) {
136 //
137 //                              try {
138 //                                      String testFile = settingsInfoComposite.getTestSuite();
139 //                                      startTests(testFile);
140 //                              } catch (IOException e) {
141 //                                      // TODO Auto-generated catch block
142 //                                      e.printStackTrace();
143 //                              }
144 //
145 //
146 //                      }
147 //
148 //                      public void mouseUp(MouseEvent arg0) {
149 //
150 //
151 //                      }
152 //
153 //              }); // end add action listener.
154
155         }
156
157         /**
158          * @param parent
159          */
160         private void buildSettingsComposite(Composite parent) {
161                 
162                 
163                 //settingsInfoComposite = new Group(parent, SWT.NONE);
164                 //settingsInfoComposite.setText("Settings");
165 //              settingsInfoComposite.setLayout(new GridLayout(2,false));
166 //              
167 //              
168 //              //the test suite to launch
169 //              Label testSuiteLabel = new Label(settingsInfoComposite, SWT.NONE);
170 //              testSuiteLabel.setText("Test suite to run:");
171 //              //testSuiteLabel.setLayoutData(new GridData())
172 //              Text testSuiteText = new Text(settingsInfoComposite, SWT.NONE);
173 //              
174 //              //the path to php
175 //              Label phpPathLabel = new Label(settingsInfoComposite, SWT.NONE);
176 //              phpPathLabel.setText("php Path:");
177 //              //testSuiteLabel.setLayoutData(new GridData())
178 //              Text phpPathText = new Text(settingsInfoComposite, SWT.NONE);
179
180         }
181
182         private void setActions() {
183                 final IActionBars actionBars = getViewSite().getActionBars();
184                 IToolBarManager toolBarManager = actionBars.getToolBarManager();
185
186                 String iconsPath = "C:\\Documents and Settings\\Ali Echihabi\\My Documents\\workspace.eclipse2.1\\net.sourceforge.phpeclipse.phpunit\\icons";
187                 
188                 ImageDescriptor descriptor = null;
189                 String icon = "";
190
191                 Action selectTestAction = new Action() {
192                         
193                         public void run() {
194                                 
195                                 settingsInfoComposite.showFileDialog(); 
196                         }
197                 };
198                 selectTestAction.setText("Select Test Suite");
199                 selectTestAction.setToolTipText("Select Test Suite");
200                 try {
201                         icon = "tsuite.gif";
202                         descriptor = ImageDescriptor.createFromURL(new URL("file://" + iconsPath + "//" + icon));
203                         selectTestAction.setImageDescriptor(descriptor);
204                 } catch (MalformedURLException e) {
205                         // TODO Auto-generated catch block
206                         e.printStackTrace();
207                 }
208                 
209                                 
210                 toolBarManager.add(selectTestAction);
211                 
212                 
213                 Action startTestAction = new Action() {
214                 
215                         public void run() {
216                                 
217                                 String testFile = settingsInfoComposite.getTestSuite();
218                                 try {
219                                         startTests(testFile);
220                                 } catch (IOException e) {
221                                         // TODO Auto-generated catch block
222                                         e.printStackTrace();
223                                 }
224                                         
225                         }
226                         
227                 };
228                 startTestAction.setText("Start Test");
229                 startTestAction.setToolTipText("Start Test Suite");
230
231                 try {
232                         icon = "start.gif";
233                         descriptor = ImageDescriptor.createFromURL(new URL("file://" + iconsPath + "//" + icon));
234                         startTestAction.setImageDescriptor(descriptor);
235                 } catch (MalformedURLException e) {
236                         // TODO Auto-generated catch block
237                         e.printStackTrace();
238                 }
239
240         
241                 toolBarManager.add(startTestAction);
242         }
243
244
245
246         /* (non-Javadoc)
247          * @see org.eclipse.ui.IWorkbenchPart#setFocus()
248          */
249         public void setFocus() {
250                 
251         }
252
253         /**
254          * mark the given test as passed in the GUI.
255          * 
256          * @param testID
257          */
258         private void markTestPassed(String testID) {
259
260                 // testid, use it in hashmap to retrieve tree item of test and
261                 // change icon color, increment pass counter, etc...
262
263                 testPool.getTest(testID).setVerdict(TestCase.PASS);
264                 
265
266         }
267
268
269         private void markTestFail(String testID) {
270                 
271                 testPool.getTest(testID).setVerdict(TestCase.FAIL);
272                 
273         }
274
275         // action to start tests:
276         public void startTests() throws IOException {
277
278 //              // preparation:
279 //              // take the full test suite (could containt other test suites).
280 //              // create temp php file that starts that suite and uses socketTestReport 
281 //              // as a test result reporter.
282 //              // add listener: localhost , port 13579
283 //              // start listening at port.
284 //
285 //              testPool = new TestPool("RUN MONDAY 11:15 PM");
286 //              listenForReports();
287 //              
288 //              try {
289 //                      Runtime.getRuntime().exec("php.exe \"C:/Program Files/Apache Group/Apache2/htdocs/phpUnit/suite.php\"");
290 //              } catch (Exception e) {
291 //                      
292 //                      e.printStackTrace();
293 //              }
294
295         startTests("C:/Program Files/Apache Group/Apache2/htdocs/phpUnit/suite.php");
296
297         }
298
299         public void startTests(String testSuite) throws IOException {
300                 
301                 //testSuite: the name of the file containing the suite we want to run.
302                 // we will put that test suite inside a contained that uses our SocketResult.
303                 
304                 //reset from previous run
305                 reset();
306                 
307                 
308                 testSuite = testSuite.replaceAll("\\\\", "/");
309                 
310                 System.out.println("new: " +  testSuite);
311                 
312                 //where the plugin's temp files should go
313                 String tempFolder = "C:\\tmp"; 
314                 String tempFileName = "temTest.php";
315                 
316                 //create the file.
317                 File testFile = new File(tempFolder + "/" + tempFileName);
318                 BufferedWriter out = new BufferedWriter(new FileWriter(testFile));
319
320                 out.write("<?php" + "\n");
321                 out.write("$path = \"C:/Documents and Settings/Ali Echihabi/My Documents/workspace.eclipse2.1/PHPUnit/phpunit\";" + "\n");
322                 out.write("include_once($path . \"/phpunit_test.php\");" + "\n");
323                 out.write("include_once $path . \"/socketTestResult.php\";" + "\n");
324
325                 //include the test suite that we want to run.
326                 String testSuiteName = "";
327                 testSuiteName = testSuite.substring(testSuite.lastIndexOf('/') + 1, testSuite.lastIndexOf('.'));
328
329                 out.write("include_once(\"" + testSuite + "\");" + "\n");
330                 
331
332                 out.write("" + "\n");
333                 out.write("" + "\n");           
334                 
335                 out.write("$suite = new TestSuite();" + "\n");
336                 out.write("$suite->addTest(new TestSuite(\"" + testSuiteName + "\"));" + "\n");
337                 
338                 
339                 
340                 //out.write("$suite->addTest(new TestSuite(\"MoreTesterTests\"));" + "\n");
341         
342                 //out.write("$suite->addTest(new TestSuite(\"ManyFailingTests\"));" + "\n");
343                 //out.write("$suite->addTest(new TestSuite(\"AssertEqualsTests\"));" + "\n");           
344                         
345                 out.write("$result = new SocketTestResult();" + "\n");
346                 out.write("$suite->run($result);" + "\n");
347                 out.write("$result->report();   " + "\n");
348
349                 out.write("" + "\n");
350                 out.write("" + "\n");
351                 
352                 out.write("?>" + "\n");
353                 
354                 out.flush();
355                 out.close();
356                 
357                 listenForReports();
358                 
359                 try {
360                         Runtime.getRuntime().exec("php.exe " + tempFolder + "/" + tempFileName);
361                 } catch (Exception e) {
362                         
363                         e.printStackTrace();
364                 }
365                 
366                 //testFile.delete();
367                 
368         }
369
370         /**
371          * 
372          */
373         private void reset() {
374                 
375                 handler = new XMLReportHandler();
376                 testPool = new TestPool("Ali Baba");
377                  
378                 progressInfoComposite.resetInfo();
379                 resultsInfoComposite.resetInfo();
380                 
381         }
382
383         /**
384          * 
385          */
386         private void listenForReports() {
387
388                 ConnectionListener conListener = new ConnectionListener();
389                 conListener.start(this);
390
391         } //end of method
392
393         /**
394          * handle this report: test passed, faile, end of all.
395          * @param report
396          */
397         public void handleReport(String report) {
398
399                 //delegate to the XML report handler.           
400                 handler.handle(report, this);
401
402         }
403
404
405         /**
406          * @param command
407          * @param testCount
408          * @param testID
409          */
410         public void handleCommand(
411                 String command,
412                 String testCount,
413                 String testID) {
414
415
416                 if (command.equals("startAll")) {
417                 
418                          //markTestingStarted(new Integer(testCount).intValue());
419                          
420                         
421                 } else if (command.equals("testSuiteStarted")) {
422
423                         //createNewTestSuite("TestSuiteName: " + testID, testID, new Integer(testCount).intValue());
424                         TestSuite suite = new TestSuite("TestSuiteName: " + testID, testID, new Integer(testCount).intValue());
425                         testPool.addTestSuite(suite);
426
427                 } else if (command.equals("testStarted")) {
428
429                         testPool.addTest(new TestCase("TestName: " + testID, testID));                  
430
431                 } else if (command.equals("testFINISHED")) {
432
433                         
434                         
435                 } else if (command.equals("endAll")) {
436
437                         
438                 }
439
440                 
441                 update();
442
443
444
445         }
446
447         /**
448          * 
449          */
450         private void update() {
451                 
452                 //progressInfoComposite.updateInfo(numTests, testPool.getNumTestsRun(), numFailures, numErrors);
453                 progressInfoComposite.updateInfo(testPool);
454                 resultsInfoComposite.updateInfo(testPool);
455                 
456         }
457
458
459         /**
460          * @param currentTestID
461          * @param verdict
462          */
463         public void setTestVerdict(String currentTestID, String verdict) {
464
465                 if (verdict.equals("passed"))
466                         markTestPassed(currentTestID);
467                 else
468                         markTestFail(currentTestID);
469
470         }
471
472         /**
473          * @param currentTestID
474          * @param exception
475          */
476         public void addTestException(String currentTestID, String exception) {
477
478                 //TODO: decide how to show exceptions. don't show them for now.
479                 //reportArea.append("   test " + currentTestID + " exception: " + exception + "\n");
480
481         }
482
483
484 } //end of class