KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > launcher > JUnitLaunchShortcut


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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  * David Saff (saff@mit.edu) - bug 102632: [JUnit] Support for JUnit 4.
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.junit.launcher;
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21
22 import org.eclipse.jface.dialogs.MessageDialog;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.window.Window;
26
27 import org.eclipse.ui.IEditorInput;
28 import org.eclipse.ui.IEditorPart;
29 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
30
31 import org.eclipse.debug.core.DebugPlugin;
32 import org.eclipse.debug.core.ILaunchConfiguration;
33 import org.eclipse.debug.core.ILaunchConfigurationType;
34 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
35 import org.eclipse.debug.core.ILaunchManager;
36
37 import org.eclipse.debug.ui.DebugUITools;
38 import org.eclipse.debug.ui.IDebugModelPresentation;
39 import org.eclipse.debug.ui.ILaunchShortcut;
40
41 import org.eclipse.jdt.core.IJavaElement;
42 import org.eclipse.jdt.core.IJavaProject;
43 import org.eclipse.jdt.core.IMethod;
44 import org.eclipse.jdt.core.IType;
45
46 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
47
48 import org.eclipse.jdt.ui.JavaElementLabelProvider;
49 import org.eclipse.jdt.ui.JavaElementLabels;
50
51 import org.eclipse.jdt.internal.junit.ui.JUnitMessages;
52 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin;
53 import org.eclipse.jdt.internal.junit.util.TestSearchEngine;
54
55 public class JUnitLaunchShortcut implements ILaunchShortcut {
56     public class LaunchCancelledByUserException extends Exception JavaDoc {
57         private static final long serialVersionUID= 1L;
58     }
59
60     /**
61      * @see ILaunchShortcut#launch(IEditorPart, String)
62      */

63     public void launch(IEditorPart editor, String JavaDoc mode) {
64         IJavaElement element= null;
65         IEditorInput input= editor.getEditorInput();
66         element= (IJavaElement) input.getAdapter(IJavaElement.class);
67
68         if (element != null) {
69             searchAndLaunch(new Object JavaDoc[] { element }, mode);
70         }
71     }
72
73     /**
74      * @see ILaunchShortcut#launch(ISelection, String)
75      */

76     public void launch(ISelection selection, String JavaDoc mode) {
77         if (selection instanceof IStructuredSelection) {
78             searchAndLaunch( ((IStructuredSelection) selection).toArray(), mode);
79         }
80     }
81
82     protected void searchAndLaunch(Object JavaDoc[] search, String JavaDoc mode) {
83         try {
84             if (search != null) {
85                 if (search.length == 0) {
86                     MessageDialog.openInformation(JUnitPlugin.getActiveWorkbenchShell(), JUnitMessages.LaunchTestAction_dialog_title,
87                             JUnitMessages.LaunchTestAction_message_notests);
88                     return;
89                 }
90                 if (search[0] instanceof IJavaElement) {
91                     IJavaElement element= (IJavaElement) search[0];
92                     if (element.getElementType() < IJavaElement.COMPILATION_UNIT) {
93                         launch(mode, describeContainerLaunch(element));
94                         return;
95                     }
96                     if (element.getElementType() == IJavaElement.METHOD) {
97                         launch(mode, describeMethodLaunch( ((IMethod) element)));
98                         return;
99                     }
100                     // launch a CU or type
101
launchType(element, mode);
102                 }
103             }
104         } catch (LaunchCancelledByUserException e) {
105             // OK, silently move on
106
}
107     }
108
109     protected void launchType(IJavaElement search, String JavaDoc mode) {
110         IType[] types= null;
111         try {
112             types= TestSearchEngine.findTests(new Object JavaDoc[] { search });
113         } catch (InterruptedException JavaDoc e) {
114             JUnitPlugin.log(e);
115             return;
116         } catch (InvocationTargetException JavaDoc e) {
117             JUnitPlugin.log(e);
118             return;
119         }
120         IType type= null;
121         if (types.length == 0) {
122             MessageDialog.openInformation(JUnitPlugin.getActiveWorkbenchShell(), JUnitMessages.LaunchTestAction_dialog_title,
123                     JUnitMessages.LaunchTestAction_message_notests);
124         } else if (types.length > 1) {
125             type= chooseType(types, mode);
126         } else {
127             type= types[0];
128         }
129         if (type != null) {
130             try {
131                 launch(mode, describeTypeLaunch(type));
132             } catch (LaunchCancelledByUserException e) {
133                 // OK, silently move on
134
}
135         }
136     }
137
138     private void launch(String JavaDoc mode, JUnitLaunchDescription description) throws LaunchCancelledByUserException {
139         ILaunchConfiguration config= findOrCreateLaunchConfiguration(mode, this, description);
140
141         if (config != null) {
142             DebugUITools.launch(config, mode);
143         }
144     }
145
146     public JUnitLaunchDescription describeContainerLaunch(IJavaElement container) {
147         JUnitLaunchDescription description= new JUnitLaunchDescription(container, getContainerLabel(container));
148         description.setContainer(container.getHandleIdentifier());
149         description.setTestKind(TestKindRegistry.getContainerTestKindId(container));
150         return description;
151     }
152
153     protected String JavaDoc getContainerLabel(IJavaElement container) {
154         String JavaDoc name= JavaElementLabels.getTextLabel(container, JavaElementLabels.ALL_FULLY_QUALIFIED);
155         return name.substring(name.lastIndexOf(IPath.SEPARATOR) + 1);
156     }
157
158     public JUnitLaunchDescription describeTypeLaunch(IType type) {
159         JUnitLaunchDescription description= new JUnitLaunchDescription(type, type.getElementName());
160         description.setMainType(type);
161         description.setTestKind(TestKindRegistry.getContainerTestKindId(type));
162         return description;
163     }
164
165     public JUnitLaunchDescription describeMethodLaunch(IMethod method) {
166         IType declaringType= method.getDeclaringType();
167
168         String JavaDoc name= declaringType.getElementName() + "." + method.getElementName(); //$NON-NLS-1$
169
JUnitLaunchDescription description= new JUnitLaunchDescription(method, name);
170         description.setMainType(declaringType);
171         description.setTestName(method.getElementName());
172         description.setTestKind(TestKindRegistry.getContainerTestKindId(method));
173         return description;
174     }
175
176     public ILaunchConfiguration findOrCreateLaunchConfiguration(String JavaDoc mode, JUnitLaunchShortcut registry, JUnitLaunchDescription description)
177             throws LaunchCancelledByUserException {
178         ILaunchConfiguration config= registry.findLaunchConfiguration(mode, description);
179
180         if (config == null) {
181             config= registry.createConfiguration(description);
182         }
183         return config;
184     }
185
186     /**
187      * Prompts the user to select a type
188      *
189      * @param types
190      * @param mode
191      * @return the selected type or <code>null</code> if none.
192      */

193     protected IType chooseType(IType[] types, String JavaDoc mode) {
194         ElementListSelectionDialog dialog= new ElementListSelectionDialog(JUnitPlugin.getActiveWorkbenchShell(), new JavaElementLabelProvider(
195                 JavaElementLabelProvider.SHOW_POST_QUALIFIED));
196         dialog.setElements(types);
197         dialog.setTitle(JUnitMessages.LaunchTestAction_dialog_title2);
198         if (mode.equals(ILaunchManager.DEBUG_MODE)) {
199             dialog.setMessage(JUnitMessages.LaunchTestAction_message_selectTestToRun);
200         } else {
201             dialog.setMessage(JUnitMessages.LaunchTestAction_message_selectTestToDebug);
202         }
203         dialog.setMultipleSelection(false);
204         if (dialog.open() == Window.OK) {
205             return (IType) dialog.getFirstResult();
206         }
207         return null;
208     }
209
210     protected ILaunchManager getLaunchManager() {
211         return DebugPlugin.getDefault().getLaunchManager();
212     }
213
214     /**
215      * Show a selection dialog that allows the user to choose one of the
216      * specified launch configurations. Return the chosen config, or
217      * <code>null</code> if the user cancelled the dialog.
218      *
219      * @param configList
220      * @param mode
221      * @return ILaunchConfiguration
222      * @throws LaunchCancelledByUserException
223      */

224     protected ILaunchConfiguration chooseConfiguration(List JavaDoc configList, String JavaDoc mode) throws LaunchCancelledByUserException {
225         IDebugModelPresentation labelProvider= DebugUITools.newDebugModelPresentation();
226         ElementListSelectionDialog dialog= new ElementListSelectionDialog(JUnitPlugin.getActiveWorkbenchShell(), labelProvider);
227         dialog.setElements(configList.toArray());
228         dialog.setTitle(JUnitMessages.LaunchTestAction_message_selectConfiguration);
229         if (mode.equals(ILaunchManager.DEBUG_MODE)) {
230             dialog.setMessage(JUnitMessages.LaunchTestAction_message_selectDebugConfiguration);
231         } else {
232             dialog.setMessage(JUnitMessages.LaunchTestAction_message_selectRunConfiguration);
233         }
234         dialog.setMultipleSelection(false);
235         int result= dialog.open();
236         labelProvider.dispose();
237         if (result == Window.OK) {
238             return (ILaunchConfiguration) dialog.getFirstResult();
239         }
240         throw new LaunchCancelledByUserException();
241     }
242
243     public ILaunchConfiguration createConfiguration(JUnitLaunchDescription description) {
244         String JavaDoc mainType= description.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME);
245         String JavaDoc testName= description.getAttribute(JUnitBaseLaunchConfiguration.TESTNAME_ATTR);
246         ILaunchConfiguration config= createConfiguration(description.getProject(), description.getName(), mainType, description.getContainer(), testName);
247         
248         try {
249             ILaunchConfigurationWorkingCopy wc= config.getWorkingCopy();
250             String JavaDoc testKind= description.getAttribute(JUnitBaseLaunchConfiguration.TEST_KIND_ATTR);
251             wc.setAttribute(JUnitBaseLaunchConfiguration.TEST_KIND_ATTR, testKind);
252             config= wc.doSave();
253         } catch (CoreException ce) {
254             JUnitPlugin.log(ce);
255         }
256         return config;
257     }
258
259     /*
260      * Overridden by org.eclipse.pde.internal.ui.launcher.JUnitWorkbenchShortcut; don't remove!
261      */

