new class to manage icons. Showing failures,etc on a test suite.
authorshleh <shleh>
Sat, 18 Sep 2004 17:42:48 +0000 (17:42 +0000)
committershleh <shleh>
Sat, 18 Sep 2004 17:42:48 +0000 (17:42 +0000)
net.sourceforge.phpeclipse.phpunit/icons/start.gif [new file with mode: 0644]
net.sourceforge.phpeclipse.phpunit/src/net/sourceforge/phpeclipse/phpunit/PHPUnitImages.java [new file with mode: 0644]
net.sourceforge.phpeclipse.phpunit/src/net/sourceforge/phpeclipse/phpunit/PHPUnitPlugin.java
net.sourceforge.phpeclipse.phpunit/src/net/sourceforge/phpeclipse/phpunit/PHPUnitView.java
net.sourceforge.phpeclipse.phpunit/src/net/sourceforge/phpeclipse/phpunit/TestPoolLabelProvider.java
net.sourceforge.phpeclipse.phpunit/src/net/sourceforge/phpeclipse/phpunit/testpool/TestCase.java
net.sourceforge.phpeclipse.phpunit/src/net/sourceforge/phpeclipse/phpunit/testpool/TestPool.java
net.sourceforge.phpeclipse.phpunit/src/net/sourceforge/phpeclipse/phpunit/testpool/TestSuite.java

diff --git a/net.sourceforge.phpeclipse.phpunit/icons/start.gif b/net.sourceforge.phpeclipse.phpunit/icons/start.gif
new file mode 100644 (file)
index 0000000..1486517
Binary files /dev/null and b/net.sourceforge.phpeclipse.phpunit/icons/start.gif differ
diff --git a/net.sourceforge.phpeclipse.phpunit/src/net/sourceforge/phpeclipse/phpunit/PHPUnitImages.java b/net.sourceforge.phpeclipse.phpunit/src/net/sourceforge/phpeclipse/phpunit/PHPUnitImages.java
new file mode 100644 (file)
index 0000000..006336a
--- /dev/null
@@ -0,0 +1,94 @@
+package net.sourceforge.phpeclipse.phpunit;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.resource.ImageRegistry;
+import org.eclipse.swt.graphics.Image;
+
+public class PHPUnitImages {
+
+       protected static final String NAME_PREFIX =
+               "net.sourceforge.phpeclipse.phpunit";
+       protected static final int NAME_PREFIX_LENGTH = NAME_PREFIX.length();
+
+       protected static URL iconBaseURL;
+
+       static {
+               String pathSuffix = "icons/";
+               try {
+                       iconBaseURL =
+                               new URL(
+                                       PHPUnitPlugin.getDefault().getDescriptor().getInstallURL(),
+                                       pathSuffix);
+               } catch (MalformedURLException e) {
+                       //PHPUnitPlugin.log(e);
+                       e.printStackTrace();
+               }
+       }
+
+       protected static final ImageRegistry IMAGE_REGISTRY = new ImageRegistry();
+
+       /*
+        * Available cached Images in the Java plugin image registry.
+        */
+
+       public static final String IMG_SELECT_TEST_SUITE =
+               NAME_PREFIX + "tsuite.gif";
+       public static final String IMG_RUN_TEST_SUITE = NAME_PREFIX + "start.gif";
+       public static final String IMG_TEST_ERROR = NAME_PREFIX + "testerr.gif";
+       public static final String IMG_TEST_FAILURE = NAME_PREFIX + "testfail.gif";
+       public static final String IMG_TEST_PASS = NAME_PREFIX + "testok.gif";
+       public static final String IMG_TEST_SUITE_ERROR = NAME_PREFIX + "tsuiteerror.gif";
+       public static final String IMG_TEST_SUITE_PASS = NAME_PREFIX + "tsuiteok.gif";
+       public static final String IMG_TEST_SUITE_FAILURE = NAME_PREFIX + "tsuitefail.gif";
+       
+       
+
+
+       public static final ImageDescriptor DESC_SELECT_TEST_SUITE = createManaged(IMG_SELECT_TEST_SUITE);
+       public static final ImageDescriptor DESC_RUN_TEST_SUITE =createManaged(IMG_RUN_TEST_SUITE);
+       public static final ImageDescriptor DESC_TEST_ERROR = createManaged(IMG_TEST_ERROR);
+       public static final ImageDescriptor DESC_TEST_FAILURE = createManaged(IMG_TEST_FAILURE);
+       public static final ImageDescriptor DESC_TEST_PASS = createManaged(IMG_TEST_PASS);
+       public static final ImageDescriptor DESC_TEST_SUITE_ERROR = createManaged(IMG_TEST_SUITE_ERROR);
+       public static final ImageDescriptor DESC_TEST_SUITE_PASS = createManaged(IMG_TEST_SUITE_PASS);
+       public static final ImageDescriptor DESC_TEST_SUITE_FAILURE = createManaged(IMG_TEST_SUITE_FAILURE);
+       
+       /**
+        * Returns the image managed under the given key in this registry.
+        * 
+        * @param key the image's key
+        * @return the image managed under the given key
+        */
+       public static Image get(String key) {
+               return IMAGE_REGISTRY.get(key);
+       }
+
+       public static ImageRegistry getImageRegistry() {
+               return IMAGE_REGISTRY;
+       }
+
+       //---- Helper methods to access icons on the file system --------------------------------------
+
+       protected static ImageDescriptor createManaged(String name) {
+               try {
+                       ImageDescriptor result =
+                               ImageDescriptor.createFromURL(
+                                       makeIconFileURL(name.substring(NAME_PREFIX_LENGTH)));
+                       IMAGE_REGISTRY.put(name, result);
+                       return result;
+               } catch (MalformedURLException e) {
+                       return ImageDescriptor.getMissingImageDescriptor();
+               }
+       }
+
+       protected static URL makeIconFileURL(String name)
+               throws MalformedURLException {
+               if (iconBaseURL == null)
+                       throw new MalformedURLException();
+
+               return new URL(iconBaseURL, name);
+       }
+}
\ No newline at end of file
index e3873a4..fb89999 100644 (file)
@@ -6,15 +6,13 @@
  */
 package net.sourceforge.phpeclipse.phpunit;
 
