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