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