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