-import java.net.MalformedURLException;
-import java.net.URL;
 import java.util.ResourceBundle;
 
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPluginDescriptor;
+import org.eclipse.core.runtime.Platform;
 import org.eclipse.jface.dialogs.IDialogSettings;
 import org.eclipse.jface.preference.IPreferenceStore;
-import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.resource.ImageRegistry;
 import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.plugin.AbstractUIPlugin;
@@ -31,21 +29,42 @@ public class PHPUnitPlugin extends AbstractUIPlugin {
 
        private static PHPUnitPlugin plugin;
        private ResourceBundle resourceBundle;
-
+       public static final String PLUGIN_ID= "net.sourceforge.phpeclipse.phpunit"; //$NON-NLS-1$
 
        /**
         * @param descriptor
         */
        public PHPUnitPlugin(IPluginDescriptor descriptor) {
+               
                super(descriptor);
                
-               plugin = this;
-
+               System.out.println("desc: "  + descriptor.getInstallURL());
+                               
+               
+               plugin= this;
+               
+               
+               String pathSuffix= "icons/"; //$NON-NLS-1$
+               
+               System.out.println("" + Platform.getLocation());
+               
+               System.out.println("" + Platform.getPluginStateLocation(plugin));
+               
+               System.out.println("" + Platform.getLogFileLocation());
+               
+               
                
        
                
        }
 
+       public String getPath() {
+               return plugin.getDescriptor().getInstallURL().getFile();
+       }
+
+       
+
        public static PHPUnitPlugin getDefault() {
                return plugin;
        }
@@ -158,25 +177,6 @@ public class PHPUnitPlugin extends AbstractUIPlugin {
                super.startup();
        }
 
-       /**
-        * @param string
-        */
-       public static ImageDescriptor getImageDescriptor(String name) {
-               
-               String iconPath = "icons/";
-               try {
-                       URL installURL = getDefault().getDescriptor().getInstallURL();
-                       URL url = new URL(installURL, iconPath + name);
-                       
-                       System.out.println("url:" + url.toExternalForm());
-                       
-                       return ImageDescriptor.createFromURL(url);
-               } catch (MalformedURLException e) {
-                       // should not happen
-                       return ImageDescriptor.getMissingImageDescriptor();
-               }               
-               
-               
-       }
+
 
 }
index 9f388dd..e304352 100644 (file)
@@ -6,8 +6,6 @@ import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
-import java.net.MalformedURLException;
-import java.net.URL;
 
 import net.sourceforge.phpeclipse.phpunit.testpool.TestCase;
 import net.sourceforge.phpeclipse.phpunit.testpool.TestPool;
@@ -15,8 +13,6 @@ import net.sourceforge.phpeclipse.phpunit.testpool.TestSuite;
 
 import org.eclipse.jface.action.Action;
 import org.eclipse.jface.action.IToolBarManager;
