1 13 package org.eclipse.jdt.junit.launcher; 14 15 import java.lang.reflect.InvocationTargetException ; 16 import java.util.ArrayList ; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IPath; 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.core.runtime.Path; 22 23 import org.eclipse.core.resources.IProject; 24 import org.eclipse.core.resources.IResource; 25 import org.eclipse.core.resources.IWorkspaceRoot; 26 import org.eclipse.core.resources.ResourcesPlugin; 27 28 import org.eclipse.swt.SWT; 29 import org.eclipse.swt.events.ModifyEvent; 30 import org.eclipse.swt.events.ModifyListener; 31 import org.eclipse.swt.events.SelectionAdapter; 32 import org.eclipse.swt.events.SelectionEvent; 33 import org.eclipse.swt.events.SelectionListener; 34 import org.eclipse.swt.graphics.Image; 35 import org.eclipse.swt.layout.GridData; 36 import org.eclipse.swt.layout.GridLayout; 37 import org.eclipse.swt.widgets.Button; 38 import org.eclipse.swt.widgets.Composite; 39 import org.eclipse.swt.widgets.Label; 40 import org.eclipse.swt.widgets.Shell; 41 import org.eclipse.swt.widgets.Text; 42 43 import org.eclipse.jface.dialogs.Dialog; 44 import org.eclipse.jface.viewers.ArrayContentProvider; 45 import org.eclipse.jface.viewers.ComboViewer; 46 import org.eclipse.jface.viewers.ILabelProvider; 47 import org.eclipse.jface.viewers.ISelection; 48 import org.eclipse.jface.viewers.ISelectionChangedListener; 49 import org.eclipse.jface.viewers.IStructuredSelection; 50 import org.eclipse.jface.viewers.LabelProvider; 51 import org.eclipse.jface.viewers.SelectionChangedEvent; 52 import org.eclipse.jface.viewers.StructuredSelection; 53 import org.eclipse.jface.viewers.Viewer; 54 import org.eclipse.jface.viewers.ViewerFilter; 55 import org.eclipse.jface.window.Window; 56 57 import org.eclipse.ui.IEditorInput; 58 import org.eclipse.ui.IEditorPart; 59 import org.eclipse.ui.IWorkbenchPage; 60 import org.eclipse.ui.IWorkbenchWindow; 61 import org.eclipse.ui.PlatformUI; 62 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 63 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; 64 import org.eclipse.ui.dialogs.SelectionDialog; 65 66 import org.eclipse.debug.core.ILaunchConfiguration; 67 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 68 69 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab; 70 71 import org.eclipse.jdt.core.IJavaElement; 72 import org.eclipse.jdt.core.IJavaModel; 73 import org.eclipse.jdt.core.IJavaProject; 74 import org.eclipse.jdt.core.IPackageFragment; 75 import org.eclipse.jdt.core.IPackageFragmentRoot; 76 import org.eclipse.jdt.core.ISourceReference; 77 import org.eclipse.jdt.core.IType; 78 import org.eclipse.jdt.core.JavaCore; 79 import org.eclipse.jdt.core.JavaModelException; 80 81 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 82 83 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 84 85 import org.eclipse.jdt.ui.JavaElementComparator; 86 import org.eclipse.jdt.ui.JavaElementLabelProvider; 87 import org.eclipse.jdt.ui.StandardJavaElementContentProvider; 88 89 import org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator; 90 import org.eclipse.jdt.internal.ui.wizards.TypedViewerFilter; 91 92 import org.eclipse.jdt.internal.junit.Messages; 93 import org.eclipse.jdt.internal.junit.launcher.ITestKind; 94 import org.eclipse.jdt.internal.junit.launcher.JUnitLaunchConfigurationConstants; 95 import org.eclipse.jdt.internal.junit.launcher.JUnitMigrationDelegate; 96 import org.eclipse.jdt.internal.junit.launcher.TestKind; 97 import org.eclipse.jdt.internal.junit.launcher.TestKindRegistry; 98 import org.eclipse.jdt.internal.junit.launcher.TestSelectionDialog; 99 import org.eclipse.jdt.internal.junit.ui.IJUnitHelpContextIds; 100 import org.eclipse.jdt.internal.junit.ui.JUnitMessages; 101 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin; 102 import org.eclipse.jdt.internal.junit.util.LayoutUtil; 103 import org.eclipse.jdt.internal.junit.util.TestSearchEngine; 104 105 112 public class JUnitLaunchConfigurationTab extends AbstractLaunchConfigurationTab { 113 114 private Label fProjLabel; 116 private Text fProjText; 117 private Button fProjButton; 118 private Button fKeepRunning; 119 120 private Text fTestText; 122 private Button fSearchButton; 123 private final Image fTestIcon= createImage("obj16/test.gif"); private String fOriginalTestMethodName; 125 private Label fTestMethodLabel; 126 private Text fContainerText; 127 private IJavaElement fContainerElement; 128 private final ILabelProvider fJavaElementLabelProvider= new JavaElementLabelProvider(); 129 130 private Button fContainerSearchButton; 131 private Button fTestContainerRadioButton; 132 private Button fTestRadioButton; 133 private Label fTestLabel; 134 135 private ComboViewer fTestLoaderViewer; 136 137 140 public JUnitLaunchConfigurationTab() { 141 } 142 143 144 147 public void createControl(Composite parent) { 148 Composite comp = new Composite(parent, SWT.NONE); 149 setControl(comp); 150 151 GridLayout topLayout = new GridLayout(); 152 topLayout.numColumns= 3; 153 comp.setLayout(topLayout); 154 155 createSingleTestSection(comp); 156 createTestContainerSelectionGroup(comp); 157 158 createSpacer(comp); 159 160 createTestLoaderGroup(comp); 161 162 createSpacer(comp); 163 164 createKeepAliveGroup(comp); 165 Dialog.applyDialogFont(comp); 166 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJUnitHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_JUNIT_MAIN_TAB); 167 validatePage(); 168 } 169 170 private void createTestLoaderGroup(Composite comp) { 171 Label loaderLabel= new Label(comp, SWT.NONE); 172 loaderLabel.setText(JUnitMessages.JUnitLaunchConfigurationTab_Test_Loader); 173 GridData gd= new GridData(); 174 gd.horizontalIndent= 0; 175 loaderLabel.setLayoutData(gd); 176 177 fTestLoaderViewer= new ComboViewer(comp, SWT.DROP_DOWN | SWT.READ_ONLY); 178 fTestLoaderViewer.getCombo().setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 179 180 ArrayList items= TestKindRegistry.getDefault().getAllKinds(); 181 fTestLoaderViewer.setContentProvider(new ArrayContentProvider()); 182 fTestLoaderViewer.setLabelProvider(new LabelProvider() { 183 public String getText(Object element) { 184 return ((TestKind) element).getDisplayName(); 185 } 186 }); 187 fTestLoaderViewer.setInput(items); 188 fTestLoaderViewer.addSelectionChangedListener(new ISelectionChangedListener() { 189 public void selectionChanged(SelectionChangedEvent event) { 190 validatePage(); 191 updateLaunchConfigurationDialog(); 192 } 193 }); 194 } 195 196 private void createSpacer(Composite comp) { 197 Label label= new Label(comp, SWT.NONE); 198 GridData gd= new GridData(); 199 gd.horizontalSpan= 3; 200 label.setLayoutData(gd); 201 } 202 203 private void createSingleTestSection(Composite comp) { 204 fTestRadioButton= new Button(comp, SWT.RADIO); 205 fTestRadioButton.setText(JUnitMessages.JUnitLaunchConfigurationTab_label_oneTest); 206 GridData gd = new GridData(); 207 gd.horizontalSpan = 3; 208 fTestRadioButton.setLayoutData(gd); 209 fTestRadioButton.addSelectionListener(new SelectionAdapter() { 210 public void widgetSelected(SelectionEvent e) { 211 if (fTestRadioButton.getSelection()) 212 testModeChanged(); 213 } 214 }); 215 216 fProjLabel = new Label(comp, SWT.NONE); 217 fProjLabel.setText(JUnitMessages.JUnitLaunchConfigurationTab_label_project); 218 gd= new GridData(); 219 gd.horizontalIndent = 25; 220 fProjLabel.setLayoutData(gd); 221 222 fProjText= new Text(comp, SWT.SINGLE | SWT.BORDER); 223 fProjText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 224 fProjText.addModifyListener(new ModifyListener() { 225 public void modifyText(ModifyEvent evt) { 226 validatePage(); 227 updateLaunchConfigurationDialog(); 228 fSearchButton.setEnabled(fTestRadioButton.getSelection() && fProjText.getText().length() > 0); 229 } 230 }); 231 232 fProjButton = new Button(comp, SWT.PUSH); 233 fProjButton.setText(JUnitMessages.JUnitLaunchConfigurationTab_label_browse); 234 fProjButton.addSelectionListener(new SelectionAdapter() { 235 public void widgetSelected(SelectionEvent evt) { 236 handleProjectButtonSelected(); 237 } 238 }); 239 setButtonGridData(fProjButton); 240 241 fTestLabel = new Label(comp, SWT.NONE); 242 gd = new GridData(); 243 gd.horizontalIndent = 25; 244 fTestLabel.setLayoutData(gd); 245 fTestLabel.setText(JUnitMessages.JUnitLaunchConfigurationTab_label_test); 246 247 248 fTestText = new Text(comp, SWT.SINGLE | SWT.BORDER); 249 fTestText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 250 fTestText.addModifyListener(new ModifyListener() { 251 public void modifyText(ModifyEvent evt) { 252 validatePage(); 253 updateLaunchConfigurationDialog(); 254 } 255 }); 256 257 fSearchButton = new Button(comp, SWT.PUSH); 258 fSearchButton.setEnabled(fProjText.getText().length() > 0); 259 fSearchButton.setText(JUnitMessages.JUnitLaunchConfigurationTab_label_search); 260 fSearchButton.addSelectionListener(new SelectionAdapter() { 261 public void widgetSelected(SelectionEvent evt) { 262 handleSearchButtonSelected(); 263 } 264 }); 265 setButtonGridData(fSearchButton); 266 267 new Label(comp, SWT.NONE); 268 269 fTestMethodLabel= new Label(comp, SWT.NONE); 270 fTestMethodLabel.setText(""); gd= new GridData(); 272 gd.horizontalSpan = 2; 273 fTestMethodLabel.setLayoutData(gd); 274 275 } 276 277 private void createTestContainerSelectionGroup(Composite comp) { 278 fTestContainerRadioButton= new Button(comp, SWT.RADIO); 279 fTestContainerRadioButton.setText(JUnitMessages.JUnitLaunchConfigurationTab_label_containerTest); 280 GridData gd = new GridData(); 281 gd.horizontalSpan = 3; 282 fTestContainerRadioButton.setLayoutData(gd); 283 fTestContainerRadioButton.addSelectionListener(new SelectionListener() { 284 public void widgetSelected(SelectionEvent e) { 285 if (fTestContainerRadioButton.getSelection()) 286 testModeChanged(); 287 } 288 public void widgetDefaultSelected(SelectionEvent e) { 289 } 290 }); 291 292 fContainerText = new Text(comp, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY); 293 gd = new GridData(GridData.FILL_HORIZONTAL); 294 gd.horizontalIndent= 25; 295 gd.horizontalSpan = 2; 296 fContainerText.setLayoutData(gd); 297 fContainerText.addModifyListener(new ModifyListener() { 298 public void modifyText(ModifyEvent evt) { 299 updateLaunchConfigurationDialog(); 300 } 301 }); 302 303 fContainerSearchButton = new Button(comp, SWT.PUSH); 304 fContainerSearchButton.setText(JUnitMessages.JUnitLaunchConfigurationTab_label_search); 305 fContainerSearchButton.addSelectionListener(new SelectionAdapter() { 306 public void widgetSelected(SelectionEvent evt) { 307 handleContainerSearchButtonSelected(); 308 } 309 }); 310 setButtonGridData(fContainerSearchButton); 311 } 312 313 private void handleContainerSearchButtonSelected() { 314 IJavaElement javaElement= chooseContainer(fContainerElement); 315 if (javaElement != null) 316 setContainerElement(javaElement); 317 } 318 319 private void setContainerElement(IJavaElement javaElement) { 320 fContainerElement= javaElement; 321 fContainerText.setText(getPresentationName(javaElement)); 322 validatePage(); 323 updateLaunchConfigurationDialog(); 324 } 325 326 private void createKeepAliveGroup(Composite comp) { 327 GridData gd; 328 fKeepRunning = new Button(comp, SWT.CHECK); 329 fKeepRunning.addSelectionListener(new SelectionListener() { 330 public void widgetSelected(SelectionEvent e) { 331 updateLaunchConfigurationDialog(); 332 } 333 334 public void widgetDefaultSelected(SelectionEvent e) { 335 } 336 }); 337 fKeepRunning.setText(JUnitMessages.JUnitLaunchConfigurationTab_label_keeprunning); 338 gd= new GridData(); 339 gd.horizontalAlignment= GridData.FILL; 340 gd.horizontalSpan= 2; 341 fKeepRunning.setLayoutData(gd); 342 } 343 344 private static Image createImage(String path) { 345 return JUnitPlugin.getImageDescriptor(path).createImage(); 346 } 347 348 351 public void initializeFrom(ILaunchConfiguration config) { 352 updateProjectFromConfig(config); 353 String containerHandle= ""; try { 355 containerHandle = config.getAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, ""); } catch (CoreException ce) { 357 } 358 359 if (containerHandle.length() > 0) 360 updateTestContainerFromConfig(config); 361 else 362 updateTestTypeFromConfig(config); 363 updateKeepRunning(config); 364 updateTestLoaderFromConfig(config); 365 } 366 367 368 private void updateTestLoaderFromConfig(ILaunchConfiguration config) { 369 ITestKind testKind= JUnitLaunchConfigurationConstants.getTestRunnerKind(config); 370 if (testKind.isNull()) 371 testKind= TestKindRegistry.getDefault().getKind(TestKindRegistry.JUNIT3_TEST_KIND_ID); 372 fTestLoaderViewer.setSelection(new StructuredSelection(testKind)); 373 } 374 375 private TestKind getSelectedTestKind() { 376 IStructuredSelection selection= (IStructuredSelection) fTestLoaderViewer.getSelection(); 377 return (TestKind) selection.getFirstElement(); 378 } 379 380 private void updateKeepRunning(ILaunchConfiguration config) { 381 boolean running= false; 382 try { 383 running= config.getAttribute(JUnitLaunchConfigurationConstants.ATTR_KEEPRUNNING, false); 384 } catch (CoreException ce) { 385 } 386 fKeepRunning.setSelection(running); 387 } 388 389 private void updateProjectFromConfig(ILaunchConfiguration config) { 390 String projectName= ""; try { 392 projectName = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""); } catch (CoreException ce) { 394 } 395 fProjText.setText(projectName); 396 } 397 398 private void updateTestTypeFromConfig(ILaunchConfiguration config) { 399 String testTypeName= ""; fOriginalTestMethodName= ""; try { 402 testTypeName = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, ""); fOriginalTestMethodName = config.getAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_METHOD_NAME, ""); } catch (CoreException ce) { 405 } 406 fTestRadioButton.setSelection(true); 407 setEnableSingleTestGroup(true); 408 setEnableContainerTestGroup(false); 409 fTestContainerRadioButton.setSelection(false); 410 fTestText.setText(testTypeName); 411 fContainerText.setText(""); setTestMethodLabel(fOriginalTestMethodName); 413 } 414 415 private void setTestMethodLabel(String testMethodName) { 416 if (!"".equals(testMethodName)) { fTestMethodLabel.setText(JUnitMessages.JUnitLaunchConfigurationTab_label_method+fOriginalTestMethodName); 418 } else { 419 fTestMethodLabel.setText(""); } 421 } 422 423 private void updateTestContainerFromConfig(ILaunchConfiguration config) { 424 String containerHandle= ""; IJavaElement containerElement = null; 426 try { 427 containerHandle = config.getAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, ""); if (containerHandle.length() > 0) { 429 containerElement= JavaCore.create(containerHandle); 430 } 431 } catch (CoreException ce) { 432 } 433 if (containerElement != null) 434 fContainerElement = containerElement; 435 fTestContainerRadioButton.setSelection(true); 436 setEnableSingleTestGroup(false); 437 setEnableContainerTestGroup(true); 438 fTestRadioButton.setSelection(false); 439 if (fContainerElement != null) 440 fContainerText.setText(getPresentationName(fContainerElement)); 441 fTestText.setText(""); } 443 444 448 public void performApply(ILaunchConfigurationWorkingCopy config) { 449 if (fTestContainerRadioButton.getSelection() && fContainerElement != null) { 450 config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, fContainerElement.getJavaProject().getElementName()); 451 config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, fContainerElement.getHandleIdentifier()); 452 config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, ""); config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_METHOD_NAME, ""); } else { 456 config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, fProjText.getText()); 457 config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, fTestText.getText()); 458 config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, ""); config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_METHOD_NAME, fOriginalTestMethodName); 460 } 461 config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_KEEPRUNNING, fKeepRunning.getSelection()); 462 try { 463 mapResources(config); 464 } catch (CoreException e) { 465 JUnitPlugin.log(e.getStatus()); 466 } 467 IStructuredSelection testKindSelection= (IStructuredSelection) fTestLoaderViewer.getSelection(); 468 if (! testKindSelection.isEmpty()) { 469 TestKind testKind= (TestKind) testKindSelection.getFirstElement(); 470 config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_RUNNER_KIND, testKind.getId()); 471 } 472 } 473 474 private void mapResources(ILaunchConfigurationWorkingCopy config) throws CoreException { 475 JUnitMigrationDelegate.mapResources(config); 476 } 477 478 479 482 public void dispose() { 483 super.dispose(); 484 fTestIcon.dispose(); 485 fJavaElementLabelProvider.dispose(); 486 } 487 488 491 public Image getImage() { 492 return fTestIcon; 493 } 494 495 498 private void handleSearchButtonSelected() { 499 Shell shell = getShell(); 500 501 IJavaProject javaProject = getJavaProject(); 502 503 IType[] types= new IType[0]; 504 boolean[] radioSetting= new boolean[2]; 505 try { 506 radioSetting[0]= fTestRadioButton.getSelection(); 509 radioSetting[1]= fTestContainerRadioButton.getSelection(); 510 511 types= TestSearchEngine.findTests(getLaunchConfigurationDialog(), javaProject, getSelectedTestKind()); 512 } catch (InterruptedException e) { 513 setErrorMessage(e.getMessage()); 514 return; 515 } catch (InvocationTargetException e) { 516 JUnitPlugin.log(e.getTargetException()); 517 return; 518 } finally { 519 fTestRadioButton.setSelection(radioSetting[0]); 520 fTestContainerRadioButton.setSelection(radioSetting[1]); 521 } 522 523 SelectionDialog dialog = new TestSelectionDialog(shell, types); 524 dialog.setTitle(JUnitMessages.JUnitLaunchConfigurationTab_testdialog_title); 525 dialog.setMessage(JUnitMessages.JUnitLaunchConfigurationTab_testdialog_message); 526 if (dialog.open() == Window.CANCEL) { 527 return; 528 } 529 530 Object [] results = dialog.getResult(); 531 if ((results == null) || (results.length < 1)) { 532 return; 533 } 534 IType type = (IType)results[0]; 535 536 if (type != null) { 537 fTestText.setText(type.getFullyQualifiedName('.')); 538 javaProject = type.getJavaProject(); 539 fProjText.setText(javaProject.getElementName()); 540 } 541 } 542 543 548 private void handleProjectButtonSelected() { 549 IJavaProject project = chooseJavaProject(); 550 if (project == null) { 551 return; 552 } 553 554 String projectName = project.getElementName(); 555 fProjText.setText(projectName); 556 } 557 558 562 private IJavaProject chooseJavaProject() { 563 IJavaProject[] projects; 564 try { 565 projects= JavaCore.create(getWorkspaceRoot()).getJavaProjects(); 566 } catch (JavaModelException e) { 567 JUnitPlugin.log(e.getStatus()); 568 projects= new IJavaProject[0]; 569 } 570 571 ILabelProvider labelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT); 572 ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), labelProvider); 573 dialog.setTitle(JUnitMessages.JUnitLaunchConfigurationTab_projectdialog_title); 574 dialog.setMessage(JUnitMessages.JUnitLaunchConfigurationTab_projectdialog_message); 575 dialog.setElements(projects); 576 577 IJavaProject javaProject = getJavaProject(); 578 if (javaProject != null) { 579 dialog.setInitialSelections(new Object [] { javaProject }); 580 } 581 if (dialog.open() == Window.OK) { 582 return (IJavaProject) dialog.getFirstResult(); 583 } 584 return null; 585 } 586 587 591 private IJavaProject getJavaProject() { 592 String projectName = fProjText.getText().trim(); 593 if (projectName.length() < 1) { 594 return null; 595 } 596 return getJavaModel().getJavaProject(projectName); 597 } 598 599 602 private IWorkspaceRoot getWorkspaceRoot() { 603 return ResourcesPlugin.getWorkspace().getRoot(); 604 } 605 606 609 private IJavaModel getJavaModel() { 610 return JavaCore.create(getWorkspaceRoot()); 611 } 612 613 614 617 public boolean isValid(ILaunchConfiguration config) { 618 return getErrorMessage() == null; 619 } 620 621 private void testModeChanged() { 622 boolean isSingleTestMode= fTestRadioButton.getSelection(); 623 setEnableSingleTestGroup(isSingleTestMode); 624 setEnableContainerTestGroup(!isSingleTestMode); 625 if (!isSingleTestMode && fContainerText.getText().length() == 0) { 626 String projText= fProjText.getText(); 627 if (Path.EMPTY.isValidSegment(projText)) { 628 IJavaProject javaProject= getJavaModel().getJavaProject(projText); 629 if (javaProject != null && javaProject.exists()) 630 setContainerElement(javaProject); 631 } 632 } 633 validatePage(); 634 updateLaunchConfigurationDialog(); 635 } 636 637 private void validatePage() { 638 setErrorMessage(null); 639 setMessage(null); 640 641 if (fTestContainerRadioButton.getSelection()) { 642 if (fContainerElement == null) { 643 setErrorMessage(JUnitMessages.JUnitLaunchConfigurationTab_error_noContainer); 644 return; 645 } 646 validateJavaProject(fContainerElement.getJavaProject()); 647 return; 648 } 649 650 String projectName= fProjText.getText().trim(); 651 if (projectName.length() == 0) { 652 setErrorMessage(JUnitMessages.JUnitLaunchConfigurationTab_error_projectnotdefined); 653 return; 654 } 655 656 IStatus status= ResourcesPlugin.getWorkspace().validatePath(IPath.SEPARATOR + projectName, IResource.PROJECT); 657 if (!status.isOK() || !Path.ROOT.isValidSegment(projectName)) { 658 setErrorMessage(Messages.format(JUnitMessages.JUnitLaunchConfigurationTab_error_invalidProjectName, projectName)); 659 return; 660 } 661 662 IProject project= getWorkspaceRoot().getProject(projectName); 663 if (!project.exists()) { 664 setErrorMessage(JUnitMessages.JUnitLaunchConfigurationTab_error_projectnotexists); 665 return; 666 } 667 IJavaProject javaProject= JavaCore.create(project); 668 validateJavaProject(javaProject); 669 670 try { 671 if (!project.hasNature(JavaCore.NATURE_ID)) { 672 setErrorMessage(JUnitMessages.JUnitLaunchConfigurationTab_error_notJavaProject); 673 return; 674 } 675 String className= fTestText.getText().trim(); 676 if (className.length() == 0) { 677 setErrorMessage(JUnitMessages.JUnitLaunchConfigurationTab_error_testnotdefined); 678 return; 679 } 680 if (javaProject.findType(className) == null) { 681 setErrorMessage(Messages.format(JUnitMessages.JUnitLaunchConfigurationTab_error_test_class_not_found, new String [] { className, projectName })); 682 return; 683 } 684 685 686 } catch (CoreException e) { 687 JUnitPlugin.log(e); 688 } 689 690 } 691 692 private void validateJavaProject(IJavaProject javaProject) { 693 if (! TestSearchEngine.hasTestCaseType(javaProject)) { 694 setErrorMessage(JUnitMessages.JUnitLaunchConfigurationTab_error_testcasenotonpath); 695 return; 696 } 697 } 698 699 private void setEnableContainerTestGroup(boolean enabled) { 700 fContainerSearchButton.setEnabled(enabled); 701 fContainerText.setEnabled(enabled); 702 } 703 704 private void setEnableSingleTestGroup(boolean enabled) { 705 fProjLabel.setEnabled(enabled); 706 fProjText.setEnabled(enabled); 707 fProjButton.setEnabled(enabled); 708 fTestLabel.setEnabled(enabled); 709 fTestText.setEnabled(enabled); 710 fSearchButton.setEnabled(enabled && fProjText.getText().length() > 0); 711 fTestMethodLabel.setEnabled(enabled); 712 } 713 714 717 public void setDefaults(ILaunchConfigurationWorkingCopy config) { 718 IJavaElement javaElement = getContext(); 719 if (javaElement != null) { 720 initializeJavaProject(javaElement, config); 721 } else { 722 config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""); config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, ""); } 730 initializeTestAttributes(javaElement, config); 731 } 732 733 private void initializeTestAttributes(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) { 734 if (javaElement != null && javaElement.getElementType() < IJavaElement.COMPILATION_UNIT) 735 initializeTestContainer(javaElement, config); 736 else 737 initializeTestType(javaElement, config); 738 } 739 740 private void initializeTestContainer(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) { 741 config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, javaElement.getHandleIdentifier()); 742 initializeName(config, javaElement.getElementName()); 743 } 744 745 private void initializeName(ILaunchConfigurationWorkingCopy config, String name) { 746 if (name == null) { 747 name= ""; } 749 if (name.length() > 0) { 750 int index = name.lastIndexOf('.'); 751 if (index > 0) { 752 name = name.substring(index + 1); 753 } 754 name= getLaunchConfigurationDialog().generateName(name); 755 config.rename(name); 756 } 757 } 758 759 762 private void initializeTestType(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) { 763 String name= ""; String testKindId= null; 765 try { 766 if (javaElement instanceof ISourceReference) { 769 ITestKind testKind= TestKindRegistry.getContainerTestKind(javaElement); 770 testKindId= testKind.getId(); 771 772 IType[] types = TestSearchEngine.findTests(getLaunchConfigurationDialog(), javaElement, testKind); 773 if ((types == null) || (types.length < 1)) { 774 return; 775 } 776 name= JavaModelUtil.getFullyQualifiedName(types[0]); 778 779 } 780 } catch (InterruptedException ie) { 781 782 } catch (InvocationTargetException ite) { 783 } 784 config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, name); 785 if (testKindId != null) 786 config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_RUNNER_KIND, testKindId); 787 initializeName(config, name); 788 } 789 790 793 public String getName() { 794 return JUnitMessages.JUnitLaunchConfigurationTab_tab_label; 795 } 796 797 private IJavaElement chooseContainer(IJavaElement initElement) { 798 Class [] acceptedClasses= new Class [] { IPackageFragmentRoot.class, IJavaProject.class, IPackageFragment.class }; 799 TypedElementSelectionValidator validator= new TypedElementSelectionValidator(acceptedClasses, false) { 800 public boolean isSelectedValid(Object element) { 801 return true; 802 } 803 }; 804 805 acceptedClasses= new Class [] { IJavaModel.class, IPackageFragmentRoot.class, IJavaProject.class, IPackageFragment.class }; 806 ViewerFilter filter= new TypedViewerFilter(acceptedClasses) { 807 public boolean select(Viewer viewer, Object parent, Object element) { 808 if (element instanceof IPackageFragmentRoot && ((IPackageFragmentRoot)element).isArchive()) 809 return false; 810 return super.select(viewer, parent, element); 811 } 812 }; 813 814 StandardJavaElementContentProvider provider= new StandardJavaElementContentProvider(); 815 ILabelProvider labelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT); 816 ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(getShell(), labelProvider, provider); 817 dialog.setValidator(validator); 818 dialog.setComparator(new JavaElementComparator()); 819 dialog.setTitle(JUnitMessages.JUnitLaunchConfigurationTab_folderdialog_title); 820 dialog.setMessage(JUnitMessages.JUnitLaunchConfigurationTab_folderdialog_message); 821 dialog.addFilter(filter); 822 dialog.setInput(JavaCore.create(getWorkspaceRoot())); 823 dialog.setInitialSelection(initElement); 824 dialog.setAllowMultiple(false); 825 826 if (dialog.open() == Window.OK) { 827 Object element= dialog.getFirstResult(); 828 return (IJavaElement)element; 829 } 830 return null; 831 } 832 833 private String getPresentationName(IJavaElement element) { 834 return fJavaElementLabelProvider.getText(element); 835 } 836 837 843 private IJavaElement getContext() { 844 IWorkbenchWindow activeWorkbenchWindow= PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 845 if (activeWorkbenchWindow == null) { 846 return null; 847 } 848 IWorkbenchPage page = activeWorkbenchWindow.getActivePage(); 849 if (page != null) { 850 ISelection selection = page.getSelection(); 851 if (selection instanceof IStructuredSelection) { 852 IStructuredSelection ss = (IStructuredSelection)selection; 853 if (!ss.isEmpty()) { 854 Object obj = ss.getFirstElement(); 855 if (obj instanceof IJavaElement) { 856 return (IJavaElement)obj; 857 } 858 if (obj instanceof IResource) { 859 IJavaElement je = JavaCore.create((IResource)obj); 860 if (je == null) { 861 IProject pro = ((IResource)obj).getProject(); 862 je = JavaCore.create(pro); 863 } 864 if (je != null) { 865 return je; 866 } 867 } 868 } 869 } 870 IEditorPart part = page.getActiveEditor(); 871 if (part != null) { 872 IEditorInput input = part.getEditorInput(); 873 return (IJavaElement) input.getAdapter(IJavaElement.class); 874 } 875 } 876 return null; 877 } 878 879 private void initializeJavaProject(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) { 880 IJavaProject javaProject = javaElement.getJavaProject(); 881 String name = null; 882 if (javaProject != null && javaProject.exists()) { 883 name = javaProject.getElementName(); 884 } 885 config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, name); 886 } 887 888 private void setButtonGridData(Button button) { 889 GridData gridData= new GridData(); 890 button.setLayoutData(gridData); 891 LayoutUtil.setButtonDimensionHint(button); 892 } 893 } 894 | Popular Tags |