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 8ad8abe6644a..a616ed3111e4 100644 --- a/java/java-tests/testSrc/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspectionTest.kt +++ b/java/java-tests/testSrc/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspectionTest.kt @@ -1,19 +1,15 @@ // 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.openapi.application.WriteAction 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.testFramework.IdeaTestUtil -import com.intellij.testFramework.IndexingTestUtil +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.junit5.RunInEdt import com.intellij.testFramework.junit5.RunMethodInEdt import com.intellij.testFramework.junit5.TestApplication import com.intellij.testFramework.junit5.fixture.* -import com.intellij.testFramework.junit5.javaCodeInsightFixture import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test @@ -29,13 +25,7 @@ class TryFinallyCanBeTryWithResourcesInspectionTest { @JvmStatic @RunMethodInEdt fun beforeAll() { - val jdk = IdeaTestUtil.getMockJdk9() - WriteAction.runAndWait { - ProjectJdkTable.getInstance().addJdk(jdk, disposable) - ProjectRootManager.getInstance(project.get()).setProjectSdk(jdk) - ModuleRootModificationUtil.setModuleSdk(module, jdk) - } - IndexingTestUtil.waitUntilIndexesAreReady(project.get()) + setUpJdk(LanguageLevel.JDK_1_9, project.get(), module, disposable) } private val disposable by disposableFixture() diff --git a/java/testFramework/shared/src/com/intellij/testFramework/IdeaTestUtil.java b/java/testFramework/shared/src/com/intellij/testFramework/IdeaTestUtil.java index a2b6fb4b1847..e66c3f68eabf 100644 --- a/java/testFramework/shared/src/com/intellij/testFramework/IdeaTestUtil.java +++ b/java/testFramework/shared/src/com/intellij/testFramework/IdeaTestUtil.java @@ -107,6 +107,13 @@ public final class IdeaTestUtil { }); } + /** + * Returns a mock JDK for the specified language level. + */ + public static @NotNull Sdk getMockJdk(@NotNull LanguageLevel level) { + return getMockJdk(level.toJavaVersion()); + } + public static @NotNull Sdk getMockJdk(@NotNull JavaVersion version) { int mockJdk = version.feature >= 25 ? 25 : version.feature >= 21 ? 21 : diff --git a/java/testFramework/src/com/intellij/testFramework/JavaJUnit5Util.kt b/java/testFramework/src/com/intellij/testFramework/JavaJUnit5Util.kt new file mode 100644 index 000000000000..e235f45b52c6 --- /dev/null +++ b/java/testFramework/src/com/intellij/testFramework/JavaJUnit5Util.kt @@ -0,0 +1,51 @@ +// 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) { + IdeaTestUtil.getMockJdk(level) + val jdk = IdeaTestUtil.getMockJdk9() + 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/junit5/fixtures.kt b/java/testFramework/src/com/intellij/testFramework/junit5/fixtures.kt deleted file mode 100644 index 19df9c608d39..000000000000 --- a/java/testFramework/src/com/intellij/testFramework/junit5/fixtures.kt +++ /dev/null @@ -1,16 +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.junit5 - -import com.intellij.openapi.project.Project -import com.intellij.platform.testFramework.junit5.codeInsight.fixture.codeInsightFixture -import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture -import com.intellij.testFramework.fixtures.impl.JavaCodeInsightTestFixtureImpl -import com.intellij.testFramework.junit5.fixture.TestFixture -import org.jetbrains.annotations.TestOnly -import java.nio.file.Path - -@TestOnly -fun javaCodeInsightFixture( - projectFixture: TestFixture, - tempDirFixture: TestFixture, -): TestFixture = codeInsightFixture(projectFixture, tempDirFixture, ::JavaCodeInsightTestFixtureImpl) \ No newline at end of file