From cf5bccb08724c1feb6209b6013b53bf893afd7c4 Mon Sep 17 00:00:00 2001 From: Georgii Ustinov Date: Fri, 7 Nov 2025 13:14:37 +0100 Subject: [PATCH] [Java. Tests] IDEA-381816 Convert TryFinallyCanBeTryWithResources inspection test to JUnit5 GitOrigin-RevId: 568edce00ce2539b272e9d9acd4ce7b481374e34 --- java/java-tests/BUILD.bazel | 1 + java/java-tests/intellij.java.tests.iml | 1 + ...lyCanBeTryWithResourcesInspectionTest.java | 41 ------------- ...allyCanBeTryWithResourcesInspectionTest.kt | 59 +++++++++++++++++++ java/testFramework/BUILD.bazel | 2 + .../intellij.java.testFramework.iml | 2 + .../intellij/testFramework/junit5/fixtures.kt | 16 +++++ .../src/fixture/codeInsightFixture.kt | 43 +++++++++++--- .../junit5/src/fixture/fixtures.kt | 9 ++- 9 files changed, 122 insertions(+), 52 deletions(-) delete mode 100644 java/java-tests/testSrc/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspectionTest.java create mode 100644 java/java-tests/testSrc/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspectionTest.kt create mode 100644 java/testFramework/src/com/intellij/testFramework/junit5/fixtures.kt diff --git a/java/java-tests/BUILD.bazel b/java/java-tests/BUILD.bazel index 6579ae6568ee..dd356a01f0f6 100644 --- a/java/java-tests/BUILD.bazel +++ b/java/java-tests/BUILD.bazel @@ -158,6 +158,7 @@ jvm_library( "//platform/syntax/syntax-scripts:scripts", "@lib//:byte-buddy", "//plugins/grazie/src:core", + "//platform/testFramework/junit5/codeInsight", ], exports = [ "//plugins/java-i18n", diff --git a/java/java-tests/intellij.java.tests.iml b/java/java-tests/intellij.java.tests.iml index d3b60546f7bf..3545570dd422 100644 --- a/java/java-tests/intellij.java.tests.iml +++ b/java/java-tests/intellij.java.tests.iml @@ -130,5 +130,6 @@ + \ No newline at end of file diff --git a/java/java-tests/testSrc/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspectionTest.java b/java/java-tests/testSrc/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspectionTest.java deleted file mode 100644 index d244a4a22ee6..000000000000 --- a/java/java-tests/testSrc/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspectionTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2000-2014 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the 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 org.jetbrains.annotations.Nullable; - -public class TryFinallyCanBeTryWithResourcesInspectionTest extends LightJavaInspectionTestCase { - - public void testTryFinallyCanBeTryWithResources() { - doTest(); - } - - @Nullable - @Override - protected InspectionProfileEntry getInspection() { - return new TryFinallyCanBeTryWithResourcesInspection(); - } - - @NotNull - @Override - protected LightProjectDescriptor getProjectDescriptor() { - return JAVA_9; - } -} diff --git a/java/java-tests/testSrc/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspectionTest.kt b/java/java-tests/testSrc/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspectionTest.kt new file mode 100644 index 000000000000..8ad8abe6644a --- /dev/null +++ b/java/java-tests/testSrc/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspectionTest.kt @@ -0,0 +1,59 @@ +// 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.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 +import java.nio.file.Path + + +@TestApplication +@RunInEdt(allMethods = false) +@TestDataPath($$"$PROJECT_ROOT/community/java/java-tests/testData/ig/com/siyeh/igtest/migration/try_finally_can_be_try_with_resources") +class TryFinallyCanBeTryWithResourcesInspectionTest { + companion object { + @BeforeAll + @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()) + } + + private val disposable by disposableFixture() + private val tempDir: TestFixture = tempPathFixture() + private val project: TestFixture = projectFixture(tempDir, openAfterCreation = true) + private val module by project.moduleFixture(tempDir, addPathToSourceRoot = true) + } + + private val testName by testNameFixture(false) + private val fixture by javaCodeInsightFixture(project, tempDir) + + @BeforeEach + fun setUp() { + fixture.enableInspections(TryFinallyCanBeTryWithResourcesInspection()) + } + + @Test + fun tryFinallyCanBeTryWithResources() { + fixture.testHighlighting("$testName.java") + } +} \ No newline at end of file diff --git a/java/testFramework/BUILD.bazel b/java/testFramework/BUILD.bazel index 3b742828bb59..9730f9aead99 100644 --- a/java/testFramework/BUILD.bazel +++ b/java/testFramework/BUILD.bazel @@ -59,6 +59,8 @@ jvm_library( "//xml/xml-psi-api:psi", "//java/java-syntax:syntax", "//platform/eel", + "//platform/testFramework/junit5", + "//platform/testFramework/junit5/codeInsight", ], exports = [ "//java/testFramework/shared", diff --git a/java/testFramework/intellij.java.testFramework.iml b/java/testFramework/intellij.java.testFramework.iml index c6da30c98a28..dda0c077e868 100644 --- a/java/testFramework/intellij.java.testFramework.iml +++ b/java/testFramework/intellij.java.testFramework.iml @@ -56,6 +56,8 @@ + + diff --git a/java/testFramework/src/com/intellij/testFramework/junit5/fixtures.kt b/java/testFramework/src/com/intellij/testFramework/junit5/fixtures.kt new file mode 100644 index 000000000000..19df9c608d39 --- /dev/null +++ b/java/testFramework/src/com/intellij/testFramework/junit5/fixtures.kt @@ -0,0 +1,16 @@ +// 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 diff --git a/platform/testFramework/junit5/codeInsight/src/fixture/codeInsightFixture.kt b/platform/testFramework/junit5/codeInsight/src/fixture/codeInsightFixture.kt index 72bcaf7804f2..ae0f6c98aa18 100644 --- a/platform/testFramework/junit5/codeInsight/src/fixture/codeInsightFixture.kt +++ b/platform/testFramework/junit5/codeInsight/src/fixture/codeInsightFixture.kt @@ -11,14 +11,19 @@ import com.intellij.testFramework.TestDataProvider import com.intellij.testFramework.fixtures.CodeInsightTestFixture import com.intellij.testFramework.fixtures.IdeaProjectTestFixture import com.intellij.testFramework.fixtures.IdeaTestExecutionPolicy +import com.intellij.testFramework.fixtures.TempDirTestFixture import com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl import com.intellij.testFramework.fixtures.impl.TempDirTestFixtureImpl +import com.intellij.testFramework.junit5.fixture.TestContext import com.intellij.testFramework.junit5.fixture.TestFixture import com.intellij.testFramework.junit5.fixture.testFixture import org.jetbrains.annotations.TestOnly import java.nio.file.Path +import kotlin.io.path.exists import kotlin.io.path.pathString +private const val COMMUNITY_PATH_PREFIX = "community" + /** * Use for JUnit5 tests to set the path to the test data on the method level. * @@ -47,7 +52,14 @@ annotation class TestSubPath(val value: String) fun codeInsightFixture( projectFixture: TestFixture, tempDirFixture: TestFixture, -): TestFixture = testFixture { context -> +): TestFixture = codeInsightFixture(projectFixture, tempDirFixture) { project, tempDir -> CodeInsightTestFixtureImpl(project, tempDir) } + +@TestOnly +fun codeInsightFixture( + projectFixture: TestFixture, + tempDirFixture: TestFixture, + fixtureConstructor: (IdeaProjectTestFixture, TempDirTestFixture) -> T, +): TestFixture = testFixture { context -> val project = projectFixture.init() val tempDir = tempDirFixture.init() @@ -77,18 +89,31 @@ fun codeInsightFixture( override fun deleteOnTearDown(): Boolean = false } - val codeInsightFixture = CodeInsightTestFixtureImpl(projectFixture, tempDirFixture) - val rootPath = context.findAnnotation(TestDataPath::class.java)?.value?.removePrefix($$"$PROJECT_ROOT/") ?: "" - val subPath = context.findAnnotation(TestSubPath::class.java)?.value ?: "" - val homeDir = IdeaTestExecutionPolicy.getHomePathWithPolicy().toNioPathOrNull() - check(homeDir != null) { - "Couldn't create nio.Path from ${IdeaTestExecutionPolicy.getHomePathWithPolicy()}" - } + val codeInsightFixture = fixtureConstructor(projectFixture, tempDirFixture) - codeInsightFixture.testDataPath = homeDir.resolve(rootPath).resolve(subPath).pathString + codeInsightFixture.testDataPath = getTestDataPathString(context) codeInsightFixture.setUp() initialized(codeInsightFixture) { codeInsightFixture.tearDown() } } + +private fun getTestDataPathString(context: TestContext): String { + val rootPath = context.findAnnotation(TestDataPath::class.java)?.value?.removePrefix($$"$PROJECT_ROOT/") ?: "" + val subPath = context.findAnnotation(TestSubPath::class.java)?.value ?: "" + val homeDir = IdeaTestExecutionPolicy.getHomePathWithPolicy().toNioPathOrNull() + check(homeDir != null) { + "Couldn't create nio.Path from ${IdeaTestExecutionPolicy.getHomePathWithPolicy()}" + } + val resolvedPath = homeDir.resolve(rootPath).resolve(subPath) + if (resolvedPath.exists()) { + return resolvedPath.pathString + } + // If the project opened as IJ community, then the test path for community will be duplicated, e.g. it is $HOME/community/community/... + // We need to handle this scenario + check(rootPath.startsWith(COMMUNITY_PATH_PREFIX)) { + "The test data path is not located in community folder, but it doesn't exist in the ultimate." + } + return homeDir.resolve("../").resolve(rootPath).resolve(subPath).pathString +} diff --git a/platform/testFramework/junit5/src/fixture/fixtures.kt b/platform/testFramework/junit5/src/fixture/fixtures.kt index e2ca9ad61689..5ad38d91cc79 100644 --- a/platform/testFramework/junit5/src/fixture/fixtures.kt +++ b/platform/testFramework/junit5/src/fixture/fixtures.kt @@ -48,9 +48,14 @@ import kotlin.io.path.Path import kotlin.io.path.copyToRecursively import kotlin.io.path.exists +// Adapted from com.intellij.clion.testFramework.nolang.junit5.core.FixturesKt.testNameFixture +@JvmOverloads @TestOnly -fun testNameFixture(): TestFixture = testFixture { - val testName = it.testName +fun testNameFixture(lowerCaseFirstLetter: Boolean = true): TestFixture = testFixture { + val testName = it.testName.replaceFirstChar { + chr -> if (lowerCaseFirstLetter) chr.lowercaseChar() else chr.uppercaseChar() + } + initialized(testName) {} }