1 13 package org.eclipse.jdt.internal.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 22 import org.eclipse.core.resources.IProject; 23 import org.eclipse.core.resources.IResource; 24 import org.eclipse.core.resources.IWorkspaceRoot; 25 import org.eclipse.core.resources.ResourcesPlugin; 26 27 import org.eclipse.swt.SWT; 28 import org.eclipse.swt.events.ModifyEvent; 29 import org.eclipse.swt.events.ModifyListener; 30 import org.eclipse.swt.events.SelectionAdapter; 31 import org.eclipse.swt.events.SelectionEvent; 32 import org.eclipse.swt.events.SelectionListener; 33 import org.eclipse.swt.graphics.Image; 34 import org.eclipse.swt.layout.GridData; 35 import org.eclipse.swt.layout.GridLayout; 36 import org.eclipse.swt.widgets.Button; 37 import org.eclipse.swt.widgets.Composite; 38 import org.eclipse.swt.widgets.Label; 39 import org.eclipse.swt.widgets.Shell; 40 import org.eclipse.swt.widgets.Text; 41 42 import org.eclipse.jface.dialogs.Dialog; 43 import org.eclipse.jface.viewers.ArrayContentProvider; 44 import org.eclipse.jface.viewers.ComboViewer; 45 import org.eclipse.jface.viewers.ILabelProvider; 46 import org.eclipse.jface.viewers.ISelectionChangedListener; 47 import org.eclipse.jface.viewers.IStructuredSelection; 48 import org.eclipse.jface.viewers.LabelProvider; 49 import org.eclipse.jface.viewers.SelectionChangedEvent; 50 import org.eclipse.jface.viewers.StructuredSelection; 51 import org.eclipse.jface.viewers.Viewer; 52 import org.eclipse.jface.viewers.ViewerFilter; 53 import org.eclipse.jface.window.Window; 54 55 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 56 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; 57 import org.eclipse.ui.dialogs.SelectionDialog; 58 59 import org.eclipse.debug.core.ILaunchConfiguration; 60 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 61 62 import org.eclipse.debug.ui.ILaunchConfigurationTab; 63 64 import org.eclipse.jdt.core.IClassFile; 65 import org.eclipse.jdt.core.ICompilationUnit; 66 import org.eclipse.jdt.core.IJavaElement; 67 import org.eclipse.jdt.core.IJavaModel; 68 import org.eclipse.jdt.core.IJavaProject; 69 import org.eclipse.jdt.core.IPackageFragment; 70 import org.eclipse.jdt.core.IPackageFragmentRoot; 71 import org.eclipse.jdt.core.ISourceReference; 72 import org.eclipse.jdt.core.IType; 73 import org.eclipse.jdt.core.JavaCore; 74 import org.eclipse.jdt.core.JavaModelException; 75 76 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 77 78 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 79 80 import org.eclipse.jdt.ui.JavaElementLabelProvider; 81 import org.eclipse.jdt.ui.JavaElementSorter; 82 import org.eclipse.jdt.ui.StandardJavaElementContentProvider; 83 84 import org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator; 85 import org.eclipse.jdt.internal.ui.wizards.TypedViewerFilter; 86 87 import org.eclipse.jdt.internal.junit.Messages; 88 import org.eclipse.jdt.internal.junit.ui.JUnitMessages; 89 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin; 90 import org.eclipse.jdt.internal.junit.util.TestSearchEngine; 91 92 96 public class JUnitMainTab extends JUnitLaunchConfigurationTab { 97 98 private Label fProjLabel; 100 private Text fProjText; 101 private Button fProjButton; 102 private Button fKeepRunning; 103 104 private Text fTestText; 106 private Button fSearchButton; 107 private final Image fTestIcon= createImage("obj16/test.gif"); private String fOriginalTestMethodName; 109 private Label fTestMethodLabel; 110 private Text fContainerText; 111 private IJavaElement fContainerElement; 112 private final ILabelProvider fJavaElementLabelProvider= new JavaElementLabelProvider(); 113 114 private Button fContainerSearchButton; 115 private Button fTestContainerRadioButton; 116 private Button fTestRadioButton; 117 private Label fTestLabel; 118 119 private ComboViewer fTestLoaderViewer; 120 121 124 public void createControl(Composite parent) { 125 Composite comp = new Composite(parent, SWT.NONE); 126 setControl(comp); 127 128 GridLayout topLayout = new GridLayout(); 129 topLayout.numColumns= 3; 130 comp.setLayout(topLayout); 131 132 createSingleTestSection(comp); 133 createTestContainerSelectionGroup(comp); 134 135 createSpacer(comp); 136 137 createTestLoaderGroup(comp); 138 139 createSpacer(comp); 140 141 createKeepAliveGroup(comp); 142 Dialog.applyDialogFont(comp); 143 validatePage(); 144 } 145 146 private void createTestLoaderGroup(Composite comp) { 147 Label loaderLabel= new Label(comp, SWT.NONE); 148 loaderLabel.setText(JUnitMessages.JUnitMainTab_Test_Loader); 149 GridData gd= new GridData(); 150 gd.horizontalIndent= 0; 151 loaderLabel.setLayoutData(gd); 152 153 fTestLoaderViewer= new ComboViewer(comp, SWT.DROP_DOWN | SWT.READ_ONLY); 154 fTestLoaderViewer.getCombo().setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 155 156 ArrayList items= TestKindRegistry.getDefault().getAllKinds(); 157 fTestLoaderViewer.setContentProvider(new ArrayContentProvider()); 158 fTestLoaderViewer.setLabelProvider(new LabelProvider() { 159 public String getText(Object element) { 160 return ((TestKind) element).getDisplayName(); 161 } 162 }); 163 fTestLoaderViewer.setInput(items); 164 fTestLoaderViewer.addSelectionChangedListener(new ISelectionChangedListener() { 165 public void selectionChanged(SelectionChangedEvent event) { 166 validatePage(); 167 updateLaunchConfigurationDialog(); 168 } 169 }); 170 } 171 172 private void createSpacer(Composite comp) { 173 Label label= new Label(comp, SWT.NONE); 174 GridData gd= new GridData(); 175 gd.horizontalSpan= 3; 176 label.setLayoutData(gd); 177 } 178 179 protected void createSingleTestSection(Composite comp) { 180 fTestRadioButton= new Button(comp, SWT.RADIO); 181 fTestRadioButton.setText(JUnitMessages.JUnitMainTab_label_oneTest); 182 GridData gd = new GridData(); 183 gd.horizontalSpan = 3; 184 fTestRadioButton.setLayoutData(gd); 185 fTestRadioButton.addSelectionListener(new SelectionAdapter() { 186 public void widgetSelected(SelectionEvent e) { 187 if (fTestRadioButton.getSelection()) 188 testModeChanged(); 189 } 190 }); 191 192 fProjLabel = new Label(comp, SWT.NONE); 193 fProjLabel.setText(JUnitMessages.JUnitMainTab_label_project); 194 gd= new GridData(); 195 gd.horizontalIndent = 25; 196 fProjLabel.setLayoutData(gd); 197 198 fProjText= new Text(comp, SWT.SINGLE | SWT.BORDER); 199 fProjText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 200 fProjText.addModifyListener(new ModifyListener() { 201 public void modifyText(ModifyEvent evt) { 202 validatePage(); 203 updateLaunchConfigurationDialog(); 204 fSearchButton.setEnabled(fTestRadioButton.getSelection() && fProjText.getText().length() > 0); 205 } 206 }); 207 208 fProjButton = new Button(comp, SWT.PUSH); 209 fProjButton.setText(JUnitMessages.JUnitMainTab_label_browse); 210 fProjButton.addSelectionListener(new SelectionAdapter() { 211 public void widgetSelected(SelectionEvent evt) { 212 handleProjectButtonSelected(); 213 } 214 }); 215 setButtonGridData(fProjButton); 216 217 fTestLabel = new Label(comp, SWT.NONE); 218 gd = new GridData(); 219 gd.horizontalIndent = 25; 220 fTestLabel.setLayoutData(gd); 221 fTestLabel.setText(JUnitMessages.JUnitMainTab_label_test); 222 223 224 fTestText = new Text(comp, SWT.SINGLE | SWT.BORDER); 225 fTestText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 226 fTestText.addModifyListener(new ModifyListener() { 227 public void modifyText(ModifyEvent evt) { 228 validatePage(); 229 updateLaunchConfigurationDialog(); 230 } 231 }); 232 233 fSearchButton = new Button(comp, SWT.PUSH); 234 fSearchButton.setEnabled(fProjText.getText().length() > 0); 235 fSearchButton.setText(JUnitMessages.JUnitMainTab_label_search); 236 fSearchButton.addSelectionListener(new SelectionAdapter() { 237 public void widgetSelected(SelectionEvent evt) { 238 handleSearchButtonSelected(); 239 } 240 }); 241 setButtonGridData(fSearchButton); 242 243 new Label(comp, SWT.NONE); 244 245 fTestMethodLabel= new Label(comp, SWT.NONE); 246 fTestMethodLabel.setText(""); gd= new GridData(); 248 gd.horizontalSpan = 2; 249 fTestMethodLabel.setLayoutData(gd); 250 251 } 252 253 protected void createTestContainerSelectionGroup(Composite comp) { 254 fTestContainerRadioButton= new Button(comp, SWT.RADIO); 255 fTestContainerRadioButton.setText(JUnitMessages.JUnitMainTab_label_containerTest); 256 GridData gd = new GridData(); 257 gd.horizontalSpan = 3; 258 fTestContainerRadioButton.setLayoutData(gd); 259 fTestContainerRadioButton.addSelectionListener(new SelectionListener() { 260 public void widgetSelected(SelectionEvent e) { 261 if (fTestContainerRadioButton.getSelection()) 262 testModeChanged(); 263 } 264 public void widgetDefaultSelected(SelectionEvent e) { 265 } 266 }); 267 268 fContainerText = new Text(comp, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY); 269 gd = new GridData(GridData.FILL_HORIZONTAL); 270 gd.horizontalIndent= 25; 271 gd.horizontalSpan = 2; 272 fContainerText.setLayoutData(gd); 273 fContainerText.addModifyListener(new ModifyListener() { 274 public void modifyText(ModifyEvent evt) { 275 updateLaunchConfigurationDialog(); 276 } 277 }); 278 279 fContainerSearchButton = new Button(comp, SWT.PUSH); 280 fContainerSearchButton.setText(JUnitMessages.JUnitMainTab_label_search); 281 fContainerSearchButton.addSelectionListener(new SelectionAdapter() { 282 public void widgetSelected(SelectionEvent evt) { 283 handleContainerSearchButtonSelected(); 284 } 285 }); 286 setButtonGridData(fContainerSearchButton); 287 } 288 289 private void handleContainerSearchButtonSelected() { 290 IJavaElement javaElement= chooseContainer(fContainerElement); 291 if (javaElement != null) 292 setContainerElement(javaElement); 293 } 294 295 private void setContainerElement(IJavaElement javaElement) { 296 fContainerElement= javaElement; 297 fContainerText.setText(getPresentationName(javaElement)); 298 validatePage(); 299 updateLaunchConfigurationDialog(); 300 } 301 302 public void createKeepAliveGroup(Composite comp) { 303 GridData gd; 304 fKeepRunning = new Button(comp, SWT.CHECK); 305 fKeepRunning.addSelectionListener(new SelectionListener() { 306 public void widgetSelected(SelectionEvent e) { 307 updateLaunchConfigurationDialog(); 308 } 309 310 public void widgetDefaultSelected(SelectionEvent e) { 311 } 312 }); 313 fKeepRunning.setText(JUnitMessages.JUnitMainTab_label_keeprunning); 314 gd= new GridData(); 315 gd.horizontalAlignment= GridData.FILL; 316 gd.horizontalSpan= 2; 317 fKeepRunning.setLayoutData(gd); 318 } 319 320 321 322 protected static Image createImage(String path) { 323 return JUnitPlugin.getImageDescriptor(path).createImage(); 324 } 325 326 327 330 public void initializeFrom(ILaunchConfiguration config) { 331 updateProjectFromConfig(config); 332 String containerHandle= ""; try { 334 containerHandle = config.getAttribute(JUnitBaseLaunchConfiguration.LAUNCH_CONTAINER_ATTR, ""); } catch (CoreException ce) { 336 } 337 338 if (containerHandle.length() > 0) 339 updateTestContainerFromConfig(config); 340 else 341 updateTestTypeFromConfig(config); 342 updateKeepRunning(config); 343 updateTestLoaderFromConfig(config); 344 } 345 346 347 private void updateTestLoaderFromConfig(ILaunchConfiguration config) { 348 TestKind testKind= TestKindRegistry.getDefault().getKind(config); 349 if (testKind == null || testKind.isNull()) 350 testKind= (TestKind) TestKindRegistry.getDefault().getAllKinds().get(0); fTestLoaderViewer.setSelection(new StructuredSelection(testKind)); 352 } 353 354 private TestKind getSelectedTestKind() { 355 IStructuredSelection selection= (IStructuredSelection) fTestLoaderViewer.getSelection(); 356 return (TestKind) selection.getFirstElement(); 357 } 358 359 private void updateKeepRunning(ILaunchConfiguration config) { 360 boolean running= false; 361 try { 362 running= config.getAttribute(JUnitBaseLaunchConfiguration.ATTR_KEEPRUNNING, false); 363 } catch (CoreException ce) { 364 } 365 fKeepRunning.setSelection(running); 366 } 367 368 protected void updateProjectFromConfig(ILaunchConfiguration config) { 369 String projectName= ""; try { 371 projectName = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""); } catch (CoreException ce) { 373 } 374 fProjText.setText(projectName); 375 } 376 377 protected void updateTestTypeFromConfig(ILaunchConfiguration config) { 378 String testTypeName= ""; fOriginalTestMethodName= ""; try { 381 testTypeName = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, ""); fOriginalTestMethodName = config.getAttribute(JUnitBaseLaunchConfiguration.TESTNAME_ATTR, ""); } catch (CoreException ce) { 384 } 385 fTestRadioButton.setSelection(true); 386 setEnableSingleTestGroup(true); 387 setEnableContainerTestGroup(false); 388 fTestContainerRadioButton.setSelection(false); 389 fTestText.setText(testTypeName); 390 fContainerText.setText(""); setTestMethodLabel(fOriginalTestMethodName); 392 } 393 394 private void setTestMethodLabel(String testMethodName) { 395 if (!"".equals(testMethodName)) { fTestMethodLabel.setText(JUnitMessages.JUnitMainTab_label_method+fOriginalTestMethodName); 397 } else { 398 fTestMethodLabel.setText(""); } 400 } 401 402 protected void updateTestContainerFromConfig(ILaunchConfiguration config) { 403 String containerHandle= ""; IJavaElement containerElement = null; 405 try { 406 containerHandle = config.getAttribute(JUnitBaseLaunchConfiguration.LAUNCH_CONTAINER_ATTR, ""); if (containerHandle.length() > 0) { 408 containerElement= JavaCore.create(containerHandle); 409 } 410 } catch (CoreException ce) { 411 } 412 if (containerElement != null) 413 fContainerElement = containerElement; 414 fTestContainerRadioButton.setSelection(true); 415 setEnableSingleTestGroup(false); 416 setEnableContainerTestGroup(true); 417 fTestRadioButton.setSelection(false); 418 if (fContainerElement != null) 419 fContainerText.setText(getPresentationName(fContainerElement)); 420 fTestText.setText(""); } 422 425 public void performApply(ILaunchConfigurationWorkingCopy config) { 426 if (fTestContainerRadioButton.getSelection() && fContainerElement != null) { 427 config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, fContainerElement.getJavaProject().getElementName()); 428 config.setAttribute(JUnitBaseLaunchConfiguration.LAUNCH_CONTAINER_ATTR, fContainerElement.getHandleIdentifier()); 429 config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, ""); config.setAttribute(JUnitBaseLaunchConfiguration.TESTNAME_ATTR, ""); } else { 433 config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, fProjText.getText()); 434 config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, fTestText.getText()); 435 config.setAttribute(JUnitBaseLaunchConfiguration.LAUNCH_CONTAINER_ATTR, ""); config.setAttribute(JUnitBaseLaunchConfiguration.TESTNAME_ATTR, fOriginalTestMethodName); 437 } 438 config.setAttribute(JUnitBaseLaunchConfiguration.ATTR_KEEPRUNNING, fKeepRunning.getSelection()); 439 440 IStructuredSelection testKindSelection= (IStructuredSelection) fTestLoaderViewer.getSelection(); 441 if (! testKindSelection.isEmpty()) { 442 TestKind testKind= (TestKind) testKindSelection.getFirstElement(); 443 config.setAttribute(JUnitBaseLaunchConfiguration.TEST_KIND_ATTR, testKind.getId()); 444 } 445 } 446 447 450 public void dispose() { 451 super.dispose(); 452 fTestIcon.dispose(); 453 fJavaElementLabelProvider.dispose(); 454 } 455 456 459 public Image getImage() { 460 return fTestIcon; 461 } 462 463 466 protected void handleSearchButtonSelected() { 467 Shell shell = getShell(); 468 469 IJavaProject javaProject = getJavaProject(); 470 471 IType[] types= new IType[0]; 472 boolean[] radioSetting= new boolean[2]; 473 try { 474 radioSetting[0]= fTestRadioButton.getSelection(); 477 radioSetting[1]= fTestContainerRadioButton.getSelection(); 478 479 types= TestSearchEngine.findTests(getLaunchConfigurationDialog(), new Object [] {javaProject}, getSelectedTestKind()); 480 } catch (InterruptedException e) { 481 setErrorMessage(e.getMessage()); 482 return; 483 } catch (InvocationTargetException e) { 484 JUnitPlugin.log(e.getTargetException()); 485 return; 486 } finally { 487 fTestRadioButton.setSelection(radioSetting[0]); 488 fTestContainerRadioButton.setSelection(radioSetting[1]); 489 } 490 491 SelectionDialog dialog = new TestSelectionDialog(shell, types); 492 dialog.setTitle(JUnitMessages.JUnitMainTab_testdialog_title); 493 dialog.setMessage(JUnitMessages.JUnitMainTab_testdialog_message); 494 if (dialog.open() == Window.CANCEL) { 495 return; 496 } 497 498 Object [] results = dialog.getResult(); 499 if ((results == null) || (results.length < 1)) { 500 return; 501 } 502 IType type = (IType)results[0]; 503 504 if (type != null) { 505 fTestText.setText(type.getFullyQualifiedName('.')); 506 javaProject = type.getJavaProject(); 507 fProjText.setText(javaProject.getElementName()); 508 } 509 } 510 511 516 protected void handleProjectButtonSelected() { 517 IJavaProject project = chooseJavaProject(); 518 if (project == null) { 519 return; 520 } 521 522 String projectName = project.getElementName(); 523 fProjText.setText(projectName); 524 } 525 526 530 protected IJavaProject chooseJavaProject() { 531 IJavaProject[] projects; 532 try { 533 projects= JavaCore.create(getWorkspaceRoot()).getJavaProjects(); 534 } catch (JavaModelException e) { 535 JUnitPlugin.log(e.getStatus()); 536 projects= new IJavaProject[0]; 537 } 538 539 ILabelProvider labelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT); 540 ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), labelProvider); 541 dialog.setTitle(JUnitMessages.JUnitMainTab_projectdialog_title); 542 dialog.setMessage(JUnitMessages.JUnitMainTab_projectdialog_message); 543 dialog.setElements(projects); 544 545 IJavaProject javaProject = getJavaProject(); 546 if (javaProject != null) { 547 dialog.setInitialSelections(new Object [] { javaProject }); 548 } 549 if (dialog.open() == Window.OK) { 550 return (IJavaProject) dialog.getFirstResult(); 551 } 552 return null; 553 } 554 555 559 protected IJavaProject getJavaProject() { 560 String projectName = fProjText.getText().trim(); 561 if (projectName.length() < 1) { 562 return null; 563 } 564 return getJavaModel().getJavaProject(projectName); 565 } 566 567 570 private IWorkspaceRoot getWorkspaceRoot() { 571 return ResourcesPlugin.getWorkspace().getRoot(); 572 } 573 574 577 private IJavaModel getJavaModel() { 578 return JavaCore.create(getWorkspaceRoot()); 579 } 580 581 584 public boolean isValid(ILaunchConfiguration config) { 585 return getErrorMessage() == null; 586 } 587 588 private void testModeChanged() { 589 boolean isSingleTestMode= fTestRadioButton.getSelection(); 590 setEnableSingleTestGroup(isSingleTestMode); 591 setEnableContainerTestGroup(!isSingleTestMode); 592 if (!isSingleTestMode && fContainerText.getText().length() == 0) { 593 IJavaProject javaProject= getJavaModel().getJavaProject(fProjText.getText()); 594 if (javaProject != null && javaProject.exists()) 595 setContainerElement(javaProject); 596 } 597 validatePage(); 598 updateLaunchConfigurationDialog(); 599 } 600 601 private void validatePage() { 602 setErrorMessage(null); 603 setMessage(null); 604 605 if (fTestContainerRadioButton.getSelection()) { 606 if (fContainerElement == null) { 607 setErrorMessage(JUnitMessages.JUnitMainTab_error_noContainer); 608 return; 609 } 610 validateJavaProject(fContainerElement.getJavaProject()); 611 return; 612 } 613 614 String projectName= fProjText.getText().trim(); 615 if (projectName.length() == 0) { 616 setErrorMessage(JUnitMessages.JUnitMainTab_error_projectnotdefined); 617 return; 618 } 619 620 IStatus status= ResourcesPlugin.getWorkspace().validatePath(IPath.SEPARATOR + projectName, IResource.PROJECT); 621 if (!status.isOK()) { 622 setErrorMessage(Messages.format(JUnitMessages.JUnitMainTab_error_invalidProjectName, projectName)); 623 return; 624 } 625 626 IProject project= getWorkspaceRoot().getProject(projectName); 627 if (!project.exists()) { 628 setErrorMessage(JUnitMessages.JUnitMainTab_error_projectnotexists); 629 return; 630 } 631 632 try { 633 if (!project.hasNature(JavaCore.NATURE_ID)) { 634 setErrorMessage(JUnitMessages.JUnitMainTab_error_notJavaProject); 635 return; 636 } 637 String className= fTestText.getText().trim(); 638 if (className.length() == 0) { 639 setErrorMessage(JUnitMessages.JUnitMainTab_error_testnotdefined); 640 return; 641 } 642 } catch (Exception e) { 643 } 644 IJavaProject javaProject= JavaCore.create(project); 645 validateJavaProject(javaProject); 646 } 647 648 private void validateJavaProject(IJavaProject javaProject) { 649 if (! TestSearchEngine.hasTestCaseType(javaProject)) { 650 setErrorMessage(JUnitMessages.JUnitMainTab_error_testcasenotonpath); 651 return; 652 } 653 } 654 655 private void setEnableContainerTestGroup(boolean enabled) { 656 fContainerSearchButton.setEnabled(enabled); 657 fContainerText.setEnabled(enabled); 658 } 659 660 private void setEnableSingleTestGroup(boolean enabled) { 661 fProjLabel.setEnabled(enabled); 662 fProjText.setEnabled(enabled); 663 fProjButton.setEnabled(enabled); 664 fTestLabel.setEnabled(enabled); 665 fTestText.setEnabled(enabled); 666 fSearchButton.setEnabled(enabled && fProjText.getText().length() > 0); 667 fTestMethodLabel.setEnabled(enabled); 668 } 669 670 673 public void setDefaults(ILaunchConfigurationWorkingCopy config) { 674 IJavaElement javaElement = getContext(); 675 if (javaElement != null) { 676 initializeJavaProject(javaElement, config); 677 } else { 678 config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""); config.setAttribute(JUnitBaseLaunchConfiguration.LAUNCH_CONTAINER_ATTR, ""); } 686 initializeTestAttributes(javaElement, config); 687 } 688 689 private void initializeTestAttributes(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) { 690 if (javaElement != null && javaElement.getElementType() < IJavaElement.COMPILATION_UNIT) 691 initializeTestContainer(javaElement, config); 692 else 693 initializeTestType(javaElement, config); 694 } 695 696 private void initializeTestContainer(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) { 697 config.setAttribute(JUnitBaseLaunchConfiguration.LAUNCH_CONTAINER_ATTR, javaElement.getHandleIdentifier()); 698 initializeName(config, javaElement.getElementName()); 699 } 700 701 private void initializeName(ILaunchConfigurationWorkingCopy config, String name) { 702 if (name == null) { 703 name= ""; } 705 if (name.length() > 0) { 706 int index = name.lastIndexOf('.'); 707 if (index > 0) { 708 name = name.substring(index + 1); 709 } 710 name= getLaunchConfigurationDialog().generateName(name); 711 config.rename(name); 712 } 713 } 714 715 718 protected void initializeTestType(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) { 719 String name= ""; ITestKind testKind= null; 721 try { 722 if ((javaElement instanceof ICompilationUnit) || 725 (javaElement instanceof ISourceReference) || 726 (javaElement instanceof IClassFile)) { 727 728 IType[] types = TestSearchEngine.findTests(new Object [] {javaElement}); 729 if ((types == null) || (types.length < 1)) { 730 return; 731 } 732 name= JavaModelUtil.getFullyQualifiedName(types[0]); 734 testKind= TestKindRegistry.getDefault().getKind(types[0]); 735 } 736 } catch (InterruptedException ie) { 737 } catch (InvocationTargetException ite) { 738 } 739 if (name == null) 740 name= ""; config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, name); 742 if (testKind != null && ! testKind.isNull()) 743 config.setAttribute(JUnitBaseLaunchConfiguration.TEST_KIND_ATTR, testKind.getId()); 744 initializeName(config, name); 745 } 746 747 750 public String getName() { 751 return JUnitMessages.JUnitMainTab_tab_label; 752 } 753 754 private IJavaElement chooseContainer(IJavaElement initElement) { 755 Class [] acceptedClasses= new Class [] { IPackageFragmentRoot.class, IJavaProject.class, IPackageFragment.class }; 756 TypedElementSelectionValidator validator= new TypedElementSelectionValidator(acceptedClasses, false) { 757 public boolean isSelectedValid(Object element) { 758 return true; 759 } 760 }; 761 762 acceptedClasses= new Class [] { IJavaModel.class, IPackageFragmentRoot.class, IJavaProject.class, IPackageFragment.class }; 763 ViewerFilter filter= new TypedViewerFilter(acceptedClasses) { 764 public boolean select(Viewer viewer, Object parent, Object element) { 765 if (element instanceof IPackageFragmentRoot && ((IPackageFragmentRoot)element).isArchive()) 766 return false; 767 return super.select(viewer, parent, element); 768 } 769 }; 770 771 StandardJavaElementContentProvider provider= new StandardJavaElementContentProvider(); 772 ILabelProvider labelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT); 773 ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(getShell(), labelProvider, provider); 774 dialog.setValidator(validator); 775 dialog.setSorter(new JavaElementSorter()); 776 dialog.setTitle(JUnitMessages.JUnitMainTab_folderdialog_title); 777 dialog.setMessage(JUnitMessages.JUnitMainTab_folderdialog_message); 778 dialog.addFilter(filter); 779 dialog.setInput(JavaCore.create(getWorkspaceRoot())); 780 dialog.setInitialSelection(initElement); 781 dialog.setAllowMultiple(false); 782 783 if (dialog.open() == Window.OK) { 784 Object element= dialog.getFirstResult(); 785 return (IJavaElement)element; 786 } 787 return null; 788 } 789 790 private String getPresentationName(IJavaElement element) { 791 return fJavaElementLabelProvider.getText(element); 792 } 793 } 794 | Popular Tags |