-import org.eclipse.jface.resource.ImageDescriptor;
-import org.eclipse.swt.SWT;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Composite;
@@ -183,11 +179,6 @@ public class PHPUnitView extends ViewPart {
                final IActionBars actionBars = getViewSite().getActionBars();
                IToolBarManager toolBarManager = actionBars.getToolBarManager();
 
-               String iconsPath = "C:\\Documents and Settings\\Ali Echihabi\\My Documents\\workspace.eclipse2.1\\net.sourceforge.phpeclipse.phpunit\\icons";
-               
-               ImageDescriptor descriptor = null;
-               String icon = "";
-
                Action selectTestAction = new Action() {
                        
                        public void run() {
@@ -195,16 +186,10 @@ public class PHPUnitView extends ViewPart {
                                testSuiteToRun = dialog.open(); 
                        }
                };
+               
                selectTestAction.setText("Select Test Suite");
-               selectTestAction.setToolTipText("Select Test Suite");
-               try {
-                       icon = "tsuite.gif";
-                       descriptor = ImageDescriptor.createFromURL(new URL("file://" + iconsPath + "//" + icon));
-                       selectTestAction.setImageDescriptor(descriptor);
-               } catch (MalformedURLException e) {
-                       // TODO Auto-generated catch block
-                       e.printStackTrace();
-               }
+               selectTestAction.setToolTipText("Select Test Suite");           
+               selectTestAction.setImageDescriptor(PHPUnitImages.DESC_SELECT_TEST_SUITE);
                
                                
                toolBarManager.add(selectTestAction);
@@ -228,18 +213,11 @@ public class PHPUnitView extends ViewPart {
                        }
                        
                };
+               
                startTestAction.setText("Start Test");
                startTestAction.setToolTipText("Start Test Suite");
-
-               try {
-                       icon = "start.gif";
-                       descriptor = ImageDescriptor.createFromURL(new URL("file://" + iconsPath + "//" + icon));
-                       startTestAction.setImageDescriptor(descriptor);
-               } catch (MalformedURLException e) {
-                       // TODO Auto-generated catch block
-                       e.printStackTrace();
-               }
-
+               startTestAction.setImageDescriptor(PHPUnitImages.DESC_RUN_TEST_SUITE);
        
                toolBarManager.add(startTestAction);
        }
@@ -275,29 +253,7 @@ public class PHPUnitView extends ViewPart {
                
        }
 