262     protected ILaunchConfiguration createConfiguration(IJavaProject project, String JavaDoc name, String JavaDoc mainType, String JavaDoc container, String JavaDoc testName) {
263         ILaunchConfiguration config= null;
264         try {
265             ILaunchConfigurationWorkingCopy wc= newWorkingCopy(name);
266             wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, mainType);
267             wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, project.getElementName());
268             wc.setAttribute(JUnitBaseLaunchConfiguration.ATTR_KEEPRUNNING, false);
269             wc.setAttribute(JUnitBaseLaunchConfiguration.LAUNCH_CONTAINER_ATTR, container);
270             if (testName.length() > 0)
271                 wc.setAttribute(JUnitBaseLaunchConfiguration.TESTNAME_ATTR, testName);
272             AssertionVMArg.setArgDefault(wc);
273             config= wc.doSave();
274         } catch (CoreException ce) {
275             JUnitPlugin.log(ce);
276         }
277         return config;
278     }
279
280     protected ILaunchConfigurationWorkingCopy newWorkingCopy(String JavaDoc name) throws CoreException {
281         ILaunchConfigurationType configType= getJUnitLaunchConfigType();
282         return configType.newInstance(null, getLaunchManager().generateUniqueLaunchConfigurationNameFrom(name));
283     }
284
285     /**
286      * Returns the local java launch config type
287      *
288      * @return ILaunchConfigurationType
289      */

