1 14 15 package org.eclipse.jdt.internal.junit.ui; 16 17 import java.io.File ; 18 import java.net.URL ; 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.FileLocator; 25 import org.eclipse.core.runtime.IConfigurationElement; 26 import org.eclipse.core.runtime.IExtensionPoint; 27 import org.eclipse.core.runtime.IPath; 28 import org.eclipse.core.runtime.IStatus; 29 import org.eclipse.core.runtime.ListenerList; 30 import org.eclipse.core.runtime.MultiStatus; 31 import org.eclipse.core.runtime.Path; 32 import org.eclipse.core.runtime.Platform; 33 import org.eclipse.core.runtime.Status; 34 35 import org.eclipse.swt.widgets.Shell; 36 37 import org.eclipse.jface.action.IAction; 38 import org.eclipse.jface.dialogs.IDialogSettings; 39 import org.eclipse.jface.resource.ImageDescriptor; 40 41 import org.eclipse.ui.IWorkbench; 42 import org.eclipse.ui.IWorkbenchPage; 43 import org.eclipse.ui.IWorkbenchWindow; 44 import org.eclipse.ui.plugin.AbstractUIPlugin; 45 46 import org.eclipse.jdt.internal.junit.model.JUnitModel; 47 48 import org.osgi.framework.Bundle; 49 import org.osgi.framework.BundleContext; 50 import org.osgi.framework.ServiceReference; 51 import org.osgi.service.packageadmin.PackageAdmin; 52 53 56 public class JUnitPlugin extends AbstractUIPlugin { 57 58 61 private static JUnitPlugin fgPlugin= null; 62 63 public static final String PLUGIN_ID= "org.eclipse.jdt.junit"; public static final String ID_EXTENSION_POINT_TESTRUN_LISTENERS= PLUGIN_ID + "." + "testRunListeners"; public static final String ID_EXTENSION_POINT_JUNIT_LAUNCHCONFIGS= PLUGIN_ID + "." + "junitLaunchConfigs"; public static final String ID_EXTENSION_POINT_TEST_KINDS= PLUGIN_ID + "." + "internal_testKinds"; 68 public final static String TEST_SUPERCLASS_NAME= "junit.framework.TestCase"; public final static String TEST_INTERFACE_NAME= "junit.framework.Test"; 71 public final static String JUNIT4_ANNOTATION_NAME= "org.junit.Test"; public static final String SIMPLE_TEST_INTERFACE_NAME= "Test"; 74 77 public final static String JUNIT_HOME= "JUNIT_HOME"; 79 83 public static final String JUNIT_SRC_HOME= "JUNIT_SRC_HOME"; 85 private static final IPath ICONS_PATH= new Path("$nl$/icons/full"); private static final String HISTORY_DIR_NAME= "history"; 88 private final JUnitModel fJUnitModel= new JUnitModel(); 89 90 91 94 private List fLegacyTestRunListeners; 95 96 99 private ListenerList fNewTestRunListeners; 100 101 104 private List fJUnitLaunchConfigTypeIDs; 105 106 private BundleContext fBundleContext; 107 108 private static boolean fIsStopped= false; 109 110 111 public JUnitPlugin() { 112 fgPlugin= this; 113 fNewTestRunListeners= new ListenerList(); 114 } 115 116 public static JUnitPlugin getDefault() { 117 return fgPlugin; 118 } 119 120 public static Shell getActiveWorkbenchShell() { 121 IWorkbenchWindow workBenchWindow= getActiveWorkbenchWindow(); 122 if (workBenchWindow == null) 123 return null; 124 return workBenchWindow.getShell(); 125 } 126 127 132 public static IWorkbenchWindow getActiveWorkbenchWindow() { 133 if (fgPlugin == null) 134 return null; 135 IWorkbench workBench= fgPlugin.getWorkbench(); 136 if (workBench == null) 137 return null; 138 return workBench.getActiveWorkbenchWindow(); 139 } 140 141 public static IWorkbenchPage getActivePage() { 142 IWorkbenchWindow activeWorkbenchWindow= getActiveWorkbenchWindow(); 143 if (activeWorkbenchWindow == null) 144 return null; 145 return activeWorkbenchWindow.getActivePage(); 146 } 147 148 public static String getPluginId() { 149 return PLUGIN_ID; 150 } 151 152 public static void log(Throwable e) { 153 log(new Status(IStatus.ERROR, getPluginId(), IStatus.ERROR, "Error", e)); } 155 156 public static void log(IStatus status) { 157 getDefault().getLog().log(status); 158 } 159 160 public static ImageDescriptor getImageDescriptor(String relativePath) { 161 IPath path= ICONS_PATH.append(relativePath); 162 return createImageDescriptor(getDefault().getBundle(), path, true); 163 } 164 165 172 public static void setLocalImageDescriptors(IAction action, String iconName) { 173 setImageDescriptors(action, "lcl16", iconName); } 175 176 private static void setImageDescriptors(IAction action, String type, String relPath) { 177 ImageDescriptor id= createImageDescriptor("d" + type, relPath, false); if (id != null) 179 action.setDisabledImageDescriptor(id); 180 181 ImageDescriptor descriptor= createImageDescriptor("e" + type, relPath, true); action.setHoverImageDescriptor(descriptor); 183 action.setImageDescriptor(descriptor); 184 } 185 186 193 private static ImageDescriptor createImageDescriptor(String pathPrefix, String imageName, boolean useMissingImageDescriptor) { 194 IPath path= ICONS_PATH.append(pathPrefix).append(imageName); 195 return createImageDescriptor(JUnitPlugin.getDefault().getBundle(), path, useMissingImageDescriptor); 196 } 197 198 211 private static ImageDescriptor createImageDescriptor(Bundle bundle, IPath path, boolean useMissingImageDescriptor) { 212 URL url= FileLocator.find(bundle, path, null); 213 if (url != null) { 214 return ImageDescriptor.createFromURL(url); 215 } 216 if (useMissingImageDescriptor) { 217 return ImageDescriptor.getMissingImageDescriptor(); 218 } 219 return null; 220 } 221 222 225 public void start(BundleContext context) throws Exception { 226 super.start(context); 227 fBundleContext= context; 228 fJUnitModel.start(); 229 } 230 231 234 public void stop(BundleContext context) throws Exception { 235 fIsStopped= true; 236 try { 237 fJUnitModel.stop(); 238 } finally { 239 super.stop(context); 240 } 241 fBundleContext= null; 242 } 243 244 public static JUnitModel getModel() { 245 return getDefault().fJUnitModel; 246 } 247 248 252 private void loadTestRunListeners() { 253 fLegacyTestRunListeners= new ArrayList (); 254 IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(ID_EXTENSION_POINT_TESTRUN_LISTENERS); 255 if (extensionPoint == null) { 256 return; 257 } 258 IConfigurationElement[] configs= extensionPoint.getConfigurationElements(); 259 MultiStatus status= new MultiStatus(PLUGIN_ID, IStatus.OK, "Could not load some testRunner extension points", null); 261 for (int i= 0; i < configs.length; i++) { 262 try { 263 Object testRunListener= configs[i].createExecutableExtension("class"); if (testRunListener instanceof org.eclipse.jdt.junit.ITestRunListener) { 265 fLegacyTestRunListeners.add(testRunListener); 266 } 267 } catch (CoreException e) { 268 status.add(e.getStatus()); 269 } 270 } 271 if (!status.isOK()) { 272 JUnitPlugin.log(status); 273 } 274 } 275 276 279 private void loadLaunchConfigTypeIDs() { 280 fJUnitLaunchConfigTypeIDs= new ArrayList (); 281 IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(ID_EXTENSION_POINT_JUNIT_LAUNCHCONFIGS); 282 if (extensionPoint == null) { 283 return; 284 } 285 IConfigurationElement[] configs= extensionPoint.getConfigurationElements(); 286 287 for (int i= 0; i < configs.length; i++) { 288 String configTypeID= configs[i].getAttribute("configTypeID"); fJUnitLaunchConfigTypeIDs.add(configTypeID); 290 } 291 } 292 293 297 public org.eclipse.jdt.junit.ITestRunListener[] getTestRunListeners() { 298 if (fLegacyTestRunListeners == null) { 299 loadTestRunListeners(); 300 } 301 return (org.eclipse.jdt.junit.ITestRunListener[]) fLegacyTestRunListeners.toArray(new org.eclipse.jdt.junit.ITestRunListener[fLegacyTestRunListeners.size()]); 302 } 303 304 307 public List getJUnitLaunchConfigTypeIDs() { 308 if (fJUnitLaunchConfigTypeIDs == null) { 309 loadLaunchConfigTypeIDs(); 310 } 311 return fJUnitLaunchConfigTypeIDs; 312 } 313 314 322 public Bundle getBundle(String bundleName) { 323 Bundle[] bundles= getBundles(bundleName, null); 324 if (bundles != null && bundles.length > 0) 325 return bundles[0]; 326 return null; 327 } 328 329 335 public Bundle[] getBundles(String bundleName, String version) { 336 Bundle[] bundles= Platform.getBundles(bundleName, version); 337 if (bundles != null) 338 return bundles; 339 340 ServiceReference serviceRef= fBundleContext.getServiceReference(PackageAdmin.class.getName()); 342 PackageAdmin admin= (PackageAdmin)fBundleContext.getService(serviceRef); 343 bundles= admin.getBundles(bundleName, version); 344 if (bundles != null && bundles.length > 0) 345 return bundles; 346 return null; 347 } 348 349 354 public void addTestRunListener(org.eclipse.jdt.junit.ITestRunListener newListener) { 355 if (fLegacyTestRunListeners == null) 356 loadTestRunListeners(); 357 358 for (Iterator iter= fLegacyTestRunListeners.iterator(); iter.hasNext();) { 359 Object o= iter.next(); 360 if (o == newListener) 361 return; 362 } 363 fLegacyTestRunListeners.add(newListener); 364 } 365 366 371 public void removeTestRunListener(org.eclipse.jdt.junit.ITestRunListener newListener) { 372 if (fLegacyTestRunListeners != null) 373 fLegacyTestRunListeners.remove(newListener); 374 } 375 376 379 public ListenerList getNewTestRunListeners() { 380 return fNewTestRunListeners; 381 } 382 383 public static boolean isStopped() { 384 return fIsStopped; 385 } 386 387 public IDialogSettings getDialogSettingsSection(String name) { 388 IDialogSettings dialogSettings= getDialogSettings(); 389 IDialogSettings section= dialogSettings.getSection(name); 390 if (section == null) { 391 section= dialogSettings.addNewSection(name); 392 } 393 return section; 394 } 395 396 public static File getHistoryDirectory() throws IllegalStateException { 397 File historyDir= getDefault().getStateLocation().append(HISTORY_DIR_NAME).toFile(); 398 if (! historyDir.isDirectory()) { 399 historyDir.mkdir(); 400 } 401 return historyDir; 402 } 403 404 } 405 | Popular Tags |