-       // action to start tests:
-       public void startTests() throws IOException {
-
-//             // preparation:
-//             // take the full test suite (could containt other test suites).
-//             // create temp php file that starts that suite and uses socketTestReport 
-//             // as a test result reporter.
-//             // add listener: localhost , port 13579
-//             // start listening at port.
-//
-//             testPool = new TestPool("RUN MONDAY 11:15 PM");
-//             listenForReports();
-//             
-//             try {
-//                     Runtime.getRuntime().exec("php.exe \"C:/Program Files/Apache Group/Apache2/htdocs/phpUnit/suite.php\"");
-//             } catch (Exception e) {
-//                     
-//                     e.printStackTrace();
-//             }
-
-       startTests("C:/Program Files/Apache Group/Apache2/htdocs/phpUnit/suite.php");
 
-       }
 
        public void startTests(String testSuite) throws IOException {
                
@@ -322,7 +278,10 @@ public class PHPUnitView extends ViewPart {
 
                out.write("<?php" + "\n");
                out.write("ob_start();" + "\n");
+               
+               //TODO: use install dir for path to phpunit. include it with plugin.
                out.write("$path = \"C:/Documents and Settings/Ali Echihabi/My Documents/workspace.eclipse2.1/PHPUnit/phpunit\";" + "\n");
+               
                out.write("include_once($path . \"/phpunit_test.php\");" + "\n");
                out.write("include_once $path . \"/socketTestResult.php\";" + "\n");
 
@@ -426,7 +385,7 @@ public class PHPUnitView extends ViewPart {
                        String testCount = args[1];
                        
                        //createNewTestSuite("TestSuiteName: " + testID, testID, new Integer(testCount).intValue());
-                       TestSuite suite = new TestSuite("TestSuiteName: " + testID, testID, new Integer(testCount).intValue());
+                       TestSuite suite = new TestSuite(null, "TestSuiteName: " + testID, testID, new Integer(testCount).intValue());
                        testPool.addTestSuite(suite);
 
                } else if (command.equals("testStarted")) {
@@ -491,4 +450,6 @@ public class PHPUnitView extends ViewPart {
        }
 
 
+
+
 } //end of class
index 344bb08..3e27112 100644 (file)
@@ -6,9 +6,6 @@
  */
 package net.sourceforge.phpeclipse.phpunit;
 
-import java.net.MalformedURLException;
-import java.net.URL;
-
 import net.sourceforge.phpeclipse.phpunit.testpool.TestCase;
 import net.sourceforge.phpeclipse.phpunit.testpool.TestSuite;
 
@@ -24,9 +21,6 @@ import org.eclipse.swt.graphics.Image;
  */
 public class TestPoolLabelProvider extends LabelProvider {
 
-       //TODO: replace with installDir + path
-       private static String iconsPath = "C:\\Documents and Settings\\Ali Echihabi\\My Documents\\workspace.eclipse2.1\\net.sourceforge.phpeclipse.phpunit\\icons";
-
        public String getText(Object element) {
                
                String text = "";
@@ -50,44 +44,39 @@ public class TestPoolLabelProvider extends LabelProvider {
        public Image getImage(Object element) {
                
                Image image = null;
-               
-               try {
+               ImageDescriptor descriptor = null;
                        
-                       String icon = "";
-                       if(element instanceof TestSuite) {
-                               
-                               TestSuite suite = (TestSuite)element;
-                               
-                               //TODO check if there has been an error, a failure...
-                               
-                               icon = "tsuite.gif";
-                               
-                       } else if(element instanceof TestCase) {
-                               
-                               TestCase test = (TestCase)element;
-                               
-                               if(test.isError())
-                                       icon = "testerr.gif";
-                               else if(test.isFailure())
-                                       icon = "testfail.gif";
-                               else if(test.isPass())
-                                       icon = "testok.gif";
-                               
-                       }
+               if(element instanceof TestSuite) {
                        
+                       TestSuite suite = (TestSuite)element;
+                       descriptor = PHPUnitImages.DESC_TEST_SUITE_PASS;
+
+
+                       if(suite.hasError())
+                               descriptor = PHPUnitImages.DESC_TEST_SUITE_ERROR;
+                       else if(suite.hasFailure())
+                               descriptor = PHPUnitImages.DESC_TEST_SUITE_FAILURE;
+                       else if(suite.isAllPass())
+                               descriptor = PHPUnitImages.DESC_TEST_SUITE_PASS;
+                                       
+                                                               
+               } else if(element instanceof TestCase) {
                        
-                       ImageDescriptor descriptor = ImageDescriptor.createFromURL(new URL("file://" + iconsPath + "//" + icon));
-                       image = descriptor.createImage();
+                       TestCase test = (TestCase)element;
                        
-               } catch (MalformedURLException e) {
+                       descriptor = PHPUnitImages.DESC_TEST_PASS;
                        
-                       e.printStackTrace();
-                       image = ImageDescriptor.getMissingImageDescriptor().createImage();
+                       if(test.isError())
+                               descriptor = PHPUnitImages.DESC_TEST_ERROR;
+                       else if(test.isFailure())
+                               descriptor = PHPUnitImages.DESC_TEST_FAILURE;
+                       else if(test.isPass())
+                               descriptor = PHPUnitImages.DESC_TEST_PASS;
+                                       
                }
                
-               
-               
-               
+               image = descriptor.createImage();
+       
                
                return image;
                
index 5d5891a..eba77d7 100644 (file)
@@ -86,6 +86,11 @@ public class TestCase {
        public void setVerdict(String string) {
                verdict = string;
                
+               if(isFailure())
+                       parentSuite.setHasFailure();
+               if(isError())
+                       parentSuite.setHasError();
+               
        }
 
        /**
index 9d565da..3c4562c 100644 (file)
@@ -18,6 +18,9 @@ public class TestSuite {
 
 
 
+       private boolean hasFailure;
+       private boolean isAllPass;
+       private boolean hasError;
        private Vector testCases; // current or actual.
        private Vector testSuites; // current or actual.
        private String name;
@@ -26,22 +29,7 @@ public class TestSuite {
        private int numTestCasesRunSoFar;
        TestSuite parent;
 
-       /**
-        * @param name
-        * @param testID
-        * @param testCount
-        */
-       public TestSuite(String name, String testID, int testCount) {
-               
-               this.parent = null;
-               this.id = testID;
-               this.name = name;
-               this.numTestCasesExpected = testCount;
-               
-               testCases = new Vector();
-               testSuites = new Vector();
-               
-       }
+
 
        /**
         * @param name
@@ -57,6 +45,10 @@ public class TestSuite {
        
                testCases = new Vector();
                testSuites = new Vector();
+               
+               hasError = false;
+               isAllPass = true;
+               hasFailure = false;
        }
                        
        public void addTestCase(TestCase test) {
@@ -203,4 +195,51 @@ public class TestSuite {
                testSuites = vector;
        }
 
+       /**
+        * @return
+        */
+       public boolean hasError() {
+
+               return hasError;                
+
+       }
+       
+       public void setHasError() {
+               
+               if(hasError)
+                       return;
+                       
+               hasError = true;
+               
+               if(parent != null)
+                       parent.setHasError();
+       }
+
+
+       /**
+        * @return
+        */
+       public boolean hasFailure() {
+
+               return hasFailure;              
+
+       }
+       
+       public void setHasFailure() {
+               
+               if(hasFailure)
+                       return;
+                       
+               hasFailure = true;
+               
+               if(parent != null)
+                       parent.setHasFailure();
+       }
+       
+
+       public boolean isAllPass() {
+               
+               return !hasError() && !hasFailure();
+               
+       }       
 }