[Java. Tests] IDEA-381816 Convert TryFinallyCanBeTryWithResources inspection test to JUnit5

GitOrigin-RevId: 568edce00ce2539b272e9d9acd4ce7b481374e34
This commit is contained in:
Georgii Ustinov
2025-11-12 20:12:33 +00:00
committed by intellij-monorepo-bot
parent fa0ae306c6
commit cf5bccb087
9 changed files with 122 additions and 52 deletions
+1
View File
@@ -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",
+1
View File
@@ -130,5 +130,6 @@
<orderEntry type="module" module-name="intellij.platform.syntax.scripts" scope="TEST" />
<orderEntry type="library" scope="TEST" name="byte-buddy" level="project" />
<orderEntry type="module" module-name="intellij.grazie.core" scope="TEST" />
<orderEntry type="module" module-name="intellij.platform.testFramework.junit5.codeInsight" scope="TEST" />
</component>
</module>
@@ -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;
}
}
@@ -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<Exception> {
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<Path> = tempPathFixture()
private val project: TestFixture<Project> = 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")
}
}
+2
View File
@@ -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",
@@ -56,6 +56,8 @@
<orderEntry type="module" module-name="intellij.xml.psi" />
<orderEntry type="module" module-name="intellij.java.syntax" />
<orderEntry type="module" module-name="intellij.platform.eel" />
<orderEntry type="module" module-name="intellij.platform.testFramework.junit5" />
<orderEntry type="module" module-name="intellij.platform.testFramework.junit5.codeInsight" />
</component>
<component name="copyright">
<Base>
@@ -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<Project>,
tempDirFixture: TestFixture<Path>,
): TestFixture<JavaCodeInsightTestFixture> = codeInsightFixture(projectFixture, tempDirFixture, ::JavaCodeInsightTestFixtureImpl)
@@ -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<Project>,
tempDirFixture: TestFixture<Path>,
): TestFixture<CodeInsightTestFixture> = testFixture { context ->
): TestFixture<CodeInsightTestFixture> = codeInsightFixture(projectFixture, tempDirFixture) { project, tempDir -> CodeInsightTestFixtureImpl(project, tempDir) }
@TestOnly
fun <T: CodeInsightTestFixture> codeInsightFixture(
projectFixture: TestFixture<Project>,
tempDirFixture: TestFixture<Path>,
fixtureConstructor: (IdeaProjectTestFixture, TempDirTestFixture) -> T,
): TestFixture<T> = 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
}
@@ -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<String> = testFixture {
val testName = it.testName
fun testNameFixture(lowerCaseFirstLetter: Boolean = true): TestFixture<String> = testFixture {
val testName = it.testName.replaceFirstChar {
chr -> if (lowerCaseFirstLetter) chr.lowercaseChar() else chr.uppercaseChar()
}
initialized(testName) {}
}