mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-107621 New Inspection: Malformed @DataPoint in JUnit theory
This commit is contained in:
@@ -1316,6 +1316,9 @@
|
||||
<localInspection language="JAVA" shortName="JUnitRule" bundle="com.siyeh.InspectionGadgetsBundle"
|
||||
key="junit.rule.display.name" implementationClass="com.siyeh.ig.junit.JUnitRuleInspection"
|
||||
groupBundle="messages.InspectionsBundle" groupKey="group.names.junit.issues" enabledByDefault="false" level="WARNING"/>
|
||||
<localInspection language="JAVA" shortName="JUnitDatapoint" bundle="com.siyeh.InspectionGadgetsBundle"
|
||||
key="junit.datapoint.display.name" implementationClass="com.siyeh.ig.junit.JUnitDatapointInspection"
|
||||
groupBundle="messages.InspectionsBundle" groupKey="group.names.junit.issues" enabledByDefault="false" level="WARNING"/>
|
||||
<localInspection language="JAVA" suppressId="MessageMissingOnJUnitAssertion" shortName="AssertsWithoutMessages"
|
||||
bundle="com.siyeh.InspectionGadgetsBundle" key="asserts.without.messages.display.name"
|
||||
groupBundle="messages.InspectionsBundle" groupKey="group.names.junit.issues" enabledByDefault="false" level="WARNING"
|
||||
|
||||
@@ -2024,4 +2024,6 @@ assignment.to.superclass.field.display.name=Constructor assigns value to field d
|
||||
assignment.to.superclass.field.problem.descriptor=Assignment to field ''{0}'' defined in superclass ''{1}''
|
||||
junit.rule.display.name=Malformed @Rule/@ClassRule field
|
||||
junit.rule.problem.descriptor=Fields annotated with @{0} should be {1}
|
||||
junit.rule.type.problem.descriptor=Field type should be subtype of org.junit.rules.TestRule
|
||||
junit.rule.type.problem.descriptor=Field type should be subtype of org.junit.rules.TestRule
|
||||
junit.datapoint.display.name=Malformed @DataPoint field
|
||||
junit.datapoint.problem.descriptor=Fields annotated with @DataPoint should be {0}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.siyeh.ig.junit;
|
||||
|
||||
import com.intellij.codeInsight.AnnotationUtil;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiField;
|
||||
import com.intellij.psi.PsiModifier;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* User: ddt
|
||||
* Date: 5/22/13
|
||||
*/
|
||||
public class JUnitDatapointInspection extends BaseInspection {
|
||||
public static final String DATAPOINT_FQN = "org.junit.experimental.theories.DataPoint";
|
||||
|
||||
|
||||
@Nls
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDisplayName() {
|
||||
return InspectionGadgetsBundle.message("junit.datapoint.display.name");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String buildErrorString(Object... infos) {
|
||||
return (String)infos[0];
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected InspectionGadgetsFix buildFix(Object... infos) {
|
||||
return infos.length > 1 ? new MakePublicStaticFix((String)infos[1], true) : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseInspectionVisitor buildVisitor() {
|
||||
return new BaseInspectionVisitor() {
|
||||
@Override
|
||||
public void visitField(PsiField field) {
|
||||
final boolean dataPointAnnotated = AnnotationUtil.isAnnotated(field, DATAPOINT_FQN, false);
|
||||
if (dataPointAnnotated) {
|
||||
final String errorMessage = JUnitRuleInspection.getPublicStaticErrorMessage(field, false, true);
|
||||
if (errorMessage != null) {
|
||||
registerError(field.getNameIdentifier(),
|
||||
InspectionGadgetsBundle.message("junit.datapoint.problem.descriptor", errorMessage),
|
||||
"Make field " + errorMessage, DATAPOINT_FQN);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -16,15 +16,11 @@
|
||||
package com.siyeh.ig.junit;
|
||||
|
||||
import com.intellij.codeInsight.AnnotationUtil;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiField;
|
||||
import com.intellij.psi.PsiModifier;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
@@ -70,7 +66,7 @@ public class JUnitRuleInspection extends BaseInspection {
|
||||
@Nullable
|
||||
@Override
|
||||
protected InspectionGadgetsFix buildFix(Object... infos) {
|
||||
return infos.length > 1 ? new MakePublicStaticFix((String)infos[1], (String)infos[2]) : null;
|
||||
return infos.length > 1 ? new MakePublicStaticFix((String)infos[1], infos[2].equals(CLASS_RULE_FQN)) : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -82,35 +78,7 @@ public class JUnitRuleInspection extends BaseInspection {
|
||||
final boolean classRuleAnnotated = REPORT_CLASS_RULE_PROBLEMS && AnnotationUtil.isAnnotated(field, CLASS_RULE_FQN, false);
|
||||
if (ruleAnnotated || classRuleAnnotated) {
|
||||
String annotation = ruleAnnotated ? RULE_FQN : CLASS_RULE_FQN;
|
||||
String errorMessage = null;
|
||||
final boolean hasStatic = field.hasModifierProperty(PsiModifier.STATIC);
|
||||
final boolean hasPublic = field.hasModifierProperty(PsiModifier.PUBLIC);
|
||||
if (!hasPublic) {
|
||||
if (classRuleAnnotated) {
|
||||
if (!hasStatic) {
|
||||
errorMessage = "public and static";
|
||||
} else {
|
||||
errorMessage = "public";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!hasStatic){
|
||||
errorMessage = "public";
|
||||
} else {
|
||||
errorMessage = "public and non-static";
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!hasStatic) {
|
||||
if (classRuleAnnotated) {
|
||||
errorMessage = "static";
|
||||
}
|
||||
}
|
||||
else if (ruleAnnotated) {
|
||||
errorMessage = "non-static";
|
||||
}
|
||||
}
|
||||
String errorMessage = getPublicStaticErrorMessage(field, ruleAnnotated, classRuleAnnotated);
|
||||
if (errorMessage != null) {
|
||||
registerError(field.getNameIdentifier(), InspectionGadgetsBundle.message("junit.rule.problem.descriptor", annotation, errorMessage), "Make field " + errorMessage, annotation);
|
||||
}
|
||||
@@ -122,31 +90,36 @@ public class JUnitRuleInspection extends BaseInspection {
|
||||
};
|
||||
}
|
||||
|
||||
private static class MakePublicStaticFix extends InspectionGadgetsFix {
|
||||
private final String myName;
|
||||
private final boolean myMakeStatic;
|
||||
|
||||
public MakePublicStaticFix(String name, String annotation) {
|
||||
myName = name;
|
||||
myMakeStatic = annotation.equals(CLASS_RULE_FQN);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
|
||||
final PsiElement element = descriptor.getPsiElement();
|
||||
if (element != null) {
|
||||
final PsiElement parent = element.getParent();
|
||||
if (parent instanceof PsiField) {
|
||||
PsiUtil.setModifierProperty((PsiField)parent, PsiModifier.PUBLIC, true);
|
||||
PsiUtil.setModifierProperty((PsiField)parent, PsiModifier.STATIC, myMakeStatic);
|
||||
static String getPublicStaticErrorMessage(PsiField field, boolean shouldBeNonStatic, boolean shouldBeStatic) {
|
||||
String errorMessage = null;
|
||||
final boolean hasStatic = field.hasModifierProperty(PsiModifier.STATIC);
|
||||
final boolean hasPublic = field.hasModifierProperty(PsiModifier.PUBLIC);
|
||||
if (!hasPublic) {
|
||||
if (shouldBeStatic) {
|
||||
if (!hasStatic) {
|
||||
errorMessage = "public and static";
|
||||
} else {
|
||||
errorMessage = "public";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!hasStatic){
|
||||
errorMessage = "public";
|
||||
} else {
|
||||
errorMessage = "public and non-static";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return myName;
|
||||
else {
|
||||
if (!hasStatic) {
|
||||
if (shouldBeStatic) {
|
||||
errorMessage = "static";
|
||||
}
|
||||
}
|
||||
else if (shouldBeNonStatic) {
|
||||
errorMessage = "non-static";
|
||||
}
|
||||
}
|
||||
return errorMessage;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.siyeh.ig.junit;
|
||||
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiField;
|
||||
import com.intellij.psi.PsiModifier;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: anna
|
||||
* Date: 5/22/13
|
||||
*/
|
||||
class MakePublicStaticFix extends InspectionGadgetsFix {
|
||||
private final String myName;
|
||||
private final boolean myMakeStatic;
|
||||
|
||||
public MakePublicStaticFix(final String name, final boolean makeStatic) {
|
||||
myName = name;
|
||||
myMakeStatic = makeStatic;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
|
||||
final PsiElement element = descriptor.getPsiElement();
|
||||
if (element != null) {
|
||||
final PsiElement parent = element.getParent();
|
||||
if (parent instanceof PsiField) {
|
||||
PsiUtil.setModifierProperty((PsiField)parent, PsiModifier.PUBLIC, true);
|
||||
PsiUtil.setModifierProperty((PsiField)parent, PsiModifier.STATIC, myMakeStatic);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return myName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<html>
|
||||
<body>
|
||||
Checks for any member that is annotated with @DataPoint but is not public or not static.
|
||||
<!-- tooltip end -->
|
||||
<p>
|
||||
<small>New in 13, Powered by InspectionGadgets</small>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,6 @@
|
||||
class Test {
|
||||
@org.junit.experimental.theories.DataPoint public static Object f1;
|
||||
@org.junit.experimental.theories.DataPoint public Object <warning descr="Fields annotated with @DataPoint should be static">f2</warning>;
|
||||
@org.junit.experimental.theories.DataPoint static Object <warning descr="Fields annotated with @DataPoint should be public">f3</warning>;
|
||||
@org.junit.experimental.theories.DataPoint Object <warning descr="Fields annotated with @DataPoint should be public and static">f4</warning>;
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2000-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/*
|
||||
* User: ddt
|
||||
* Date: 22-Mai-2013
|
||||
*/
|
||||
package com.siyeh.ig.junit;
|
||||
|
||||
import com.intellij.openapi.application.PluginPathManager;
|
||||
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class JUnitDatapointInspectionTest extends LightCodeInsightFixtureTestCase {
|
||||
public void testWrongdatapoint() throws Exception {
|
||||
myFixture.addClass("package org.junit.experimental.theories;\n" +
|
||||
"public @interface DataPoint {}");
|
||||
myFixture.testHighlighting(true, false, false, getTestName(true) + ".java");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
myFixture.enableInspections(new JUnitDatapointInspection());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return PluginPathManager.getPluginHomePath("InspectionGadgets") + "/test/com/siyeh/igtest/junit/rule/";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user