From 8e42e396daa70de2c4050f6fa147ebef893fff6f Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Tue, 2 May 2017 21:35:26 +0300 Subject: [PATCH] junit 5: don't warn about implicit injected parameters or methods with explicit ExtendWith annotation (IDEA-172259) --- .../siyeh/ig/junit/JUnitCommonClassNames.java | 3 +++ .../JUnit5MalformedParameterizedInspection.kt | 18 +++++++++++++++--- .../JUnit5MalformedParameterizedTest.java | 3 +++ .../MalformedSourcesImplicitParameters.java | 10 ++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 plugins/junit/testData/codeInsight/malformedParameterized/MalformedSourcesImplicitParameters.java 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 df72a841029a..b59e373c613f 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/JUnitCommonClassNames.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/JUnitCommonClassNames.java @@ -38,4 +38,7 @@ public class JUnitCommonClassNames { 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"; public static final String ORG_JUNIT_JUPITER_API_REPETITION_INFO = "org.junit.jupiter.api.RepetitionInfo"; + public static final String ORG_JUNIT_JUPITER_API_TEST_INFO = "org.junit.jupiter.api.TestInfo"; + public static final String ORG_JUNIT_JUPITER_API_TEST_REPORTER = "org.junit.jupiter.api.TestReporter"; + public static final String ORG_JUNIT_JUPITER_API_EXTENSION_EXTEND_WITH = "org.junit.jupiter.api.extension.ExtendWith"; } 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 1e4da0708864..450c1527e068 100644 --- a/plugins/junit/src/com/intellij/execution/junit/codeInsight/JUnit5MalformedParameterizedInspection.kt +++ b/plugins/junit/src/com/intellij/execution/junit/codeInsight/JUnit5MalformedParameterizedInspection.kt @@ -16,6 +16,7 @@ package com.intellij.execution.junit.codeInsight import com.intellij.codeInsight.AnnotationUtil +import com.intellij.codeInsight.MetaAnnotationUtil import com.intellij.codeInsight.daemon.impl.analysis.JavaGenericsUtil import com.intellij.codeInsight.daemon.impl.quickfix.DeleteElementFix import com.intellij.codeInsight.intention.QuickFixFactory @@ -33,6 +34,7 @@ import com.intellij.util.containers.ContainerUtil import com.siyeh.InspectionGadgetsBundle import com.siyeh.ig.junit.JUnitCommonClassNames import org.jetbrains.annotations.Nls +import java.util.* class JUnit5MalformedParameterizedInspection : BaseJavaBatchLocalInspectionTool() { @@ -84,8 +86,8 @@ class JUnit5MalformedParameterizedInspection : BaseJavaBatchLocalInspectionTool( if (valuesSource == null && enumSource == null && noMultiArgsProvider) { holder.registerProblem(parameterizedAnnotation, "No sources are provided, the suite would be empty") } - else if (method.parameterList.parametersCount > 1 && noMultiArgsProvider) { - holder.registerProblem(valuesSource ?: enumSource!!, "Multiple parameters are not supported by this source") + else if (noMultiArgsProvider && hasMultipleParameters(method)) { + holder.registerProblem(valuesSource ?: enumSource!!, "Multiple parameters are not supported by this source") } } } @@ -147,7 +149,7 @@ class JUnit5MalformedParameterizedInspection : BaseJavaBatchLocalInspectionTool( holder.registerProblem(attributeValue, "Method source \'$providerName\' must have one of the following return type: Stream, Iterator, Iterable or Object[]") } - else if (method.parameterList.parametersCount > 1 && !isArgumentsInheritor(componentType)) { + else if (hasMultipleParameters(method) && !isArgumentsInheritor(componentType)) { holder.registerProblem(attributeValue, "Multiple parameters have to be wrapped in Arguments") } } @@ -214,4 +216,14 @@ class JUnit5MalformedParameterizedInspection : BaseJavaBatchLocalInspectionTool( } } } + + private fun hasMultipleParameters(method: PsiMethod): Boolean { + return method.parameterList.parameters + .filter { it -> + !InheritanceUtil.isInheritor(it.type, JUnitCommonClassNames.ORG_JUNIT_JUPITER_API_TEST_INFO) && + !InheritanceUtil.isInheritor(it.type, JUnitCommonClassNames.ORG_JUNIT_JUPITER_API_TEST_REPORTER) + } + .count() > 1 && !MetaAnnotationUtil.isMetaAnnotated(method, Collections.singleton( + JUnitCommonClassNames.ORG_JUNIT_JUPITER_API_EXTENSION_EXTEND_WITH)) + } } diff --git a/plugins/junit/test/com/intellij/execution/junit/codeInsight/JUnit5MalformedParameterizedTest.java b/plugins/junit/test/com/intellij/execution/junit/codeInsight/JUnit5MalformedParameterizedTest.java index dca24b580e98..e9f298e9cc50 100644 --- a/plugins/junit/test/com/intellij/execution/junit/codeInsight/JUnit5MalformedParameterizedTest.java +++ b/plugins/junit/test/com/intellij/execution/junit/codeInsight/JUnit5MalformedParameterizedTest.java @@ -35,6 +35,8 @@ public class JUnit5MalformedParameterizedTest extends LightInspectionTestCase { "public @interface ParameterizedTest {}"); addEnvironmentClass("package org.junit.jupiter.api;\n" + "public @interface Test {}"); + addEnvironmentClass("package org.junit.jupiter.api;\n" + + "public interface TestInfo {}"); addEnvironmentClass("package org.junit.jupiter.params.provider;\n" + "public @interface MethodSource {String[] names();}"); addEnvironmentClass("package org.junit.jupiter.params.provider;\n" + @@ -53,6 +55,7 @@ public class JUnit5MalformedParameterizedTest extends LightInspectionTestCase { public void testMalformedSources() { doTest(); } public void testMalformedSourcesArguments() { doTest(); } public void testMalformedSourcesImplicitConversion() { doTest(); } + public void testMalformedSourcesImplicitParameters() { doTest(); } @Override protected String getBasePath() { diff --git a/plugins/junit/testData/codeInsight/malformedParameterized/MalformedSourcesImplicitParameters.java b/plugins/junit/testData/codeInsight/malformedParameterized/MalformedSourcesImplicitParameters.java new file mode 100644 index 000000000000..77d7406fc19c --- /dev/null +++ b/plugins/junit/testData/codeInsight/malformedParameterized/MalformedSourcesImplicitParameters.java @@ -0,0 +1,10 @@ +import org.junit.jupiter.api.TestInfo; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +class MyJunit5 { + @ParameterizedTest + @ValueSource(strings = "foo") + void testWithRegularParameterResolver(String argument, TestInfo testReporter) { + } +}