[Java. Refactorings] Extract common base for test methods

IDEA-351758

GitOrigin-RevId: 3732c0bac541df2ae4d9a8fc103b0b9b625ed30d
This commit is contained in:
Georgii Ustinov
2024-05-03 13:05:45 +03:00
committed by intellij-monorepo-bot
parent 72ccd6afd4
commit 3a961fdccd
13 changed files with 46 additions and 68 deletions

View File

@@ -2,14 +2,21 @@
package com.intellij.refactoring.rename.naming
import com.intellij.codeInsight.TestFrameworks
import com.intellij.ide.projectView.impl.ProjectRootsUtil
import com.intellij.java.refactoring.JavaRefactoringBundle
import com.intellij.openapi.module.Module
import com.intellij.psi.PsiElement
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.search.PsiShortNamesCache
import com.intellij.util.containers.ContainerUtil
import java.util.regex.Pattern
abstract class AutomaticTestMethodRenamerFactory : AutomaticRenamerFactory {
override fun isApplicable(element: PsiElement): Boolean {
val file = element.containingFile
return !ProjectRootsUtil.isInTestSource(file)
}
override fun getOptionName(): String = JavaRefactoringBundle.message("rename.test.method")
protected class AutomaticTestMethodRenamer(oldMethodName: String?,

View File

@@ -1,7 +1,6 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.refactoring.rename.naming
import com.intellij.ide.projectView.impl.ProjectRootsUtil
import com.intellij.openapi.module.ModuleUtil
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiMethod
@@ -11,8 +10,7 @@ import com.intellij.usageView.UsageInfo
class JavaAutomaticTestMethodRenamerFactory : AutomaticTestMethodRenamerFactory() {
override fun isApplicable(element: PsiElement): Boolean {
if (element !is PsiMethod) return false
val file = element.containingFile
return !ProjectRootsUtil.isInTestSource(file)
return super.isApplicable(element)
}
override fun isEnabled(): Boolean = JavaRefactoringSettings.getInstance().isRenameTestMethods

View File

@@ -0,0 +1,16 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.refactoring.rename
import com.intellij.jvm.analysis.JavaJvmAnalysisTestUtil
import com.intellij.jvm.analysis.testFramework.AutomaticTestMethodRenamerTestBase
class JavaAutomaticTestMethodRenamerTest : AutomaticTestMethodRenamerTestBase() {
override fun getBasePath(): String = JavaJvmAnalysisTestUtil.TEST_DATA_PROJECT_RELATIVE_BASE_PATH + "/refactoring/renameTestMethod"
fun testTestMethod() = doTest("ClassTest.java")
fun testHelperMethod() = doTest("ClassTest.java")
fun testHelperClass() = doTest("TestUtil.java")
}

View File

@@ -0,0 +1,15 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.refactoring.rename
import com.intellij.jvm.analysis.KotlinJvmAnalysisTestUtil
import com.intellij.jvm.analysis.testFramework.AutomaticTestMethodRenamerTestBase
class KotlinAutomaticTestMethodRenamerTest : AutomaticTestMethodRenamerTestBase() {
override fun getBasePath(): String = KotlinJvmAnalysisTestUtil.TEST_DATA_PROJECT_RELATIVE_BASE_PATH + "/refactoring/renameTestMethod"
fun testTestMethod() = doTest("ClassTest.kt")
fun testHelperMethod() = doTest("ClassTest.kt")
fun testHelperClass() = doTest("TestUtil.kt")
}

View File

@@ -1,7 +1,5 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.java.refactoring.rename
package com.intellij.jvm.analysis.testFramework
import com.intellij.JavaTestUtil
import com.intellij.openapi.module.Module
import com.intellij.openapi.roots.ContentEntry
import com.intellij.openapi.roots.ModifiableRootModel
@@ -9,18 +7,14 @@ import com.intellij.refactoring.rename.naming.AutomaticRenamerFactory
import com.intellij.testFramework.LightProjectDescriptor
import com.intellij.testFramework.PsiTestUtil
import com.intellij.testFramework.fixtures.DefaultLightProjectDescriptor
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase
import com.intellij.util.PathUtil
import junit.framework.TestCase
import org.junit.jupiter.api.Nested
import java.io.File
class JavaAutomaticTestMethodRenamerAntiTest : LightJavaCodeInsightFixtureTestCase() {
override fun getTestDataPath(): String = JavaTestUtil.getJavaTestDataPath() + "/refactoring/renameTestMethod"
override fun getProjectDescriptor(): LightProjectDescriptor =
object : DefaultLightProjectDescriptor() {
abstract class AutomaticTestMethodRenamerTestBase : LightJvmCodeInsightFixtureTestCase() {
override fun getProjectDescriptor(): LightProjectDescriptor {
return object : DefaultLightProjectDescriptor() {
override fun configureModule(module: Module, model: ModifiableRootModel, contentEntry: ContentEntry) {
super.configureModule(module, model, contentEntry)
val jar1 = File(PathUtil.getJarPathForClass(Nested::class.java))
@@ -28,14 +22,8 @@ class JavaAutomaticTestMethodRenamerAntiTest : LightJavaCodeInsightFixtureTestCa
contentEntry.addSourceFolder(contentEntry.url + "/${getTestName(true)}/test_src", true)
}
}
fun testTestMethod() = doTest("ClassTest.java")
fun testHelperMethod() = doTest("ClassTest.java")
fun testHelperClass() = doTest("TestUtil.java")
private fun doTest(filename: String) {
}
protected fun doTest(filename: String) {
val filePath = "${getTestName(true)}/test_src/$filename"
myFixture.configureByFile(filePath)
val element = myFixture.elementAtCaret

View File

@@ -1,44 +0,0 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.refactoring.rename
import com.intellij.openapi.module.Module
import com.intellij.openapi.roots.ContentEntry
import com.intellij.openapi.roots.ModifiableRootModel
import com.intellij.refactoring.rename.naming.AutomaticRenamerFactory
import com.intellij.testFramework.LightProjectDescriptor
import junit.framework.TestCase
import org.jetbrains.kotlin.idea.base.plugin.artifacts.TestKotlinArtifacts
import org.jetbrains.kotlin.idea.base.test.TestRoot
import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
import org.jetbrains.kotlin.test.TestMetadata
@TestRoot("idea/tests")
@TestMetadata("testData/refactoring/renameTestMethod")
class KotlinAutomaticTestMethodRenamerAntiTest : KotlinLightCodeInsightFixtureTestCase() {
override fun getProjectDescriptor(): LightProjectDescriptor {
val jarPaths = listOf(TestKotlinArtifacts.kotlinStdlib, ConfigLibraryUtil.ATTACHABLE_LIBRARIES["JUnit5"]!!)
return object : KotlinWithJdkAndRuntimeLightProjectDescriptor(jarPaths, listOf(TestKotlinArtifacts.kotlinStdlibSources)) {
override fun configureModule(module: Module, model: ModifiableRootModel, contentEntry: ContentEntry) {
super.configureModule(module, model, contentEntry)
contentEntry.addSourceFolder(contentEntry.url + "/${getTestName(true)}/test_src", true)
}
}
}
fun testTestMethod() = doTest("ClassTest.kt")
fun testHelperMethod() = doTest("ClassTest.kt")
fun testHelperClass() = doTest("TestUtil.kt")
private fun doTest(filename : String) {
val filePath = "${getTestName(true)}/test_src/$filename"
myFixture.configureByFile(filePath)
val element = myFixture.elementAtCaret
AutomaticRenamerFactory.EP_NAME.extensionList.forEach {
TestCase.assertFalse(it.isApplicable(element))
}
}
}

View File

@@ -1,7 +1,6 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.refactoring.rename
import com.intellij.ide.projectView.impl.ProjectRootsUtil
import com.intellij.psi.PsiElement
import com.intellij.refactoring.rename.naming.AutomaticRenamer
import com.intellij.refactoring.rename.naming.AutomaticTestMethodRenamerFactory
@@ -14,8 +13,7 @@ import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
class KotlinAutomaticTestMethodRenamerFactory : AutomaticTestMethodRenamerFactory() {
override fun isApplicable(element: PsiElement): Boolean {
if (element !is KtNamedFunction) return false
val file = element.containingKtFile
return !ProjectRootsUtil.isInTestSource(file)
return super.isApplicable(element)
}
override fun createRenamer(element: PsiElement, newName: String, usages: MutableCollection<UsageInfo>): AutomaticRenamer {