junit 5: skip meta annotations to highlight current file only

This commit is contained in:
Anna Kozlova
2017-05-11 18:35:39 +03:00
parent 071f35ba7d
commit 981feac61b
3 changed files with 20 additions and 17 deletions
@@ -78,6 +78,8 @@ public class JUnitUtil {
public static final Collection<String> TEST5_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(TEST5_ANNOTATION,
TEST5_FACTORY_ANNOTATION,
CUSTOM_TESTABLE_ANNOTATION));
public static final Collection<String> TEST5_JUPITER_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(TEST5_ANNOTATION,
TEST5_FACTORY_ANNOTATION));
private static final List<String> INSTANCE_CONFIGS = Arrays.asList(BEFORE_ANNOTATION_NAME, AFTER_ANNOTATION_NAME);
private static final List<String> INSTANCE_5_CONFIGS = Arrays.asList(BEFORE_EACH_ANNOTATION_NAME, AFTER_EACH_ANNOTATION_NAME);
@@ -60,18 +60,18 @@ class JUnit5MalformedParameterizedInspection : BaseJavaBatchLocalInspectionTool(
return object : JavaElementVisitor() {
override fun visitMethod(method: PsiMethod) {
val parameterizedAnnotation = MetaAnnotationUtil.findMetaAnnotations(method, Collections.singletonList(JUnitCommonClassNames.ORG_JUNIT_JUPITER_PARAMS_PARAMETERIZED_TEST)).findFirst().orElse(null)
val testAnnotation = MetaAnnotationUtil.findMetaAnnotations(method, JUnitUtil.TEST5_ANNOTATIONS).findFirst().orElse(null)
if (parameterizedAnnotation != null) {
if (testAnnotation != null && method.parameterList.parametersCount > 0) {
holder.registerProblem(testAnnotation,
val parameterizedAnnotation = AnnotationUtil.findAnnotations(method, Collections.singletonList(JUnitCommonClassNames.ORG_JUNIT_JUPITER_PARAMS_PARAMETERIZED_TEST))
val testAnnotation = AnnotationUtil.findAnnotations(method, JUnitUtil.TEST5_JUPITER_ANNOTATIONS)
if (parameterizedAnnotation.isNotEmpty()) {
if (testAnnotation.isNotEmpty() && method.parameterList.parametersCount > 0) {
holder.registerProblem(testAnnotation[0],
"Suspicious combination @Test and @ParameterizedTest",
DeleteElementFix(testAnnotation))
DeleteElementFix(testAnnotation[0]))
}
var noMultiArgsProvider = true
var source : PsiAnnotation? = null
MetaAnnotationUtil.findMetaAnnotations(method, JUnitCommonClassNames.SOURCE_ANNOTATIONS).forEach {
AnnotationUtil.findAnnotations(method, JUnitCommonClassNames.SOURCE_ANNOTATIONS).forEach {
when (it.qualifiedName) {
JUnitCommonClassNames.ORG_JUNIT_JUPITER_PARAMS_PROVIDER_METHOD_SOURCE -> {
checkMethodSource(method, it)
@@ -95,17 +95,17 @@ class JUnit5MalformedParameterizedInspection : BaseJavaBatchLocalInspectionTool(
if (noMultiArgsProvider) {
if (source == null) {
holder.registerProblem(parameterizedAnnotation, "No sources are provided, the suite would be empty")
holder.registerProblem(parameterizedAnnotation[0], "No sources are provided, the suite would be empty")
}
else if (hasMultipleParameters(method)) {
holder.registerProblem(source!!, "Multiple parameters are not supported by this source")
}
}
}
else if (testAnnotation != null && MetaAnnotationUtil.isMetaAnnotated(method, JUnitCommonClassNames.SOURCE_ANNOTATIONS)) {
holder.registerProblem(testAnnotation,
else if (testAnnotation.isNotEmpty() && MetaAnnotationUtil.isMetaAnnotated(method, JUnitCommonClassNames.SOURCE_ANNOTATIONS)) {
holder.registerProblem(testAnnotation[0],
"Suspicious combination @Test and parameterized source",
ChangeAnnotationFix(testAnnotation, JUnitCommonClassNames.ORG_JUNIT_JUPITER_PARAMS_PARAMETERIZED_TEST))
ChangeAnnotationFix(testAnnotation[0], JUnitCommonClassNames.ORG_JUNIT_JUPITER_PARAMS_PARAMETERIZED_TEST))
}
}
@@ -258,7 +258,7 @@ class ChangeAnnotationFix(testAnnotation: PsiAnnotation, val targetAnnotation: S
override fun invoke(project: Project, file: PsiFile, editor: Editor?, startElement: PsiElement, endElement: PsiElement) {
val annotation = JavaPsiFacade.getElementFactory(project).createAnnotationFromText("@" + targetAnnotation, startElement)
JavaCodeStyleManager.getInstance(project).shortenClassReferences(startElement.replace(annotation));
JavaCodeStyleManager.getInstance(project).shortenClassReferences(startElement.replace(annotation))
}
override fun getText() = "Change to " + StringUtil.getShortName(targetAnnotation)
@@ -15,6 +15,7 @@
*/
package com.intellij.execution.junit.codeInsight
import com.intellij.codeInsight.AnnotationUtil
import com.intellij.codeInsight.MetaAnnotationUtil
import com.intellij.codeInsight.daemon.impl.quickfix.DeleteElementFix
import com.intellij.codeInspection.BaseJavaBatchLocalInspectionTool
@@ -49,10 +50,10 @@ class JUnit5MalformedRepeatedTestInspection : BaseJavaBatchLocalInspectionTool()
val modifierList = method.modifierList
val repeatedAnno = modifierList.findAnnotation(JUnitCommonClassNames.ORG_JUNIT_JUPITER_API_REPEATED_TEST)
if (repeatedAnno != null) {
val testAnno = MetaAnnotationUtil.findMetaAnnotations(method, JUnitUtil.TEST5_ANNOTATIONS).findFirst().orElse(null)
if (testAnno != null) {
holder.registerProblem(testAnno, "Suspicious combination @Test and @RepeatedTest",
DeleteElementFix(testAnno))
val testAnno = AnnotationUtil.findAnnotations(method, JUnitUtil.TEST5_JUPITER_ANNOTATIONS)
if (testAnno.isNotEmpty()) {
holder.registerProblem(testAnno[0], "Suspicious combination @Test and @RepeatedTest",
DeleteElementFix(testAnno[0]))
}
val repeatedNumber = repeatedAnno.findDeclaredAttributeValue("value")
if (repeatedNumber is PsiExpression) {
@@ -67,7 +68,7 @@ class JUnit5MalformedRepeatedTestInspection : BaseJavaBatchLocalInspectionTool()
val repetitionType = JavaPsiFacade.getElementFactory(holder.project).createType(repetitionInfo!!)
val repetitionInfoParam = method.parameterList.parameters.find { it.type.isAssignableFrom(repetitionType) }
if (repetitionInfoParam != null) {
if (MetaAnnotationUtil.isMetaAnnotated(method, JUnitUtil.TEST5_ANNOTATIONS)) {
if (MetaAnnotationUtil.isMetaAnnotated(method, JUnitUtil.TEST5_JUPITER_ANNOTATIONS)) {
holder.registerProblem(repetitionInfoParam.nameIdentifier ?: repetitionInfoParam, "RepetitionInfo is injected for @RepeatedTest only")
}
else {