290     protected ILaunchConfigurationType getJUnitLaunchConfigType() {
291         ILaunchManager lm= getLaunchManager();
292         return lm.getLaunchConfigurationType(JUnitLaunchConfiguration.ID_JUNIT_APPLICATION);
293     }
294
295     public ILaunchConfiguration findLaunchConfiguration(String JavaDoc mode, JUnitLaunchDescription description) throws LaunchCancelledByUserException {
296         ILaunchConfigurationType configType= getJUnitLaunchConfigType();
297         List JavaDoc candidateConfigs= Collections.EMPTY_LIST;
298         try {
299             ILaunchConfiguration[] configs= getLaunchManager().getLaunchConfigurations(configType);
300             candidateConfigs= new ArrayList JavaDoc(configs.length);
301             for (int i= 0; i < configs.length; i++) {
302                 ILaunchConfiguration config= configs[i];
303                 if (description.attributesMatch(config)) {
304                     candidateConfigs.add(config);
305                 }
306             }
307         } catch (CoreException e) {
308             JUnitPlugin.log(e);
309         }
310
311         // If there are no existing configs associated with the IType, create
312
// one.
313
// If there is exactly one config associated with the IType, return it.
314
// Otherwise, if there is more than one config associated with the
315
// IType, prompt the
316
// user to choose one.
317
int candidateCount= candidateConfigs.size();
318         if (candidateCount < 1) {
319             return null;
320         } else if (candidateCount == 1) {
321             return (ILaunchConfiguration) candidateConfigs.get(0);
322         } else {
323             // Prompt the user to choose a config. A null result means the user
324
// cancelled the dialog, in which case this method returns null,
325
// since cancelling the dialog should also cancel launching
326
// anything.
327
ILaunchConfiguration config= chooseConfiguration(candidateConfigs, mode);
328             if (config != null) {
329                 return config;
330             }
331         }
332         return null;
333     }
334 }
335
Popular Tags