junit 5: support meta annotations in malformed repeated tests

This commit is contained in:
Anna Kozlova
2017-05-11 13:59:44 +03:00
parent f107910ffb
commit 4d7ef9844a
2 changed files with 12 additions and 10 deletions
@@ -64,9 +64,13 @@ public class JUnitUtil {
public static final String AFTER_CLASS_ANNOTATION_NAME = "org.junit.AfterClass";
public static final String BEFORE_CLASS_ANNOTATION_NAME = "org.junit.BeforeClass";
public static final Collection<String> TEST5_CONFIG_METHODS = Collections.unmodifiableList(Arrays.asList(BEFORE_EACH_ANNOTATION_NAME,
AFTER_EACH_ANNOTATION_NAME));
public static final String BEFORE_ALL_ANNOTATION_NAME = "org.junit.jupiter.api.BeforeAll";
public static final String AFTER_ALL_ANNOTATION_NAME = "org.junit.jupiter.api.AfterAll";
public static final Collection<String> TEST5_STATIC_CONFIG_METHODS = Collections.unmodifiableList(Arrays.asList(BEFORE_ALL_ANNOTATION_NAME,
AFTER_ALL_ANNOTATION_NAME));
private static final Collection<String> TEST_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(TEST_ANNOTATION,
TEST5_ANNOTATION,
@@ -15,6 +15,7 @@
*/
package com.intellij.execution.junit.codeInsight
import com.intellij.codeInsight.MetaAnnotationUtil
import com.intellij.codeInsight.daemon.impl.quickfix.DeleteElementFix
import com.intellij.codeInspection.BaseJavaBatchLocalInspectionTool
import com.intellij.codeInspection.ProblemsHolder
@@ -48,7 +49,7 @@ class JUnit5MalformedRepeatedTestInspection : BaseJavaBatchLocalInspectionTool()
val modifierList = method.modifierList
val repeatedAnno = modifierList.findAnnotation(JUnitCommonClassNames.ORG_JUNIT_JUPITER_API_REPEATED_TEST)
if (repeatedAnno != null) {
val testAnno = modifierList.findAnnotation(JUnitCommonClassNames.ORG_JUNIT_JUPITER_API_TEST)
val testAnno = MetaAnnotationUtil.findMetaAnnotations(method, JUnitUtil.TEST5_ANNOTATIONS).findFirst().orElse(null)
if (testAnno != null) {
holder.registerProblem(testAnno, "Suspicious combination @Test and @RepeatedTest",
DeleteElementFix(testAnno))
@@ -66,21 +67,18 @@ class JUnit5MalformedRepeatedTestInspection : BaseJavaBatchLocalInspectionTool()
val repetitionType = JavaPsiFacade.getElementFactory(holder.project).createType(repetitionInfo!!)
val repetitionInfoParam = method.parameterList.parameters.find { it.type.isAssignableFrom(repetitionType) }
if (repetitionInfoParam != null) {
val testAnno = modifierList.findAnnotation(JUnitCommonClassNames.ORG_JUNIT_JUPITER_API_TEST)
if (testAnno != null) {
if (MetaAnnotationUtil.isMetaAnnotated(method, JUnitUtil.TEST5_ANNOTATIONS)) {
holder.registerProblem(repetitionInfoParam.nameIdentifier ?: repetitionInfoParam, "RepetitionInfo is injected for @RepeatedTest only")
}
else {
val beforeAll = modifierList.findAnnotation(JUnitUtil.BEFORE_ALL_ANNOTATION_NAME)
val afterAll = modifierList.findAnnotation(JUnitUtil.AFTER_ALL_ANNOTATION_NAME)
if (beforeAll != null || afterAll != null) {
val anno = MetaAnnotationUtil.findMetaAnnotations(method, JUnitUtil.TEST5_STATIC_CONFIG_METHODS).findFirst().orElse(null)
if (anno != null) {
val qName = anno.qualifiedName
holder.registerProblem(repetitionInfoParam.nameIdentifier ?: repetitionInfoParam,
"RepetitionInfo is injected for @BeforeEach/@AfterEach only, but not for " + StringUtil.getShortName((beforeAll ?: afterAll)!!.qualifiedName!!))
"RepetitionInfo is injected for @BeforeEach/@AfterEach only, but not for " + StringUtil.getShortName(qName!!))
}
else {
val beforeEach = modifierList.findAnnotation(JUnitUtil.BEFORE_EACH_ANNOTATION_NAME)
val afterEach = modifierList.findAnnotation(JUnitUtil.AFTER_EACH_ANNOTATION_NAME)
if ((beforeEach != null || afterEach != null) && method.containingClass?.methods?.find { it.modifierList.findAnnotation(JUnitUtil.TEST5_ANNOTATION) != null} != null) {
if (MetaAnnotationUtil.isMetaAnnotated(method, JUnitUtil.TEST5_CONFIG_METHODS) && method.containingClass?.methods?.find { MetaAnnotationUtil.isMetaAnnotated(it, JUnitUtil.TEST5_ANNOTATIONS)} != null) {
holder.registerProblem(repetitionInfoParam.nameIdentifier ?: repetitionInfoParam,
"RepetitionInfo won't be injected for @Test methods")
}