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