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.events.MouseEvent;
21 import org.eclipse.swt.events.MouseListener;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Button;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.ui.IActionBars;
27 import org.eclipse.ui.ISharedImages;
28 import org.eclipse.ui.IViewSite;
29 import org.eclipse.ui.PartInitException;
30 import org.eclipse.ui.PlatformUI;
31 import org.eclipse.ui.part.ViewPart;
34 * @author Ali Echihabi
36 * To change the template for this generated type comment go to
37 * Window>Preferences>Java>Code Generation>Code and Comments
40 * Created on May 22, 2004
42 * To change the template for this generated file go to
43 * Window>Preferences>Java>Code Generation>Code and Comments
47 * @author Ali Echihabi (ali_echihabi@ieee.org)
49 * Plugin for PHP unit Testing.
52 * This the main view showing the progress and reports.
56 public class PHPUnitView extends ViewPart {
64 * The first level nodes are the test suites.
65 * children are nested test suites.
66 * leafs: test functions.
67 * hierarchy: package->testsuite1->testcase->test_function
70 private static PHPUnitView view = null;
72 private XMLReportHandler handler;
74 private TestPool testPool;
76 //private Button startButton;
78 private ProgressInfoComposite progressInfoComposite;
79 private ResultsInfoComposite resultsInfoComposite;
80 private SettingsInfoComposite settingsInfoComposite; //TODO: move somewhere else, launcher, wizard or preferences.
84 public PHPUnitView() {
91 public static PHPUnitView getDefault() {
97 public void createPartControl(Composite parent) {
99 //parent.setLayout(new FillLayout(SWT.VERTICAL));
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 settingsInfoComposite.showFileDialog();
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() {
217 String testFile = settingsInfoComposite.getTestSuite();
219 startTests(testFile);
220 } catch (IOException e) {
221 // TODO Auto-generated catch block
228 startTestAction.setText("Start Test");
229 startTestAction.setToolTipText("Start Test Suite");
233 descriptor = ImageDescriptor.createFromURL(new URL("file://" + iconsPath + "//" + icon));
234 startTestAction.setImageDescriptor(descriptor);
235 } catch (MalformedURLException e) {
236 // TODO Auto-generated catch block
241 toolBarManager.add(startTestAction);
247 * @see org.eclipse.ui.IWorkbenchPart#setFocus()
249 public void setFocus() {
254 * mark the given test as passed in the GUI.
258 private void markTestPassed(String testID) {
260 // testid, use it in hashmap to retrieve tree item of test and
261 // change icon color, increment pass counter, etc...
263 testPool.getTest(testID).setVerdict(TestCase.PASS);
269 private void markTestFail(String testID) {
271 testPool.getTest(testID).setVerdict(TestCase.FAIL);
275 // action to start tests:
276 public void startTests() throws IOException {
279 // // take the full test suite (could containt other test suites).
280 // // create temp php file that starts that suite and uses socketTestReport
281 // // as a test result reporter.
282 // // add listener: localhost , port 13579
283 // // start listening at port.
285 // testPool = new TestPool("RUN MONDAY 11:15 PM");
286 // listenForReports();
289 // Runtime.getRuntime().exec("php.exe \"C:/Program Files/Apache Group/Apache2/htdocs/phpUnit/suite.php\"");
290 // } catch (Exception e) {
292 // e.printStackTrace();
295 startTests("C:/Program Files/Apache Group/Apache2/htdocs/phpUnit/suite.php");
299 public void startTests(String testSuite) throws IOException {
301 //testSuite: the name of the file containing the suite we want to run.
302 // we will put that test suite inside a contained that uses our SocketResult.
304 //reset from previous run
308 testSuite = testSuite.replaceAll("\\\\", "/");
310 System.out.println("new: " + testSuite);
312 //where the plugin's temp files should go
313 String tempFolder = "C:\\tmp";
314 String tempFileName = "temTest.php";
317 File testFile = new File(tempFolder + "/" + tempFileName);
318 BufferedWriter out = new BufferedWriter(new FileWriter(testFile));
320 out.write("<?php" + "\n");
321 out.write("$path = \"C:/Documents and Settings/Ali Echihabi/My Documents/workspace.eclipse2.1/PHPUnit/phpunit\";" + "\n");
322 out.write("include_once($path . \"/phpunit_test.php\");" + "\n");
323 out.write("include_once $path . \"/socketTestResult.php\";" + "\n");
325 //include the test suite that we want to run.
326 String testSuiteName = "";
327 testSuiteName = testSuite.substring(testSuite.lastIndexOf('/') + 1, testSuite.lastIndexOf('.'));
329 out.write("include_once(\"" + testSuite + "\");" + "\n");
332 out.write("" + "\n");
333 out.write("" + "\n");
335 out.write("$suite = new TestSuite();" + "\n");
336 out.write("$suite->addTest(new TestSuite(\"" + testSuiteName + "\"));" + "\n");
340 //out.write("$suite->addTest(new TestSuite(\"MoreTesterTests\"));" + "\n");
342 //out.write("$suite->addTest(new TestSuite(\"ManyFailingTests\"));" + "\n");
343 //out.write("$suite->addTest(new TestSuite(\"AssertEqualsTests\"));" + "\n");
345 out.write("$result = new SocketTestResult();" + "\n");
346 out.write("$suite->run($result);" + "\n");
347 out.write("$result->report(); " + "\n");
349 out.write("" + "\n");
350 out.write("" + "\n");
352 out.write("?>" + "\n");
360 Runtime.getRuntime().exec("php.exe " + tempFolder + "/" + tempFileName);
361 } catch (Exception e) {
373 private void reset() {
375 handler = new XMLReportHandler();
376 testPool = new TestPool("Ali Baba");
378 progressInfoComposite.resetInfo();
379 resultsInfoComposite.resetInfo();
386 private void listenForReports() {
388 ConnectionListener conListener = new ConnectionListener();
389 conListener.start(this);
394 * handle this report: test passed, faile, end of all.
397 public void handleReport(String report) {
399 //delegate to the XML report handler.
400 handler.handle(report, this);
410 public void handleCommand(
416 if (command.equals("startAll")) {
418 //markTestingStarted(new Integer(testCount).intValue());
421 } else if (command.equals("testSuiteStarted")) {
423 //createNewTestSuite("TestSuiteName: " + testID, testID, new Integer(testCount).intValue());
424 TestSuite suite = new TestSuite("TestSuiteName: " + testID, testID, new Integer(testCount).intValue());
425 testPool.addTestSuite(suite);
427 } else if (command.equals("testStarted")) {
429 testPool.addTest(new TestCase("TestName: " + testID, testID));
431 } else if (command.equals("testFINISHED")) {
435 } else if (command.equals("endAll")) {
450 private void update() {
452 //progressInfoComposite.updateInfo(numTests, testPool.getNumTestsRun(), numFailures, numErrors);
453 progressInfoComposite.updateInfo(testPool);
454 resultsInfoComposite.updateInfo(testPool);
460 * @param currentTestID
463 public void setTestVerdict(String currentTestID, String verdict) {
465 if (verdict.equals("passed"))
466 markTestPassed(currentTestID);
468 markTestFail(currentTestID);
473 * @param currentTestID
476 public void addTestException(String currentTestID, String exception) {
478 //TODO: decide how to show exceptions. don't show them for now.
479 //reportArea.append(" test " + currentTestID + " exception: " + exception + "\n");