mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IG: warn about multiple data provider methods, make method static, better quick-fix name
for "Parameterized test class without data provider method" inspection GitOrigin-RevId: ea89f9902d74015c385b0c828cc3d654f407ca9a
This commit is contained in:
committed by
intellij-monorepo-bot
parent
7275ed3569
commit
7f007c09d9
+33
-4
@@ -4,7 +4,10 @@ package com.siyeh.ig.junit;
|
||||
import com.intellij.codeInsight.AnnotationUtil;
|
||||
import com.intellij.codeInsight.daemon.impl.quickfix.CreateMethodQuickFix;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.openapi.application.WriteAction;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.ReadonlyStatusHandler;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
@@ -19,16 +22,29 @@ import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import com.siyeh.ig.psiutils.TestUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ParameterizedParametersStaticCollectionInspection extends BaseInspection {
|
||||
|
||||
protected static final String PARAMETERS_FQN = "org.junit.runners.Parameterized.Parameters";
|
||||
|
||||
@Override
|
||||
protected InspectionGadgetsFix buildFix(final Object... infos) {
|
||||
if (infos.length == 0) return null;
|
||||
if (infos[0] instanceof PsiClass) {
|
||||
final PsiClass aClass = (PsiClass)infos[0];
|
||||
final String signature = "@" + PARAMETERS_FQN + " public static java.lang.Iterable<java.lang.Object[]> parameters()";
|
||||
return new DelegatingFix(CreateMethodQuickFix.createFix(aClass, signature, ""));
|
||||
return new DelegatingFix(CreateMethodQuickFix.createFix(aClass, signature, "")) {
|
||||
@Override
|
||||
public @NotNull String getName() {
|
||||
return getFamilyName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getFamilyName() {
|
||||
return InspectionGadgetsBundle.message("fix.data.provider.create.method.fix.name");
|
||||
}
|
||||
};
|
||||
}
|
||||
return new InspectionGadgetsFix() {
|
||||
|
||||
@@ -44,6 +60,13 @@ public class ParameterizedParametersStaticCollectionInspection extends BaseInspe
|
||||
return;
|
||||
}
|
||||
final PsiMethod method = (PsiMethod)element;
|
||||
WriteAction.run(() -> {
|
||||
final VirtualFile vFile = method.getContainingFile().getVirtualFile();
|
||||
if (ReadonlyStatusHandler.getInstance(method.getProject()).ensureFilesWritable(List.of(vFile)).hasReadonlyFiles()) {
|
||||
return;
|
||||
}
|
||||
method.getModifierList().setModifierProperty(PsiModifier.STATIC, true);
|
||||
});
|
||||
final PsiType type = (PsiType)infos[1];
|
||||
final ChangeSignatureProcessor csp =
|
||||
new ChangeSignatureProcessor(project, method, false, PsiModifier.PUBLIC, method.getName(), type, new ParameterInfoImpl[0]);
|
||||
@@ -67,6 +90,9 @@ public class ParameterizedParametersStaticCollectionInspection extends BaseInspe
|
||||
@Override
|
||||
@NotNull
|
||||
protected String buildErrorString(Object... infos) {
|
||||
if (infos.length == 0) {
|
||||
return InspectionGadgetsBundle.message("fix.data.provider.multiple.methods.problem");
|
||||
}
|
||||
return infos.length > 1
|
||||
? InspectionGadgetsBundle.message("fix.data.provider.signature.incorrect.problem")
|
||||
: InspectionGadgetsBundle.message("fix.data.provider.signature.missing.method.problem");
|
||||
@@ -86,12 +112,12 @@ public class ParameterizedParametersStaticCollectionInspection extends BaseInspe
|
||||
if (iterableClass == null) {
|
||||
return;
|
||||
}
|
||||
boolean methodFound = false;
|
||||
int methodFound = 0;
|
||||
for (PsiMethod method : aClass.getMethods()) {
|
||||
if (!AnnotationUtil.isAnnotated(method, PARAMETERS_FQN, 0)) {
|
||||
continue;
|
||||
}
|
||||
methodFound = true;
|
||||
methodFound++;
|
||||
final boolean notPublic = !method.hasModifierProperty(PsiModifier.PUBLIC);
|
||||
final boolean notStatic = !method.hasModifierProperty(PsiModifier.STATIC);
|
||||
|
||||
@@ -113,9 +139,12 @@ public class ParameterizedParametersStaticCollectionInspection extends BaseInspe
|
||||
registerMethodError(method, signatureText, returnType);
|
||||
}
|
||||
}
|
||||
if (!methodFound) {
|
||||
if (methodFound == 0) {
|
||||
registerClassError(aClass, aClass);
|
||||
}
|
||||
else if (methodFound > 1) {
|
||||
registerClassError(aClass);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
@org.junit.runner.RunWith(org.junit.runners.Parameterized.class)
|
||||
public class Createmethod {
|
||||
@Parameterized.Parameters
|
||||
public static Iterable<Object[]> parameters() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
@org.junit.runner.RunWith(org.junit.runners.Parameterized.class)
|
||||
public class <warning descr="Multiple @Parameters data provider methods present in class 'MultipleMethods'">MultipleMethods</warning> {
|
||||
|
||||
@org.junit.runners.Parameterized.Parameters
|
||||
public static Object[] parameters() {
|
||||
return null;
|
||||
}
|
||||
@org.junit.runners.Parameterized.Parameters
|
||||
public static Object[] parameters2() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
@org.junit.runner.RunWith(org.junit.runners.Parameterized.class)
|
||||
public class Wrongsignature {
|
||||
@org.junit.runners.Parameterized.Parameters
|
||||
public static java.util.Collection regExValues() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+17
-2
@@ -1,9 +1,12 @@
|
||||
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.siyeh.ig.junit;
|
||||
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.testFramework.LightProjectDescriptor;
|
||||
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.LightJavaInspectionTestCase;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ParameterizedParametersStaticCollectionInspectionTest extends LightJavaCodeInsightFixtureTestCase {
|
||||
@@ -12,7 +15,6 @@ public class ParameterizedParametersStaticCollectionInspectionTest extends Light
|
||||
return LightJavaInspectionTestCase.INSPECTION_GADGETS_TEST_DATA_PATH + "com/siyeh/igtest/junit/parameterized";
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@@ -39,15 +41,28 @@ public class ParameterizedParametersStaticCollectionInspectionTest extends Light
|
||||
myFixture.testHighlighting(getTestName(false) + ".java");
|
||||
}
|
||||
|
||||
private void checkQuickFix(@NotNull @Nls String intentionName) {
|
||||
final IntentionAction intention = myFixture.getAvailableIntention(intentionName);
|
||||
assertNotNull(intention);
|
||||
myFixture.launchAction(intention);
|
||||
myFixture.checkResultByFile(getTestName(false) + ".after.java");
|
||||
}
|
||||
|
||||
public void testCreatemethod() {
|
||||
doTest();
|
||||
checkQuickFix(InspectionGadgetsBundle.message("fix.data.provider.create.method.fix.name"));
|
||||
}
|
||||
|
||||
public void testWrongsignature() {
|
||||
doTest();
|
||||
checkQuickFix(InspectionGadgetsBundle.message("fix.data.provider.signature.fix.name", "public static Collection regExValues()"));
|
||||
}
|
||||
|
||||
public void testWrongsignature() { doTest(); }
|
||||
public void testWrongsignature1() { doTest(); }
|
||||
public void testWrongsignature2() { doTest(); }
|
||||
public void testWrongsignature3() { doTest(); }
|
||||
public void testCorrectSignature() { doTest(); }
|
||||
public void testCorrectSignature2() { doTest(); }
|
||||
public void testMultipleMethods() { doTest(); }
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user