1 13 14 package org.eclipse.jdt.internal.junit.launcher; 15 16 import java.util.Set ; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.core.runtime.NullProgressMonitor; 21 import org.eclipse.core.runtime.SubProgressMonitor; 22 23 import org.eclipse.jdt.core.Flags; 24 import org.eclipse.jdt.core.ICompilationUnit; 25 import org.eclipse.jdt.core.IJavaElement; 26 import org.eclipse.jdt.core.IJavaProject; 27 import org.eclipse.jdt.core.IRegion; 28 import org.eclipse.jdt.core.IType; 29 import org.eclipse.jdt.core.ITypeHierarchy; 30 import org.eclipse.jdt.core.JavaModelException; 31 import org.eclipse.jdt.core.dom.IMethodBinding; 32 import org.eclipse.jdt.core.dom.ITypeBinding; 33 import org.eclipse.jdt.core.dom.Modifier; 34 35 import org.eclipse.jdt.internal.junit.ui.JUnitMessages; 36 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin; 37 import org.eclipse.jdt.internal.junit.util.TestSearchEngine; 38 39 public class JUnit3TestFinder implements ITestFinder { 40 41 public void findTestsInContainer(IJavaElement element, Set result, IProgressMonitor pm) throws CoreException { 42 if (element == null || result == null) { 43 throw new IllegalArgumentException (); 44 } 45 46 if (pm == null) 47 pm= new NullProgressMonitor(); 48 49 pm.beginTask(JUnitMessages.TestSearchEngine_message_searching, 10); 50 try { 51 if (element instanceof IType) { 52 if (isTest((IType) element)) { 53 result.add(element); 54 } 55 } else if (element instanceof ICompilationUnit) { 56 IType[] types= ((ICompilationUnit) element).getAllTypes(); 57 for (int i= 0; i < types.length; i++) { 58 IType type= types[i]; 59 if (isTest(types[i])) { 60 result.add(type); 61 } 62 } 63 } else { 64 findTestCases(element, result, new SubProgressMonitor(pm, 7)); 65 if (pm.isCanceled()) { 66 return; 67 } 68 TestSearchEngine.findSuiteMethods(element, result, new SubProgressMonitor(pm, 3)); 69 } 70 if (pm.isCanceled()) { 71 return; 72 } 73 } finally { 74 pm.done(); 75 } 76 } 77 78 private static void findTestCases(IJavaElement element, Set result, IProgressMonitor pm) throws JavaModelException { 79 IJavaProject javaProject= element.getJavaProject(); 80 81 IType testCaseType= javaProject.findType(JUnitPlugin.TEST_INTERFACE_NAME); 82 if (testCaseType == null) 83 return; 84 85 IRegion region= TestSearchEngine.getRegion(element); 86 ITypeHierarchy typeHierarchy= javaProject.newTypeHierarchy(testCaseType, region, pm); 87 TestSearchEngine.findTestImplementorClasses(typeHierarchy, testCaseType, region, result); 88 } 89 90 public boolean isTest(ITypeBinding type) throws JavaModelException { 91 if (!type.isClass() || !Modifier.isPublic(type.getModifiers())) { 92 return false; 93 } 94 95 IMethodBinding[] declaredMethods= type.getDeclaredMethods(); 96 for (int i= 0; i < declaredMethods.length; i++) { 97 IMethodBinding curr= declaredMethods[i]; 98 if ("suite".equals(curr.getName())) { int flags= curr.getModifiers(); 100 if (Modifier.isPublic(flags) && Modifier.isStatic(flags) && curr.getParameterTypes().length == 0 && 101 JUnitPlugin.SIMPLE_TEST_INTERFACE_NAME.equals(curr.getReturnType().getQualifiedName())) { 102 return true; 103 } 104 } 105 } 106 107 if (TestSearchEngine.isTestImplementor(type)) { 108 return true; 109 } 110 111 return false; 112 } 113 114 public boolean isTest(IType type) throws JavaModelException { 115 return TestSearchEngine.isAccessibleClass(type) && (TestSearchEngine.hasSuiteMethod(type) || isTestImplementor(type)); 116 } 117 118 private static boolean isTestImplementor(IType type) throws JavaModelException { 119 if (!Flags.isAbstract(type.getFlags()) && TestSearchEngine.isTestImplementor(type)) { 120 return true; 121 } 122 return false; 123 } 124 } 125 | Popular Tags |