1 /*******************************************************************************
2  * Copyright (c) 2006, 2015 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *     David Saff (saff@mit.edu) - initial API and implementation
14  *             (bug 102632: [JUnit] Support for JUnit 4.)
15  *******************************************************************************/
16 package org.eclipse.jdt.internal.junit.launcher;
17 
18 import java.util.Set;
19 
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.NullProgressMonitor;
23 import org.eclipse.core.runtime.SubProgressMonitor;
24 
25 import org.eclipse.jdt.core.Flags;
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.IRegion;
30 import org.eclipse.jdt.core.IType;
31 import org.eclipse.jdt.core.ITypeHierarchy;
32 import org.eclipse.jdt.core.JavaModelException;
33 
34 import org.eclipse.jdt.internal.junit.JUnitCorePlugin;
35 import org.eclipse.jdt.internal.junit.JUnitMessages;
36 import org.eclipse.jdt.internal.junit.util.CoreTestSearchEngine;
37 
38 public class JUnit3TestFinder implements ITestFinder {
39 
40 	@Override
findTestsInContainer(IJavaElement element, Set<IType> result, IProgressMonitor pm)41 	public void findTestsInContainer(IJavaElement element, Set<IType> 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((IType) element);
54 				}
55 			} else if (element instanceof ICompilationUnit) {
56 				IType[] types= ((ICompilationUnit) element).getAllTypes();
57 				for (IType type : types) {
58 					if (isTest(type)) {
59 						result.add(type);
60 					}
61 				}
62 			} else {
63 				findTestCases(element, result, new SubProgressMonitor(pm, 7));
64 				if (pm.isCanceled()) {
65 					return;
66 				}
67 				CoreTestSearchEngine.findSuiteMethods(element, result, new SubProgressMonitor(pm, 3));
68 			}
69 			if (pm.isCanceled()) {
70 				return;
71 			}
72 		} finally {
73 			pm.done();
74 		}
75 	}
76 
findTestCases(IJavaElement element, Set<IType> result, IProgressMonitor pm)77 	private static void findTestCases(IJavaElement element, Set<IType> result, IProgressMonitor pm) throws JavaModelException {
78 		IJavaProject javaProject= element.getJavaProject();
79 
80 		IType testCaseType= javaProject.findType(JUnitCorePlugin.TEST_INTERFACE_NAME);
81 		if (testCaseType == null)
82 			return;
83 
84 		IRegion region= CoreTestSearchEngine.getRegion(element);
85 		ITypeHierarchy typeHierarchy= javaProject.newTypeHierarchy(testCaseType, region, pm);
86 		CoreTestSearchEngine.findTestImplementorClasses(typeHierarchy, testCaseType, region, result);
87 	}
88 
89 	@Override
isTest(IType type)90 	public boolean isTest(IType type) throws JavaModelException {
91 		return CoreTestSearchEngine.isAccessibleClass(type) && (CoreTestSearchEngine.hasSuiteMethod(type) || isTestImplementor(type));
92 	}
93 
isTestImplementor(IType type)94 	private static boolean isTestImplementor(IType type) throws JavaModelException {
95 		if (!Flags.isAbstract(type.getFlags()) && CoreTestSearchEngine.isTestImplementor(type)) {
96 			return true;
97 		}
98 		return false;
99 	}
100 }
101