IG: check for TestCase instead of inheritors of Assert
authorBas Leijdekkers <basleijdekkers@gmail.com>
Mon, 10 Oct 2016 12:19:11 +0000 (14:19 +0200)
committerBas Leijdekkers <basleijdekkers@gmail.com>
Mon, 10 Oct 2016 15:28:55 +0000 (17:28 +0200)
plugins/InspectionGadgets/src/com/siyeh/ig/junit/JUnit4AnnotatedMethodInJUnit3TestCaseInspection.java
plugins/InspectionGadgets/test/com/siyeh/igfixes/junit/convert_to_junit4/LocalMethod.after.java [new file with mode: 0644]
plugins/InspectionGadgets/test/com/siyeh/igfixes/junit/convert_to_junit4/LocalMethod.java [new file with mode: 0644]
plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/junit/ConvertToJUnit4FixTest.java

index adeb95611857ce5fce03418a9a85119359f299cb..9c013012b3cf0b4d62297b9ec2050eec9d1a5a92 100644 (file)
@@ -22,7 +22,6 @@ 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.intellij.psi.util.InheritanceUtil;
 import com.siyeh.InspectionGadgetsBundle;
 import com.siyeh.ig.InspectionGadgetsFix;
 import com.siyeh.ig.fixes.RenameFix;
@@ -38,7 +37,7 @@ public class JUnit4AnnotatedMethodInJUnit3TestCaseInspection extends JUnit4Annot
   @NotNull
   @Override
   protected InspectionGadgetsFix[] buildFixes(Object... infos) {
-    final List<InspectionGadgetsFix> fixes = new ArrayList(3);
+    final List<InspectionGadgetsFix> fixes = new ArrayList<>(3);
     final PsiMethod method = (PsiMethod)infos[1];
     if (AnnotationUtil.isAnnotated(method, IGNORE, false)) {
       fixes.add(new RemoveIgnoreAndRename(method));
@@ -224,14 +223,15 @@ public class JUnit4AnnotatedMethodInJUnit3TestCaseInspection extends JUnit4Annot
         return;
       }
       final PsiMethod method = expression.resolveMethod();
-      if (method == null) {
+      if (method == null || !method.hasModifierProperty(PsiModifier.STATIC)) {
         return;
       }
       final PsiClass aClass = method.getContainingClass();
       if (aClass == null) {
         return;
       }
-      if (!InheritanceUtil.isInheritor(aClass, "junit.framework.Assert")) {
+      final String name = aClass.getQualifiedName();
+      if (!"junit.framework.Assert".equals(name) && !"junit.framework.TestCase".equals(name)) {
         return;
       }
       @NonNls final String newExpressionText = "org.junit.Assert." + expression.getText();
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/junit/convert_to_junit4/LocalMethod.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/junit/convert_to_junit4/LocalMethod.after.java
new file mode 100644 (file)
index 0000000..af23e53
--- /dev/null
@@ -0,0 +1,12 @@
+
+import junit.framework.TestCase;
+
+public class LocalMethod {
+
+  @org.junit.Test
+  public void testForeign() {
+    what();
+  }
+
+  void what() {}
+}
\ No newline at end of file
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/junit/convert_to_junit4/LocalMethod.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/junit/convert_to_junit4/LocalMethod.java
new file mode 100644 (file)
index 0000000..0931c23
--- /dev/null
@@ -0,0 +1,12 @@
+
+import junit.framework.TestCase;
+
+public class LocalMethod extends TestCase {
+
+  @org.junit.Test
+  public void <caret>testForeign() {
+    what();
+  }
+
+  void what() {}
+}
\ No newline at end of file
index 44636efb18048997366b07a3a3f3b46bbf8b3e6d..e5d5f2cdefbb7824a65c177e174c5e7c47dd1b7e 100644 (file)
@@ -24,7 +24,13 @@ import com.siyeh.ig.junit.JUnit4AnnotatedMethodInJUnit3TestCaseInspection;
  */
 public class ConvertToJUnit4FixTest extends IGQuickFixesTestCase {
 
-  public void testOtherMethods() { doTest(InspectionGadgetsBundle.message("convert.junit3.test.class.quickfix", getTestName(false))); }
+  public void testOtherMethods() { doTest(); }
+  public void testLocalMethod() { doTest(); }
+
+  @Override
+  protected void doTest() {
+    doTest(InspectionGadgetsBundle.message("convert.junit3.test.class.quickfix", getTestName(false)));
+  }
 
   @Override
   protected void setUp() throws Exception {