From 981feac61b39f23f2aa84993697c9e413d6d160b Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Thu, 11 May 2017 16:45:19 +0300 Subject: [PATCH] junit 5: skip meta annotations to highlight current file only --- .../intellij/execution/junit/JUnitUtil.java | 2 ++ .../JUnit5MalformedParameterizedInspection.kt | 24 +++++++++---------- .../JUnit5MalformedRepeatedTestInspection.kt | 11 +++++---- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/java/execution/impl/src/com/intellij/execution/junit/JUnitUtil.java b/java/execution/impl/src/com/intellij/execution/junit/JUnitUtil.java index 4cff4bd834ea..a7fd6938e534 100644 --- a/java/execution/impl/src/com/intellij/execution/junit/JUnitUtil.java +++ b/java/execution/impl/src/com/intellij/execution/junit/JUnitUtil.java @@ -78,6 +78,8 @@ public class JUnitUtil { public static final Collection TEST5_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(TEST5_ANNOTATION, TEST5_FACTORY_ANNOTATION, CUSTOM_TESTABLE_ANNOTATION)); + public static final Collection TEST5_JUPITER_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(TEST5_ANNOTATION, + TEST5_FACTORY_ANNOTATION)); private static final List INSTANCE_CONFIGS = Arrays.asList(BEFORE_ANNOTATION_NAME, AFTER_ANNOTATION_NAME); private static final List INSTANCE_5_CONFIGS = Arrays.asList(BEFORE_EACH_ANNOTATION_NAME, AFTER_EACH_ANNOTATION_NAME); diff --git a/plugins/junit/src/com/intellij/execution/junit/codeInsight/JUnit5MalformedParameterizedInspection.kt b/plugins/junit/src/com/intellij/execution/junit/codeInsight/JUnit5MalformedParameterizedInspection.kt index ab245528b108..92644c11d0a0 100644 --- a/plugins/junit/src/com/intellij/execution/junit/codeInsight/JUnit5MalformedParameterizedInspection.kt +++ b/plugins/junit/src/com/intellij/execution/junit/codeInsight/JUnit5MalformedParameterizedInspection.kt @@ -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) diff --git a/plugins/junit/src/com/intellij/execution/junit/codeInsight/JUnit5MalformedRepeatedTestInspection.kt b/plugins/junit/src/com/intellij/execution/junit/codeInsight/JUnit5MalformedRepeatedTestInspection.kt index 9d1fcade2800..a1f34f293e80 100644 --- a/plugins/junit/src/com/intellij/execution/junit/codeInsight/JUnit5MalformedRepeatedTestInspection.kt +++ b/plugins/junit/src/com/intellij/execution/junit/codeInsight/JUnit5MalformedRepeatedTestInspection.kt @@ -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 {