1 11 package org.eclipse.jdt.internal.junit.ui; 12 13 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.HashSet ; 16 import java.util.Set ; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IProgressMonitor; 20 21 import org.eclipse.core.resources.ResourcesPlugin; 22 23 import org.eclipse.jdt.core.IJavaElement; 24 import org.eclipse.jdt.core.IJavaProject; 25 import org.eclipse.jdt.core.IType; 26 import org.eclipse.jdt.core.JavaCore; 27 import org.eclipse.jdt.core.JavaModelException; 28 import org.eclipse.jdt.core.search.IJavaSearchConstants; 29 import org.eclipse.jdt.core.search.IJavaSearchScope; 30 import org.eclipse.jdt.core.search.SearchEngine; 31 import org.eclipse.jdt.core.search.SearchMatch; 32 import org.eclipse.jdt.core.search.SearchParticipant; 33 import org.eclipse.jdt.core.search.SearchPattern; 34 import org.eclipse.jdt.core.search.SearchRequestor; 35 36 import org.eclipse.swt.widgets.Shell; 37 38 import org.eclipse.jface.dialogs.ErrorDialog; 39 import org.eclipse.jface.dialogs.MessageDialog; 40 import org.eclipse.jface.operation.IRunnableWithProgress; 41 import org.eclipse.jface.viewers.ILabelProvider; 42 import org.eclipse.jface.window.Window; 43 44 import org.eclipse.ui.PlatformUI; 45 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 46 47 import org.eclipse.jdt.ui.JavaElementLabelProvider; 48 49 import org.eclipse.jdt.internal.junit.Messages; 50 51 54 public class TestMethodSelectionDialog extends ElementListSelectionDialog { 55 56 private IJavaElement fElement; 57 58 public static class TestReferenceCollector extends SearchRequestor { 59 Set fResult= new HashSet (200); 60 61 public void acceptSearchMatch(SearchMatch match) throws CoreException { 62 IJavaElement enclosingElement= (IJavaElement) match.getElement(); 63 if (enclosingElement.getElementName().startsWith("test")) fResult.add(enclosingElement); 65 } 66 67 public Object [] getResult() { 68 return fResult.toArray(); 69 } 70 } 71 72 public TestMethodSelectionDialog(Shell shell, IJavaElement element) { 73 super(shell, new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_PARAMETERS | JavaElementLabelProvider.SHOW_POST_QUALIFIED)); 74 fElement= element; 75 } 76 77 80 protected void configureShell(Shell newShell) { 81 super.configureShell(newShell); 82 PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJUnitHelpContextIds.TEST_SELECTION_DIALOG); 83 } 84 85 88 public int open() { 89 Object [] elements; 90 IType testType= findTestType(); 91 92 if (testType == null) 93 return CANCEL; 94 95 try { 96 elements= searchTestMethods(fElement, testType); 97 } catch (InterruptedException e) { 98 return CANCEL; 99 } catch (InvocationTargetException e) { 100 MessageDialog.openError(getParentShell(), JUnitMessages.TestMethodSelectionDialog_error_title, e.getTargetException().getMessage()); 101 return CANCEL; 102 } 103 104 if (elements.length == 0) { 105 String msg= Messages.format(JUnitMessages.TestMethodSelectionDialog_notfound_message, fElement.getElementName()); 106 MessageDialog.openInformation(getParentShell(), JUnitMessages.TestMethodSelectionDialog_no_tests_title, msg); 107 return CANCEL; 108 } 109 setElements(elements); 110 return super.open(); 111 } 112 113 private IType findTestType() { 114 String qualifiedName= JUnitPlugin.TEST_INTERFACE_NAME; 115 IJavaProject[] projects; 116 Set result= new HashSet (); 117 try { 118 projects= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects(); 119 for (int i= 0; i < projects.length; i++) { 120 IJavaProject project= projects[i]; 121 IType type= project.findType(qualifiedName); 122 if (type != null) 123 result.add(type); 124 } 125 } catch (JavaModelException e) { 126 ErrorDialog.openError(getParentShell(), JUnitMessages.TestMethodSelectionDialog_error_notfound_title, JUnitMessages.TestMethodSelectionDialog_error_notfound_message, e.getStatus()); 127 return null; 128 } 129 if (result.size() == 0) { 130 String msg= Messages.format(JUnitMessages.TestMethodSelectionDialog_test_not_found, JUnitPlugin.TEST_INTERFACE_NAME); 131 MessageDialog.openError(getParentShell(), JUnitMessages.TestMethodSelectionDialog_select_dialog_title, msg); 132 return null; 133 } 134 if (result.size() == 1) 135 return (IType)result.toArray()[0]; 136 137 return selectTestType(result); 138 } 139 140 private IType selectTestType(Set result) { 141 ILabelProvider labelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_PARAMETERS | JavaElementLabelProvider.SHOW_ROOT); 142 ElementListSelectionDialog dialog= new ElementListSelectionDialog(getParentShell(), labelProvider); 143 dialog.setTitle(JUnitMessages.TestMethodSelectionDialog_dialog_title); 144 String msg= Messages.format(JUnitMessages.TestMethodSelectionDialog_testproject, "junit.framework.Test"); dialog.setMessage(msg); 146 IJavaProject[] projects= new IJavaProject[result.size()]; 147 IType[] testTypes= (IType[]) result.toArray(new IType[result.size()]); 148 for (int i= 0; i < projects.length; i++) 149 projects[i]= testTypes[i].getJavaProject(); 150 dialog.setElements(projects); 151 if (dialog.open() == Window.CANCEL) 152 return null; 153 IJavaProject project= (IJavaProject) dialog.getFirstResult(); 154 for (int i= 0; i < testTypes.length; i++) { 155 if (testTypes[i].getJavaProject().equals(project)) 156 return testTypes[i]; 157 } 158 return null; 159 } 160 161 public Object [] searchTestMethods(final IJavaElement element, final IType testType) throws InvocationTargetException , InterruptedException { 162 final TestReferenceCollector[] col= new TestReferenceCollector[1]; 163 164 IRunnableWithProgress runnable= new IRunnableWithProgress() { 165 public void run(IProgressMonitor pm) throws InvocationTargetException { 166 try { 167 col[0]= doSearchTestMethods(element, testType, pm); 168 } catch (CoreException e) { 169 throw new InvocationTargetException (e); 170 } 171 } 172 }; 173 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(runnable); 174 return col[0].getResult(); 175 } 176 177 private TestReferenceCollector doSearchTestMethods(IJavaElement element, IType testType, IProgressMonitor pm) throws CoreException{ 178 int matchRule= SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE | SearchPattern.R_ERASURE_MATCH; 179 SearchPattern pattern= SearchPattern.createPattern(element, IJavaSearchConstants.REFERENCES, matchRule); 180 SearchParticipant[] participants= new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()}; 181 IJavaSearchScope scope= SearchEngine.createHierarchyScope(testType); 182 TestReferenceCollector requestor= new TestReferenceCollector(); 183 new SearchEngine().search(pattern, participants, scope, requestor, pm); 184 return requestor; 185 } 186 } 187 | Popular Tags |