Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
package org.eclipse.jdt.experimental.junit.codemining.tester;

import java.util.Arrays;
import java.util.Optional;

import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IAnnotation;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.experimental.JavaCodeMiningPlugin;

public interface IJUnitMethodTester {

public static boolean isTestMethod(IMethod method, boolean onlyPublicMethod, String[] annotations) {
public static boolean isTestMethod(IMethod method, boolean onlyPublicMethod, String[] annotationSignatures) {
if (isMethod(method, onlyPublicMethod)) {
for (String annotation : annotations) {
if (method.getAnnotation(annotation).exists()) {
return true;
try {
Optional<IAnnotation> annotation = Arrays.stream(method.getAnnotations())
.filter(a -> annotationSignatures[0].equals(a.getElementName())).findFirst();
if(annotation.isPresent()) {
String[][] resolveType = method.getDeclaringType().resolveType(annotation.get().getElementName());
if (resolveType.length > 0) {
return Arrays.equals(annotationSignatures, 1, annotationSignatures.length - 1, resolveType[0], 0, resolveType[0].length - 1);
}
}
} catch (JavaModelException e) {
JavaCodeMiningPlugin.getDefault().getLog().error(e.getMessage(), e);
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
package org.eclipse.jdt.experimental.junit.codemining.tester;

import java.util.stream.Stream;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.experimental.JavaCodeMiningPlugin;

public class JUnit3MethodTester implements IJUnitMethodTester {

public static final IJUnitMethodTester INSTANCE = new JUnit3MethodTester();

@Override
public boolean isTestMethod(IMethod method) {
return IJUnitMethodTester.isMethod(method, true) && method.getElementName().startsWith("test");
return IJUnitMethodTester.isMethod(method, true) && isTestClass(method.getDeclaringType())
&& method.getElementName().startsWith("test");
}

private boolean isTestClass(IType type) {
try {
return Stream.of(type.newSupertypeHierarchy(new NullProgressMonitor()).getAllSupertypes(type))
.filter(t -> "junit.framework.TestCase".equals(t.getFullyQualifiedName())).findFirst().isPresent();
} catch (JavaModelException e) {
JavaCodeMiningPlugin.getDefault().getLog().error("isTestClass check for JUnit3 failed.", e);
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ public class JUnit4MethodTester implements IJUnitMethodTester {

public static final IJUnitMethodTester INSTANCE = new JUnit4MethodTester();

private final String[] JUNIT_TEST_ANNOTATIONS = new String[] { "Test", "org.junit.Test" };
private final String[][] JUNIT_TEST_ANNOTATIONS = new String[][] { { "Test", "org.junit", "Test" },
{ "org.junit.Test", "org.junit", "Test" } };

@Override
public boolean isTestMethod(IMethod method) {
return IJUnitMethodTester.isTestMethod(method, true, JUNIT_TEST_ANNOTATIONS);
return IJUnitMethodTester.isTestMethod(method, true, JUNIT_TEST_ANNOTATIONS[0])
|| IJUnitMethodTester.isTestMethod(method, true, JUNIT_TEST_ANNOTATIONS[1]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ public class JUnit5MethodTester implements IJUnitMethodTester {

public static final IJUnitMethodTester INSTANCE = new JUnit5MethodTester();

private final String[] JUNIT_TEST_ANNOTATIONS = new String[] { "Test", "org.junit.jupiter.api.Test", "ParameterizedTest", "org.junit.jupiter.params.ParameterizedTest" };
private final String[][] JUNIT_TEST_ANNOTATIONS = new String[][] { { "Test", "org.junit.jupiter.api", "Test" },
{ "org.junit.jupiter.api.Test", "org.junit.jupiter.api", "Test" },
{ "ParameterizedTest", "org.junit.jupiter.params", "ParameterizedTest" },
{ "org.junit.jupiter.params.ParameterizedTest", "org.junit.jupiter.params", "ParameterizedTest" } };

@Override
public boolean isTestMethod(IMethod method) {
return IJUnitMethodTester.isTestMethod(method, false, JUNIT_TEST_ANNOTATIONS);
return IJUnitMethodTester.isTestMethod(method, false, JUNIT_TEST_ANNOTATIONS[0])
|| IJUnitMethodTester.isTestMethod(method, false, JUNIT_TEST_ANNOTATIONS[1])
|| IJUnitMethodTester.isTestMethod(method, false, JUNIT_TEST_ANNOTATIONS[2])
|| IJUnitMethodTester.isTestMethod(method, false, JUNIT_TEST_ANNOTATIONS[3]);
}

}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<repository>
<id>photon</id>
<layout>p2</layout>
<url>http://download.eclipse.org/releases/photon/</url>
<url>http://download.eclipse.org/releases/2020-06/</url>
</repository>
<repository>
<id>egit</id>
Expand Down