KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > util > TestSearchEngine


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.junit.util;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Set JavaDoc;
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 /**
52  * Custom Search engine for suite() methods
53  */

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 JavaDoc, InterruptedException JavaDoc {
63         final Set JavaDoc result= new HashSet JavaDoc();
64
65         IRunnableWithProgress runnable= new IRunnableWithProgress() {
66             public void run(IProgressMonitor pm) throws InterruptedException JavaDoc, InvocationTargetException JavaDoc {
67                 try {
68                     testKind.getFinder().findTestsInContainer(element, result, pm);
69                 } catch (CoreException e) {
70                     throw new InvocationTargetException JavaDoc(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             // not available
122
}
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             // not available
131
}
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 JavaDoc[0]); //$NON-NLS-1$
163
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             // for projects only add the contained source folders
179
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 JavaDoc 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) // do the cheaper tests first
198
&& 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 JavaDoc fResult;
207         
208         public SuiteMethodTypesCollector(Collection JavaDoc result) {
209             fResult= result;
210         }
211
212         public void acceptSearchMatch(SearchMatch match) throws CoreException {
213             Object JavaDoc 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 JavaDoc result, IProgressMonitor pm) throws CoreException {
231         // fix for bug 36449 JUnit should constrain tests to selected project
232
// [JUnit]
233
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); //$NON-NLS-1$
238
SearchParticipant[] participants= new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
239         new SearchEngine().search(suitePattern, participants, scope, requestor, pm);
240     }
241     
242 }
243
Popular Tags