mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
junit 5: malformed source/@Test annotations (IDEA-172448)
This commit is contained in:
+8
-1
@@ -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";
|
||||
|
||||
+29
-1
@@ -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)
|
||||
|
||||
}
|
||||
|
||||
@@ -68,4 +68,8 @@ class ParameterizedTestsDemo {
|
||||
<warning descr="Suspicious combination @Test and @ParameterizedTest">@org.junit.jupiter.api.Test</warning>
|
||||
void testWithTestAnnotation(int i) { }
|
||||
|
||||
@ValueSource(ints = {1})
|
||||
<warning descr="Suspicious combination @Test and parameterized source">@org.junit.jupiter.api.Test</warning>
|
||||
void testWithTestAnnotationNoParameterized(int i) { }
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user