Skip to content
Closed
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
Expand Up @@ -37,30 +37,23 @@
* @author Phillip Webb
*/
public class SpringJUnit5Check extends AbstractSpringCheck {

private static final String JUNIT4_TEST_ANNOTATION = "org.junit.Test";

private static final List<String> TEST_ANNOTATIONS;
static {
Set<String> annotations = new LinkedHashSet<>();
addAnnotation(annotations, JUNIT4_TEST_ANNOTATION);
addAnnotation(annotations, "org.junit.jupiter.api.RepeatedTest");
addAnnotation(annotations, "org.junit.jupiter.api.Test");
addAnnotation(annotations, "org.junit.jupiter.api.TestFactory");
addAnnotation(annotations, "org.junit.jupiter.api.TestTemplate");
addAnnotation(annotations, "org.junit.jupiter.params.ParameterizedTest");
TEST_ANNOTATIONS = Collections.unmodifiableList(new ArrayList<>(annotations));
}

private static final List<String> LIFECYCLE_ANNOTATIONS;
static {
Set<String> annotations = new LinkedHashSet<>();
addAnnotation(annotations, "org.junit.jupiter.api.BeforeAll");
addAnnotation(annotations, "org.junit.jupiter.api.BeforeEach");
addAnnotation(annotations, "org.junit.jupiter.api.AfterAll");
addAnnotation(annotations, "org.junit.jupiter.api.AfterEach");
LIFECYCLE_ANNOTATIONS = Collections.unmodifiableList(new ArrayList<>(annotations));
}
private static final List<String> TEST_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(
"RepeatedTest",
"Test",
"TestFactory",
"TestTemplate",
"ParameterizedTest"
));

private static final List<String> LIFECYCLE_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(
"BeforeAll",
"BeforeEach",
"AfterAll",
"AfterEach"
)
);

private static final Set<String> BANNED_IMPORTS;
static {
Expand All @@ -75,14 +68,6 @@ public class SpringJUnit5Check extends AbstractSpringCheck {
BANNED_IMPORTS = Collections.unmodifiableSet(bannedImports);
}

private static void addAnnotation(Set<String> annotations, String annotation) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that removing this method and the usage of it will introduce a regression. The javadoc for AnnotationUtil.containsAnnotation(DetailAst, List<String>) says that it "accepts both simple and fully-qualified names". If a user has annotated a method with, for example, @org.junit.Test rather than importing org.junit.test and annotating with @Test, this change will cause the usage to be missed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method accepts both simple and fully-qualified names, e.g. "Override" will match both java.lang.Override and Override.

Which means that if we are using only the non FQN then we receive a check for both, there is also a test for that:
JUnit5BadAnnotation

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we are using only the non FQN

I don't think we can't be certain that this is the case. One user could write @org.junit.Test while another could write @Test along with import org.junit.Test.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant is that I believe it catches both

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I see what you mean now. I agree that just the simple names should be sufficient.

We try to keep issues and pull requests focused on a single change. To that end, would you mind splitting the corrections to JUnit5BadModifier and the simplifications above into separate pull requests?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I have updated this one to be the refactor, I'll create a new one for the test

annotations.add(annotation);
int lastDot = annotation.lastIndexOf(".");
if (lastDot != -1) {
annotations.add(annotation.substring(lastDot + 1));
}
}

private List<String> unlessImports = new ArrayList<>();

private final List<DetailAST> testMethods = new ArrayList<>();
Expand Down