From 71ae5608f6aee5879bce06215f1a50cf9b977266 Mon Sep 17 00:00:00 2001 From: Georgii Ustinov Date: Fri, 14 Nov 2025 08:38:35 +0100 Subject: [PATCH] [Java. Tests] IDEA-381948 IDEA-381948 Remove wrapper into the object class for JavaCodeInsightFixture GitOrigin-RevId: 7e82b01fd5997479da037c54dbaeec4902aca1c1 --- .../ForCanBeForeachInspectionTest.java | 62 ++++++++++++------- ...allyCanBeTryWithResourcesInspectionTest.kt | 4 +- .../intellij/testFramework/JavaJUnit5Util.kt | 50 --------------- .../testFramework/javaCodeInsightFixture.kt | 46 ++++++++++++++ 4 files changed, 89 insertions(+), 73 deletions(-) delete mode 100644 java/testFramework/src/com/intellij/testFramework/JavaJUnit5Util.kt create mode 100644 java/testFramework/src/com/intellij/testFramework/javaCodeInsightFixture.kt diff --git a/java/java-tests/testSrc/com/siyeh/ig/migration/ForCanBeForeachInspectionTest.java b/java/java-tests/testSrc/com/siyeh/ig/migration/ForCanBeForeachInspectionTest.java index 2a1463566ce5..7b5da6ea7ca1 100644 --- a/java/java-tests/testSrc/com/siyeh/ig/migration/ForCanBeForeachInspectionTest.java +++ b/java/java-tests/testSrc/com/siyeh/ig/migration/ForCanBeForeachInspectionTest.java @@ -1,30 +1,50 @@ -// Copyright 2000-2019 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. +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.siyeh.ig.migration; -import com.intellij.codeInspection.InspectionProfileEntry; -import com.intellij.testFramework.LightProjectDescriptor; -import com.siyeh.ig.LightJavaInspectionTestCase; -import org.jetbrains.annotations.NotNull; +import com.intellij.ide.impl.OpenProjectTask; +import com.intellij.openapi.Disposable; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.module.Module; +import com.intellij.pom.java.LanguageLevel; +import com.intellij.testFramework.TestDataPath; +import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture; +import com.intellij.testFramework.junit5.RunInEdt; +import com.intellij.testFramework.junit5.TestApplication; +import com.intellij.testFramework.junit5.fixture.TestFixture; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -public class ForCanBeForeachInspectionTest extends LightJavaInspectionTestCase { +import java.nio.file.Path; - public void testForCanBeForEach() { - doTest(); +import static com.intellij.testFramework.JavaCodeInsightFixtureKt.javaCodeInsightFixture; +import static com.intellij.testFramework.JavaCodeInsightFixtureKt.setUpJdk; +import static com.intellij.testFramework.junit5.fixture.FixturesKt.*; + +@TestApplication +@RunInEdt(allMethods = false) +@TestDataPath("$PROJECT_ROOT/community/java/java-tests/testData/ig/com/siyeh/igtest/migration/foreach") +public class ForCanBeForeachInspectionTest { + private static final TestFixture disposable = disposableFixture(); + private static final TestFixture tempDir = tempPathFixture(); + private static final TestFixture project = projectFixture(tempDir, OpenProjectTask.build(), true); + private static final TestFixture module = moduleFixture(project, tempDir, true); + + private final TestFixture testName = testNameFixture(false); + private final TestFixture fixture = javaCodeInsightFixture(project, tempDir); + + @BeforeAll + static void beforeAll() { + setUpJdk(LanguageLevel.JDK_1_8, project.get(), module.get(), disposable.get()); } - @Override - protected InspectionProfileEntry getInspection() { - return new ForCanBeForeachInspection(); + @BeforeEach + void setUp() { + fixture.get().enableInspections(new ForCanBeForeachInspection()); } - @Override - protected String getBasePath() { - return "/java/java-tests/testData/ig/com/siyeh/igtest/migration/foreach"; + @Test + void forCanBeForEach() { + fixture.get().testHighlighting(testName.get() + ".java"); } - - @NotNull - @Override - protected LightProjectDescriptor getProjectDescriptor() { - return JAVA_8; - } -} \ No newline at end of file +} diff --git a/java/java-tests/testSrc/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspectionTest.kt b/java/java-tests/testSrc/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspectionTest.kt index ffb0e80a883c..34f56b63c62f 100644 --- a/java/java-tests/testSrc/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspectionTest.kt +++ b/java/java-tests/testSrc/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspectionTest.kt @@ -2,13 +2,13 @@ package com.siyeh.ig.migration import com.intellij.pom.java.LanguageLevel -import com.intellij.testFramework.JavaJUnit5Util.javaCodeInsightFixture -import com.intellij.testFramework.JavaJUnit5Util.setUpJdk import com.intellij.testFramework.TestDataPath +import com.intellij.testFramework.javaCodeInsightFixture import com.intellij.testFramework.junit5.RunInEdt import com.intellij.testFramework.junit5.RunMethodInEdt import com.intellij.testFramework.junit5.TestApplication import com.intellij.testFramework.junit5.fixture.* +import com.intellij.testFramework.setUpJdk import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test diff --git a/java/testFramework/src/com/intellij/testFramework/JavaJUnit5Util.kt b/java/testFramework/src/com/intellij/testFramework/JavaJUnit5Util.kt deleted file mode 100644 index 8253ba330e0d..000000000000 --- a/java/testFramework/src/com/intellij/testFramework/JavaJUnit5Util.kt +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -package com.intellij.testFramework - -import com.intellij.openapi.Disposable -import com.intellij.openapi.application.WriteAction -import com.intellij.openapi.module.Module -import com.intellij.openapi.project.Project -import com.intellij.openapi.projectRoots.ProjectJdkTable -import com.intellij.openapi.roots.ModuleRootModificationUtil -import com.intellij.openapi.roots.ProjectRootManager -import com.intellij.platform.testFramework.junit5.codeInsight.fixture.codeInsightFixture -import com.intellij.pom.java.LanguageLevel -import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture -import com.intellij.testFramework.fixtures.impl.JavaCodeInsightTestFixtureImpl -import com.intellij.testFramework.junit5.fixture.TestFixture -import com.intellij.util.concurrency.annotations.RequiresEdt -import org.jetbrains.annotations.TestOnly -import java.nio.file.Path - -object JavaJUnit5Util { - /** - * Creates [JavaCodeInsightTestFixture] fixture that can be used when running JUnit5 tests. - */ - @JvmStatic - @TestOnly - fun javaCodeInsightFixture( - projectFixture: TestFixture, - tempDirFixture: TestFixture, - ): TestFixture = codeInsightFixture(projectFixture, tempDirFixture, ::JavaCodeInsightTestFixtureImpl) - - - /** - * Configures jdk for the project and the module inside JUnit5 test. - * This method should be called inside method annotated with [org.junit.jupiter.api.BeforeAll]. - * - * @param level version of the JDK to be set - */ - @JvmStatic - @TestOnly - @RequiresEdt - fun setUpJdk(level: LanguageLevel, project: Project, module: Module, disposable: Disposable) { - val jdk = IdeaTestUtil.getMockJdk(level) - WriteAction.runAndWait { - ProjectJdkTable.getInstance().addJdk(jdk, disposable) - ProjectRootManager.getInstance(project).setProjectSdk(jdk) - ModuleRootModificationUtil.setModuleSdk(module, jdk) - } - IndexingTestUtil.waitUntilIndexesAreReady(project) - } -} \ No newline at end of file diff --git a/java/testFramework/src/com/intellij/testFramework/javaCodeInsightFixture.kt b/java/testFramework/src/com/intellij/testFramework/javaCodeInsightFixture.kt new file mode 100644 index 000000000000..047270a04c14 --- /dev/null +++ b/java/testFramework/src/com/intellij/testFramework/javaCodeInsightFixture.kt @@ -0,0 +1,46 @@ +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.intellij.testFramework + +import com.intellij.openapi.Disposable +import com.intellij.openapi.application.WriteAction +import com.intellij.openapi.module.Module +import com.intellij.openapi.project.Project +import com.intellij.openapi.projectRoots.ProjectJdkTable +import com.intellij.openapi.roots.ModuleRootModificationUtil +import com.intellij.openapi.roots.ProjectRootManager +import com.intellij.platform.testFramework.junit5.codeInsight.fixture.codeInsightFixture +import com.intellij.pom.java.LanguageLevel +import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture +import com.intellij.testFramework.fixtures.impl.JavaCodeInsightTestFixtureImpl +import com.intellij.testFramework.junit5.fixture.TestFixture +import com.intellij.util.concurrency.annotations.RequiresEdt +import org.jetbrains.annotations.TestOnly +import java.nio.file.Path + +/** + * Creates [JavaCodeInsightTestFixture] fixture that can be used when running JUnit5 tests. + */ +@TestOnly +fun javaCodeInsightFixture( + projectFixture: TestFixture, + tempDirFixture: TestFixture, +): TestFixture = codeInsightFixture(projectFixture, tempDirFixture, ::JavaCodeInsightTestFixtureImpl) + + +/** + * Configures jdk for the project and the module inside JUnit5 test. + * This method should be called inside method annotated with [org.junit.jupiter.api.BeforeAll]. + * + * @param level version of the JDK to be set + */ +@TestOnly +@RequiresEdt +fun setUpJdk(level: LanguageLevel, project: Project, module: Module, disposable: Disposable) { + val jdk = IdeaTestUtil.getMockJdk(level) + WriteAction.runAndWait { + ProjectJdkTable.getInstance().addJdk(jdk, disposable) + ProjectRootManager.getInstance(project).setProjectSdk(jdk) + ModuleRootModificationUtil.setModuleSdk(module, jdk) + } + IndexingTestUtil.waitUntilIndexesAreReady(project) +} \ No newline at end of file