1 11 package org.eclipse.jdt.internal.junit.util; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.Collection ; 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.jface.operation.IRunnableContext; 22 import org.eclipse.jface.operation.IRunnableWithProgress; 23 24 import org.eclipse.jdt.core.Flags; 25 import org.eclipse.jdt.core.IClassFile; 26 import org.eclipse.jdt.core.ICompilationUnit; 27 import org.eclipse.jdt.core.IJavaElement; 28 import org.eclipse.jdt.core.IJavaProject; 29 import org.eclipse.jdt.core.IMethod; 30 import org.eclipse.jdt.core.IPackageFragmentRoot; 31 import org.eclipse.jdt.core.IRegion; 32 import org.eclipse.jdt.core.IType; 33 import org.eclipse.jdt.core.ITypeHierarchy; 34 import org.eclipse.jdt.core.JavaCore; 35 import org.eclipse.jdt.core.JavaModelException; 36 import org.eclipse.jdt.core.Signature; 37 import org.eclipse.jdt.core.dom.ITypeBinding; 38 import org.eclipse.jdt.core.dom.Modifier; 39 import org.eclipse.jdt.core.search.IJavaSearchConstants; 40 import org.eclipse.jdt.core.search.IJavaSearchScope; 41 import org.eclipse.jdt.core.search.SearchEngine; 42 import org.eclipse.jdt.core.search.SearchMatch; 43 import org.eclipse.jdt.core.search.SearchParticipant; 44 import org.eclipse.jdt.core.search.SearchPattern; 45 import org.eclipse.jdt.core.search.SearchRequestor; 46 47 import org.eclipse.jdt.internal.junit.launcher.ITestKind; 48 import org.eclipse.jdt.internal.junit.launcher.TestKindRegistry; 49 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin; 50 51 54 public class TestSearchEngine { 55 56 public static boolean isTestOrTestSuite(IType declaringType) throws CoreException { 57 ITestKind testKind= TestKindRegistry.getContainerTestKind(declaringType); 58 return testKind.getFinder().isTest(declaringType); 59 } 60 61 62 public static IType[] findTests(IRunnableContext context, final IJavaElement element, final ITestKind testKind) throws InvocationTargetException , InterruptedException { 63 final Set result= new HashSet (); 64 65 IRunnableWithProgress runnable= new IRunnableWithProgress() { 66 public void run(IProgressMonitor pm) throws InterruptedException , InvocationTargetException { 67 try { 68 testKind.getFinder().findTestsInContainer(element, result, pm); 69 } catch (CoreException e) { 70 throw new InvocationTargetException (e); 71 } 72 } 73 }; 74 context.run(true, true, runnable); 75 return (IType[]) result.toArray(new IType[result.size()]); 76 } 77 78 public static boolean isAccessibleClass(IType type) throws JavaModelException { 79 int flags= type.getFlags(); 80 if (Flags.isInterface(flags)) { 81 return false; 82 } 83 IJavaElement parent= type.getParent(); 84 while (true) { 85 if (parent instanceof ICompilationUnit || parent instanceof IClassFile) { 86 return true; 87 } 88 if (!(parent instanceof IType) || !Flags.isStatic(flags) || !Flags.isPublic(flags)) { 89 return false; 90 } 91 flags= ((IType) parent).getFlags(); 92 parent= parent.getParent(); 93 } 94 } 95 96 public static boolean isAccessibleClass(ITypeBinding type) throws JavaModelException { 97 if (type.isInterface()) { 98 return false; 99 } 100 int modifiers= type.getModifiers(); 101 while (true) { 102 if (type.getDeclaringMethod() != null) { 103 return false; 104 } 105 ITypeBinding declaringClass= type.getDeclaringClass(); 106 if (declaringClass == null) { 107 return true; 108 } 109 if (!Modifier.isStatic(modifiers) || !Modifier.isPublic(modifiers)) { 110 return false; 111 } 112 modifiers= declaringClass.getModifiers(); 113 type= declaringClass; 114 } 115 } 116 117 public static boolean hasTestCaseType(IJavaProject javaProject) { 118 try { 119 return javaProject != null && javaProject.findType(JUnitPlugin.TEST_SUPERCLASS_NAME) != null; 120 } catch (JavaModelException e) { 121 } 123 return false; 124 } 125 126 public static boolean hasTestAnnotation(IJavaProject project) { 127 try { 128 return project != null && project.findType(JUnitPlugin.JUNIT4_ANNOTATION_NAME) != null; 129 } catch (JavaModelException e) { 130 } 132 return false; 133 } 134 135 public static boolean isTestImplementor(IType type) throws JavaModelException { 136 ITypeHierarchy typeHier= type.newSupertypeHierarchy(null); 137 IType[] superInterfaces= typeHier.getAllInterfaces(); 138 for (int i= 0; i < superInterfaces.length; i++) { 139 if (JUnitPlugin.TEST_INTERFACE_NAME.equals(superInterfaces[i].getFullyQualifiedName('.'))) { 140 return true; 141 } 142 } 143 return false; 144 } 145 146 public static boolean isTestImplementor(ITypeBinding type) { 147 ITypeBinding superType= type.getSuperclass(); 148 if (superType != null && isTestImplementor(superType)) { 149 return true; 150 } 151 ITypeBinding[] interfaces= type.getInterfaces(); 152 for (int i= 0; i < interfaces.length; i++) { 153 ITypeBinding curr= interfaces[i]; 154 if (JUnitPlugin.TEST_INTERFACE_NAME.equals(curr.getQualifiedName()) || isTestImplementor(curr)) { 155 return true; 156 } 157 } 158 return false; 159 } 160 161 public static boolean hasSuiteMethod(IType type) throws JavaModelException { 162 IMethod method= type.getMethod("suite", new String [0]); if (!method.exists()) 164 return false; 165 166 if (!Flags.isStatic(method.getFlags()) || !Flags.isPublic(method.getFlags())) { 167 return false; 168 } 169 if (!Signature.getSimpleName(Signature.toString(method.getReturnType())).equals(JUnitPlugin.SIMPLE_TEST_INTERFACE_NAME)) { 170 return false; 171 } 172 return true; 173 } 174 175 public static IRegion getRegion(IJavaElement element) throws JavaModelException { 176 IRegion result= JavaCore.newRegion(); 177 if (element.getElementType() == IJavaElement.JAVA_PROJECT) { 178 IPackageFragmentRoot[] roots= ((IJavaProject) element).getPackageFragmentRoots(); 180 for (int i= 0; i < roots.length; i++) { 181 if (!roots[i].isArchive()) { 182 result.add(roots[i]); 183 } 184 } 185 } else { 186 result.add(element); 187 } 188 return result; 189 } 190 191 public static void findTestImplementorClasses(ITypeHierarchy typeHierarchy, IType testInterface, IRegion region, Set result) 192 throws JavaModelException { 193 IType[] subtypes= typeHierarchy.getAllSubtypes(testInterface); 194 for (int i= 0; i < subtypes.length; i++) { 195 IType type= subtypes[i]; 196 int cachedFlags= typeHierarchy.getCachedFlags(type); 197 if (!Flags.isInterface(cachedFlags) && !Flags.isAbstract(cachedFlags) && region.contains(type) && TestSearchEngine.isAccessibleClass(type)) { 199 result.add(type); 200 } 201 } 202 } 203 204 private static class SuiteMethodTypesCollector extends SearchRequestor { 205 206 private Collection fResult; 207 208 public SuiteMethodTypesCollector(Collection result) { 209 fResult= result; 210 } 211 212 public void acceptSearchMatch(SearchMatch match) throws CoreException { 213 Object enclosingElement= match.getElement(); 214 if (!(enclosingElement instanceof IMethod)) 215 return; 216 217 IMethod method= (IMethod) enclosingElement; 218 if (!Flags.isStatic(method.getFlags()) || !Flags.isPublic(method.getFlags())) { 219 return; 220 } 221 222 IType declaringType= ((IMethod) enclosingElement).getDeclaringType(); 223 if (!TestSearchEngine.isAccessibleClass(declaringType)) { 224 return; 225 } 226 fResult.add(declaringType); 227 } 228 } 229 230 public static void findSuiteMethods(IJavaElement element, Set result, IProgressMonitor pm) throws CoreException { 231 IJavaSearchScope scope= SearchEngine.createJavaSearchScope(new IJavaElement[] { element }, IJavaSearchScope.SOURCES); 234 235 SearchRequestor requestor= new SuiteMethodTypesCollector(result); 236 int matchRule= SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE | SearchPattern.R_ERASURE_MATCH; 237 SearchPattern suitePattern= SearchPattern.createPattern("suite() Test", IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, matchRule); SearchParticipant[] participants= new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }; 239 new SearchEngine().search(suitePattern, participants, scope, requestor, pm); 240 } 241 242 } 243 | Popular Tags |