9f388dde53735da6216f34949c7d7b191bba3b02
[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.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.FileDialog;
24 import org.eclipse.ui.IActionBars;
25 import org.eclipse.ui.part.ViewPart;
26
27 /**
28  * @author Ali Echihabi
29  *
30  * To change the template for this generated type comment go to
31  * Window>Preferences>Java>Code Generation>Code and Comments
32  */
33 /*
34  * Created on May 22, 2004
35  *
36  * To change the template for this generated file go to
37  * Window>Preferences>Java>Code Generation>Code and Comments
38  */
39
40 /**
41  * @author Ali Echihabi (ali_echihabi@ieee.org)
42  *
43  * Plugin for PHP unit Testing.
44  * www.phpeclipse.de
45  * 
46  * This the main view showing the progress and reports.
47  * 
48  */
49
50 public class PHPUnitView extends ViewPart {
51
52
53
54
55         /*
56          * like J Unit
57          * a tree.
58          * The first level nodes are the test suites.
59          * children are nested test suites.
60          * leafs: test functions.
61          * hierarchy: package->testsuite1->testcase->test_function
62          */
63
64         private static PHPUnitView view = null;
65
66         private XMLReportHandler handler;
67         
68         private TestPool testPool;
69
70         //private Button startButton;
71
72         private ProgressInfoComposite progressInfoComposite;
73         private ResultsInfoComposite resultsInfoComposite;
74         //private SettingsInfoComposite settingsInfoComposite; //TODO: move somewhere else, launcher, wizard or preferences.
75         private FileDialog dialog;
76
77         private String testSuiteToRun = "";
78
79
80         public PHPUnitView() {
81                 
82                 if(view == null)
83                         view = this;
84                         
85                         
86
87         }
88         
89         public static PHPUnitView getDefault() {
90                 
91                 
92                 return view; 
93         }
94
95         public void createPartControl(Composite parent) {
96
97                 //parent.setLayout(new FillLayout(SWT.VERTICAL));
98
99                 dialog = new FileDialog(parent.getShell());
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                                 testSuiteToRun = dialog.open(); 
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                                 
218                                 try {
219                                         if(testSuiteToRun == null || testSuiteToRun == "")
220                                                 return;
221                                                 
222                                         startTests(testSuiteToRun);
223                                 } catch (IOException e) {
224                                         // TODO Auto-generated catch block
225                                         e.printStackTrace();
226                                 }
227                                         
228                         }
229                         
230                 };
231                 startTestAction.setText("Start Test");
232                 startTestAction.setToolTipText("Start Test Suite");
233
234                 try {
235                         icon = "start.gif";
236                         descriptor = ImageDescriptor.createFromURL(new URL("file://" + iconsPath + "//" + icon));
237                         startTestAction.setImageDescriptor(descriptor);
238                 } catch (MalformedURLException e) {
239                         // TODO Auto-generated catch block
240                         e.printStackTrace();
241                 }
242
243         
244                 toolBarManager.add(startTestAction);
245         }
246
247
248
249         /* (non-Javadoc)
250          * @see org.eclipse.ui.IWorkbenchPart#setFocus()
251          */
252         public void setFocus() {
253                 
254         }
255
256         /**
257          * mark the given test as passed in the GUI.
258          * 
259          * @param testID
260          */
261         private void markTestPassed(String testID) {
262
263                 // testid, use it in hashmap to retrieve tree item of test and
264                 // change icon color, increment pass counter, etc...
265
266                 testPool.getTest(testID).setVerdict(TestCase.PASS);
267                 
268
269         }
270
271
272         private void markTestFail(String testID) {
273                 
274                 testPool.getTest(testID).setVerdict(TestCase.FAIL);
275                 
276         }
277
278         // action to start tests:
279         public void startTests() throws IOException {
280
281 //              // preparation:
282 //              // take the full test suite (could containt other test suites).
283 //              // create temp php file that starts that suite and uses socketTestReport 
284 //              // as a test result reporter.
285 //              // add listener: localhost , port 13579
286 //              // start listening at port.
287 //
288 //              testPool = new TestPool("RUN MONDAY 11:15 PM");
289 //              listenForReports();
290 //              
291 //              try {
292 //                      Runtime.getRuntime().exec("php.exe \"C:/Program Files/Apache Group/Apache2/htdocs/phpUnit/suite.php\"");
293 //              } catch (Exception e) {
294 //                      
295 //                      e.printStackTrace();
296 //              }
297
298         startTests("C:/Program Files/Apache Group/Apache2/htdocs/phpUnit/suite.php");
299
300         }
301
302         public void startTests(String testSuite) throws IOException {
303                 
304                 //testSuite: the name of the file containing the suite we want to run.
305                 // we will put that test suite inside a contained that uses our SocketResult.
306                 
307                 //reset from previous run
308                 reset();
309                 
310                 
311                 testSuite = testSuite.replaceAll("\\\\", "/");
312                 
313                 System.out.println("new: " +  testSuite);
314                 
315                 //where the plugin's temp files should go
316                 String tempFolder = "C:\\tmp"; 
317                 String tempFileName = "temTest.php";
318                 
319                 //create the file.
320                 File testFile = new File(tempFolder + "/" + tempFileName);
321                 BufferedWriter out = new BufferedWriter(new FileWriter(testFile));
322
323                 out.write("<?php" + "\n");
324                 out.write("ob_start();" + "\n");
325                 out.write("$path = \"C:/Documents and Settings/Ali Echihabi/My Documents/workspace.eclipse2.1/PHPUnit/phpunit\";" + "\n");
326                 out.write("include_once($path . \"/phpunit_test.php\");" + "\n");
327                 out.write("include_once $path . \"/socketTestResult.php\";" + "\n");
328
329                 //include the test suite that we want to run.
330                 String testSuiteName = "";
331                 testSuiteName = testSuite.substring(testSuite.lastIndexOf('/') + 1, testSuite.lastIndexOf('.'));
332
333                 out.write("include_once(\"" + testSuite + "\");" + "\n");
334                 
335
336                 out.write("" + "\n");
337                 out.write("" + "\n");           
338                 
339                 out.write("$suite = new TestSuite();" + "\n");
340                 out.write("$suite->addTest(new TestSuite(\"" + testSuiteName + "\"));" + "\n");
341                 
342         
343                 out.write("$result = new SocketTestResult();" + "\n");
344                 out.write("$suite->run($result);" + "\n");
345                 out.write("$result->report();   " + "\n");
346
347                 out.write("" + "\n");
348                 out.write("" + "\n");
349                 
350                 out.write("$output = ob_get_contents();" + "\n");
351                 out.write("$fileHandle = fopen('c:/tmp/phpOut.txt');" + "\n");
352                 out.write("fclose($fileHandle);" + "\n");
353                 
354                 
355                 out.write("ob_end();" + "\n");
356                 out.write("?>" + "\n");
357                 
358                 out.flush();
359                 out.close();
360                 
361                 listenForReports();
362                 
363                 try {
364                         Runtime.getRuntime().exec("php.exe " + tempFolder + "/" + tempFileName);
365                 } catch (Exception e) {
366                         
367                         e.printStackTrace();
368                 }
369                 
370                 //testFile.delete();
371                 
372         }
373
374         /**
375          * 
376          */
377         private void reset() {
378                 
379                 handler = new XMLReportHandler();
380                 testPool = new TestPool("Ali Baba");
381                  
382                 progressInfoComposite.resetInfo();
383                 resultsInfoComposite.resetInfo();
384                 
385         }
386
387         /**
388          * 
389          */
390         private void listenForReports() {
391
392                 ConnectionListener conListener = new ConnectionListener();
393                 conListener.start(this);
394
395         } //end of method
396
397         /**
398          * handle this report: test passed, faile, end of all.
399          * @param report
400          */
401         public void handleReport(String report) {
402
403                 //delegate to the XML report handler.           
404                 handler.handle(report, this);
405
406         }
407
408
409         /**
410          * @param command
411          * @param testCount
412          * @param testID
413          */
414         public void handleCommand(
415                 String command, String[] args) {
416         
417
418                 if (command.equals("startAll")) {
419                 
420                          //markTestingStarted(new Integer(testCount).intValue());
421                          
422                         
423                 } else if (command.equals("testSuiteStarted")) {
424
425                         String testID = args[0];
426                         String testCount = args[1];
427                         
428                         //createNewTestSuite("TestSuiteName: " + testID, testID, new Integer(testCount).intValue());
429                         TestSuite suite = new TestSuite("TestSuiteName: " + testID, testID, new Integer(testCount).intValue());
430                         testPool.addTestSuite(suite);
431
432                 } else if (command.equals("testStarted")) {
433
434                         String testID = args[0];
435                         String testCount = args[1];
436                         String testName = args[2];
437                         String parentTestSuiteName = args[3];
438                         
439                         testPool.addTest(new TestCase(testID, testName, parentTestSuiteName));                  
440
441                 } else if (command.equals("testFINISHED")) {
442
443                         
444                         
445                 } else if (command.equals("endAll")) {
446
447                         
448                 }
449
450                 
451                 update();
452
453
454
455         }
456
457         /**
458          * 
459          */
460         private void update() {
461                 
462                 //progressInfoComposite.updateInfo(numTests, testPool.getNumTestsRun(), numFailures, numErrors);
463                 progressInfoComposite.updateInfo(testPool);
464                 resultsInfoComposite.updateInfo(testPool);
465                 
466         }
467
468
469         /**
470          * @param currentTestID
471          * @param verdict
472          */
473         public void setTestVerdict(String currentTestID, String verdict) {
474
475                 if (verdict.equals("passed"))
476                         markTestPassed(currentTestID);
477                 else
478                         markTestFail(currentTestID);
479
480         }
481
482         /**
483          * @param currentTestID
484          * @param exception
485          */
486         public void addTestException(String currentTestID, String exception) {
487
488                 //TODO: decide how to show exceptions. don't show them for now.
489                 //reportArea.append("   test " + currentTestID + " exception: " + exception + "\n");
490
491         }
492
493
494 } //end of class