junit: skip inner classes for known runners in test detection (IDEA-185621)
authorAnna Kozlova <anna.kozlova@jetbrains.com>
Fri, 26 Jan 2018 13:33:41 +0000 (16:33 +0300)
committerAnna Kozlova <anna.kozlova@jetbrains.com>
Fri, 26 Jan 2018 13:38:13 +0000 (16:38 +0300)
replace mock junit with project library

15 files changed:
java/execution/impl/src/com/intellij/execution/junit/JUnitUtil.java
java/java-tests/testData/junit/configurations/mock JUnit/junit/framework/TestCase.class [deleted file]
java/java-tests/testData/junit/configurations/mock JUnit/junit/framework/TestCase.java [deleted file]
java/java-tests/testData/junit/configurations/mock JUnit/junit/framework/ThirdPartyClass.class [deleted file]
java/java-tests/testData/junit/configurations/mock JUnit/junit/framework/ThirdPartyClass.java [deleted file]
java/java-tests/testData/junit/configurations/mock JUnit/org/junit/Test.class [deleted file]
java/java-tests/testData/junit/configurations/mock JUnit/org/junit/Test.java [deleted file]
java/java-tests/testData/junit/configurations/mock JUnit/org/junit/runner/RunWith.class [deleted file]
java/java-tests/testData/junit/configurations/mock JUnit/org/junit/runner/RunWith.java [deleted file]
java/java-tests/testData/junit/configurations/module1/test1/DerivedTest.java
java/java-tests/testData/junit/configurations/module1/test1/ThirdPartyTest.java [new file with mode: 0644]
java/java-tests/testData/junit/configurations/module1/test1/nested/TestWithJunit4.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/java/execution/BaseConfigurationTestCase.java
java/java-tests/testSrc/com/intellij/java/execution/ConfigurationRefactoringsTest.java
java/java-tests/testSrc/com/intellij/java/execution/ConfigurationsTest.java

