Skip to content

Commit 68a65ea

Browse files
authored
Add Junit5-params dependency for projects without it when needed #620 (#829)
Autoadd Junit5-params dependency for junit5 projects without it when needed
1 parent 79b2516 commit 68a65ea

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/Domain.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ sealed class TestFramework(
173173
override val displayName: String,
174174
override val description: String = "Use $displayName as test framework",
175175
) : CodeGenerationSettingItem {
176+
var isParametrizedTestsConfigured = false
176177
var isInstalled: Boolean = false
177178
abstract val mainPackage: String
178179
abstract val assertionsClass: ClassId

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/util/DependencyPatterns.kt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ fun TestFramework.patterns(): Patterns {
2626
return Patterns(moduleLibraryPatterns, libraryPatterns)
2727
}
2828

29+
30+
fun TestFramework.parametrizedTestsPatterns(): Patterns {
31+
val moduleLibraryPatterns = when (this) {
32+
Junit4 -> emptyList()
33+
Junit5 -> emptyList() // emptyList here because JUnit5 module may not be enough for parametrized tests if :junit-jupiter-params: is not installed
34+
TestNg -> testNgModulePatterns
35+
}
36+
val libraryPatterns = when (this) {
37+
Junit4 -> emptyList()
38+
Junit5 -> junit5ParametrizedTestsPatterns
39+
TestNg -> testNgPatterns
40+
}
41+
42+
return Patterns(moduleLibraryPatterns, libraryPatterns)
43+
}
44+
45+
2946
fun MockFramework.patterns(): Patterns {
3047
val moduleLibraryPatterns = when (this) {
3148
MockFramework.MOCKITO -> mockitoModulePatterns
@@ -47,11 +64,16 @@ val JUNIT_5_MVN_PATTERN = Regex("org\\.junit\\.jupiter:junit-jupiter-api:5(\\.[0
4764
val JUNIT_5_BASIC_PATTERN = Regex("JUnit5\\.4")
4865
val junit5Patterns = listOf(JUNIT_5_JAR_PATTERN, JUNIT_5_MVN_PATTERN, JUNIT_5_BASIC_PATTERN)
4966

67+
val JUNIT_5_PARAMETRIZED_JAR_PATTERN = Regex("junit-jupiter-params-5(\\.[0-9]+){1,2}")
68+
val JUNIT_5_PARAMETRIZED_MVN_PATTERN = Regex("org\\.junit\\.jupiter\\.junit-jupiter-params:5(\\.[0-9]+){1,2}")
69+
val junit5ParametrizedTestsPatterns = listOf(JUNIT_5_JAR_PATTERN, JUNIT_5_BASIC_PATTERN,
70+
JUNIT_5_PARAMETRIZED_JAR_PATTERN, JUNIT_5_PARAMETRIZED_MVN_PATTERN)
71+
5072
val JUNIT5_BASIC_MODULE_PATTERN = Regex("junit-jupiter")
5173
val junit5ModulePatterns = listOf(JUNIT5_BASIC_MODULE_PATTERN)
5274

5375
val TEST_NG_JAR_PATTERN = Regex("testng-[0-9](\\.[0-9]+){2}")
54-
val TEST_NG_MVN_PATTERN = Regex("org\\.testng:testng:(\\.[0-9]+){3}")
76+
val TEST_NG_MVN_PATTERN = Regex("org\\.testng:testng:[0-9](\\.[0-9]+){2}")
5577
val TEST_NG_BASIC_PATTERN = Regex("testng")
5678
val testNgPatterns = listOf(TEST_NG_JAR_PATTERN, TEST_NG_MVN_PATTERN, TEST_NG_BASIC_PATTERN)
5779

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/models/ExternalLibraryDescriptors.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ fun jUnit5LibraryDescriptor(versionInProject: String?) =
1212
fun testNgLibraryDescriptor(versionInProject: String?) =
1313
ExternalLibraryDescriptor("org.testng", "testng", "6.8.8", null, versionInProject ?: "6.9.6")
1414

15+
fun jUnit5ParametrizedTestsLibraryDescriptor(versionInProject: String?) =
16+
ExternalLibraryDescriptor("org.junit.jupiter", "junit-jupiter-params", "5.8.1", null, versionInProject ?: "5.8.1")
17+
1518
fun mockitoCoreLibraryDescriptor(versionInProject: String?) =
1619
ExternalLibraryDescriptor("org.mockito", "mockito-core", "3.5.0", "4.2.0", versionInProject ?: "4.2.0")

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/GenerateTestsDialogWindow.kt

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ import org.utbot.framework.util.Conflict
116116
import org.utbot.intellij.plugin.models.GenerateTestsModel
117117
import org.utbot.intellij.plugin.models.jUnit4LibraryDescriptor
118118
import org.utbot.intellij.plugin.models.jUnit5LibraryDescriptor
119+
import org.utbot.intellij.plugin.models.jUnit5ParametrizedTestsLibraryDescriptor
119120
import org.utbot.intellij.plugin.models.mockitoCoreLibraryDescriptor
120121
import org.utbot.intellij.plugin.models.packageName
121122
import org.utbot.intellij.plugin.models.testNgLibraryDescriptor
@@ -125,6 +126,7 @@ import org.utbot.intellij.plugin.ui.utils.LibrarySearchScope
125126
import org.utbot.intellij.plugin.ui.utils.addSourceRootIfAbsent
126127
import org.utbot.intellij.plugin.ui.utils.allLibraries
127128
import org.utbot.intellij.plugin.ui.utils.findFrameworkLibrary
129+
import org.utbot.intellij.plugin.ui.utils.findParametrizedTestsLibrary
128130
import org.utbot.intellij.plugin.ui.utils.getOrCreateTestResourcesPath
129131
import org.utbot.intellij.plugin.ui.utils.isBuildWithGradle
130132
import org.utbot.intellij.plugin.ui.utils.kotlinTargetPlatform
@@ -196,6 +198,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
196198

197199
TestFramework.allItems.forEach {
198200
it.isInstalled = findFrameworkLibrary(model.project, model.testModule, it) != null
201+
it.isParametrizedTestsConfigured = findParametrizedTestsLibrary(model.project, model.testModule, it) != null
199202
}
200203
MockFramework.allItems.forEach {
201204
it.isInstalled = findFrameworkLibrary(model.project, model.testModule, it) != null
@@ -532,6 +535,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
532535
configureTestFrameworkIfRequired()
533536
configureMockFrameworkIfRequired()
534537
configureStaticMockingIfRequired()
538+
configureParametrizedTestsIfRequired()
535539

536540
super.doOKAction()
537541
}
@@ -665,8 +669,14 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
665669
//region configure frameworks
666670

667671
private fun configureTestFrameworkIfRequired() {
668-
if (!testFrameworks.item.isInstalled) {
672+
val testFramework = testFrameworks.item
673+
if (!testFramework.isInstalled) {
669674
configureTestFramework()
675+
676+
// Configuring framework will configure parametrized tests automatically
677+
// TODO: do something more general here
678+
// Note: we can't just update isParametrizedTestsConfigured as before because project.allLibraries() won't be updated immediately
679+
testFramework.isParametrizedTestsConfigured = true
670680
}
671681

672682
model.conflictTriggers[Conflict.TestFrameworkConflict] = TestFramework.allItems.count { it.isInstalled } > 1
@@ -684,6 +694,12 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
684694
}
685695
}
686696

697+
private fun configureParametrizedTestsIfRequired() {
698+
if (parametrizedTestSources.item != ParametrizedTestSource.DO_NOT_PARAMETRIZE && !testFrameworks.item.isParametrizedTestsConfigured) {
699+
configureParametrizedTests()
700+
}
701+
}
702+
687703
private fun configureTestFramework() {
688704
val selectedTestFramework = testFrameworks.item
689705

@@ -742,6 +758,27 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
742758
}
743759
}
744760

761+
private fun configureParametrizedTests() {
762+
// TODO: currently first three declarations are copy-pasted from configureTestFramework(), maybe fix this somehow?
763+
val selectedTestFramework = testFrameworks.item
764+
765+
val libraryInProject =
766+
findFrameworkLibrary(model.project, model.testModule, selectedTestFramework, LibrarySearchScope.Project)
767+
val versionInProject = libraryInProject?.libraryName?.parseVersion()
768+
769+
val libraryDescriptor: ExternalLibraryDescriptor? = when (selectedTestFramework) {
770+
Junit4 -> error("Parametrized tests are not supported for JUnit 4")
771+
Junit5 -> jUnit5ParametrizedTestsLibraryDescriptor(versionInProject)
772+
TestNg -> null // Parametrized tests come with TestNG by default
773+
}
774+
775+
selectedTestFramework.isParametrizedTestsConfigured = true
776+
libraryDescriptor?.let {
777+
addDependency(model.testModule, it)
778+
.onError { selectedTestFramework.isParametrizedTestsConfigured = false }
779+
}
780+
}
781+
745782
/**
746783
* Adds the dependency for selected framework via [JavaProjectModelModificationService].
747784
*

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/utils/LibraryMatcher.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.utbot.framework.plugin.api.MockFramework
66
import com.intellij.openapi.module.Module
77
import com.intellij.openapi.project.Project
88
import com.intellij.openapi.roots.LibraryOrderEntry
9+
import org.utbot.framework.codegen.model.util.parametrizedTestsPatterns
910
import org.utbot.framework.codegen.model.util.Patterns
1011

1112
fun findFrameworkLibrary(
@@ -24,6 +25,13 @@ fun findFrameworkLibrary(
2425
scope: LibrarySearchScope = LibrarySearchScope.Module,
2526
): LibraryOrderEntry? = findMatchingLibrary(project, testModule, mockFramework.patterns(), scope)
2627

28+
fun findParametrizedTestsLibrary(
29+
project: Project,
30+
testModule: Module,
31+
testFramework: TestFramework,
32+
scope: LibrarySearchScope = LibrarySearchScope.Module,
33+
): LibraryOrderEntry? = findMatchingLibrary(project, testModule, testFramework.parametrizedTestsPatterns(), scope)
34+
2735
private fun findMatchingLibrary(
2836
project: Project,
2937
testModule: Module,

0 commit comments

Comments
 (0)