1 12 package org.eclipse.jdt.internal.junit.launcher; 13 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.ArrayList ; 16 import java.util.Collections ; 17 import java.util.List ; 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 { 57 private static final long serialVersionUID= 1L; 58 } 59 60 63 public void launch(IEditorPart editor, String 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 [] { element }, mode); 70 } 71 } 72 73 76 public void launch(ISelection selection, String mode) { 77 if (selection instanceof IStructuredSelection) { 78 searchAndLaunch( ((IStructuredSelection) selection).toArray(), mode); 79 } 80 } 81 82 protected void searchAndLaunch(Object [] search, String 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 launchType(element, mode); 102 } 103 } 104 } catch (LaunchCancelledByUserException e) { 105 } 107 } 108 109 protected void launchType(IJavaElement search, String mode) { 110 IType[] types= null; 111 try { 112 types= TestSearchEngine.findTests(new Object [] { search }); 113 } catch (InterruptedException e) { 114 JUnitPlugin.log(e); 115 return; 116 } catch (InvocationTargetException 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 } 135 } 136 } 137 138 private void launch(String 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 getContainerLabel(IJavaElement container) { 154 String 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 name= declaringType.getElementName() + "." + method.getElementName(); 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 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 193 protected IType chooseType(IType[] types, String 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 224 protected ILaunchConfiguration chooseConfiguration(List configList, String 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 mainType= description.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME); 245 String 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 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 262 protected ILaunchConfiguration createConfiguration(IJavaProject project, String name, String mainType, String container, String 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 name) throws CoreException { 281 ILaunchConfigurationType configType= getJUnitLaunchConfigType(); 282 return configType.newInstance(null, getLaunchManager().generateUniqueLaunchConfigurationNameFrom(name)); 283 } 284 285 290 protected ILaunchConfigurationType getJUnitLaunchConfigType() { 291 ILaunchManager lm= getLaunchManager(); 292 return lm.getLaunchConfigurationType(JUnitLaunchConfiguration.ID_JUNIT_APPLICATION); 293 } 294 295 public ILaunchConfiguration findLaunchConfiguration(String mode, JUnitLaunchDescription description) throws LaunchCancelledByUserException { 296 ILaunchConfigurationType configType= getJUnitLaunchConfigType(); 297 List candidateConfigs= Collections.EMPTY_LIST; 298 try { 299 ILaunchConfiguration[] configs= getLaunchManager().getLaunchConfigurations(configType); 300 candidateConfigs= new ArrayList (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 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 ILaunchConfiguration config= chooseConfiguration(candidateConfigs, mode); 328 if (config != null) { 329 return config; 330 } 331 } 332 return null; 333 } 334 } 335 | Popular Tags |