index 22901597d9741c9a7b760daa6fe3b9fdea97defc..541f1529a293e9f646fd8a9e78c33db6eca9386d 100644 (file)
@@ -166,14 +166,8 @@ public class JUnitUtil {
     final PsiClass topLevelClass = PsiTreeUtil.getTopmostParentOfType(psiClass, PsiClass.class);
     if (topLevelClass != null) {
       final PsiAnnotation annotation = AnnotationUtil.findAnnotationInHierarchy(topLevelClass, Collections.singleton(RUN_WITH));
-      if (annotation != null) {
-        final PsiAnnotationMemberValue attributeValue = annotation.findAttributeValue("value");
-        if (attributeValue instanceof PsiClassObjectAccessExpression) {
-          final String runnerName = ((PsiClassObjectAccessExpression)attributeValue).getOperand().getType().getCanonicalText();
-          if (!(PARAMETERIZED_CLASS_NAME.equals(runnerName) || SUITE_CLASS_NAME.equals(runnerName))) {
-            return true;
-          }
-        }
+      if (annotation != null && !isInheritorOrSelfRunner(annotation, RUNNERS_REQUIRE_ANNOTATION_ON_TEST_METHOD)) {
+        return true;
       }
     }
 
diff --git a/java/java-tests/testData/junit/configurations/mock JUnit/junit/framework/TestCase.class b/java/java-tests/testData/junit/configurations/mock JUnit/junit/framework/TestCase.class
deleted file mode 100644 (file)
index bdd44ce..0000000
Binary files a/java/java-tests/testData/junit/configurations/mock JUnit/junit/framework/TestCase.class and /dev/null differ
diff --git a/java/java-tests/testData/junit/configurations/mock JUnit/junit/framework/TestCase.java b/java/java-tests/testData/junit/configurations/mock JUnit/junit/framework/TestCase.java
deleted file mode 100644 (file)
index eaa745c..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-package junit.framework;
-public class TestCase {}
diff --git a/java/java-tests/testData/junit/configurations/mock JUnit/junit/framework/ThirdPartyClass.class b/java/java-tests/testData/junit/configurations/mock JUnit/junit/framework/ThirdPartyClass.class
deleted file mode 100644 (file)
index 1c51d38..0000000
Binary files a/java/java-tests/testData/junit/configurations/mock JUnit/junit/framework/ThirdPartyClass.class and /dev/null differ
diff --git a/java/java-tests/testData/junit/configurations/mock JUnit/junit/framework/ThirdPartyClass.java b/java/java-tests/testData/junit/configurations/mock JUnit/junit/framework/ThirdPartyClass.java
deleted file mode 100644 (file)
index d4f54aa..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-package junit.framework;
-public class ThirdPartyClass extends TestCase {}
diff --git a/java/java-tests/testData/junit/configurations/mock JUnit/org/junit/Test.class b/java/java-tests/testData/junit/configurations/mock JUnit/org/junit/Test.class
deleted file mode 100644 (file)
index 7123197..0000000
Binary files a/java/java-tests/testData/junit/configurations/mock JUnit/org/junit/Test.class and /dev/null differ
diff --git a/java/java-tests/testData/junit/configurations/mock JUnit/org/junit/Test.java b/java/java-tests/testData/junit/configurations/mock JUnit/org/junit/Test.java
deleted file mode 100644 (file)
index 66c78b4..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-package org.junit;
-public @interface Test {}
\ No newline at end of file
diff --git a/java/java-tests/testData/junit/configurations/mock JUnit/org/junit/runner/RunWith.class b/java/java-tests/testData/junit/configurations/mock JUnit/org/junit/runner/RunWith.class
deleted file mode 100644 (file)
index e059434..0000000
Binary files a/java/java-tests/testData/junit/configurations/mock JUnit/org/junit/runner/RunWith.class and /dev/null differ
diff --git a/java/java-tests/testData/junit/configurations/mock JUnit/org/junit/runner/RunWith.java b/java/java-tests/testData/junit/configurations/mock JUnit/org/junit/runner/RunWith.java
deleted file mode 100644 (file)
index b4b9e99..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-package org.junit.runner;
-@interface RunWith {
-    Class<? extends Runner> value();
-}
index 4669cc0058d0a4d40a3ce743c5bf094d8b271c17..c6c632bf68b8c6cf413d44c07940dafc372d315d 100644 (file)
@@ -1,6 +1,4 @@
 package test1;
 
-import junit.framework.ThirdPartyClass;
-
-public class DerivedTest extends ThirdPartyClass {
+public class DerivedTest extends ThirdPartyTest {
 }
\ No newline at end of file
diff --git a/java/java-tests/testData/junit/configurations/module1/test1/ThirdPartyTest.java b/java/java-tests/testData/junit/configurations/module1/test1/ThirdPartyTest.java
new file mode 100644 (file)
index 0000000..863666c
--- /dev/null
@@ -0,0 +1,3 @@
+package test1;
+import junit.framework.*;
+public class ThirdPartyTest extends TestCase {}
\ No newline at end of file
diff --git a/java/java-tests/testData/junit/configurations/module1/test1/nested/TestWithJunit4.java b/java/java-tests/testData/junit/configurations/module1/test1/nested/TestWithJunit4.java
new file mode 100644 (file)
index 0000000..39aa54c
--- /dev/null
@@ -0,0 +1,9 @@
+package test1.nested;
+
+@org.junit.runner.RunWith(org.junit.runners.Parameterized.class)
+public class TestWithJunit4 {
+  @org.junit.Test
+  public void test1() {}
+  
+  public static class InnerNoTests {}
+}
\ No newline at end of file
index 74e2d3c5a4bcccc0c2089349d0d8fdf83aeb0574..2e1d68297c2a2ec953909a24a2e8a8774768577e 100644 (file)
@@ -23,6 +23,7 @@ import com.intellij.execution.actions.ConfigurationFromContext;
 import com.intellij.execution.actions.RunConfigurationProducer;
 import com.intellij.execution.configurations.RunConfiguration;
 import com.intellij.execution.junit.JUnitConfiguration;
+import com.intellij.execution.junit.JUnitUtil;
 import com.intellij.execution.testframework.AbstractJavaTestConfigurationProducer;
 import com.intellij.openapi.actionSystem.CommonDataKeys;
 import com.intellij.openapi.actionSystem.LangDataKeys;
@@ -37,6 +38,7 @@ import com.intellij.openapi.roots.ModuleRootManager;
 import com.intellij.openapi.roots.ModuleRootModificationUtil;
 import com.intellij.openapi.vfs.LocalFileSystem;
 import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.project.IntelliJProjectConfiguration;
 import com.intellij.psi.JavaPsiFacade;
 import com.intellij.psi.PsiClass;
 import com.intellij.psi.PsiElement;
@@ -51,7 +53,6 @@ import java.util.List;
 public abstract class BaseConfigurationTestCase extends IdeaTestCase {
   protected TempFiles myTempFiles;
   private final List<Module> myModulesToDispose = new ArrayList<>();
-  protected static final String MOCK_JUNIT = "mock JUnit";
 
   @Override
   protected void setUp() throws Exception {
@@ -83,13 +84,11 @@ public abstract class BaseConfigurationTestCase extends IdeaTestCase {
       PsiTestUtil.addContentRoot(module, module1Content);
     }
 
-    VirtualFile mockJUnit = findFile(MOCK_JUNIT);
-    ModuleRootModificationUtil.addModuleLibrary(module, mockJUnit.getUrl());
+    IntelliJProjectConfiguration.LibraryRoots junit4Library = IntelliJProjectConfiguration.getProjectLibrary("JUnit4");
+    ModuleRootModificationUtil.addModuleLibrary(module, "JUnit4", junit4Library.getClassesUrls(), junit4Library.getSourcesUrls());
     ModuleRootModificationUtil.setModuleSdk(module, ModuleRootManager.getInstance(myModule).getSdk());
     GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module);
-    VirtualFile testCase = mockJUnit.findFileByRelativePath("junit/framework/TestCase.java");
-    assertNotNull(testCase);
-    assertTrue(scope.contains(testCase));
+    assertNotNull(JavaPsiFacade.getInstance(getProject()).findClass(JUnitUtil.TEST_CASE_CLASS, scope));
     Module missingModule = createTempModule();
     addDependency(module, missingModule);
     ModuleManager.getInstance(myProject).disposeModule(missingModule);
index ae13e0c2725862261103fd70fcf81061871bff9f..fa87631dbd208cfb4faf94de689d4cf9abaa8dd4 100644 (file)
@@ -13,7 +13,10 @@ import com.intellij.execution.junit.AllInPackageConfigurationProducer;
 import com.intellij.execution.junit.JUnitConfiguration;
 import com.intellij.execution.junit.JUnitConfigurationType;
 import com.intellij.execution.testframework.AbstractJavaTestConfigurationProducer;
+import com.intellij.openapi.vfs.JarFileSystem;
+import com.intellij.openapi.vfs.LocalFileSystem;
 import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.project.IntelliJProjectConfiguration;
 import com.intellij.psi.*;
 import com.intellij.refactoring.PackageWrapper;
 import com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesProcessor;
@@ -22,10 +25,11 @@ import com.intellij.refactoring.move.moveMembers.MockMoveMembersOptions;
 import com.intellij.refactoring.move.moveMembers.MoveMembersProcessor;
 import com.intellij.refactoring.rename.RenameProcessor;
 import com.intellij.testFramework.MapDataContext;
-import java.util.HashSet;
 import org.jetbrains.annotations.NotNull;
 
+import java.io.File;
 import java.io.IOException;
+import java.util.HashSet;
 
 public class ConfigurationRefactoringsTest extends BaseConfigurationTestCase {
   private static final String APPLICATION_CODE = "public class Application {" +
@@ -194,7 +198,12 @@ public class ConfigurationRefactoringsTest extends BaseConfigurationTestCase {
   private void initModule() {
     mySource.initModule();
     mySource.copyJdkFrom(myModule);
-    mySource.addLibrary(findFile(MOCK_JUNIT));
+    IntelliJProjectConfiguration.LibraryRoots junit4Library = IntelliJProjectConfiguration.getProjectLibrary("JUnit4");
+    for (File file : junit4Library.getClasses()) {
+      VirtualFile libFile = LocalFileSystem.getInstance().findFileByIoFile(file);
+      assertNotNull(libFile);
+      mySource.addLibrary(JarFileSystem.getInstance().getJarRootForLocalFile(libFile));
+    }
   }
 
   private void move(final PsiElement psiElement, String packageName) {
index 3b5464eb3be7b72cd1d57bda1139c62b310ab6e0..3981a9d918fc16fa69831d090e930d061ce780aa 100644 (file)
@@ -38,6 +38,7 @@ import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.openapi.vfs.JarFileSystem;
 import com.intellij.openapi.vfs.LocalFileSystem;
 import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.project.IntelliJProjectConfiguration;
 import com.intellij.psi.*;
 import com.intellij.psi.search.GlobalSearchScope;
 import com.intellij.rt.ant.execution.SegmentedOutputStream;
@@ -158,7 +159,7 @@ public class ConfigurationsTest extends BaseConfigurationTestCase {
     PsiClass psiClass = findTestA(module1);
     PsiClass psiClass2 = findTestA(getModule2());
     PsiClass derivedTest = findClass(module1, "test1.DerivedTest");
-    PsiClass baseTestCase = findClass("junit.framework.ThirdPartyClass", module1AndLibraries);
+    PsiClass baseTestCase = findClass("test1.ThirdPartyTest", module1AndLibraries);
     PsiClass testB = findClass(getModule3(), "test1.TestB");
     assertNotNull(testCase);
     assertNotNull(derivedTest);
@@ -177,6 +178,8 @@ public class ConfigurationsTest extends BaseConfigurationTestCase {
         psiClass2.getQualifiedName(),
         derivedTest.getQualifiedName(), RT_INNER_TEST_NAME,
         "test1.nested.TestA",
+        "test1.nested.TestWithJunit4",
+        "test1.ThirdPartyTest",
         testB.getQualifiedName()},
       lines);
   }
@@ -259,7 +262,10 @@ public class ConfigurationsTest extends BaseConfigurationTestCase {
     CHECK.singleOccurence(classPath, getOutput(module2, true));
     CHECK.singleOccurence(classPath, getOutput(module3, false));
     CHECK.singleOccurence(classPath, getOutput(module3, true));
-    CHECK.singleOccurence(classPath, getFSPath(findFile(MOCK_JUNIT)));
+    IntelliJProjectConfiguration.LibraryRoots junit4Library = IntelliJProjectConfiguration.getProjectLibrary("JUnit4");
+    for (File file : junit4Library.getClasses()) {
+      CHECK.singleOccurence(classPath, file.getPath());
+    }
   }
 
   public void testExternalizeJUnitConfiguration() {
@@ -489,7 +495,10 @@ public class ConfigurationsTest extends BaseConfigurationTestCase {
         CompilerTester tester = new CompilerTester(project, Arrays.asList(ModuleManager.getInstance(project).getModules()));
         try {
           List<CompilerMessage> messages = tester.make();
-          assertFalse(messages.stream().anyMatch(message -> message.getCategory() == CompilerMessageCategory.ERROR));
+          assertFalse(messages.stream().filter(message -> message.getCategory() == CompilerMessageCategory.ERROR)
+                              .map(message -> message.getMessage())
+                              .findFirst().orElse("Compiles fine"),
+                      messages.stream().anyMatch(message -> message.getCategory() == CompilerMessageCategory.ERROR));
           task.startSearch();
         }
         finally {