1 package net.sourceforge.phpeclipse.phpunit;
5 import java.io.BufferedWriter;
7 import java.io.FileWriter;
8 import java.io.IOException;
9 import java.net.MalformedURLException;
12 import net.sourceforge.phpeclipse.phpunit.testpool.TestCase;
13 import net.sourceforge.phpeclipse.phpunit.testpool.TestPool;
14 import net.sourceforge.phpeclipse.phpunit.testpool.TestSuite;
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;
28 * @author Ali Echihabi
30 * To change the template for this generated type comment go to
31 * Window>Preferences>Java>Code Generation>Code and Comments
34 * Created on May 22, 2004
36 * To change the template for this generated file go to
37 * Window>Preferences>Java>Code Generation>Code and Comments
41 * @author Ali Echihabi (ali_echihabi@ieee.org)
43 * Plugin for PHP unit Testing.
46 * This the main view showing the progress and reports.
50 public class PHPUnitView extends ViewPart {
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
64 private static PHPUnitView view = null;
66 private XMLReportHandler handler;
68 private TestPool testPool;
70 //private Button startButton;
72 private ProgressInfoComposite progressInfoComposite;
73 private ResultsInfoComposite resultsInfoComposite;
74 //private SettingsInfoComposite settingsInfoComposite; //TODO: move somewhere else, launcher, wizard or preferences.
75 private FileDialog dialog;
77 private String testSuiteToRun = "";
80 public PHPUnitView() {
89 public static PHPUnitView getDefault() {
95 public void createPartControl(Composite parent) {
97 //parent.setLayout(new FillLayout(SWT.VERTICAL));
99 dialog = new FileDialog(parent.getShell());
101 GridLayout gridLayout = new GridLayout();
102 gridLayout.numColumns = 1;
104 // set title and layout
105 parent.setLayout(gridLayout);
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));
117 //Build the result info composite
118 resultsInfoComposite = new ResultsInfoComposite(parent);
119 resultsInfoComposite.setLayoutData(new GridData(GridData.GRAB_VERTICAL | GridData.FILL_BOTH));
121 //build the settings composite
122 //buildSettingsComposite(parent);
124 //settingsInfoComposite = new SettingsInfoComposite(parent, SWT.NONE);
127 // startButton = new Button(parent, SWT.CENTER);
128 // startButton.setText("Start Tests");
129 // startButton.addMouseListener(new MouseListener() {
131 // public void mouseDoubleClick(MouseEvent arg0) {
135 // public void mouseDown(MouseEvent arg0) {
138 // String testFile = settingsInfoComposite.getTestSuite();
139 // startTests(testFile);
140 // } catch (IOException e) {
141 // // TODO Auto-generated catch block
142 // e.printStackTrace();
148 // public void mouseUp(MouseEvent arg0) {
153 // }); // end add action listener.
160 private void buildSettingsComposite(Composite parent) {
163 //settingsInfoComposite = new Group(parent, SWT.NONE);
164 //settingsInfoComposite.setText("Settings");
165 // settingsInfoComposite.setLayout(new GridLayout(2,false));
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);
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);
182 private void setActions() {
183 final IActionBars actionBars = getViewSite().getActionBars();
184 IToolBarManager toolBarManager = actionBars.getToolBarManager();
186 String iconsPath = "C:\\Documents and Settings\\Ali Echihabi\\My Documents\\workspace.eclipse2.1\\net.sourceforge.phpeclipse.phpunit\\icons";
188 ImageDescriptor descriptor = null;
191 Action selectTestAction = new Action() {
195 testSuiteToRun = dialog.open();
198 selectTestAction.setText("Select Test Suite");
199 selectTestAction.setToolTipText("Select Test Suite");
202 descriptor = ImageDescriptor.createFromURL(new URL("file://" + iconsPath + "//" + icon));
203 selectTestAction.setImageDescriptor(descriptor);
204 } catch (MalformedURLException e) {
205 // TODO Auto-generated catch block
210 toolBarManager.add(selectTestAction);
213 Action startTestAction = new Action() {
219 if(testSuiteToRun == null || testSuiteToRun == "")
222 startTests(testSuiteToRun);
223 } catch (IOException e) {
224 // TODO Auto-generated catch block
231 startTestAction.setText("Start Test");
232 startTestAction.setToolTipText("Start Test Suite");
236 descriptor = ImageDescriptor.createFromURL(new URL("file://" + iconsPath + "//" + icon));
237 startTestAction.setImageDescriptor(descriptor);
238 } catch (MalformedURLException e) {
239 // TODO Auto-generated catch block
244 toolBarManager.add(startTestAction);
250 * @see org.eclipse.ui.IWorkbenchPart#setFocus()
252 public void setFocus() {
257 * mark the given test as passed in the GUI.
261 private void markTestPassed(String testID) {
263 // testid, use it in hashmap to retrieve tree item of test and
264 // change icon color, increment pass counter, etc...
266 testPool.getTest(testID).setVerdict(TestCase.PASS);
272 private void markTestFail(String testID) {
274 testPool.getTest(testID).setVerdict(TestCase.FAIL);
278 // action to start tests:
279 public void startTests() throws IOException {
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.
288 // testPool = new TestPool("RUN MONDAY 11:15 PM");
289 // listenForReports();
292 // Runtime.getRuntime().exec("php.exe \"C:/Program Files/Apache Group/Apache2/htdocs/phpUnit/suite.php\"");
293 // } catch (Exception e) {
295 // e.printStackTrace();
298 startTests("C:/Program Files/Apache Group/Apache2/htdocs/phpUnit/suite.php");
302 public void startTests(String testSuite) throws IOException {
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.
307 //reset from previous run
311 testSuite = testSuite.replaceAll("\\\\", "/");
313 System.out.println("new: " + testSuite);
315 //where the plugin's temp files should go
316 String tempFolder = "C:\\tmp";
317 String tempFileName = "temTest.php";
320 File testFile = new File(tempFolder + "/" + tempFileName);
321 BufferedWriter out = new BufferedWriter(new FileWriter(testFile));
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");
329 //include the test suite that we want to run.
330 String testSuiteName = "";
331 testSuiteName = testSuite.substring(testSuite.lastIndexOf('/') + 1, testSuite.lastIndexOf('.'));
333 out.write("include_once(\"" + testSuite + "\");" + "\n");
336 out.write("" + "\n");
337 out.write("" + "\n");
339 out.write("$suite = new TestSuite();" + "\n");
340 out.write("$suite->addTest(new TestSuite(\"" + testSuiteName + "\"));" + "\n");
343 out.write("$result = new SocketTestResult();" + "\n");
344 out.write("$suite->run($result);" + "\n");
345 out.write("$result->report(); " + "\n");
347 out.write("" + "\n");
348 out.write("" + "\n");
350 out.write("$output = ob_get_contents();" + "\n");
351 out.write("$fileHandle = fopen('c:/tmp/phpOut.txt');" + "\n");
352 out.write("fclose($fileHandle);" + "\n");
355 out.write("ob_end();" + "\n");
356 out.write("?>" + "\n");
364 Runtime.getRuntime().exec("php.exe " + tempFolder + "/" + tempFileName);
365 } catch (Exception e) {
377 private void reset() {
379 handler = new XMLReportHandler();
380 testPool = new TestPool("Ali Baba");
382 progressInfoComposite.resetInfo();
383 resultsInfoComposite.resetInfo();
390 private void listenForReports() {
392 ConnectionListener conListener = new ConnectionListener();
393 conListener.start(this);
398 * handle this report: test passed, faile, end of all.
401 public void handleReport(String report) {
403 //delegate to the XML report handler.
404 handler.handle(report, this);
414 public void handleCommand(
415 String command, String[] args) {
418 if (command.equals("startAll")) {
420 //markTestingStarted(new Integer(testCount).intValue());
423 } else if (command.equals("testSuiteStarted")) {
425 String testID = args[0];
426 String testCount = args[1];
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);
432 } else if (command.equals("testStarted")) {
434 String testID = args[0];
435 String testCount = args[1];
436 String testName = args[2];
437 String parentTestSuiteName = args[3];
439 testPool.addTest(new TestCase(testID, testName, parentTestSuiteName));
441 } else if (command.equals("testFINISHED")) {
445 } else if (command.equals("endAll")) {
460 private void update() {
462 //progressInfoComposite.updateInfo(numTests, testPool.getNumTestsRun(), numFailures, numErrors);
463 progressInfoComposite.updateInfo(testPool);
464 resultsInfoComposite.updateInfo(testPool);
470 * @param currentTestID
473 public void setTestVerdict(String currentTestID, String verdict) {
475 if (verdict.equals("passed"))
476 markTestPassed(currentTestID);
478 markTestFail(currentTestID);
483 * @param currentTestID
486 public void addTestException(String currentTestID, String exception) {
488 //TODO: decide how to show exceptions. don't show them for now.
489 //reportArea.append(" test " + currentTestID + " exception: " + exception + "\n");