diff options
Diffstat (limited to 'plugins/InspectionGadgets')
58 files changed, 1146 insertions, 397 deletions
diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml index c8854d4dbb26..81cc13aea292 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml @@ -202,6 +202,10 @@ key="equals.between.inconvertible.types.display.name" groupBundle="messages.InspectionsBundle" groupKey="group.names.probable.bugs" enabledByDefault="true" level="WARNING" implementationClass="com.siyeh.ig.bugs.EqualsBetweenInconvertibleTypesInspection"/> + <localInspection language="JAVA" shortName="EqualsWithItself" bundle="com.siyeh.InspectionGadgetsBundle" + key="equals.with.itself.display.name" groupBundle="messages.InspectionsBundle" + groupKey="group.names.probable.bugs" enabledByDefault="true" level="WARNING" + implementationClass="com.siyeh.ig.bugs.EqualsWithItselfInspection"/> <localInspection language="JAVA" suppressId="NonFinalFieldReferenceInEquals" shortName="EqualsUsesNonFinalVariable" bundle="com.siyeh.InspectionGadgetsBundle" key="non.final.field.in.equals.display.name" groupBundle="messages.InspectionsBundle" groupKey="group.names.probable.bugs" enabledByDefault="false" level="WARNING" @@ -1379,6 +1383,14 @@ key="junit3.style.test.method.in.junit4.class.display.name" groupBundle="messages.InspectionsBundle" groupKey="group.names.junit.issues" enabledByDefault="false" level="WARNING" implementationClass="com.siyeh.ig.junit.JUnit3StyleTestMethodInJUnit4ClassInspection"/> + <localInspection language="JAVA" shortName="JUnit3MethodNamingConvention" + bundle="com.siyeh.InspectionGadgetsBundle" key="junit3.method.naming.convention.display.name" + groupBundle="messages.InspectionsBundle" groupKey="group.names.junit.issues" enabledByDefault="false" + level="WARNING" implementationClass="com.siyeh.ig.junit.JUnit3MethodNamingConventionInspection"/> + <localInspection language="JAVA" shortName="JUnit4MethodNamingConvention" + bundle="com.siyeh.InspectionGadgetsBundle" key="junit4.method.naming.convention.display.name" + groupBundle="messages.InspectionsBundle" groupKey="group.names.junit.issues" enabledByDefault="false" + level="WARNING" implementationClass="com.siyeh.ig.junit.JUnit4MethodNamingConventionInspection"/> <localInspection language="JAVA" shortName="JUnit4AnnotatedMethodInJUnit3TestCase" bundle="com.siyeh.InspectionGadgetsBundle" key="junit4.test.method.in.class.extending.junit3.testcase.display.name" groupBundle="messages.InspectionsBundle" groupKey="group.names.junit.issues" enabledByDefault="true" level="WARNING" diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties index 1dfdfb9eb50a..1df72421fd4d 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties @@ -1741,6 +1741,7 @@ ignore.test.method.in.class.extending.junit3.testcase.problem.descriptor=JUnit 3 ignore.test.method.in.class.extending.junit3.testcase.quickfix=Remove ''@Ignore'' and rename method to ''{0}'' convert.junit3.test.class.quickfix=Convert JUnit 3 class ''{0}'' to JUnit 4 remove.junit4.test.annotation.quickfix=Remove '@Test' annotation +remove.junit4.test.annotation.and.rename.quickfix=Remove '@Test' annotation and rename to ''{0}'' equals.called.on.enum.constant.display.name='equals()' called on Enum value equals.called.on.enum.constant.problem.descriptor=<code>#ref()</code> called on Enum value #loc equals.called.on.enum.constant.quickfix=Replace 'equals()' with '==' @@ -2100,3 +2101,13 @@ assignment.to.lambda.parameter.problem.descriptor=Assignment to lambda parameter class.with.only.private.constructors.display.name=Class with only 'private' constructors should be declared 'final' class.with.only.private.constructors.problem.descriptor=Class <code>#ref</code> with only 'private' constructors should be declared 'final' property.value.set.to.itself.display.name=Property value set to itself +equals.with.itself.display.name='equals()' called on itself +equals.with.itself.problem.descriptor=Identical qualifier and argument to <code>#ref()</code> call +junit4.method.naming.convention.display.name=JUnit 4 test method naming convention +junit4.method.naming.convention.problem.descriptor.short=JUnit 4 test method name <code>#ref</code> is too short ({0} < {1}) #loc +junit4.method.naming.convention.problem.descriptor.long=JUnit 4 test method name <code>#ref</code> is too long ({0} > {1}) #loc +junit4.method.naming.convention.problem.descriptor.regex.mismatch=JUnit 4 test method name <code>#ref</code> doesn''t match regex ''{0}'' #loc +junit3.method.naming.convention.display.name=JUnit 3 test method naming convention +junit3.method.naming.convention.problem.descriptor.short=JUnit 3 test method name <code>#ref</code> is too short ({0} < {1}) #loc +junit3.method.naming.convention.problem.descriptor.long=JUnit 3 test method name <code>#ref</code> is too long ({0} > {1}) #loc +junit3.method.naming.convention.problem.descriptor.regex.mismatch=JUnit 3 test method name <code>#ref</code> doesn''t match regex ''{0}'' #loc diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/abstraction/TypeMayBeWeakenedInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/abstraction/TypeMayBeWeakenedInspection.java index bf23939f2c86..2787f34e4dcf 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/abstraction/TypeMayBeWeakenedInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/abstraction/TypeMayBeWeakenedInspection.java @@ -21,7 +21,6 @@ import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; import com.intellij.openapi.project.Project; import com.intellij.psi.*; import com.intellij.psi.codeStyle.JavaCodeStyleManager; -import com.intellij.psi.search.GlobalSearchScope; import com.intellij.psi.search.searches.OverridingMethodsSearch; import com.intellij.util.Query; import com.siyeh.InspectionGadgetsBundle; @@ -64,9 +63,9 @@ public class TypeMayBeWeakenedInspection extends BaseInspection { @NonNls final StringBuilder builder = new StringBuilder(); final Iterator<PsiClass> iterator = weakerClasses.iterator(); if (iterator.hasNext()) { - builder.append('\'').append(iterator.next().getQualifiedName()).append('\''); + builder.append('\'').append(getClassName(iterator.next())).append('\''); while (iterator.hasNext()) { - builder.append(", '").append(iterator.next().getQualifiedName()).append('\''); + builder.append(", '").append(getClassName(iterator.next())).append('\''); } } final Object info = infos[0]; @@ -85,6 +84,14 @@ public class TypeMayBeWeakenedInspection extends BaseInspection { return InspectionGadgetsBundle.message("type.may.be.weakened.problem.descriptor", builder.toString()); } + private static String getClassName(PsiClass aClass) { + final String qualifiedName = aClass.getQualifiedName(); + if (qualifiedName == null) { + return aClass.getName(); + } + return qualifiedName; + } + @Override @Nullable public JComponent createOptionsPanel() { @@ -106,11 +113,11 @@ public class TypeMayBeWeakenedInspection extends BaseInspection { final Iterable<PsiClass> weakerClasses = (Iterable<PsiClass>)infos[1]; final Collection<InspectionGadgetsFix> fixes = new ArrayList(); for (PsiClass weakestClass : weakerClasses) { - final String qualifiedName = weakestClass.getQualifiedName(); - if (qualifiedName == null) { + final String className = getClassName(weakestClass); + if (className == null) { continue; } - fixes.add(new TypeMayBeWeakenedFix(qualifiedName)); + fixes.add(new TypeMayBeWeakenedFix(className)); } return fixes.toArray(new InspectionGadgetsFix[fixes.size()]); } @@ -162,31 +169,30 @@ public class TypeMayBeWeakenedInspection extends BaseInspection { if (!(oldType instanceof PsiClassType)) { return; } - final PsiClassType classType = (PsiClassType)oldType; - final PsiType[] parameterTypes = classType.getParameters(); - final GlobalSearchScope scope = element.getResolveScope(); + final PsiClassType oldClassType = (PsiClassType)oldType; + final PsiType[] parameterTypes = oldClassType.getParameters(); final JavaPsiFacade facade = JavaPsiFacade.getInstance(project); - final PsiClass aClass = facade.findClass(fqClassName, scope); - if (aClass == null) { + final PsiElementFactory factory = facade.getElementFactory(); + final PsiType type = factory.createTypeFromText(fqClassName, element); + if (!(type instanceof PsiClassType)) { return; } - final PsiTypeParameter[] typeParameters = aClass.getTypeParameters(); - final PsiElementFactory factory = facade.getElementFactory(); - final PsiClassType type; - if (typeParameters.length != 0 && typeParameters.length == parameterTypes.length) { - final Map<PsiTypeParameter, PsiType> typeParameterMap = new HashMap(); - for (int i = 0; i < typeParameters.length; i++) { - final PsiTypeParameter typeParameter = typeParameters[i]; - final PsiType parameterType = parameterTypes[i]; - typeParameterMap.put(typeParameter, parameterType); + PsiClassType classType = (PsiClassType)type; + final PsiClass aClass = classType.resolve(); + if (aClass != null) { + final PsiTypeParameter[] typeParameters = aClass.getTypeParameters(); + if (typeParameters.length != 0 && typeParameters.length == parameterTypes.length) { + final Map<PsiTypeParameter, PsiType> typeParameterMap = new HashMap(); + for (int i = 0; i < typeParameters.length; i++) { + final PsiTypeParameter typeParameter = typeParameters[i]; + final PsiType parameterType = parameterTypes[i]; + typeParameterMap.put(typeParameter, parameterType); + } + final PsiSubstitutor substitutor = factory.createSubstitutor(typeParameterMap); + classType = factory.createType(aClass, substitutor); } - final PsiSubstitutor substitutor = factory.createSubstitutor(typeParameterMap); - type = factory.createType(aClass, substitutor); - } - else { - type = factory.createTypeByFQClassName(fqClassName, scope); } - final PsiJavaCodeReferenceElement referenceElement = factory.createReferenceElementByType(type); + final PsiJavaCodeReferenceElement referenceElement = factory.createReferenceElementByType(classType); final PsiElement replacement = componentReferenceElement.replace(referenceElement); final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project); javaCodeStyleManager.shortenClassReferences(replacement); diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/EqualsWithItselfInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/EqualsWithItselfInspection.java new file mode 100644 index 000000000000..698ace189a6b --- /dev/null +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/EqualsWithItselfInspection.java @@ -0,0 +1,78 @@ +/* + * Copyright 2000-2014 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. + */ +package com.siyeh.ig.bugs; + +import com.intellij.psi.PsiExpression; +import com.intellij.psi.PsiExpressionList; +import com.intellij.psi.PsiMethodCallExpression; +import com.intellij.psi.PsiReferenceExpression; +import com.siyeh.InspectionGadgetsBundle;import com.siyeh.ig.BaseInspection; +import com.siyeh.ig.BaseInspectionVisitor; +import com.siyeh.ig.psiutils.EquivalenceChecker; +import com.siyeh.ig.psiutils.MethodCallUtils; +import com.siyeh.ig.psiutils.SideEffectChecker; +import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.NotNull; + +/** + * @author Bas Leijdekkers + */ +public class EqualsWithItselfInspection extends BaseInspection { + @Nls + @NotNull + @Override + public String getDisplayName() { + return InspectionGadgetsBundle.message("equals.with.itself.display.name"); + } + + @NotNull + @Override + protected String buildErrorString(Object... infos) { + return InspectionGadgetsBundle.message("equals.with.itself.problem.descriptor"); + } + + @Override + public BaseInspectionVisitor buildVisitor() { + return new EqualsWithIfSelfVisitor(); + } + + private static class EqualsWithIfSelfVisitor extends BaseInspectionVisitor { + + @Override + public void visitMethodCallExpression(PsiMethodCallExpression expression) { + super.visitMethodCallExpression(expression); + if (!MethodCallUtils.isEqualsCall(expression)) { + return; + } + final PsiReferenceExpression methodExpression = expression.getMethodExpression(); + final PsiExpression qualifier = methodExpression.getQualifierExpression(); + if (qualifier == null) { + return; + } + final PsiExpressionList argumentList = expression.getArgumentList(); + final PsiExpression[] arguments = argumentList.getExpressions(); + if (arguments.length != 1) { + return; + } + final PsiExpression argument = arguments[0]; + if (!EquivalenceChecker.expressionsAreEquivalent(qualifier, argument) || + SideEffectChecker.mayHaveSideEffects(qualifier)) { + return; + } + registerMethodCallError(expression); + } + } +} diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/inheritance/StaticInheritanceFix.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/inheritance/StaticInheritanceFix.java index 1289bbdbe694..c24de84ba1ea 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/inheritance/StaticInheritanceFix.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/inheritance/StaticInheritanceFix.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -30,6 +30,7 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Computable; import com.intellij.psi.*; import com.intellij.psi.impl.DebugUtil; +import com.intellij.psi.search.SearchScope; import com.intellij.psi.search.searches.ReferencesSearch; import com.intellij.psi.util.InheritanceUtil; import com.intellij.psi.util.PsiTreeUtil; @@ -91,7 +92,13 @@ class StaticInheritanceFix extends InspectionGadgetsFix { @Override public void run(@NotNull ProgressIndicator indicator) { for (final PsiField field : allFields) { - final Query<PsiReference> search = ReferencesSearch.search(field, implementingClass.getUseScope(), false); + SearchScope scope = ApplicationManager.getApplication().runReadAction(new Computable<SearchScope>() { + @Override + public SearchScope compute() { + return implementingClass.getUseScope(); + } + }); + final Query<PsiReference> search = ReferencesSearch.search(field, scope, false); for (PsiReference reference : search) { if (!(reference instanceof PsiReferenceExpression)) { continue; diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/JUnit3MethodNamingConventionInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/JUnit3MethodNamingConventionInspectionBase.java new file mode 100644 index 000000000000..8f27342d8d2b --- /dev/null +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/JUnit3MethodNamingConventionInspectionBase.java @@ -0,0 +1,102 @@ +/* + * Copyright 2000-2014 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. + */ +package com.siyeh.ig.junit; + +import com.intellij.psi.PsiIdentifier; +import com.intellij.psi.PsiMethod; +import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.BaseInspectionVisitor; +import com.siyeh.ig.naming.ConventionInspection; +import com.siyeh.ig.psiutils.LibraryUtil; +import com.siyeh.ig.psiutils.MethodUtils; +import com.siyeh.ig.psiutils.TestUtils; +import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.NotNull; + +/** + * @author Bas Leijdekkers + */ +public class JUnit3MethodNamingConventionInspectionBase extends ConventionInspection { + + @Nls + @NotNull + @Override + public String getDisplayName() { + return InspectionGadgetsBundle.message("junit3.method.naming.convention.display.name"); + } + + @Override + @NotNull + public String buildErrorString(Object... infos) { + final String methodName = (String)infos[0]; + final int length = methodName.length(); + if (length < getMinLength()) { + return InspectionGadgetsBundle.message("junit3.method.naming.convention.problem.descriptor.short", + Integer.valueOf(length), Integer.valueOf(getMinLength())); + } + else if (length > getMaxLength()) { + return InspectionGadgetsBundle.message("junit3.method.naming.convention.problem.descriptor.long", + Integer.valueOf(length), Integer.valueOf(getMaxLength())); + } + return InspectionGadgetsBundle.message("junit3.method.naming.convention.problem.descriptor.regex.mismatch", getRegex()); + } + + @Override + protected String getDefaultRegex() { + return "test[A-Za-z_\\d]*"; + } + + @Override + protected int getDefaultMinLength() { + return 8; + } + + @Override + protected int getDefaultMaxLength() { + return 64; + } + + @Override + public BaseInspectionVisitor buildVisitor() { + return new JUnit3MethodNamingConventionVisitor(); + } + + private class JUnit3MethodNamingConventionVisitor extends BaseInspectionVisitor { + + @Override + public void visitMethod(PsiMethod method) { + super.visitMethod(method); + if (!TestUtils.isJUnit3TestMethod(method) || !TestUtils.isRunnable(method)) { + return; + } + final PsiIdentifier nameIdentifier = method.getNameIdentifier(); + if (nameIdentifier == null) { + return; + } + final String name = method.getName(); + if (isValid(name)) { + return; + } + if (!isOnTheFly() && MethodUtils.hasSuper(method)) { + return; + } + if (LibraryUtil.isOverrideOfLibraryMethod(method)) { + return; + } + registerMethodError(method, name); + } + } +} diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/JUnit3StyleTestMethodInJUnit4ClassInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/JUnit3StyleTestMethodInJUnit4ClassInspection.java index 8a6ebeff68ce..c39865049e95 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/JUnit3StyleTestMethodInJUnit4ClassInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/JUnit3StyleTestMethodInJUnit4ClassInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -25,6 +25,7 @@ import com.siyeh.ig.DelegatingFix; import com.siyeh.ig.InspectionGadgetsFix; import com.siyeh.ig.psiutils.TestUtils; import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -71,70 +72,45 @@ public class JUnit3StyleTestMethodInJUnit4ClassInspection extends BaseInspection if (!name.startsWith("test")) { return; } - if (method.hasModifierProperty(PsiModifier.ABSTRACT) || !method.hasModifierProperty(PsiModifier.PUBLIC)) { + if (!TestUtils.isRunnable(method)) { return; } if (TestUtils.isJUnit4TestMethod(method)) { return; } - final PsiType returnType = method.getReturnType(); - if (returnType == null || !returnType.equals(PsiType.VOID)) { - return; - } - final PsiParameterList parameterList = method.getParameterList(); - if (parameterList.getParametersCount() != 0) { - return; - } final PsiClass containingClass = method.getContainingClass(); if (TestUtils.isJUnitTestClass(containingClass)) { return; } - if (!containsReferenceToClass(containingClass, "org.junit.Test")) { + if (!containsJUnit4Annotation(containingClass)) { return; } registerMethodError(method); } } - public static boolean containsReferenceToClass(PsiElement element, String fullyQualifiedName) { - final ClassReferenceVisitor visitor = new ClassReferenceVisitor(fullyQualifiedName); + public static boolean containsJUnit4Annotation(PsiElement element) { + final JUnit4AnnotationVisitor visitor = new JUnit4AnnotationVisitor(); element.accept(visitor); - return visitor.isReferenceFound(); + return visitor.isJUnit4AnnotationFound(); } - private static class ClassReferenceVisitor extends JavaRecursiveElementVisitor { - - private final String fullyQualifiedName; - private boolean referenceFound = false; + private static class JUnit4AnnotationVisitor extends JavaRecursiveElementWalkingVisitor { - private ClassReferenceVisitor(String fullyQualifiedName) { - this.fullyQualifiedName = fullyQualifiedName; - } + private boolean myJUnit4AnnotationFound = false; @Override - public void visitReferenceElement(PsiJavaCodeReferenceElement reference) { - super.visitReferenceElement(reference); - if (referenceFound) { - return; - } - if (!(reference.getParent() instanceof PsiAnnotation)) { - // optimization - return; - } - final PsiElement element = reference.resolve(); - if (!(element instanceof PsiClass) || element instanceof PsiTypeParameter) { - return; - } - final PsiClass aClass = (PsiClass)element; - final String classQualifiedName = aClass.getQualifiedName(); - if (classQualifiedName == null || !classQualifiedName.equals(fullyQualifiedName)) { + public void visitAnnotation(PsiAnnotation annotation) { + super.visitAnnotation(annotation); + @NonNls final String qualifiedName = annotation.getQualifiedName(); + if (qualifiedName == null || !qualifiedName.startsWith("org.junit.")) { return; } - referenceFound = true; + myJUnit4AnnotationFound = true; } - public boolean isReferenceFound() { - return referenceFound; + public boolean isJUnit4AnnotationFound() { + return myJUnit4AnnotationFound; } } } diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/JUnit4MethodNamingConventionInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/JUnit4MethodNamingConventionInspectionBase.java new file mode 100644 index 000000000000..eb0f3d7b91c7 --- /dev/null +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/JUnit4MethodNamingConventionInspectionBase.java @@ -0,0 +1,102 @@ +/* + * Copyright 2000-2014 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. + */ +package com.siyeh.ig.junit; + +import com.intellij.psi.PsiIdentifier; +import com.intellij.psi.PsiMethod; +import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.BaseInspectionVisitor; +import com.siyeh.ig.naming.ConventionInspection; +import com.siyeh.ig.psiutils.LibraryUtil; +import com.siyeh.ig.psiutils.MethodUtils; +import com.siyeh.ig.psiutils.TestUtils; +import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.NotNull; + +/** + * @author Bas Leijdekkers + */ +public class JUnit4MethodNamingConventionInspectionBase extends ConventionInspection { + + @Nls + @NotNull + @Override + public String getDisplayName() { + return InspectionGadgetsBundle.message("junit4.method.naming.convention.display.name"); + } + + @Override + @NotNull + public String buildErrorString(Object... infos) { + final String methodName = (String)infos[0]; + final int length = methodName.length(); + if (length < getMinLength()) { + return InspectionGadgetsBundle.message("junit4.method.naming.convention.problem.descriptor.short", + Integer.valueOf(length), Integer.valueOf(getMinLength())); + } + else if (length > getMaxLength()) { + return InspectionGadgetsBundle.message("junit4.method.naming.convention.problem.descriptor.long", + Integer.valueOf(length), Integer.valueOf(getMaxLength())); + } + return InspectionGadgetsBundle.message("junit4.method.naming.convention.problem.descriptor.regex.mismatch", getRegex()); + } + + @Override + protected String getDefaultRegex() { + return "[a-z][A-Za-z_\\d]*"; + } + + @Override + protected int getDefaultMinLength() { + return 4; + } + + @Override + protected int getDefaultMaxLength() { + return 64; + } + + @Override + public BaseInspectionVisitor buildVisitor() { + return new JUnit4MethodNamingConventionVisitor(); + } + + private class JUnit4MethodNamingConventionVisitor extends BaseInspectionVisitor { + + @Override + public void visitMethod(PsiMethod method) { + super.visitMethod(method); + if (!TestUtils.isJUnit4TestMethod(method) || !TestUtils.isRunnable(method)) { + return; + } + final PsiIdentifier nameIdentifier = method.getNameIdentifier(); + if (nameIdentifier == null) { + return; + } + final String name = method.getName(); + if (isValid(name)) { + return; + } + if (!isOnTheFly() && MethodUtils.hasSuper(method)) { + return; + } + if (LibraryUtil.isOverrideOfLibraryMethod(method)) { + return; + } + registerMethodError(method, name); + } + } +} diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/TestMethodIsPublicVoidNoArgInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/TestMethodIsPublicVoidNoArgInspection.java index 02d8cd5b7c8e..4b9a794a143a 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/TestMethodIsPublicVoidNoArgInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/TestMethodIsPublicVoidNoArgInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2011 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2014 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,14 +15,14 @@ */ package com.siyeh.ig.junit; -import com.intellij.codeInsight.AnnotationUtil; -import com.intellij.psi.*; -import com.intellij.psi.util.InheritanceUtil; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.PsiModifier; +import com.intellij.psi.PsiParameterList; +import com.intellij.psi.PsiType; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; import com.siyeh.ig.psiutils.TestUtils; -import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; public class TestMethodIsPublicVoidNoArgInspection extends BaseInspection { @@ -70,23 +70,20 @@ public class TestMethodIsPublicVoidNoArgInspection extends BaseInspection { @Override public void visitMethod(@NotNull PsiMethod method) { //note: no call to super; - @NonNls final String methodName = method.getName(); - if (!methodName.startsWith("test") && - !TestUtils.isJUnit4TestMethod(method)) { + if (method.isConstructor()) { return; } - final PsiType returnType = method.getReturnType(); - if (returnType == null) { + if (!TestUtils.isJUnit3TestMethod(method) && !TestUtils.isJUnit4TestMethod(method)) { return; } + final PsiType returnType = method.getReturnType(); final PsiParameterList parameterList = method.getParameterList(); final boolean takesArguments; final boolean isStatic; if (parameterList.getParametersCount() == 0) { takesArguments = false; isStatic = method.hasModifierProperty(PsiModifier.STATIC); - if (!isStatic && returnType.equals(PsiType.VOID) && - method.hasModifierProperty(PsiModifier.PUBLIC)) { + if (!isStatic && PsiType.VOID.equals(returnType) && method.hasModifierProperty(PsiModifier.PUBLIC)) { return; } } @@ -94,14 +91,6 @@ public class TestMethodIsPublicVoidNoArgInspection extends BaseInspection { isStatic = false; takesArguments = true; } - final PsiClass targetClass = method.getContainingClass(); - if (!AnnotationUtil.isAnnotated(method, "org.junit.Test", true)) { - if (targetClass == null || - !InheritanceUtil.isInheritor(targetClass, - "junit.framework.TestCase")) { - return; - } - } registerMethodError(method, Boolean.valueOf(takesArguments), Boolean.valueOf(isStatic)); } diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/naming/InstanceMethodNamingConventionInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/naming/InstanceMethodNamingConventionInspectionBase.java index 3ec38f55053c..964ce0b0aace 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/naming/InstanceMethodNamingConventionInspectionBase.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/naming/InstanceMethodNamingConventionInspectionBase.java @@ -15,25 +15,22 @@ */ package com.siyeh.ig.naming; +import com.intellij.openapi.extensions.Extensions; import com.intellij.psi.PsiIdentifier; import com.intellij.psi.PsiMethod; import com.intellij.psi.PsiModifier; -import com.intellij.util.ui.CheckBox; +import com.intellij.testIntegration.TestFramework; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspectionVisitor; import com.siyeh.ig.psiutils.LibraryUtil; import com.siyeh.ig.psiutils.MethodUtils; +import com.siyeh.ig.psiutils.TestUtils; import org.jetbrains.annotations.NotNull; -import javax.swing.*; - public class InstanceMethodNamingConventionInspectionBase extends ConventionInspection { private static final int DEFAULT_MIN_LENGTH = 4; private static final int DEFAULT_MAX_LENGTH = 32; - @SuppressWarnings("PublicField") - public boolean ignoreNativeMethods = true; - @Override @NotNull public String getDisplayName() { @@ -59,13 +56,6 @@ public class InstanceMethodNamingConventionInspectionBase extends ConventionInsp } @Override - public JComponent[] createExtraOptions() { - return new JComponent[] { - new CheckBox("ignore 'native' methods", this, "ignoreNativeMethods") - }; - } - - @Override protected String getDefaultRegex() { return "[a-z][A-Za-z\\d]*"; } @@ -80,6 +70,16 @@ public class InstanceMethodNamingConventionInspectionBase extends ConventionInsp return DEFAULT_MAX_LENGTH; } + private static boolean isTestNGTestMethod(PsiMethod method) { + final TestFramework[] testFrameworks = Extensions.getExtensions(TestFramework.EXTENSION_NAME); + for (TestFramework framework : testFrameworks) { + if ("TestNG".equals(framework.getName())) { + return framework.isTestMethod(method); + } + } + return false; + } + @Override public BaseInspectionVisitor buildVisitor() { return new NamingConventionsVisitor(); @@ -93,13 +93,24 @@ public class InstanceMethodNamingConventionInspectionBase extends ConventionInsp if (method.isConstructor() || method.hasModifierProperty(PsiModifier.STATIC)) { return; } - if (ignoreNativeMethods && method.hasModifierProperty(PsiModifier.NATIVE)) { + if (method.hasModifierProperty(PsiModifier.NATIVE) && isInspectionEnabled("NativeMethodNamingConvention", method)) { return; } final PsiIdentifier nameIdentifier = method.getNameIdentifier(); if (nameIdentifier == null) { return; } + if (TestUtils.isRunnable(method)) { + if (TestUtils.isJUnit4TestMethod(method) && isInspectionEnabled("JUnit4MethodNamingConvention", method)) { + return; + } + if (TestUtils.isJUnit3TestMethod(method) && isInspectionEnabled("JUnit3MethodNamingConvention", method)) { + return; + } + } + if (isTestNGTestMethod(method) && isInspectionEnabled("TestNGMethodNamingConvention", method)) { + return; + } final String name = method.getName(); if (isValid(name)) { return; diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/naming/NativeMethodNamingConventionInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/naming/NativeMethodNamingConventionInspectionBase.java index a8ceb11a11b1..2231fa152678 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/naming/NativeMethodNamingConventionInspectionBase.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/naming/NativeMethodNamingConventionInspectionBase.java @@ -77,7 +77,7 @@ public class NativeMethodNamingConventionInspectionBase extends ConventionInspec @Override public void visitMethod(@NotNull PsiMethod method) { super.visitMethod(method); - if (method.isConstructor() || method.hasModifierProperty(PsiModifier.STATIC)) { + if (method.isConstructor()) { return; } if (!method.hasModifierProperty(PsiModifier.NATIVE)) { diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/naming/StaticMethodNamingConventionInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/naming/StaticMethodNamingConventionInspectionBase.java index 116d2208e452..3e0d08998ec0 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/naming/StaticMethodNamingConventionInspectionBase.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/naming/StaticMethodNamingConventionInspectionBase.java @@ -17,20 +17,14 @@ package com.siyeh.ig.naming; import com.intellij.psi.PsiMethod; import com.intellij.psi.PsiModifier; -import com.intellij.util.ui.CheckBox; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspectionVisitor; import org.jetbrains.annotations.NotNull; -import javax.swing.*; - public class StaticMethodNamingConventionInspectionBase extends ConventionInspection { private static final int DEFAULT_MIN_LENGTH = 4; private static final int DEFAULT_MAX_LENGTH = 32; - @SuppressWarnings("PublicField") - public boolean ignoreNativeMethods = true; - @Override @NotNull public String getDisplayName() { @@ -61,13 +55,6 @@ public class StaticMethodNamingConventionInspectionBase extends ConventionInspec } @Override - public JComponent[] createExtraOptions() { - return new JComponent[]{ - new CheckBox("ignore 'native' methods", this, "ignoreNativeMethods") - }; - } - - @Override protected String getDefaultRegex() { return "[a-z][A-Za-z\\d]*"; } @@ -95,7 +82,7 @@ public class StaticMethodNamingConventionInspectionBase extends ConventionInspec if (!method.hasModifierProperty(PsiModifier.STATIC)) { return; } - if (ignoreNativeMethods && method.hasModifierProperty(PsiModifier.NATIVE)) { + if (method.hasModifierProperty(PsiModifier.NATIVE) && isInspectionEnabled("NativeMethodNamingConvention", method)) { return; } final String name = method.getName(); diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/PointlessArithmeticExpressionInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/PointlessArithmeticExpressionInspection.java index 023b4bc9f949..2dde6969545d 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/PointlessArithmeticExpressionInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/PointlessArithmeticExpressionInspection.java @@ -29,6 +29,7 @@ import com.siyeh.ig.InspectionGadgetsFix; import com.siyeh.ig.PsiReplacementUtil; import com.siyeh.ig.psiutils.EquivalenceChecker; import com.siyeh.ig.psiutils.ExpressionUtils; +import com.siyeh.ig.psiutils.SideEffectChecker; import gnu.trove.THashSet; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; @@ -232,7 +233,7 @@ public class PointlessArithmeticExpressionInspection for (int i = 0; i < expressions.length; i++) { PsiExpression expression = expressions[i]; if (previousExpression != null && - (isZero(expression) || i == 1 && EquivalenceChecker.expressionsAreEquivalent(previousExpression, expression))) { + (isZero(expression) || areExpressionsIdenticalWithoutSideEffects(previousExpression, expression, i))) { return true; } previousExpression = expression; @@ -260,9 +261,10 @@ public class PointlessArithmeticExpressionInspection private boolean divideExpressionIsPointless(PsiExpression[] expressions) { PsiExpression previousExpression = null; - for (PsiExpression expression : expressions) { + for (int i = 0; i < expressions.length; i++) { + final PsiExpression expression = expressions[i]; if (previousExpression != null && - (isOne(expression) || EquivalenceChecker.expressionsAreEquivalent(previousExpression, expression))) { + (isOne(expression) || areExpressionsIdenticalWithoutSideEffects(previousExpression, expression, i))) { return true; } previousExpression = expression; @@ -272,15 +274,21 @@ public class PointlessArithmeticExpressionInspection private boolean modExpressionIsPointless(PsiExpression[] expressions) { PsiExpression previousExpression = null; - for (PsiExpression expression : expressions) { + for (int i = 0; i < expressions.length; i++) { + final PsiExpression expression = expressions[i]; if (previousExpression != null && - (isOne(expression) || EquivalenceChecker.expressionsAreEquivalent(previousExpression, expression))) { + (isOne(expression) || areExpressionsIdenticalWithoutSideEffects(previousExpression, expression, i))) { return true; } previousExpression = expression; } return false; } + + private boolean areExpressionsIdenticalWithoutSideEffects(PsiExpression expression1, PsiExpression expression2, int index) { + return index == 1 && EquivalenceChecker.expressionsAreEquivalent(expression1, expression2) && + !SideEffectChecker.mayHaveSideEffects(expression1); + } } boolean isZero(PsiExpression expression) { diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ImportUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ImportUtils.java index 9b4a3a832913..7bfaae8cc722 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ImportUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ImportUtils.java @@ -67,6 +67,9 @@ public class ImportUtils { !hasDefaultImportConflict(qualifiedName, javaFile) && !hasOnDemandImportConflict(qualifiedName, javaFile)) { return; } + if (hasExactImportConflict(qualifiedName, javaFile)) { + return; + } final Project project = importList.getProject(); final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); final PsiElementFactory elementFactory = psiFacade.getElementFactory(); @@ -156,7 +159,7 @@ public class ImportUtils { return false; } - private static boolean hasExactImportConflict(String fqName, PsiJavaFile file) { + public static boolean hasExactImportConflict(String fqName, PsiJavaFile file) { final PsiImportList imports = file.getImportList(); if (imports == null) { return false; diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/InheritanceUtil.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/InheritanceUtil.java index 9661ba10e4c4..faea625f1db5 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/InheritanceUtil.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/InheritanceUtil.java @@ -15,6 +15,7 @@ */ package com.siyeh.ig.psiutils; +import com.intellij.codeInspection.inheritance.ImplementedAtRuntimeCondition; import com.intellij.openapi.progress.ProgressManager; import com.intellij.psi.CommonClassNames; import com.intellij.psi.PsiClass; @@ -80,6 +81,11 @@ public class InheritanceUtil { public static boolean hasImplementation(@NotNull PsiClass aClass) { final SearchScope scope = GlobalSearchScope.projectScope(aClass.getProject()); if (aClass.isInterface() && FunctionalExpressionSearch.search(aClass, scope).findFirst() != null) return true; + for (ImplementedAtRuntimeCondition condition : ImplementedAtRuntimeCondition.EP_NAME.getExtensions()) { + if (condition.isImplementedAtRuntime(aClass)) { + return true; + } + } final Query<PsiClass> search = ClassInheritorsSearch.search(aClass, scope, true, true); return !search.forEach(new Processor<PsiClass>() { @Override diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/SideEffectChecker.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/SideEffectChecker.java index 5063ef7c2ea7..782b667469af 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/SideEffectChecker.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/SideEffectChecker.java @@ -17,6 +17,7 @@ package com.siyeh.ig.psiutils; import com.intellij.psi.*; import com.intellij.psi.tree.IElementType; +import com.intellij.psi.util.PropertyUtil; import org.jetbrains.annotations.NotNull; public class SideEffectChecker { @@ -59,6 +60,14 @@ public class SideEffectChecker { return; } super.visitMethodCallExpression(expression); + final PsiReferenceExpression methodExpression = expression.getMethodExpression(); + final String methodName = methodExpression.getReferenceName(); + if ((methodName.startsWith("is") || methodName.startsWith("get")) && expression.getArgumentList().getExpressions().length == 0) { + final PsiMethod method = expression.resolveMethod(); + if (PropertyUtil.isSimpleGetter(method)) { + return; + } + } mayHaveSideEffects = true; } diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/TestUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/TestUtils.java index cf2133299768..dcaf8390dc5d 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/TestUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/TestUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2013 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2014 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,8 +22,8 @@ import com.intellij.openapi.roots.ProjectFileIndex; import com.intellij.openapi.roots.ProjectRootManager; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.*; -import com.intellij.psi.util.*; import com.intellij.psi.util.InheritanceUtil; +import com.intellij.psi.util.PsiTreeUtil; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -66,34 +66,37 @@ public class TestUtils { } public static boolean isJUnitTestMethod(@Nullable PsiMethod method) { + return isRunnable(method) && (isJUnit3TestMethod(method) || isJUnit4TestMethod(method)); + } + + public static boolean isRunnable(PsiMethod method) { if (method == null) { return false; } - if (isJUnit4TestMethod(method)) { - return true; - } - final String methodName = method.getName(); - @NonNls final String test = "test"; - if (!methodName.startsWith(test)) { - return false; - } if (method.hasModifierProperty(PsiModifier.ABSTRACT) || + method.hasModifierProperty(PsiModifier.STATIC) || !method.hasModifierProperty(PsiModifier.PUBLIC)) { return false; } final PsiType returnType = method.getReturnType(); - if (returnType == null) { + if (!PsiType.VOID.equals(returnType)) { return false; } - if (!returnType.equals(PsiType.VOID)) { + final PsiParameterList parameterList = method.getParameterList(); + return parameterList.getParametersCount() == 0; + } + + public static boolean isJUnit3TestMethod(@Nullable PsiMethod method) { + if (method == null) { return false; } - final PsiParameterList parameterList = method.getParameterList(); - if (parameterList.getParametersCount() != 0) { + final String methodName = method.getName(); + @NonNls final String test = "test"; + if (!methodName.startsWith(test)) { return false; } - final PsiClass targetClass = method.getContainingClass(); - return isJUnitTestClass(targetClass); + final PsiClass containingClass = method.getContainingClass(); + return isJUnitTestClass(containingClass); } public static boolean isJUnit4TestMethod(@Nullable PsiMethod method) { diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/UnnecessarilyQualifiedInnerClassAccessInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/UnnecessarilyQualifiedInnerClassAccessInspection.java index 78e32e5eee60..e449ad98fc8f 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/UnnecessarilyQualifiedInnerClassAccessInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/UnnecessarilyQualifiedInnerClassAccessInspection.java @@ -99,7 +99,10 @@ public class UnnecessarilyQualifiedInnerClassAccessInspection extends BaseInspec } final PsiClass aClass = (PsiClass)target; ImportUtils.addImportIfNeeded(aClass, element); - element.delete(); + final String shortName = aClass.getName(); + if (isReferenceToTarget(shortName, aClass, parent)) { + element.delete(); + } } } @@ -108,6 +111,20 @@ public class UnnecessarilyQualifiedInnerClassAccessInspection extends BaseInspec return new UnnecessarilyQualifiedInnerClassAccessVisitor(); } + private static boolean isReferenceToTarget(String referenceText, @NotNull PsiClass target, PsiElement context) { + final PsiJavaCodeReferenceElement reference = + JavaPsiFacade.getElementFactory(target.getProject()).createReferenceFromText(referenceText, context); + final JavaResolveResult[] results = reference.multiResolve(false); + if (results.length == 0) { + return true; + } + if (results.length > 1) { + return false; + } + final JavaResolveResult result = results[0]; + return result.isAccessible() && target.equals(result.getElement()); + } + private class UnnecessarilyQualifiedInnerClassAccessVisitor extends BaseInspectionVisitor { @@ -175,20 +192,6 @@ public class UnnecessarilyQualifiedInnerClassAccessInspection extends BaseInspec visitReferenceElement(expression); } - private boolean isReferenceToTarget(String referenceText, @NotNull PsiClass target, PsiElement context) { - final PsiJavaCodeReferenceElement reference = - JavaPsiFacade.getElementFactory(target.getProject()).createReferenceFromText(referenceText, context); - final JavaResolveResult[] results = reference.multiResolve(false); - if (results.length == 0) { - return true; - } - if (results.length > 1) { - return false; - } - final JavaResolveResult result = results[0]; - return result.isAccessible() && target.equals(result.getElement()); - } - private boolean isInImportOrPackage(PsiElement element) { while (element instanceof PsiJavaCodeReferenceElement) { element = element.getParent(); diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/junit/JUnit3MethodNamingConventionInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/junit/JUnit3MethodNamingConventionInspection.java new file mode 100644 index 000000000000..29dcf0bf1a01 --- /dev/null +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/junit/JUnit3MethodNamingConventionInspection.java @@ -0,0 +1,30 @@ +/* + * Copyright 2000-2014 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. + */ +package com.siyeh.ig.junit; + +import com.siyeh.ig.InspectionGadgetsFix; +import com.siyeh.ig.fixes.RenameFix; + +/** + * @author Bas Leijdekkers + */ +public class JUnit3MethodNamingConventionInspection extends JUnit3MethodNamingConventionInspectionBase { + + @Override + protected InspectionGadgetsFix buildFix(Object... infos) { + return new RenameFix(); + } +} diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/junit/JUnit4AnnotatedMethodInJUnit3TestCaseInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/junit/JUnit4AnnotatedMethodInJUnit3TestCaseInspection.java index 252411527cc1..89ad944a0f78 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/junit/JUnit4AnnotatedMethodInJUnit3TestCaseInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/junit/JUnit4AnnotatedMethodInJUnit3TestCaseInspection.java @@ -18,6 +18,7 @@ package com.siyeh.ig.junit; import com.intellij.codeInsight.AnnotationUtil; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.*; import com.intellij.psi.codeStyle.JavaCodeStyleManager; import com.siyeh.InspectionGadgetsBundle; @@ -41,7 +42,16 @@ public class JUnit4AnnotatedMethodInJUnit3TestCaseInspection extends JUnit4Annot fixes.add(new RemoveIgnoreAndRename(method)); } if (TestUtils.isJUnit4TestMethod(method)) { - fixes.add(new RemoveTestAnnotationFix()); + String methodName = method.getName(); + String newMethodName; + if (methodName.startsWith("test")) { + newMethodName = null; + } + else { + boolean lowCaseStyle = methodName.contains("_"); + newMethodName = "test" + (lowCaseStyle ? "_" + methodName : StringUtil.capitalize(methodName)); + } + fixes.add(new RemoveTestAnnotationFix(newMethodName)); } final PsiClass aClass = (PsiClass)infos[0]; final String className = aClass.getName(); @@ -233,22 +243,33 @@ public class JUnit4AnnotatedMethodInJUnit3TestCaseInspection extends JUnit4Annot } } - private static class RemoveTestAnnotationFix extends InspectionGadgetsFix { + private static class RemoveTestAnnotationFix extends RenameFix { + private final String myNewName; + + public RemoveTestAnnotationFix(String newName) { + super(newName); + myNewName = newName; + } + @Override @NotNull public String getFamilyName() { - return getName(); + return InspectionGadgetsBundle.message("remove.junit4.test.annotation.quickfix"); } @Override @NotNull public String getName() { - return InspectionGadgetsBundle.message("remove.junit4.test.annotation.quickfix"); + return myNewName == null ? getFamilyName() + : InspectionGadgetsBundle.message("remove.junit4.test.annotation.and.rename.quickfix", myNewName); } @Override - protected void doFix(Project project, ProblemDescriptor descriptor) { + public void doFix(Project project, ProblemDescriptor descriptor) { deleteAnnotation(descriptor, "org.junit.Test"); + if (myNewName != null) { + super.doFix(project, descriptor); + } } } } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/junit/JUnit4MethodNamingConventionInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/junit/JUnit4MethodNamingConventionInspection.java new file mode 100644 index 000000000000..22c8937d48d9 --- /dev/null +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/junit/JUnit4MethodNamingConventionInspection.java @@ -0,0 +1,30 @@ +/* + * Copyright 2000-2014 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. + */ +package com.siyeh.ig.junit; + +import com.siyeh.ig.InspectionGadgetsFix; +import com.siyeh.ig.fixes.RenameFix; + +/** + * @author Bas Leijdekkers + */ +public class JUnit4MethodNamingConventionInspection extends JUnit4MethodNamingConventionInspectionBase { + + @Override + protected InspectionGadgetsFix buildFix(Object... infos) { + return new RenameFix(); + } +} diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/EqualsWithItself.html b/plugins/InspectionGadgets/src/inspectionDescriptions/EqualsWithItself.html new file mode 100644 index 000000000000..3e1491edd22e --- /dev/null +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/EqualsWithItself.html @@ -0,0 +1,10 @@ +<html> +<body> +Reports call to <b>equals()</b> were an object is compared for equality with itself. +This means that the argument and the qualifier to the call are identical. +In this case <b>equals()</b> will always return <b>true</b>. +<!-- tooltip end --> +<p> +<small>New in 14</small> +</body> +</html>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/InstanceMethodNamingConvention.html b/plugins/InspectionGadgets/src/inspectionDescriptions/InstanceMethodNamingConvention.html index a6933cf0c7b8..4bf1c67710c5 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/InstanceMethodNamingConvention.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/InstanceMethodNamingConvention.html @@ -8,8 +8,6 @@ methods are ignored by this inspection. Use the fields below to specify minimum length, maximum length and regular expression expected for instance method names. Specify <b>0</b> to not check the length of names. Regular expressions are in standard <b>java.util.regex</b> format. <p> -Use the checkbox below to ignore native methods. -<p> </body> </html>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/JUnit3MethodNamingConvention.html b/plugins/InspectionGadgets/src/inspectionDescriptions/JUnit3MethodNamingConvention.html new file mode 100644 index 000000000000..253c7297cdcc --- /dev/null +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/JUnit3MethodNamingConvention.html @@ -0,0 +1,13 @@ +<html> +<body> +Reports JUnit 3 test methods whose names are either too short, too long, or do not follow the specified regular expression pattern. +When this inspection is enabled, the <i>Instance method naming convention</i> inspection +will ignore JUnit 3 test methods automatically. +<!-- tooltip end --> +<p> +Use the fields below to specify minimum length, maximum length and regular expression expected for JUnit 3 test method names. +Specify <b>0</b> to not check the length of names. Regular expressions are in standard <b>java.util.regex</b> format. +<p> +<small>New in 14</small> +</body> +</html>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/JUnit4MethodNamingConvention.html b/plugins/InspectionGadgets/src/inspectionDescriptions/JUnit4MethodNamingConvention.html new file mode 100644 index 000000000000..a53cc8817015 --- /dev/null +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/JUnit4MethodNamingConvention.html @@ -0,0 +1,13 @@ +<html> +<body> +Reports JUnit 4 test methods whose names are either too short, too long, or do not follow the specified regular expression pattern. +When this inspection is enabled, the <i>Instance method naming convention</i> inspection +will ignore JUnit 4 test methods automatically. +<!-- tooltip end --> +<p> +Use the fields below to specify minimum length, maximum length and regular expression expected for JUnit 4 test method names. +Specify <b>0</b> to not check the length of names. Regular expressions are in standard <b>java.util.regex</b> format. +<p> +<small>New in 14</small> +</body> +</html>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/NativeMethodNamingConvention.html b/plugins/InspectionGadgets/src/inspectionDescriptions/NativeMethodNamingConvention.html index c2e866b3d223..2ffd3a2a27a3 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/NativeMethodNamingConvention.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/NativeMethodNamingConvention.html @@ -1,11 +1,13 @@ <html> <body> -Reports 'native' methods whose names are either too short, too long, or do not follow +Reports <b>native</b> methods whose names are either too short, too long, or do not follow the specified regular expression pattern. Methods that override library methods are ignored by this inspection. +When this inspection is enabled, the <i>Instance method naming convention</i> and +<i>'static' method naming convention</i> inspections will ignore <b>native</b> methods automatically. <!-- tooltip end --> <p> -Use the fields below to specify minimum length, maximum length and regular expression expected for 'native' method names. +Use the fields below to specify minimum length, maximum length and regular expression expected for <b>native</b> method names. Specify <b>0</b> to not check the length of names. Regular expressions are in standard <b>java.util.regex</b> format. <p> <small>New in 14</small> diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/StaticMethodNamingConvention.html b/plugins/InspectionGadgets/src/inspectionDescriptions/StaticMethodNamingConvention.html index 37d12a6af08d..c0ced0ba01bd 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/StaticMethodNamingConvention.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/StaticMethodNamingConvention.html @@ -7,8 +7,6 @@ the specified regular expression pattern. Use the fields below to specify minimum length, maximum length and regular expression expected for static method names. Specify <b>0</b> to not check the length of names. Regular expressions are in standard <b>java.util.regex</b> format. <p> -Use the checkbox below to ignore native methods. -<p> </body> </html>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/TestMethodIsPublicVoidNoArg.html b/plugins/InspectionGadgets/src/inspectionDescriptions/TestMethodIsPublicVoidNoArg.html index a31b39a1a710..fe227cae1fbc 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/TestMethodIsPublicVoidNoArg.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/TestMethodIsPublicVoidNoArg.html @@ -1,8 +1,8 @@ <html> <body> -Reports any JUnit test methods whose names which are not declared +Reports any JUnit test methods which are declared <b>static</b>, not declared <b>public</b>, do not return -<b>void</b>, or take arguments. +<b>void</b>, or have parameters. Such test methods are easy to create inadvertently, but will not be executed by JUnit test runners. <!-- tooltip end --> diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/abstraction/type_may_be_weakened/LocalClass.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/abstraction/type_may_be_weakened/LocalClass.after.java new file mode 100644 index 000000000000..42653ac7af63 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/abstraction/type_may_be_weakened/LocalClass.after.java @@ -0,0 +1,11 @@ +class LocalClass { + + void foo() { + class A<T> { + void foo() {} + } + class B<T> extends A<T> {} + A<String> bb = new B(); + bb.foo(); + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/abstraction/type_may_be_weakened/LocalClass.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/abstraction/type_may_be_weakened/LocalClass.java new file mode 100644 index 000000000000..1c905035b914 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/abstraction/type_may_be_weakened/LocalClass.java @@ -0,0 +1,11 @@ +class LocalClass { + + void foo() { + class A<T> { + void foo() {} + } + class B<T> extends A<T> {} + B<String> b<caret>b = new B(); + bb.foo(); + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/abstraction/weaken_type/TypeMayBeWeakened.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/abstraction/weaken_type/TypeMayBeWeakened.java index 370aba53a19c..a134fcd8d9fb 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/abstraction/weaken_type/TypeMayBeWeakened.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/abstraction/weaken_type/TypeMayBeWeakened.java @@ -162,4 +162,15 @@ class Test implements Foo2 { interface Foo { void bar(); } -interface Foo2 extends Foo {}
\ No newline at end of file +interface Foo2 extends Foo {} +class Helper { + + void foo() { + class A<T> { + void foo() {} + } + class B<T> extends A<T> {} + B<String> <warning descr="Type of variable 'b' may be weakened to 'A'">b</warning> = new B(); + b.foo(); + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/equals_with_itself/EqualsWithItself.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/equals_with_itself/EqualsWithItself.java new file mode 100644 index 000000000000..03347da345a5 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/equals_with_itself/EqualsWithItself.java @@ -0,0 +1,23 @@ +class EqualsWithItself { + + boolean foo(Object o) { + return o.<warning descr="Identical qualifier and argument to 'equals()' call">equals</warning>(((o))); + } + + boolean withGetter() { + return getValue().<warning descr="Identical qualifier and argument to 'equals()' call">equals</warning>(getValue()); + } + + boolean withMethodCall() { + return build().equals(build()); + } + + private Integer value = 1; + public Integer getValue() { + return value; + } + + public Object build() { + return new Object(); + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/TestCaseWithNoTestMethodsInspection.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/TestCaseWithNoTestMethodsInspection.java deleted file mode 100644 index f2f0afea3a7c..000000000000 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/TestCaseWithNoTestMethodsInspection.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.siyeh.igtest.junit; - -import junit.framework.TestCase; - -public class TestCaseWithNoTestMethodsInspection extends TestCase -{ - public TestCaseWithNoTestMethodsInspection() - { - } - - public void teardown() - { - - } -} diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/junit3_method_naming_convention/JUnit3MethodNamingConvention.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/junit3_method_naming_convention/JUnit3MethodNamingConvention.java new file mode 100644 index 000000000000..fbe2e70e0945 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/junit3_method_naming_convention/JUnit3MethodNamingConvention.java @@ -0,0 +1,10 @@ +public class JUnit3MethodNamingConvention extends junit.framework.TestCase { + + public void <warning descr="JUnit 3 test method name 'testA' is too short (5 < 8)">testA</warning>() {} + + public void <warning descr="JUnit 3 test method name 'testAbcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz' is too long (82 > 64)">testAbcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz</warning>() {} + + public void <warning descr="JUnit 3 test method name 'testGiveMeMore$$$' doesn't match regex 'test[A-Za-z_\d]*'">testGiveMeMore$$$</warning>() {} + + public void test_me_properly() {} +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/junit3_style_test_method_in_junit4_class/BeforeAnnotationUsed.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/junit3_style_test_method_in_junit4_class/BeforeAnnotationUsed.java new file mode 100644 index 000000000000..6ef566f99035 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/junit3_style_test_method_in_junit4_class/BeforeAnnotationUsed.java @@ -0,0 +1,9 @@ +import org.junit.Before; + +public class BeforeAnnotationUsed { + + @Before + public void before() {} + + public void <warning descr="Old style JUnit test method 'testOldStyle()' in JUnit 4 class">testOldStyle</warning>() {} +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/junit3_style_test_method_in_junit4_class/JUnit3StyleTestMethodInJUnit4Class.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/junit3_style_test_method_in_junit4_class/JUnit3StyleTestMethodInJUnit4Class.java new file mode 100644 index 000000000000..e5df9e6d40ba --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/junit3_style_test_method_in_junit4_class/JUnit3StyleTestMethodInJUnit4Class.java @@ -0,0 +1,10 @@ +import org.junit.Test; + +public class JUnit3StyleTestMethodInJUnit4Class { + + @Test + public void junit4Test() { + } + + public void <warning descr="Old style JUnit test method 'testJUnit3()' in JUnit 4 class">testJUnit3</warning>() {} +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/junit4_method_naming_convention/JUnit4MethodNamingConvention.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/junit4_method_naming_convention/JUnit4MethodNamingConvention.java new file mode 100644 index 000000000000..69379a9abb87 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/junit4_method_naming_convention/JUnit4MethodNamingConvention.java @@ -0,0 +1,16 @@ +import org.junit.Test; + +public class JUnit4MethodNamingConvention { + + @Test + public void <warning descr="JUnit 4 test method name 'a' is too short (1 < 4)">a</warning>() {} + + @Test + public void <warning descr="JUnit 4 test method name 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz' is too long (78 > 64)">abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz</warning>() {} + + @Test + public void <warning descr="JUnit 4 test method name 'more$$$' doesn't match regex '[a-z][A-Za-z_\d]*'">more$$$</warning>() {} + + @Test + public void assure_foo_is_never_null() {} +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/test_case_with_no_test_methods/TestCaseWithNoTestMethods.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/test_case_with_no_test_methods/TestCaseWithNoTestMethods.java new file mode 100644 index 000000000000..559dcce72720 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/test_case_with_no_test_methods/TestCaseWithNoTestMethods.java @@ -0,0 +1,19 @@ +public class <warning descr="JUnit test case 'TestCaseWithNoTestMethods' has no tests">TestCaseWithNoTestMethods</warning> extends junit.framework.TestCase { + + TestCaseWithNoTestMethods() {} + + public int testOne() { + return 1; + } + + public static void testTwo() {} + void testThree() {} + public void testFour(int i) {} + + public void setUp() throws Exception { + super.setUp(); + } + public void tearDown() throws Exception { + super.tearDown(); + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/test_method_is_public_void_no_arg/JUnit3TestMethodIsPublicVoidNoArg.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/test_method_is_public_void_no_arg/JUnit3TestMethodIsPublicVoidNoArg.java new file mode 100644 index 000000000000..68a5df1305f6 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/test_method_is_public_void_no_arg/JUnit3TestMethodIsPublicVoidNoArg.java @@ -0,0 +1,16 @@ +public class JUnit3TestMethodIsPublicVoidNoArg extends junit.framework.TestCase { + + public JUnit3TestMethodIsPublicVoidNoArg() {} + + void <warning descr="Test method 'testOne()' is not declared 'public void'">testOne</warning>() {} + + public int <warning descr="Test method 'testTwo()' is not declared 'public void'">testTwo</warning>() { + return 2; + } + + public static void <warning descr="Test method 'testThree()' should not be 'static'">testThree</warning>() {} + + public void <warning descr="Test method 'testFour()' should probably not have parameters">testFour</warning>(int i) {} + + public void testFive() {} +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/test_method_is_public_void_no_arg/JUnit4TestMethodIsPublicVoidNoArg.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/test_method_is_public_void_no_arg/JUnit4TestMethodIsPublicVoidNoArg.java new file mode 100644 index 000000000000..8641acb3b49e --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/junit/test_method_is_public_void_no_arg/JUnit4TestMethodIsPublicVoidNoArg.java @@ -0,0 +1,24 @@ +import org.junit.Test; + +public class JUnit4TestMethodIsPublicVoidNoArg { + + @Test + JUnit4TestMethodIsPublicVoidNoArg() {} + + @Test + void <warning descr="Test method 'testOne()' is not declared 'public void'">testOne</warning>() {} + + @Test + public int <warning descr="Test method 'testTwo()' is not declared 'public void'">testTwo</warning>() { + return 2; + } + + @Test + public static void <warning descr="Test method 'testThree()' should not be 'static'">testThree</warning>() {} + + @Test + public void <warning descr="Test method 'testFour()' should probably not have parameters">testFour</warning>(int i) {} + + @Test + public void testFive() {} +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/if_switch/IfCanBeSwitch.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/if_switch/IfCanBeSwitch.java index 7bfc8aa08ff0..a0456798059d 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/if_switch/IfCanBeSwitch.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/if_switch/IfCanBeSwitch.java @@ -2,15 +2,15 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; class IfCanBeSwitch { void m1(int i) { // ok - if (i == 0) System.out.println("zero"); else if (i == 1) System.out.println("one"); else System.out.println("many"); + <warning descr="'if' statement replaceable with 'switch' statement">if</warning> (i == 0) System.out.println("zero"); else if (i == 1) System.out.println("one"); else System.out.println("many"); } void m1(char c) { // ok - if (c == '0') System.out.println("zero"); else if (c == '1') System.out.println("one"); else System.out.println("many"); + <warning descr="'if' statement replaceable with 'switch' statement">if</warning> (c == '0') System.out.println("zero"); else if (c == '1') System.out.println("one"); else System.out.println("many"); } void m1(byte i) { // ok - if (i == (byte)0) System.out.println("zero"); else if (i == (byte)1) System.out.println("one"); else System.out.println("many"); + <warning descr="'if' statement replaceable with 'switch' statement">if</warning> (i == (byte)0) System.out.println("zero"); else if (i == (byte)1) System.out.println("one"); else System.out.println("many"); } void m2(int i) { // bad, long literals @@ -23,7 +23,7 @@ class IfCanBeSwitch { void polyadic() { String s = null; - if (s.equals("asdf") || s.equals("addd") || s.equals("lkjh")) { + <warning descr="'if' statement replaceable with 'switch' statement">if</warning> (s.equals("asdf") || s.equals("addd") || s.equals("lkjh")) { System.out.println("asdf"); } else if (s.equals("null")) { @@ -59,14 +59,14 @@ class IfCanBeSwitch { } void nullSafe(String earth) { - if (earth.equals("foo")) { + <warning descr="'if' statement replaceable with 'switch' statement">if</warning> (earth.equals("foo")) { } else if ("bar".equals(earth)) { } else { } } void nullSafe2(@NotNull String narf) { - if ("foo".equals((narf))) { + <warning descr="'if' statement replaceable with 'switch' statement">if</warning> ("foo".equals((narf))) { // do this } else if ("bar".equals(narf)){ // do that @@ -75,4 +75,21 @@ class IfCanBeSwitch { // do something else. } } + + Num num; + enum Num { + ONE, TWO + } + Num getNum() { + return num; + } + void ifWithGetterToSwitch() { + <warning descr="'if' statement replaceable with 'switch' statement">if</warning> (getNum() == Num.ONE) { + System.out.println(1); + } else if (getNum() == Num.TWO) { + System.out.println(2); + } else { + System.out.println("-"); + } + } }
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/if_switch/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/if_switch/expected.xml deleted file mode 100644 index e56ebb574747..000000000000 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/if_switch/expected.xml +++ /dev/null @@ -1,44 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<problems> - <problem> - <file>IfCanBeSwitch.java</file> - <line>5</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'if' replaceable with 'switch'</problem_class> - <description><code>if</code> statement replaceable with 'switch' statement #loc</description> - </problem> - - <problem> - <file>IfCanBeSwitch.java</file> - <line>9</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'if' replaceable with 'switch'</problem_class> - <description><code>if</code> statement replaceable with 'switch' statement #loc</description> - </problem> - - <problem> - <file>IfCanBeSwitch.java</file> - <line>13</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'if' replaceable with 'switch'</problem_class> - <description><code>if</code> statement replaceable with 'switch' statement #loc</description> - </problem> - - <problem> - <file>IfCanBeSwitch.java</file> - <line>26</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'if' replaceable with 'switch'</problem_class> - <description><code>if</code> statement replaceable with 'switch' statement #loc</description> - </problem> - - <problem> - <file>IfCanBeSwitch.java</file> - <line>62</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'if' replaceable with 'switch'</problem_class> - <description><code>if</code> statement replaceable with 'switch' statement #loc</description> - </problem> - - <problem> - <file>IfCanBeSwitch.java</file> - <line>69</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'if' replaceable with 'switch'</problem_class> - <description><code>if</code> statement replaceable with 'switch' statement #loc</description> - </problem> -</problems>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/naming/native_method_naming_convention/NativeMethodNamingConvention.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/naming/native_method_naming_convention/NativeMethodNamingConvention.java index 9f39c4ceb884..9b8a8e66d289 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/naming/native_method_naming_convention/NativeMethodNamingConvention.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/naming/native_method_naming_convention/NativeMethodNamingConvention.java @@ -28,4 +28,6 @@ public class NativeMethodNamingConvention implements Runnable public native void run(); private void a() {} + + public static native void <warning descr="'native' method name 'b' is too short">b</warning>(); } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/pointless_arithmetic_expression/PointlessArithmeticExpression.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/pointless_arithmetic_expression/PointlessArithmeticExpression.java index 4ba7a97033ad..9a10262e3148 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/pointless_arithmetic_expression/PointlessArithmeticExpression.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/pointless_arithmetic_expression/PointlessArithmeticExpression.java @@ -1,5 +1,7 @@ package com.siyeh.igtest.numeric.pointless_arithmetic_expression; +import java.util.Random; + public class PointlessArithmeticExpression { private static final int ZERO_CONSTANT = 0; @@ -8,11 +10,11 @@ public class PointlessArithmeticExpression public static void main(String[] args) { final int i = 2; - final int j = i + 0; + final int j = <warning descr="'i + 0' can be replaced with 'i'">i + 0</warning>; System.out.println(j); - int k = 0+j; + int k = <warning descr="'0+j' can be replaced with 'j'">0+j</warning>; System.out.println(k); - k = j - 0; + k = <warning descr="'j - 0' can be replaced with 'j'">j - 0</warning>; System.out.println(k); k = 0 - j; System.out.println(k); @@ -20,11 +22,11 @@ public class PointlessArithmeticExpression System.out.println(k); k = j * ONE_CONSTANT; System.out.println(k); - k = j / 1; + k = <warning descr="'j / 1' can be replaced with 'j'">j / 1</warning>; System.out.println(k); String string = "foo" + 0; - k = j%1; + k = <warning descr="'j%1' can be replaced with '0'">j%1</warning>; System.out.println(k); if(k<=Integer.MAX_VALUE) @@ -90,9 +92,9 @@ public class PointlessArithmeticExpression } void more(int i) { - System.out.println(i / i); - System.out.println(i - i); - System.out.println(i % i); + System.out.println(<warning descr="'i / i' can be replaced with '1'">i / i</warning>); + System.out.println(<warning descr="'i - i' can be replaced with '0'">i - i</warning>); + System.out.println(<warning descr="'i % i' can be replaced with '0'">i % i</warning>); } } class Main { @@ -111,18 +113,29 @@ class Main { return (CONST + (new Main(5).i) * 8) - (Main.CONST + new Main(5).i * (8)); } - int one = 5/5; + int one = <warning descr="'5/5' can be replaced with '1'">5/5</warning>; } class Expanded {{ - int m = 1/**/ - (byte)0 - 9; // warn - int j = 8 * 0 * 8; - int k = 1 + /*a*/0 +/**/ 9; - byte l = (byte) (1L - 1L); + int m = <warning descr="'1/**/ - (byte)0 - 9' can be replaced with '1/**/ - 9'">1/**/ - (byte)0 - 9</warning>; // warn + int j = <warning descr="'8 * 0 * 8' can be replaced with '0'">8 * 0 * 8</warning>; + int k = <warning descr="'1 + /*a*/0 +/**/ 9' can be replaced with '1 + /*a*//**/ 9'">1 + /*a*/0 +/**/ 9</warning>; + byte l = (byte) (<warning descr="'1L - 1L' can be replaced with '0L'">1L - 1L</warning>); byte u = 1; - int z = 2 / 1 / 1; - System.out.println(u * 1); - long g = 8L / 8L; - long h = 9L * 0L; - int a = 8 * 0 * 8 * ; // don't warn + int z = <warning descr="'2 / 1 / 1' can be replaced with '2 / 1'">2 / 1 / 1</warning>; + System.out.println(<warning descr="'u * 1' can be replaced with 'u'">u * 1</warning>); + long g = <warning descr="'8L / 8L' can be replaced with '1L'">8L / 8L</warning>; + long h = <warning descr="'9L * 0L' can be replaced with '0L'">9L * 0L</warning>; + int a = 8 * 0 * 8 *<error descr="Expression expected"> </error>; // don't warn int minus = 2 - 1 - 1; -}}
\ No newline at end of file + int div = 3 / 2 / 2; + int mod = 3 % 2 % 2; +}} +class SideEffects { + public static void main( String args[] ){ + Random rand = new Random(); + int array[] = {1, 2, 4}; + int i = 1; + int b = array[i++] - array[i++]; + System.out.println(rand.nextInt(1000) - rand.nextInt(1000)); + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/pointless_arithmetic_expression/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/pointless_arithmetic_expression/expected.xml deleted file mode 100644 index f6af12237ea0..000000000000 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/pointless_arithmetic_expression/expected.xml +++ /dev/null @@ -1,129 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<problems> - - <problem> - <file>PointlessArithmeticExpression.java</file> - <line>11</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class> - <description><code>i + 0</code> can be replaced with 'i' #loc</description> - </problem> - - <problem> - <file>PointlessArithmeticExpression.java</file> - <line>13</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class> - <description><code>0+j</code> can be replaced with 'j' #loc</description> - </problem> - - <problem> - <file>PointlessArithmeticExpression.java</file> - <line>15</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class> - <description><code>j - 0</code> can be replaced with 'j' #loc</description> - </problem> - - <problem> - <file>PointlessArithmeticExpression.java</file> - <line>23</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class> - <description><code>j / 1</code> can be replaced with 'j' #loc</description> - </problem> - - <problem> - <file>PointlessArithmeticExpression.java</file> - <line>27</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class> - <description><code>j%1</code> can be replaced with '0' #loc</description> - </problem> - - <problem> - <file>PointlessArithmeticExpression.java</file> - <line>93</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class> - <description><code>i / i</code> can be replaced with '1' #loc</description> - </problem> - - <problem> - <file>PointlessArithmeticExpression.java</file> - <line>94</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class> - <description><code>i - i</code> can be replaced with '0' #loc</description> - </problem> - - <problem> - <file>PointlessArithmeticExpression.java</file> - <line>95</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class> - <description><code>i % i</code> can be replaced with '0' #loc</description> - </problem> - - <problem> - <file>PointlessArithmeticExpression.java</file> - <line>111</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class> - <description><code>(CONST + (new Main(5).i) * 8) - (Main.CONST + new Main(5).i * (8))</code> can be replaced with '0' #loc</description> - </problem> - - <problem> - <file>PointlessArithmeticExpression.java</file> - <line>114</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class> - <description><code>5/5</code> can be replaced with '1' #loc</description> - </problem> - - <problem> - <file>PointlessArithmeticExpression.java</file> - <line>117</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class> - <description><code>1/**/ - (byte)0 - 9</code> can be replaced with '1/**/ - 9' #loc</description> - </problem> - - <problem> - <file>PointlessArithmeticExpression.java</file> - <line>118</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class> - <description><code>8 * 0 * 8</code> can be replaced with '0' #loc</description> - </problem> - - <problem> - <file>PointlessArithmeticExpression.java</file> - <line>119</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class> - <description><code>1 + /*a*/0 +/**/ 9</code> can be replaced with '1 + /*a*//**/ 9' #loc</description> - </problem> - - <problem> - <file>PointlessArithmeticExpression.java</file> - <line>120</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class> - <description><code>1L - 1L</code> can be replaced with '0L' #loc</description> - </problem> - - <problem> - <file>PointlessArithmeticExpression.java</file> - <line>122</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class> - <description><code>2 / 1 / 1</code> can be replaced with '2 / 1' #loc</description> - </problem> - - <problem> - <file>PointlessArithmeticExpression.java</file> - <line>123</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class> - <description><code>u * 1</code> can be replaced with 'u' #loc</description> - </problem> - - <problem> - <file>PointlessArithmeticExpression.java</file> - <line>124</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class> - <description><code>8L / 8L</code> can be replaced with '1L' #loc</description> - </problem> - - <problem> - <file>PointlessArithmeticExpression.java</file> - <line>125</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class> - <description><code>9L * 0L</code> can be replaced with '0L' #loc</description> - </problem> -</problems>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/LightInspectionTestCase.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/LightInspectionTestCase.java index d262e31333ba..951d14ad9f8b 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/LightInspectionTestCase.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/LightInspectionTestCase.java @@ -109,15 +109,20 @@ public abstract class LightInspectionTestCase extends LightCodeInsightFixtureTes lastWord = lastWord.substring(0, lastWord.length() - 10); } final int length = lastWord.length(); + boolean upperCase = false; for (int i = 0; i < length; i++) { final char ch = lastWord.charAt(i); if (Character.isUpperCase(ch)) { - if (i != 0) { - basePath.append('_'); + if (!upperCase) { + upperCase = true; + if (i != 0) { + basePath.append('_'); + } } basePath.append(Character.toLowerCase(ch)); } else { + upperCase = false; basePath.append(ch); } } diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/EqualsWithItselfInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/EqualsWithItselfInspectionTest.java new file mode 100644 index 000000000000..868884ffa6db --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/EqualsWithItselfInspectionTest.java @@ -0,0 +1,34 @@ +/* + * Copyright 2000-2014 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. + */ +package com.siyeh.ig.bugs; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class EqualsWithItselfInspectionTest extends LightInspectionTestCase { + + public void testEqualsWithItself() { doTest(); } + + @Nullable + @Override + protected InspectionProfileEntry getInspection() { + return new EqualsWithItselfInspection(); + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/abstraction/TypeMayBeWeakenedFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/abstraction/TypeMayBeWeakenedFixTest.java index 58373f3a3955..d4b9ec3039bb 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/abstraction/TypeMayBeWeakenedFixTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/abstraction/TypeMayBeWeakenedFixTest.java @@ -12,10 +12,13 @@ public class TypeMayBeWeakenedFixTest extends IGQuickFixesTestCase { @Override public void setUp() throws Exception { super.setUp(); - myFixture.enableInspections(new TypeMayBeWeakenedInspection()); + final TypeMayBeWeakenedInspection inspection = new TypeMayBeWeakenedInspection(); + inspection.onlyWeakentoInterface = false; + myFixture.enableInspections(inspection); myRelativePath = "abstraction/type_may_be_weakened"; } public void testShorten() { doTest(InspectionGadgetsBundle.message("type.may.be.weakened.quickfix", "java.util.Collection")); } + public void testLocalClass() { doTest(InspectionGadgetsBundle.message("type.may.be.weakened.quickfix", "A")); } } diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/JUnit3MethodNamingConventionInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/JUnit3MethodNamingConventionInspectionTest.java new file mode 100644 index 000000000000..d71792bcbbc9 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/JUnit3MethodNamingConventionInspectionTest.java @@ -0,0 +1,45 @@ +/* + * Copyright 2000-2014 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. + */ +package com.siyeh.ig.junit; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class JUnit3MethodNamingConventionInspectionTest extends LightInspectionTestCase { + + public void testJUnit3MethodNamingConvention() { doTest(); } + + @Nullable + @Override + protected InspectionProfileEntry getInspection() { + return new JUnit3MethodNamingConventionInspection(); + } + + @Override + protected String[] getEnvironmentClasses() { + return new String[] { + "package junit.framework;" + + "public abstract class TestCase {" + + " protected void setUp() throws Exception {}" + + " protected void tearDown() throws Exception {}" + + "}" + }; + } +} diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/JUnit3StyleTestMethodInJUnit4ClassInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/JUnit3StyleTestMethodInJUnit4ClassInspectionTest.java new file mode 100644 index 000000000000..eb7e979f81e1 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/JUnit3StyleTestMethodInJUnit4ClassInspectionTest.java @@ -0,0 +1,57 @@ +/* + * Copyright 2000-2014 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. + */ +package com.siyeh.ig.junit; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class JUnit3StyleTestMethodInJUnit4ClassInspectionTest extends LightInspectionTestCase { + + public void testJUnit3StyleTestMethodInJUnit4Class() { doTest(); } + public void testBeforeAnnotationUsed() { doTest(); } + + @Nullable + @Override + protected InspectionProfileEntry getInspection() { + return new JUnit3StyleTestMethodInJUnit4ClassInspection(); + } + + @Override + protected String[] getEnvironmentClasses() { + return new String[] { + "package org.junit;" + + "import java.lang.annotation.ElementType;" + + "import java.lang.annotation.Retention;" + + "import java.lang.annotation.RetentionPolicy;" + + "import java.lang.annotation.Target;" + + "@Retention(RetentionPolicy.RUNTIME)" + + "@Target({ElementType.METHOD})" + + "public @interface Before {}", + "package org.junit;" + + "import java.lang.annotation.ElementType;" + + "import java.lang.annotation.Retention;" + + "import java.lang.annotation.RetentionPolicy;" + + "import java.lang.annotation.Target;" + + "@Retention(RetentionPolicy.RUNTIME)" + + "@Target({ElementType.METHOD})" + + "public @interface Test {}" + }; + } +} diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/JUnit4MethodNamingConventionInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/JUnit4MethodNamingConventionInspectionTest.java new file mode 100644 index 000000000000..bff20e9bd24a --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/JUnit4MethodNamingConventionInspectionTest.java @@ -0,0 +1,48 @@ +/* + * Copyright 2000-2014 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. + */ +package com.siyeh.ig.junit; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class JUnit4MethodNamingConventionInspectionTest extends LightInspectionTestCase { + + public void testJUnit4MethodNamingConvention() { doTest(); } + + @Nullable + @Override + protected InspectionProfileEntry getInspection() { + return new JUnit4MethodNamingConventionInspection(); + } + + @Override + protected String[] getEnvironmentClasses() { + return new String[] { + "package org.junit;" + + "import java.lang.annotation.ElementType;" + + "import java.lang.annotation.Retention;" + + "import java.lang.annotation.RetentionPolicy;" + + "import java.lang.annotation.Target;" + + "@Retention(RetentionPolicy.RUNTIME)" + + "@Target({ElementType.METHOD})" + + "public @interface Test {}" + }; + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/TestCaseWithNoTestMethodsInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/TestCaseWithNoTestMethodsInspectionTest.java new file mode 100644 index 000000000000..e8566eacdcd0 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/TestCaseWithNoTestMethodsInspectionTest.java @@ -0,0 +1,45 @@ +/* + * Copyright 2000-2014 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. + */ +package com.siyeh.ig.junit; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class TestCaseWithNoTestMethodsInspectionTest extends LightInspectionTestCase { + + public void testTestCaseWithNoTestMethods() { doTest(); } + + @Nullable + @Override + protected InspectionProfileEntry getInspection() { + return new TestCaseWithNoTestMethodsInspection(); + } + + @Override + protected String[] getEnvironmentClasses() { + return new String[] { + "package junit.framework;" + + "public abstract class TestCase {" + + " protected void setUp() throws Exception {}" + + " protected void tearDown() throws Exception {}" + + "}" + }; + } +} diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/TestMethodIsPublicVoidNoArgInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/TestMethodIsPublicVoidNoArgInspectionTest.java new file mode 100644 index 000000000000..72406191f2b1 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/junit/TestMethodIsPublicVoidNoArgInspectionTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2000-2014 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. + */ +package com.siyeh.ig.junit; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class TestMethodIsPublicVoidNoArgInspectionTest extends LightInspectionTestCase { + + public void testJUnit3TestMethodIsPublicVoidNoArg() { doTest(); } + public void testJUnit4TestMethodIsPublicVoidNoArg() { doTest(); } + + @Nullable + @Override + protected InspectionProfileEntry getInspection() { + return new TestMethodIsPublicVoidNoArgInspection(); + } + + @Override + protected String[] getEnvironmentClasses() { + return new String[] { + "package org.junit; " + + "public @interface Test {\n" + + " java.lang.Class<? extends java.lang.Throwable> expected() default org.junit.Test.None.class;" + + "}", + "package junit.framework;" + + "public abstract class TestCase {}"}; + } +} diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/migration/IfCanBeSwitchInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/migration/IfCanBeSwitchInspectionTest.java index 9b1f2574dd77..a13e61bfd234 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/migration/IfCanBeSwitchInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/migration/IfCanBeSwitchInspectionTest.java @@ -15,25 +15,36 @@ */ package com.siyeh.ig.migration; +import com.intellij.codeInspection.InspectionProfileEntry; import com.intellij.codeInspection.ex.LocalInspectionToolWrapper; import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.roots.LanguageLevelProjectExtension; import com.intellij.pom.java.LanguageLevel; import com.intellij.testFramework.IdeaTestUtil; +import com.intellij.testFramework.LightProjectDescriptor; import com.siyeh.ig.IGInspectionTestCase; +import com.siyeh.ig.LightInspectionTestCase; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; -public class IfCanBeSwitchInspectionTest extends IGInspectionTestCase { +public class IfCanBeSwitchInspectionTest extends LightInspectionTestCase { - @Override - protected Sdk getTestProjectSdk() { - LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_7); - return IdeaTestUtil.getMockJdk17(); + public void testIfCanBeSwitch() throws Exception { + doTest(); } - public void test() throws Exception { + @Nullable + @Override + protected InspectionProfileEntry getInspection() { final IfCanBeSwitchInspection inspection = new IfCanBeSwitchInspection(); inspection.suggestIntSwitches = true; + inspection.suggestEnumSwitches = true; inspection.setOnlySuggestNullSafe(true); - doTest("com/siyeh/igtest/migration/if_switch", new LocalInspectionToolWrapper(inspection)); + return inspection; + } + + @Override + protected String getBasePath() { + return "/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/if_switch"; } } diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/naming/InstanceMethodNamingConventionInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/naming/InstanceMethodNamingConventionInspectionTest.java index e408509b51d5..6479236acbb3 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/naming/InstanceMethodNamingConventionInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/naming/InstanceMethodNamingConventionInspectionTest.java @@ -24,6 +24,14 @@ import com.siyeh.ig.LightInspectionTestCase; public class InstanceMethodNamingConventionInspectionTest extends LightInspectionTestCase { @Override + protected void setUp() throws Exception { + super.setUp(); + final NativeMethodNamingConventionInspection inspection = new NativeMethodNamingConventionInspection(); + inspection.m_minLength = 0; + myFixture.enableInspections(inspection); + } + + @Override protected InspectionProfileEntry getInspection() { return new InstanceMethodNamingConventionInspection(); } diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/naming/com/siyeh/ig/naming/OverloadedVarargsMethodInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/naming/OverloadedVarargsMethodInspectionTest.java index 2ca586feef18..cc20bb3cc8fb 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/naming/com/siyeh/ig/naming/OverloadedVarargsMethodInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/naming/OverloadedVarargsMethodInspectionTest.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.siyeh.ig.naming.com.siyeh.ig.naming; +package com.siyeh.ig.naming; import com.intellij.codeInspection.InspectionProfileEntry; import com.siyeh.ig.LightInspectionTestCase; diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/naming/StaticMethodNamingConventionInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/naming/StaticMethodNamingConventionInspectionTest.java index 53e2b6a37f87..5c1eb29822c9 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/naming/StaticMethodNamingConventionInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/naming/StaticMethodNamingConventionInspectionTest.java @@ -24,6 +24,14 @@ import com.siyeh.ig.LightInspectionTestCase; public class StaticMethodNamingConventionInspectionTest extends LightInspectionTestCase { @Override + protected void setUp() throws Exception { + super.setUp(); + final NativeMethodNamingConventionInspection inspection = new NativeMethodNamingConventionInspection(); + inspection.m_minLength = 0; + myFixture.enableInspections(inspection); + } + + @Override protected InspectionProfileEntry getInspection() { return new StaticMethodNamingConventionInspection(); } diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/numeric/PointlessArithmeticExpressionInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/numeric/PointlessArithmeticExpressionInspectionTest.java index 48914c37605f..58bbf452bd9c 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/numeric/PointlessArithmeticExpressionInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/numeric/PointlessArithmeticExpressionInspectionTest.java @@ -1,11 +1,18 @@ package com.siyeh.ig.numeric; -import com.siyeh.ig.IGInspectionTestCase; +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; +import org.jetbrains.annotations.Nullable; -public class PointlessArithmeticExpressionInspectionTest extends IGInspectionTestCase { +public class PointlessArithmeticExpressionInspectionTest extends LightInspectionTestCase { - public void test() throws Exception { - doTest("com/siyeh/igtest/numeric/pointless_arithmetic_expression", - new PointlessArithmeticExpressionInspection()); + public void testPointlessArithmeticExpression() { + doTest(); + } + + @Nullable + @Override + protected InspectionProfileEntry getInspection() { + return new PointlessArithmeticExpressionInspection(); } }
\ No newline at end of file |