diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/JUnitCommonClassNames.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/JUnitCommonClassNames.java index b59e373c613f..cb905cd1abc4 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/JUnitCommonClassNames.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/JUnitCommonClassNames.java @@ -30,10 +30,17 @@ public class JUnitCommonClassNames { public static final String ORG_JUNIT_JUPITER_PARAMS_PROVIDER_METHOD_SOURCE = "org.junit.jupiter.params.provider.MethodSource"; public static final String ORG_JUNIT_JUPITER_PARAMS_VALUES_SOURCE = "org.junit.jupiter.params.provider.ValueSource"; public static final String ORG_JUNIT_JUPITER_PARAMS_ENUM_SOURCE = "org.junit.jupiter.params.provider.EnumSource"; - public static final String ORG_JUNIT_JUPITER_PARAMS_PROVIDER_ARGUMENTS = "org.junit.jupiter.params.provider.Arguments"; public static final String ORG_JUNIT_JUPITER_PARAMS_PROVIDER_CSV_SOURCE = "org.junit.jupiter.params.provider.CsvSource"; public static final String ORG_JUNIT_JUPITER_PARAMS_PROVIDER_CSV_FILE_SOURCE = "org.junit.jupiter.params.provider.CsvFileSource"; public static final String ORG_JUNIT_JUPITER_PARAMS_PROVIDER_ARGUMENTS_SOURCE = "org.junit.jupiter.params.provider.ArgumentsSource"; + public static final String[] SOURCE_ANNOTATIONS = { + ORG_JUNIT_JUPITER_PARAMS_PROVIDER_METHOD_SOURCE, + ORG_JUNIT_JUPITER_PARAMS_VALUES_SOURCE, + ORG_JUNIT_JUPITER_PARAMS_ENUM_SOURCE, ORG_JUNIT_JUPITER_PARAMS_PROVIDER_CSV_SOURCE, + ORG_JUNIT_JUPITER_PARAMS_PROVIDER_CSV_FILE_SOURCE, + ORG_JUNIT_JUPITER_PARAMS_PROVIDER_ARGUMENTS_SOURCE + }; + public static final String ORG_JUNIT_JUPITER_PARAMS_PROVIDER_ARGUMENTS = "org.junit.jupiter.params.provider.Arguments"; public static final String ORG_JUNIT_JUPITER_PARAMS_CONVERTER_CONVERT_WITH = "org.junit.jupiter.params.converter.ConvertWith"; public static final String ORG_JUNIT_JUPITER_API_TEST = "org.junit.jupiter.api.Test"; public static final String ORG_JUNIT_JUPITER_API_REPEATED_TEST = "org.junit.jupiter.api.RepeatedTest"; 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 7af2300dda31..64f9f5b5a02e 100644 --- a/plugins/junit/src/com/intellij/execution/junit/codeInsight/JUnit5MalformedParameterizedInspection.kt +++ b/plugins/junit/src/com/intellij/execution/junit/codeInsight/JUnit5MalformedParameterizedInspection.kt @@ -22,12 +22,17 @@ import com.intellij.codeInsight.daemon.impl.quickfix.CreateMethodQuickFix import com.intellij.codeInsight.daemon.impl.quickfix.DeleteElementFix import com.intellij.codeInsight.intention.QuickFixFactory import com.intellij.codeInspection.BaseJavaBatchLocalInspectionTool +import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement import com.intellij.codeInspection.ProblemHighlightType import com.intellij.codeInspection.ProblemsHolder import com.intellij.execution.junit.codeInsight.references.MethodSourceReference +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.project.Project import com.intellij.openapi.projectRoots.JavaSdkVersion import com.intellij.openapi.projectRoots.JavaVersionService +import com.intellij.openapi.util.text.StringUtil import com.intellij.psi.* +import com.intellij.psi.codeStyle.JavaCodeStyleManager import com.intellij.psi.util.InheritanceUtil import com.intellij.psi.util.PsiUtil import com.intellij.psi.util.TypeConversionUtil @@ -56,8 +61,8 @@ class JUnit5MalformedParameterizedInspection : BaseJavaBatchLocalInspectionTool( override fun visitMethod(method: PsiMethod) { val modifierList = method.modifierList val parameterizedAnnotation = modifierList.findAnnotation(JUnitCommonClassNames.ORG_JUNIT_JUPITER_PARAMS_PARAMETERIZED_TEST) + val testAnnotation = modifierList.findAnnotation(JUnitCommonClassNames.ORG_JUNIT_JUPITER_API_TEST) if (parameterizedAnnotation != null) { - val testAnnotation = modifierList.findAnnotation(JUnitCommonClassNames.ORG_JUNIT_JUPITER_API_TEST) if (testAnnotation != null && method.parameterList.parametersCount > 0) { holder.registerProblem(testAnnotation, "Suspicious combination @Test and @ParameterizedTest", @@ -91,6 +96,16 @@ class JUnit5MalformedParameterizedInspection : BaseJavaBatchLocalInspectionTool( holder.registerProblem(valuesSource ?: enumSource!!, "Multiple parameters are not supported by this source") } } + else if (testAnnotation != null) { + for (annotation in JUnitCommonClassNames.SOURCE_ANNOTATIONS) { + if (modifierList.findAnnotation(annotation) != null) { + holder.registerProblem(testAnnotation, + "Suspicious combination @Test and parameterized source", + ChangeAnnotationFix(testAnnotation, JUnitCommonClassNames.ORG_JUNIT_JUPITER_PARAMS_PARAMETERIZED_TEST)) + break; + } + } + } } private fun checkEnumSource(method: PsiMethod, enumSource: PsiAnnotation) { @@ -235,3 +250,16 @@ class JUnit5MalformedParameterizedInspection : BaseJavaBatchLocalInspectionTool( JUnitCommonClassNames.ORG_JUNIT_JUPITER_API_EXTENSION_EXTEND_WITH)) } } + + +class ChangeAnnotationFix(testAnnotation: PsiAnnotation, val targetAnnotation: String) : LocalQuickFixAndIntentionActionOnPsiElement(testAnnotation) { + override fun getFamilyName() = "Replace annotation" + + 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)); + } + + override fun getText() = "Change to " + StringUtil.getShortName(targetAnnotation) + +} diff --git a/plugins/junit/testData/codeInsight/malformedParameterized/MalformedSources.java b/plugins/junit/testData/codeInsight/malformedParameterized/MalformedSources.java index c10df71d92a6..a2482d1a87a4 100644 --- a/plugins/junit/testData/codeInsight/malformedParameterized/MalformedSources.java +++ b/plugins/junit/testData/codeInsight/malformedParameterized/MalformedSources.java @@ -68,4 +68,8 @@ class ParameterizedTestsDemo { @org.junit.jupiter.api.Test void testWithTestAnnotation(int i) { } + @ValueSource(ints = {1}) + @org.junit.jupiter.api.Test + void testWithTestAnnotationNoParameterized(int i) { } + }