KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > ui > JUnitPlugin


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Julien Ruaux: jruaux@octo.com
11  * Vincent Massol: vmassol@octo.com
12  * David Saff (saff@mit.edu) - bug 102632: [JUnit] Support for JUnit 4.
13  *******************************************************************************/

14
15 package org.eclipse.jdt.internal.junit.ui;
16
17 import java.io.File JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
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 /**
54  * The plug-in runtime class for the JUnit plug-in.
55  */

56 public class JUnitPlugin extends AbstractUIPlugin {
57     
58     /**
59      * The single instance of this plug-in runtime class.
60      */

61     private static JUnitPlugin fgPlugin= null;
62
63     public static final String JavaDoc PLUGIN_ID= "org.eclipse.jdt.junit"; //$NON-NLS-1$
64
public static final String JavaDoc ID_EXTENSION_POINT_TESTRUN_LISTENERS= PLUGIN_ID + "." + "testRunListeners"; //$NON-NLS-1$ //$NON-NLS-2$
65
public static final String JavaDoc ID_EXTENSION_POINT_JUNIT_LAUNCHCONFIGS= PLUGIN_ID + "." + "junitLaunchConfigs"; //$NON-NLS-1$ //$NON-NLS-2$
66
public static final String JavaDoc ID_EXTENSION_POINT_TEST_KINDS= PLUGIN_ID + "." + "internal_testKinds"; //$NON-NLS-1$ //$NON-NLS-2$
67

68     public final static String JavaDoc TEST_SUPERCLASS_NAME= "junit.framework.TestCase"; //$NON-NLS-1$
69
public final static String JavaDoc TEST_INTERFACE_NAME= "junit.framework.Test"; //$NON-NLS-1$
70

71     public final static String JavaDoc JUNIT4_ANNOTATION_NAME= "org.junit.Test"; //$NON-NLS-1$
72
public static final String JavaDoc SIMPLE_TEST_INTERFACE_NAME= "Test"; //$NON-NLS-1$
73

74     /**
75      * The class path variable referring to the junit home location
76      */

77     public final static String JavaDoc JUNIT_HOME= "JUNIT_HOME"; //$NON-NLS-1$
78

79     /**
80      * The class path variable referring to the junit source location
81      * @since 3.2
82      */

83     public static final String JavaDoc JUNIT_SRC_HOME= "JUNIT_SRC_HOME"; //$NON-NLS-1$
84

85     private static final IPath ICONS_PATH= new Path("$nl$/icons/full"); //$NON-NLS-1$
86
private static final String JavaDoc HISTORY_DIR_NAME= "history"; //$NON-NLS-1$
87

88     private final JUnitModel fJUnitModel= new JUnitModel();
89     
90
91     /**
92      * List storing the registered test run listeners
93      */

94     private List JavaDoc/*<ITestRunListener>*/ fLegacyTestRunListeners;
95     
96     /**
97      * List storing the registered test run listeners
98      */

99     private ListenerList/*<TestRunListener>*/ fNewTestRunListeners;
100
101     /**
102      * List storing the registered JUnit launch configuration types
103      */

104     private List JavaDoc 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     /**
128      * Returns the active workbench window
129      *
130      * @return the active workbench window
131      */

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 JavaDoc getPluginId() {
149         return PLUGIN_ID;
150     }
151
152     public static void log(Throwable JavaDoc e) {
153         log(new Status(IStatus.ERROR, getPluginId(), IStatus.ERROR, "Error", e)); //$NON-NLS-1$
154
}
155
156     public static void log(IStatus status) {
157         getDefault().getLog().log(status);
158     }
159
160     public static ImageDescriptor getImageDescriptor(String JavaDoc relativePath) {
161         IPath path= ICONS_PATH.append(relativePath);
162         return createImageDescriptor(getDefault().getBundle(), path, true);
163     }
164     
165     /**
166      * Sets the three image descriptors for enabled, disabled, and hovered to an action. The actions
167      * are retrieved from the *lcl16 folders.
168      *
169      * @param action the action
170      * @param iconName the icon name
171      */

172     public static void setLocalImageDescriptors(IAction action, String JavaDoc iconName) {
173         setImageDescriptors(action, "lcl16", iconName); //$NON-NLS-1$
174
}
175
176     private static void setImageDescriptors(IAction action, String JavaDoc type, String JavaDoc relPath) {
177         ImageDescriptor id= createImageDescriptor("d" + type, relPath, false); //$NON-NLS-1$
178
if (id != null)
179             action.setDisabledImageDescriptor(id);
180     
181         ImageDescriptor descriptor= createImageDescriptor("e" + type, relPath, true); //$NON-NLS-1$
182
action.setHoverImageDescriptor(descriptor);
183         action.setImageDescriptor(descriptor);
184     }
185     
186     /*
187      * Creates an image descriptor for the given prefix and name in the JDT UI bundle. The path can
188      * contain variables like $NL$.
189      * If no image could be found, <code>useMissingImageDescriptor</code> decides if either
190      * the 'missing image descriptor' is returned or <code>null</code>.
191      * or <code>null</code>.
192      */

193     private static ImageDescriptor createImageDescriptor(String JavaDoc pathPrefix, String JavaDoc imageName, boolean useMissingImageDescriptor) {
194         IPath path= ICONS_PATH.append(pathPrefix).append(imageName);
195         return createImageDescriptor(JUnitPlugin.getDefault().getBundle(), path, useMissingImageDescriptor);
196     }
197     
198     /**
199      * Creates an image descriptor for the given path in a bundle. The path can
200      * contain variables like $NL$. If no image could be found,
201      * <code>useMissingImageDescriptor</code> decides if either the 'missing
202      * image descriptor' is returned or <code>null</code>.
203      *
204      * @param bundle
205      * @param path
206      * @param useMissingImageDescriptor
207      * @return an {@link ImageDescriptor}, or <code>null</code> iff there's
208      * no image at the given location and
209      * <code>useMissingImageDescriptor</code> is <code>true</code>
210      */

211     private static ImageDescriptor createImageDescriptor(Bundle bundle, IPath path, boolean useMissingImageDescriptor) {
212         URL JavaDoc 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     /**
223      * @see AbstractUIPlugin#start(BundleContext)
224      */

225     public void start(BundleContext context) throws Exception JavaDoc {
226         super.start(context);
227         fBundleContext= context;
228         fJUnitModel.start();
229     }
230
231     /**
232      * @see AbstractUIPlugin#stop(BundleContext)
233      */

234     public void stop(BundleContext context) throws Exception JavaDoc {
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     /**
249      * Initializes TestRun Listener extensions
250      * @deprecated
251      */

252     private void loadTestRunListeners() {
253         fLegacyTestRunListeners= new ArrayList JavaDoc();
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); //$NON-NLS-1$
260

261         for (int i= 0; i < configs.length; i++) {
262             try {
263                 Object JavaDoc testRunListener= configs[i].createExecutableExtension("class"); //$NON-NLS-1$
264
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     /**
277      * Loads the registered JUnit launch configurations
278      */

279     private void loadLaunchConfigTypeIDs() {
280         fJUnitLaunchConfigTypeIDs= new ArrayList JavaDoc();
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 JavaDoc configTypeID= configs[i].getAttribute("configTypeID"); //$NON-NLS-1$
289
fJUnitLaunchConfigTypeIDs.add(configTypeID);
290         }
291     }
292
293     /**
294      * @return an array of all TestRun listeners
295      * @deprecated
296      */

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     /**
305      * @return a list of all JUnit launch configuration types
306      */

307     public List JavaDoc/*<String>*/ getJUnitLaunchConfigTypeIDs() {
308         if (fJUnitLaunchConfigTypeIDs == null) {
309             loadLaunchConfigTypeIDs();
310         }
311         return fJUnitLaunchConfigTypeIDs;
312     }
313
314     /**
315      * Returns the bundle for a given bundle name,
316      * regardless whether the bundle is resolved or not.
317      *
318      * @param bundleName the bundle name
319      * @return the bundle
320      * @since 3.2
321      */

322     public Bundle getBundle(String JavaDoc bundleName) {
323         Bundle[] bundles= getBundles(bundleName, null);
324         if (bundles != null && bundles.length > 0)
325             return bundles[0];
326         return null;
327     }
328     
329     /**
330      * Returns the bundles for a given bundle name,
331      *
332      * @param bundleName the bundle name
333      * @return the bundles of the given name
334      */

335     public Bundle[] getBundles(String JavaDoc bundleName, String JavaDoc version) {
336         Bundle[] bundles= Platform.getBundles(bundleName, version);
337         if (bundles != null)
338             return bundles;
339         
340         // Accessing unresolved bundle
341
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     /**
350      * Adds a TestRun listener to the collection of listeners
351      * @param newListener the listener to add
352      * @deprecated
353      */

354     public void addTestRunListener(org.eclipse.jdt.junit.ITestRunListener newListener) {
355         if (fLegacyTestRunListeners == null)
356             loadTestRunListeners();
357         
358         for (Iterator JavaDoc iter= fLegacyTestRunListeners.iterator(); iter.hasNext();) {
359             Object JavaDoc o= iter.next();
360             if (o == newListener)
361                 return;
362         }
363         fLegacyTestRunListeners.add(newListener);
364     }
365
366     /**
367      * Removes a TestRun listener to the collection of listeners
368      * @param newListener the listener to remove
369      * @deprecated
370      */

371     public void removeTestRunListener(org.eclipse.jdt.junit.ITestRunListener newListener) {
372         if (fLegacyTestRunListeners != null)
373             fLegacyTestRunListeners.remove(newListener);
374     }
375     
376     /**
377      * @return a <code>ListenerList</code> of all <code>TestRunListener</code>s
378      */

379     public ListenerList/*<TestRunListener>*/ getNewTestRunListeners() {
380         return fNewTestRunListeners;
381     }
382
383     public static boolean isStopped() {
384         return fIsStopped;
385     }
386     
387     public IDialogSettings getDialogSettingsSection(String JavaDoc 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 JavaDoc getHistoryDirectory() throws IllegalStateException JavaDoc {
397         File JavaDoc 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