From 39aed0475df1b00f2208b590f732f24535af64bc Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Fri, 4 Jan 2019 19:58:16 +0100 Subject: [PATCH] convert InspectionTestCase to light --- .../deadCode/annotationInterface/expected.xml | 7 +- .../expected.xml | 8 +- .../deadCode/mutableCalls/expected.xml | 10 +- .../deadCode/packageLocal/expected.xml | 6 + .../unusedLibrary/simple/expected.xml | 2 +- .../DeprecationInspectionTest.java | 32 +++-- .../NullableStuffInspectionAncientTest.java | 48 ++----- .../codeInspection/RedundantSuppressTest.java | 6 +- .../TestOnlyInspectionTest.java | 27 ++-- .../UnusedDeclarationInSubScopeTest.java | 19 +-- .../codeInspection/UnusedDeclarationTest.java | 25 ++-- .../UnusedLibraryInspectionTest.java | 37 ++++-- .../testFramework/InspectionTestCase.java | 123 +++++++----------- .../testFramework/LightTestMigration.java | 48 ++----- .../LightCodeInsightFixtureTestCase.java | 1 + .../SSBasedInspectionTest.java | 4 +- ...nMethodIsAlwaysInvertedInspectionTest.java | 12 +- .../com/siyeh/ig/IGInspectionTestCase.java | 2 +- ...ssaryFullyQualifiedNameInspectionTest.java | 8 -- .../InvalidPropertyKeyInspectionTest.java | 2 +- .../DuplicatePropertyInspection.java | 28 ++-- .../inspection/I18NInspectionTest.java | 2 +- 22 files changed, 188 insertions(+), 269 deletions(-) diff --git a/java/java-tests/testData/inspection/deadCode/annotationInterface/expected.xml b/java/java-tests/testData/inspection/deadCode/annotationInterface/expected.xml index 5aba82f1957e..627d48dfd1c1 100644 --- a/java/java-tests/testData/inspection/deadCode/annotationInterface/expected.xml +++ b/java/java-tests/testData/inspection/deadCode/annotationInterface/expected.xml @@ -1,9 +1,14 @@ + + Test.java + 4 + Interface is not implemented. + Test.java 5 - Method is never used. + Method owner class is never instantiated OR An instantiation is not reachable from entry points. diff --git a/java/java-tests/testData/inspection/deadCode/junitAbstractClassWithoutInheritor/expected.xml b/java/java-tests/testData/inspection/deadCode/junitAbstractClassWithoutInheritor/expected.xml index 13075abae5df..c8650ca523f3 100644 --- a/java/java-tests/testData/inspection/deadCode/junitAbstractClassWithoutInheritor/expected.xml +++ b/java/java-tests/testData/inspection/deadCode/junitAbstractClassWithoutInheritor/expected.xml @@ -4,6 +4,12 @@ Test.java 3 - Method is never used. + Method owner class is never instantiated OR An instantiation is not reachable from entry points. + + + Test.java + 1 + + Abstract class is not implemented. diff --git a/java/java-tests/testData/inspection/deadCode/mutableCalls/expected.xml b/java/java-tests/testData/inspection/deadCode/mutableCalls/expected.xml index 3d2c41b8c555..ed258df0bcf5 100644 --- a/java/java-tests/testData/inspection/deadCode/mutableCalls/expected.xml +++ b/java/java-tests/testData/inspection/deadCode/mutableCalls/expected.xml @@ -1,16 +1,22 @@ + + A.java + 1 + unused declaration + Class is not instantiated. + A.java 2 unused declaration - Method has usage(s) but they all belong to calls chain that has no members reachable from entry points. + Method owner class is never instantiated OR An instantiation is not reachable from entry points. A.java 5 unused declaration - Method has usage(s) but they all belong to calls chain that has no members reachable from entry points. + Method owner class is never instantiated OR An instantiation is not reachable from entry points. diff --git a/java/java-tests/testData/inspection/deadCode/packageLocal/expected.xml b/java/java-tests/testData/inspection/deadCode/packageLocal/expected.xml index 54733714fd19..5b5a920989f5 100644 --- a/java/java-tests/testData/inspection/deadCode/packageLocal/expected.xml +++ b/java/java-tests/testData/inspection/deadCode/packageLocal/expected.xml @@ -6,6 +6,12 @@ unused declaration Class has one instantiation but it is not reachable from entry points. + + A.java + 1 + unused declaration + Class is not instantiated. + A.java 4 diff --git a/java/java-tests/testData/inspection/unusedLibrary/simple/expected.xml b/java/java-tests/testData/inspection/unusedLibrary/simple/expected.xml index 9cd23b0df42b..212555bb2a13 100644 --- a/java/java-tests/testData/inspection/unusedLibrary/simple/expected.xml +++ b/java/java-tests/testData/inspection/unusedLibrary/simple/expected.xml @@ -1,7 +1,7 @@ - testSimple.iml + light_idea_test_case Unused library Unused library 'JUnit' diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/DeprecationInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/DeprecationInspectionTest.java index 5a2db65218be..1f1841582ba5 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/DeprecationInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/DeprecationInspectionTest.java @@ -4,16 +4,31 @@ package com.intellij.java.codeInspection; import com.intellij.JavaTestUtil; import com.intellij.codeInspection.deprecation.DeprecationInspection; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.roots.ContentEntry; import com.intellij.openapi.roots.JavaModuleExternalPaths; -import com.intellij.openapi.roots.ModuleRootModificationUtil; +import com.intellij.openapi.roots.ModifiableRootModel; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vfs.VfsUtilCore; import com.intellij.testFramework.InspectionTestCase; +import com.intellij.testFramework.LightProjectDescriptor; +import com.intellij.testFramework.fixtures.DefaultLightProjectDescriptor; +import org.jetbrains.annotations.NotNull; /** * @author max */ public class DeprecationInspectionTest extends InspectionTestCase { + + private final DefaultLightProjectDescriptor myProjectDescriptor = new DefaultLightProjectDescriptor() { + @Override + public void configureModule(@NotNull Module module, @NotNull ModifiableRootModel model, @NotNull ContentEntry contentEntry) { + super.configureModule(module, model, contentEntry); + model.getModuleExtension(JavaModuleExternalPaths.class) + .setExternalAnnotationUrls(new String[]{VfsUtilCore.pathToUrl(FileUtil.toSystemIndependentName(getTestDataPath() + "/deprecation/" + getTestName(true) + "/extAnnotations"))}); + } + }; + @Override protected String getTestDataPath() { return JavaTestUtil.getJavaTestDataPath() + "/inspection"; @@ -69,20 +84,13 @@ public class DeprecationInspectionTest extends InspectionTestCase { doTest("deprecation/" + getTestName(true), tool); } - /** - * Sets up external deprecation annotations - * for the current test module. - */ - private void configureExternalAnnotationsUrls(String... urls) { - ModuleRootModificationUtil.updateModel(myModule, (root) -> { - JavaModuleExternalPaths extension = root.getModuleExtension(JavaModuleExternalPaths.class); - extension.setExternalAnnotationUrls(urls); - }); + @NotNull + @Override + protected LightProjectDescriptor getProjectDescriptor() { + return myProjectDescriptor; } public void testExternallyDeprecatedDefaultConstructor() { - String url = VfsUtilCore.pathToUrl(FileUtil.toSystemIndependentName(getTestDataPath()) + "/deprecation/externallyDeprecatedDefaultConstructor/extAnnotations"); - configureExternalAnnotationsUrls(url); doTest(); } } diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/NullableStuffInspectionAncientTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/NullableStuffInspectionAncientTest.java index f97db725556d..48c5d2dbeb64 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/NullableStuffInspectionAncientTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/NullableStuffInspectionAncientTest.java @@ -19,13 +19,8 @@ package com.intellij.java.codeInspection; import com.intellij.JavaTestUtil; import com.intellij.codeInspection.ex.LocalInspectionToolWrapper; import com.intellij.codeInspection.nullable.NullableStuffInspection; -import com.intellij.openapi.application.WriteAction; -import com.intellij.openapi.projectRoots.Sdk; -import com.intellij.openapi.projectRoots.SdkModificator; -import com.intellij.openapi.roots.OrderRootType; -import com.intellij.openapi.vfs.VirtualFile; import com.intellij.testFramework.InspectionTestCase; -import com.intellij.testFramework.PsiTestUtil; +import com.intellij.testFramework.LightProjectDescriptor; import org.jetbrains.annotations.NotNull; public class NullableStuffInspectionAncientTest extends InspectionTestCase { @@ -48,43 +43,18 @@ public class NullableStuffInspectionAncientTest extends InspectionTestCase { super.tearDown(); } + @NotNull + @Override + protected LightProjectDescriptor getProjectDescriptor() { + return JAVA_1_4_ANNOTATED; + } + public void testJdk14() { - doTest("nullableProblems/" + getTestName(true), new LocalInspectionToolWrapper(myInspection), "java 1.4"); + doTest("nullableProblems/" + getTestName(true), new LocalInspectionToolWrapper(myInspection)); } public void testJdkAnnotationsWithoutJetBrainsAnnotations() { - doTest("nullableProblems/" + getTestName(true), new LocalInspectionToolWrapper(myInspection), "java 1.5"); + doTest("nullableProblems/" + getTestName(true), new LocalInspectionToolWrapper(myInspection)); } - @Override - protected Sdk getTestProjectSdk() { - Sdk sdk = super.getTestProjectSdk(); - sdk = removeAnnotationsJar(sdk); - if ("testJdkAnnotationsWithoutJetBrainsAnnotations".equals(getName())) { - sdk = PsiTestUtil.addJdkAnnotations(sdk); - } - return sdk; - } - - @NotNull - private static Sdk removeAnnotationsJar(@NotNull Sdk sdk) { - return WriteAction.compute(() -> { - Sdk clone; - try { - clone = (Sdk)sdk.clone(); - } - catch (CloneNotSupportedException e) { - throw new RuntimeException(e); - } - final SdkModificator sdkMod = clone.getSdkModificator(); - for (VirtualFile file : sdkMod.getRoots(OrderRootType.CLASSES)) { - if ("annotations.jar".equals(file.getName())) { - sdkMod.removeRoot(file, OrderRootType.CLASSES); - break; - } - } - sdkMod.commitChanges(); - return clone; - }); - } } \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/RedundantSuppressTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/RedundantSuppressTest.java index 8c0bd56b76aa..44d7b4bf09b5 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/RedundantSuppressTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/RedundantSuppressTest.java @@ -71,7 +71,7 @@ public class RedundantSuppressTest extends InspectionTestCase { } public void testModuleInfo() { - doTest("redundantSuppress/" + getTestName(true), myWrapper,"java 1.5",false); + doTest("redundantSuppress/" + getTestName(true), myWrapper, false); } public void testDefaultFile() { @@ -83,7 +83,7 @@ public class RedundantSuppressTest extends InspectionTestCase { } public void testAnnotator() { - doTest("redundantSuppress/" + getTestName(true), myWrapper,"java 1.5",false); + doTest("redundantSuppress/" + getTestName(true), myWrapper, false); } public void testIgnoreUnused() { @@ -112,6 +112,6 @@ public class RedundantSuppressTest extends InspectionTestCase { } private void doTest() { - doTest("redundantSuppress/" + getTestName(true), myWrapper,"java 1.5",true); + doTest("redundantSuppress/" + getTestName(true), myWrapper, true); } } diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/TestOnlyInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/TestOnlyInspectionTest.java index cddf68165c9d..d53c97bf118d 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/TestOnlyInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/TestOnlyInspectionTest.java @@ -18,20 +18,29 @@ package com.intellij.java.codeInspection; import com.intellij.analysis.AnalysisScope; import com.intellij.codeInspection.ex.LocalInspectionToolWrapper; import com.intellij.codeInspection.testOnly.TestOnlyInspection; -import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.roots.ContentEntry; +import com.intellij.openapi.roots.ModifiableRootModel; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.testFramework.InspectionTestCase; -import com.intellij.testFramework.PsiTestUtil; +import com.intellij.testFramework.LightProjectDescriptor; +import com.intellij.testFramework.fixtures.DefaultLightProjectDescriptor; import org.jetbrains.annotations.NotNull; public class TestOnlyInspectionTest extends InspectionTestCase { + + private final static DefaultLightProjectDescriptor ourProjectDescriptor = new DefaultLightProjectDescriptor() { + @Override + public void configureModule(@NotNull Module module, @NotNull ModifiableRootModel model, @NotNull ContentEntry contentEntry) { + super.configureModule(module, model, contentEntry); + contentEntry.addSourceFolder(contentEntry.getUrl() + "/test", true); + } + }; + + @NotNull @Override - protected void setupRootModel(@NotNull String testDir, @NotNull VirtualFile[] sourceDir, String jdkName) { - super.setupRootModel(testDir, sourceDir, jdkName); - VirtualFile projectDir = LocalFileSystem.getInstance().findFileByPath(testDir); - assertNotNull(projectDir); - VirtualFile test = projectDir.findChild("test"); - if (test != null) PsiTestUtil.addSourceRoot(myModule, test, true); + protected LightProjectDescriptor getProjectDescriptor() { + return ourProjectDescriptor; } @NotNull @@ -68,6 +77,6 @@ public class TestOnlyInspectionTest extends InspectionTestCase { private void doTest() { TestOnlyInspection i = new TestOnlyInspection(); - doTest("testOnly/" + getTestName(true), new LocalInspectionToolWrapper(i), "java 1.5"); + doTest("testOnly/" + getTestName(true), new LocalInspectionToolWrapper(i)); } } \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedDeclarationInSubScopeTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedDeclarationInSubScopeTest.java index c9ef600229a5..b77b4caabf92 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedDeclarationInSubScopeTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedDeclarationInSubScopeTest.java @@ -4,30 +4,17 @@ package com.intellij.java.codeInspection; import com.intellij.analysis.AnalysisScope; -import com.intellij.openapi.roots.ModuleRootManager; -import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.psi.PsiManager; -import com.intellij.testFramework.PsiTestUtil; import org.jetbrains.annotations.NotNull; public class UnusedDeclarationInSubScopeTest extends AbstractUnusedDeclarationTest { - @Override - protected void setupRootModel(@NotNull String testDir, @NotNull VirtualFile[] sourceDir, String jdkName) { - super.setupRootModel(testDir, sourceDir, jdkName); - VirtualFile projectDir = LocalFileSystem.getInstance().findFileByPath(testDir); - assertNotNull(projectDir); - VirtualFile test = projectDir.findChild("test_src"); - if (test != null) PsiTestUtil.addSourceRoot(myModule, test, true); - } @NotNull @Override protected AnalysisScope createAnalysisScope(VirtualFile sourceDir) { - VirtualFile[] roots = ModuleRootManager.getInstance(myModule).getSourceRoots(false); - assertEquals(1, roots.length); - PsiManager psiManager = PsiManager.getInstance(myProject); - return new AnalysisScope(psiManager.findDirectory(roots[0])); + AnalysisScope scope = super.createAnalysisScope(sourceDir); + scope.setIncludeTestSource(false); + return scope; } public void testParameterUsedInOutOfScopeOverrider() { diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedDeclarationTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedDeclarationTest.java index c335763ec59a..43be89903fda 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedDeclarationTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedDeclarationTest.java @@ -16,8 +16,8 @@ package com.intellij.java.codeInspection; import com.intellij.codeInspection.ex.EntryPointsManagerBase; -import com.intellij.openapi.roots.LanguageLevelProjectExtension; import com.intellij.pom.java.LanguageLevel; +import com.intellij.testFramework.IdeaTestUtil; /** * @author max @@ -74,23 +74,19 @@ public class UnusedDeclarationTest extends AbstractUnusedDeclarationTest { } public void testSuppress() { - LanguageLevelProjectExtension.getInstance(getJavaFacade().getProject()).setLanguageLevel(LanguageLevel.JDK_1_5); - doTest(); + doTest5(); } public void testSuppress1() { - LanguageLevelProjectExtension.getInstance(getJavaFacade().getProject()).setLanguageLevel(LanguageLevel.JDK_1_5); - doTest(); + doTest5(); } public void testSuppress2() { - LanguageLevelProjectExtension.getInstance(getJavaFacade().getProject()).setLanguageLevel(LanguageLevel.JDK_1_5); - doTest(); + doTest5(); } public void testChainOfSuppressions() { - LanguageLevelProjectExtension.getInstance(getJavaFacade().getProject()).setLanguageLevel(LanguageLevel.JDK_1_5); - doTest(); + doTest5(); } public void testSuppressByNoinspectionTag() { @@ -142,13 +138,11 @@ public class UnusedDeclarationTest extends AbstractUnusedDeclarationTest { } public void testAnnotationInterface() { - LanguageLevelProjectExtension.getInstance(getJavaFacade().getProject()).setLanguageLevel(LanguageLevel.JDK_1_5); - doTest(); + doTest5(); } public void testUnusedEnum() { - LanguageLevelProjectExtension.getInstance(getJavaFacade().getProject()).setLanguageLevel(LanguageLevel.JDK_1_5); - doTest(); + doTest5(); } public void testJunitEntryPoint() { @@ -204,7 +198,6 @@ public class UnusedDeclarationTest extends AbstractUnusedDeclarationTest { } public void testFunctionalExpressions() { - LanguageLevelProjectExtension.getInstance(getJavaFacade().getProject()).setLanguageLevel(LanguageLevel.JDK_1_8); doTest(); } @@ -259,4 +252,8 @@ public class UnusedDeclarationTest extends AbstractUnusedDeclarationTest { public void testMethodCallQualifiedWithSuper() { doTest(); } + + private void doTest5() { + IdeaTestUtil.withLevel(myModule, LanguageLevel.JDK_1_5,() -> doTest()); + } } diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedLibraryInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedLibraryInspectionTest.java index 1c9f6d02f1d5..1f699ae16dc9 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedLibraryInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedLibraryInspectionTest.java @@ -19,33 +19,42 @@ package com.intellij.java.codeInspection; import com.intellij.JavaTestUtil; import com.intellij.analysis.AnalysisScope; import com.intellij.codeInspection.unusedLibraries.UnusedLibrariesInspection; -import com.intellij.openapi.roots.DependencyScope; -import com.intellij.openapi.roots.LibraryOrderEntry; -import com.intellij.openapi.roots.ModuleRootManager; -import com.intellij.openapi.roots.OrderEntry; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.roots.*; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.project.IntelliJProjectConfiguration; import com.intellij.testFramework.InspectionTestCase; +import com.intellij.testFramework.LightProjectDescriptor; import com.intellij.testFramework.PsiTestUtil; +import com.intellij.testFramework.fixtures.DefaultLightProjectDescriptor; import org.jetbrains.annotations.NotNull; public class UnusedLibraryInspectionTest extends InspectionTestCase { + + private final DefaultLightProjectDescriptor myProjectDescriptor = new DefaultLightProjectDescriptor() { + @Override + public void configureModule(@NotNull Module module, @NotNull ModifiableRootModel model, @NotNull ContentEntry contentEntry) { + super.configureModule(module, model, contentEntry); + PsiTestUtil.addProjectLibrary(model, "JUnit", IntelliJProjectConfiguration.getProjectLibraryClassesRootPaths("JUnit4")); + if (getTestName(true).endsWith("Runtime")) { + for (OrderEntry entry : model.getOrderEntries()) { + if (entry instanceof LibraryOrderEntry && "JUnit".equals(((LibraryOrderEntry)entry).getLibraryName())) { + ((LibraryOrderEntry)entry).setScope(DependencyScope.RUNTIME); + } + } + } + } + }; + @Override protected String getTestDataPath() { return JavaTestUtil.getJavaTestDataPath() + "/inspection/unusedLibrary"; } + @NotNull @Override - protected void setupRootModel(@NotNull String testDir, @NotNull VirtualFile[] sourceDir, String sdkName) { - super.setupRootModel(testDir, sourceDir, sdkName); - PsiTestUtil.addProjectLibrary(getModule(), "JUnit", IntelliJProjectConfiguration.getProjectLibraryClassesRootPaths("JUnit4")); - if (getTestName(true).endsWith("Runtime")) { - for (OrderEntry entry : ModuleRootManager.getInstance(getModule()).getOrderEntries()) { - if (entry instanceof LibraryOrderEntry && "JUnit".equals(((LibraryOrderEntry)entry).getLibraryName())) { - ((LibraryOrderEntry)entry).setScope(DependencyScope.RUNTIME); - } - } - } + protected LightProjectDescriptor getProjectDescriptor() { + return myProjectDescriptor; } private void doTest() { diff --git a/java/testFramework/src/com/intellij/testFramework/InspectionTestCase.java b/java/testFramework/src/com/intellij/testFramework/InspectionTestCase.java index 9158154c37fa..95fd7ca1c2e5 100644 --- a/java/testFramework/src/com/intellij/testFramework/InspectionTestCase.java +++ b/java/testFramework/src/com/intellij/testFramework/InspectionTestCase.java @@ -27,20 +27,21 @@ import com.intellij.codeInspection.deadCode.UnusedDeclarationPresentation; import com.intellij.codeInspection.ex.*; import com.intellij.codeInspection.reference.EntryPoint; import com.intellij.codeInspection.reference.RefElement; -import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.ex.PathManagerEx; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.extensions.ExtensionPoint; import com.intellij.openapi.extensions.Extensions; -import com.intellij.openapi.projectRoots.Sdk; -import com.intellij.openapi.roots.LanguageLevelProjectExtension; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.roots.ContentEntry; +import com.intellij.openapi.roots.ModifiableRootModel; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VfsUtilCore; import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.pom.java.LanguageLevel; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiManager; import com.intellij.psi.util.PsiUtilCore; +import com.intellij.testFramework.fixtures.DefaultLightProjectDescriptor; +import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase; import com.intellij.testFramework.fixtures.impl.GlobalInspectionContextForTests; import com.intellij.util.containers.ContainerUtil; import org.jdom.Element; @@ -56,9 +57,17 @@ import java.util.List; * @author max */ @SuppressWarnings("HardCodedStringLiteral") -public abstract class InspectionTestCase extends PsiTestCase { +public abstract class InspectionTestCase extends LightCodeInsightFixtureTestCase { private static final boolean MIGRATE_TEST = false; private static final Logger LOG = Logger.getInstance("#com.intellij.testFramework.InspectionTestCase"); + private static final DefaultLightProjectDescriptor ourDescriptor = new DefaultLightProjectDescriptor() { + @Override + public void configureModule(@NotNull Module module, @NotNull ModifiableRootModel model, @NotNull ContentEntry contentEntry) { + super.configureModule(module, model, contentEntry); + contentEntry.addSourceFolder(contentEntry.getUrl() + "/ext_src", false); + contentEntry.addSourceFolder(contentEntry.getUrl() + "/test_src", true); + } + }; private EntryPoint myUnusedCodeExtension; private VirtualFile ext_src; private LightTestMigration myMigration; @@ -73,7 +82,7 @@ public abstract class InspectionTestCase extends PsiTestCase { } public InspectionManagerEx getManager() { - return (InspectionManagerEx)InspectionManager.getInstance(myProject); + return (InspectionManagerEx)InspectionManager.getInstance(getProject()); } public void doTest(@NonNls @NotNull String folderName, @NotNull LocalInspectionTool tool) { @@ -89,65 +98,58 @@ public abstract class InspectionTestCase extends PsiTestCase { } public void doTest(@NonNls @NotNull String folderName, @NotNull GlobalInspectionTool tool, boolean checkRange, boolean runDeadCodeFirst) { - doTest(folderName, new GlobalInspectionToolWrapper(tool), "java 1.4", checkRange, runDeadCodeFirst); + doTest(folderName, new GlobalInspectionToolWrapper(tool), checkRange, runDeadCodeFirst); } public void doTest(@NonNls @NotNull String folderName, @NotNull InspectionToolWrapper tool) { - doTest(folderName, tool, "java 1.4"); + doTest(folderName, tool, false); } - public void doTest(@NonNls @NotNull String folderName, @NotNull InspectionToolWrapper tool, final boolean checkRange) { - doTest(folderName, tool, "java 1.4", checkRange); - } - - public void doTest(@NonNls @NotNull String folderName, @NotNull LocalInspectionTool tool, @NonNls final String jdkName) { - doTest(folderName, new LocalInspectionToolWrapper(tool), jdkName); - } - - public void doTest(@NonNls @NotNull String folderName, @NotNull InspectionToolWrapper tool, @NonNls final String jdkName) { - doTest(folderName, tool, jdkName, false); - } - - public void doTest(@NonNls @NotNull String folderName, @NotNull InspectionToolWrapper tool, @NonNls final String jdkName, boolean checkRange) { - doTest(folderName, tool, jdkName, checkRange, false); + public void doTest(@NonNls @NotNull String folderName, + @NotNull InspectionToolWrapper tool, + boolean checkRange) { + doTest(folderName, tool, checkRange, false); } public void doTest(@NonNls @NotNull String folderName, @NotNull InspectionToolWrapper toolWrapper, - @NonNls final String jdkName, boolean checkRange, boolean runDeadCodeFirst, @NotNull InspectionToolWrapper... additional) { final String testDir = getTestDataPath() + "/" + folderName; final List> tools = getTools(runDeadCodeFirst, toolWrapper, additional); - GlobalInspectionContextImpl context = runTool(testDir, jdkName, toolWrapper, tools); + GlobalInspectionContextImpl context = runTool(folderName, toolWrapper, tools); InspectionTestUtil.compareToolResults(context, checkRange, testDir, ContainerUtil.append(Collections.singletonList(toolWrapper), additional)); if (MIGRATE_TEST) { - myMigration = new LightTestMigration(getTestName(false), getClass(), testDir, tools, getTestProjectSdk()); + myMigration = new LightTestMigration(getTestName(false), getClass(), testDir, tools); } } - protected void runTool(@NonNls @NotNull String testDir, @NonNls final String jdkName, @NotNull InspectionToolWrapper tool) { - runTool(testDir, jdkName, tool, Collections.singletonList(tool)); - } - - protected GlobalInspectionContextImpl runTool(@NotNull final String testDir, - final String jdkName, + protected GlobalInspectionContextImpl runTool(@NotNull final String testName, @NotNull InspectionToolWrapper toolWrapper, List> tools) { - final VirtualFile[] sourceDir = new VirtualFile[1]; - ApplicationManager.getApplication().runWriteAction(() -> { - try { - setupRootModel(testDir, sourceDir, jdkName); - } - catch (Exception e) { - LOG.error(e); - } - }); - VirtualFile projectDir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(new File(testDir)); - AnalysisScope scope = createAnalysisScope(sourceDir[0].equals(projectDir) ? projectDir : sourceDir[0].getParent()); + VirtualFile projectDir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(new File(getTestDataPath(), testName)); + assertNotNull(projectDir); + + VirtualFile srcDir; + if (projectDir.findChild("src") != null) { + srcDir = myFixture.copyDirectoryToProject(testName + "/src", ""); + } + else { + srcDir = myFixture.copyDirectoryToProject(testName,""); + } + + if (projectDir.findChild("ext_src") != null) { + ext_src = myFixture.copyDirectoryToProject(testName + "/ext_src", "ext_src"); + } + + if (projectDir.findChild("test_src") != null) { + myFixture.copyDirectoryToProject(testName + "/test_src", "test_src"); + } + + AnalysisScope scope = createAnalysisScope(srcDir); GlobalInspectionContextForTests globalContext = InspectionsKt.createGlobalContextForTool(scope, getProject(), tools); @@ -170,31 +172,14 @@ public abstract class InspectionTestCase extends PsiTestCase { @NotNull protected AnalysisScope createAnalysisScope(VirtualFile sourceDir) { - PsiManager psiManager = PsiManager.getInstance(myProject); + PsiManager psiManager = PsiManager.getInstance(getProject()); return new AnalysisScope(psiManager.findDirectory(sourceDir)); } - protected void setupRootModel(@NotNull String testDir, @NotNull VirtualFile[] sourceDir, final String sdkName) { - VirtualFile projectDir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(new File(testDir)); - assertNotNull("could not find project dir " + testDir, projectDir); - sourceDir[0] = projectDir.findChild("src"); - if (sourceDir[0] == null) { - sourceDir[0] = projectDir; - } - // IMPORTANT! The jdk must be obtained in a way it is obtained in the normal program! - //ProjectJdkEx jdk = ProjectJdkTable.getInstance().getInternalJdk(); - PsiTestUtil.removeAllRoots(myModule, getTestProjectSdk()); - PsiTestUtil.addContentRoot(myModule, projectDir); - PsiTestUtil.addSourceRoot(myModule, sourceDir[0]); - ext_src = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(new File(testDir + "/ext_src")); - if (ext_src != null) { - PsiTestUtil.addSourceRoot(myModule, ext_src); - } - - VirtualFile test_src = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(new File(testDir + "/test_src")); - if (test_src != null) { - PsiTestUtil.addSourceRoot(myModule, test_src, true); - } + @NotNull + @Override + protected LightProjectDescriptor getProjectDescriptor() { + return ourDescriptor; } @Override @@ -261,16 +246,6 @@ public abstract class InspectionTestCase extends PsiTestCase { } } - @Override - protected void setUpJdk() { - } - - protected Sdk getTestProjectSdk() { - Sdk sdk = IdeaTestUtil.getMockJdk18(); - LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_8); - return sdk; - } - @Override @NonNls protected String getTestDataPath() { diff --git a/java/testFramework/src/com/intellij/testFramework/LightTestMigration.java b/java/testFramework/src/com/intellij/testFramework/LightTestMigration.java index 6a467a0157e5..78787135e295 100644 --- a/java/testFramework/src/com/intellij/testFramework/LightTestMigration.java +++ b/java/testFramework/src/com/intellij/testFramework/LightTestMigration.java @@ -9,8 +9,6 @@ import com.intellij.codeInspection.ex.InspectionProfileImpl; import com.intellij.codeInspection.ex.InspectionToolWrapper; import com.intellij.openapi.application.ex.PathManagerEx; import com.intellij.openapi.project.Project; -import com.intellij.openapi.projectRoots.Sdk; -import com.intellij.openapi.roots.AnnotationOrderRootType; import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; @@ -18,10 +16,8 @@ import com.intellij.profile.codeInspection.ProjectInspectionProfileManager; import com.intellij.testFramework.fixtures.*; import com.intellij.testFramework.fixtures.impl.LightTempDirTestFixtureImpl; import com.siyeh.ig.LightInspectionTestCase; -import one.util.streamex.EntryStream; import one.util.streamex.StreamEx; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.junit.ComparisonFailure; import java.io.IOException; @@ -31,7 +27,10 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.text.MessageFormat; import java.time.Year; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; import java.util.stream.Collectors; /** @@ -45,7 +44,6 @@ class LightTestMigration { private final Class myTestClass; private final Path myDir; private final List> myTools; - private final Sdk mySdk; private final List myTestNames = new ArrayList<>(); private static LightTestMigration ourPrevious; @@ -54,25 +52,17 @@ class LightTestMigration { Runtime.getRuntime().addShutdownHook(new Thread(LightTestMigration::flush)); } - private static final Map JDK_MAP = EntryStream.of( - "java 1.7", "JAVA_1_7", - "java 1.8", "JAVA_8", - "java 9", "JAVA_9", - "java 10", "JAVA_10" - ).toMap(); private Path myBaseDir; private Path myBasePath; LightTestMigration(String name, Class testClass, String dir, - List> tools, - Sdk sdk) { + List> tools) { myName = name; myTestClass = testClass; myDir = Paths.get(dir); myTools = tools; - mySdk = sdk; } void tryMigrate() throws Exception { @@ -96,13 +86,7 @@ class LightTestMigration { myBaseDir = myName.isEmpty() ? myDir : myDir.getParent(); Path targetFile = myBaseDir.resolve(testName + ".java"); IdeaTestFixtureFactory factory = IdeaTestFixtureFactory.getFixtureFactory(); - LightProjectDescriptor descriptor = new LightProjectDescriptor() { - @Nullable - @Override - public Sdk getSdk() { - return mySdk; - } - }; + LightProjectDescriptor descriptor = new LightProjectDescriptor(); TestFixtureBuilder fixtureBuilder = factory.createLightFixtureBuilder(descriptor); IdeaProjectTestFixture fixture = fixtureBuilder.getFixture(); JavaCodeInsightTestFixture javaFixture = @@ -161,11 +145,9 @@ class LightTestMigration { String inspections = myTools.stream().map(InspectionToolWrapper::getTool).map(InspectionProfileEntry::getClass).map(Class::getSimpleName) .map(name -> "new " + name + "()").collect(Collectors.joining(", ")); - System.out.println("JDK version: " + mySdk.getVersionString()); - String guessedJdk = guessJdk(); String year = Year.now().toString(); String classTemplate = MessageFormat.format( - "// Copyright 2000-{7} JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.\n" + + "// Copyright 2000-{6} JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.\n" + "package {0};\n\n" + "{1}" + "\n" + @@ -175,12 +157,6 @@ class LightTestMigration { " return {3};\n" + " '}'\n" + "\n" + - " @NotNull\n" + - " @Override\n" + - " protected LightProjectDescriptor getProjectDescriptor() '{'\n" + - " return {6};\n" + - " '}'\n" + - "\n" + " private void doTest() '{'\n" + " myFixture.enableInspections({4});\n" + " myFixture.testHighlighting(getTestName(false) + \".java\");\n" + @@ -188,7 +164,7 @@ class LightTestMigration { "\n" + "{5}" + "'}'\n", - myTestClass.getPackage().getName(), imports, myTestClass.getSimpleName(), pathSpec, inspections, testMethods, guessedJdk, year); + myTestClass.getPackage().getName(), imports, myTestClass.getSimpleName(), pathSpec, inspections, testMethods, year); System.out.println("Class template: (" + myTestClass.getSimpleName() + ".java)"); System.out.println("=============================="); System.out.println(classTemplate); @@ -203,14 +179,6 @@ class LightTestMigration { .map(name -> "import " + name + ";\n").collect(Collectors.joining()); } - private String guessJdk() { - String guessedJdk = JDK_MAP.getOrDefault(mySdk.getVersionString(), "JAVA_LATEST"); - if (mySdk.getRootProvider().getFiles(AnnotationOrderRootType.getInstance()).length != 0) { - guessedJdk += "_ANNOTATED"; - } - return guessedJdk; - } - private static String getExpectedText(Path javaFile, String fileText, JavaCodeInsightTestFixture javaFixture) { javaFixture.configureByText(javaFile.getFileName().toString(), fileText); diff --git a/java/testFramework/src/com/intellij/testFramework/fixtures/LightCodeInsightFixtureTestCase.java b/java/testFramework/src/com/intellij/testFramework/fixtures/LightCodeInsightFixtureTestCase.java index 1b1d56306aa8..3bd11f0c9752 100644 --- a/java/testFramework/src/com/intellij/testFramework/fixtures/LightCodeInsightFixtureTestCase.java +++ b/java/testFramework/src/com/intellij/testFramework/fixtures/LightCodeInsightFixtureTestCase.java @@ -49,6 +49,7 @@ public abstract class LightCodeInsightFixtureTestCase extends UsefulTestCase { } @NotNull public static final LightProjectDescriptor JAVA_1_4 = new ProjectDescriptor(LanguageLevel.JDK_1_4); + @NotNull public static final LightProjectDescriptor JAVA_1_4_ANNOTATED = new ProjectDescriptor(LanguageLevel.JDK_1_4, true); @NotNull public static final LightProjectDescriptor JAVA_1_5 = new ProjectDescriptor(LanguageLevel.JDK_1_5); @NotNull public static final LightProjectDescriptor JAVA_1_6 = new ProjectDescriptor(LanguageLevel.JDK_1_6); @NotNull public static final LightProjectDescriptor JAVA_1_7 = new ProjectDescriptor(LanguageLevel.JDK_1_7); diff --git a/platform/structuralsearch/testSource/com/intellij/structuralsearch/SSBasedInspectionTest.java b/platform/structuralsearch/testSource/com/intellij/structuralsearch/SSBasedInspectionTest.java index 1a115fa6798a..68f98654fe55 100644 --- a/platform/structuralsearch/testSource/com/intellij/structuralsearch/SSBasedInspectionTest.java +++ b/platform/structuralsearch/testSource/com/intellij/structuralsearch/SSBasedInspectionTest.java @@ -32,7 +32,7 @@ public class SSBasedInspectionTest extends InspectionTestCase { options.setSearchPattern("f();"); configurations.add(configuration2); - inspection.setConfigurations(configurations, myProject); + inspection.setConfigurations(configurations, getProject()); myWrapper = new LocalInspectionToolWrapper(inspection); } @@ -41,7 +41,7 @@ public class SSBasedInspectionTest extends InspectionTestCase { } private void doTest() { - doTest("ssBased/" + getTestName(true), myWrapper,"java 1.5"); + doTest("ssBased/" + getTestName(true), myWrapper); } @Override diff --git a/plugins/InspectionGadgets/testsrc/com/intellij/codeInspection/BooleanMethodIsAlwaysInvertedInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/intellij/codeInspection/BooleanMethodIsAlwaysInvertedInspectionTest.java index 500bde67e223..81e174f55d7b 100644 --- a/plugins/InspectionGadgets/testsrc/com/intellij/codeInspection/BooleanMethodIsAlwaysInvertedInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/intellij/codeInspection/BooleanMethodIsAlwaysInvertedInspectionTest.java @@ -4,8 +4,8 @@ package com.intellij.codeInspection; import com.intellij.codeInspection.booleanIsAlwaysInverted.BooleanMethodIsAlwaysInvertedInspection; -import com.intellij.openapi.roots.LanguageLevelProjectExtension; import com.intellij.pom.java.LanguageLevel; +import com.intellij.testFramework.IdeaTestUtil; import com.siyeh.ig.IGInspectionTestCase; public class BooleanMethodIsAlwaysInvertedInspectionTest extends IGInspectionTestCase { @@ -63,15 +63,7 @@ public class BooleanMethodIsAlwaysInvertedInspectionTest extends IGInspectionTes } public void testMethodReferenceIgnored() { - final LanguageLevelProjectExtension projectExtension = LanguageLevelProjectExtension.getInstance(getJavaFacade().getProject()); - final LanguageLevel oldLevel = projectExtension.getLanguageLevel(); - try { - projectExtension.setLanguageLevel(LanguageLevel.JDK_1_8); - doTest(); - } - finally { - projectExtension.setLanguageLevel(oldLevel); - } + IdeaTestUtil.withLevel(myModule, LanguageLevel.JDK_1_8, () -> doTest()); } private void doTest() { diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/IGInspectionTestCase.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/IGInspectionTestCase.java index 62aed92ba01b..64fd5e297e0e 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/IGInspectionTestCase.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/IGInspectionTestCase.java @@ -33,7 +33,7 @@ public abstract class IGInspectionTestCase extends InspectionTestCase { @Override public void doTest(@NotNull @NonNls final String folderName, @NotNull final LocalInspectionTool tool) { - super.doTest(folderName, new LocalInspectionToolWrapper(tool), "java 1.5"); + super.doTest(folderName, new LocalInspectionToolWrapper(tool)); } } diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryFullyQualifiedNameInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryFullyQualifiedNameInspectionTest.java index 5d0e54a7ea19..3323ffc69501 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryFullyQualifiedNameInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryFullyQualifiedNameInspectionTest.java @@ -1,19 +1,11 @@ package com.siyeh.ig.style; -import com.intellij.openapi.projectRoots.Sdk; import com.intellij.psi.codeStyle.JavaCodeStyleSettings; -import com.intellij.testFramework.IdeaTestUtil; import com.siyeh.ig.IGInspectionTestCase; public class UnnecessaryFullyQualifiedNameInspectionTest extends IGInspectionTestCase { private static final String BASE_DIR = "com/siyeh/igtest/style/"; - @Override - protected Sdk getTestProjectSdk() { - // uses awt - return IdeaTestUtil.getMockJdk17(); - } - public void testFqnInJavadoc_Unnecessary_WhenFullyQualifyIfNotImported() { doTestWithFqnInJavadocSetting(BASE_DIR + "unnecessary_fqn_javadoc_fully_qualify_if_not_imported", JavaCodeStyleSettings.FULLY_QUALIFY_NAMES_IF_NOT_IMPORTED); } diff --git a/plugins/java-i18n/testSrc/com/intellij/codeInspection/i18n/InvalidPropertyKeyInspectionTest.java b/plugins/java-i18n/testSrc/com/intellij/codeInspection/i18n/InvalidPropertyKeyInspectionTest.java index 8dd1a415f0a9..9e0ceb860c9e 100644 --- a/plugins/java-i18n/testSrc/com/intellij/codeInspection/i18n/InvalidPropertyKeyInspectionTest.java +++ b/plugins/java-i18n/testSrc/com/intellij/codeInspection/i18n/InvalidPropertyKeyInspectionTest.java @@ -22,7 +22,7 @@ import com.intellij.testFramework.InspectionTestCase; public class InvalidPropertyKeyInspectionTest extends InspectionTestCase { private void doTest() { LocalInspectionToolWrapper tool = new LocalInspectionToolWrapper(new InvalidPropertyKeyInspection()); - doTest("invalidPropertyKey/" + getTestName(true), tool, "java 1.5"); + doTest("invalidPropertyKey/" + getTestName(true), tool); } public void testSimple() { diff --git a/plugins/properties/properties-psi-impl/src/com/intellij/codeInspection/duplicatePropertyInspection/DuplicatePropertyInspection.java b/plugins/properties/properties-psi-impl/src/com/intellij/codeInspection/duplicatePropertyInspection/DuplicatePropertyInspection.java index a97b87784876..c2246c380552 100644 --- a/plugins/properties/properties-psi-impl/src/com/intellij/codeInspection/duplicatePropertyInspection/DuplicatePropertyInspection.java +++ b/plugins/properties/properties-psi-impl/src/com/intellij/codeInspection/duplicatePropertyInspection/DuplicatePropertyInspection.java @@ -35,8 +35,6 @@ import org.jetbrains.annotations.NotNull; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.net.MalformedURLException; -import java.net.URL; import java.util.*; public class DuplicatePropertyInspection extends GlobalSimpleInspectionTool { @@ -67,18 +65,13 @@ public class DuplicatePropertyInspection extends GlobalSimpleInspectionTool { HTMLComposer.appendAfterHeaderIndention(anchor); HTMLComposer.appendAfterHeaderIndention(anchor); anchor.append(""); anchor.append(elementToLink.getText().replaceAll("\\$", "\\\\\\$")); anchor.append(""); @@ -103,14 +96,9 @@ public class DuplicatePropertyInspection extends GlobalSimpleInspectionTool { final int lineNumber = doc.getLineNumber(psiElement.getTextOffset()) + 1; lineAnchor.append(" ").append(InspectionsBundle.message("inspection.export.results.at.line")).append(" "); lineAnchor.append(""); lineAnchor.append(lineNumber); lineAnchor.append(""); diff --git a/plugins/ui-designer/testSrc/com/intellij/uiDesigner/inspection/I18NInspectionTest.java b/plugins/ui-designer/testSrc/com/intellij/uiDesigner/inspection/I18NInspectionTest.java index d54b79665a52..a0ce84385c92 100644 --- a/plugins/ui-designer/testSrc/com/intellij/uiDesigner/inspection/I18NInspectionTest.java +++ b/plugins/ui-designer/testSrc/com/intellij/uiDesigner/inspection/I18NInspectionTest.java @@ -31,7 +31,7 @@ public class I18NInspectionTest extends InspectionTestCase { public void testFormTabbedPaneTitle() { LocalInspectionToolWrapper wrapper = new LocalInspectionToolWrapper(new I18nFormInspection()); InspectionsKt.enableInspectionTool(getProject(), wrapper, getTestRootDisposable()); - doTest("i18n/" + getTestName(true), new LocalInspectionToolWrapper(new I18nInspection()), "java 1.4", false, false, + doTest("i18n/" + getTestName(true), new LocalInspectionToolWrapper(new I18nInspection()), false, false, wrapper); } }