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