From 7ce1752791cbb8e76fe97ee29e0453dc8cd16c84 Mon Sep 17 00:00:00 2001 From: IlyaMuravjov <71839386+IlyaMuravjov@users.noreply.github.com> Date: Fri, 29 Sep 2023 21:01:47 +0300 Subject: [PATCH] Automatically install `spring-security-test` when `spring-security-core` is present (#2624) --- .../models/ExternalLibraryDescriptors.kt | 14 +++++++++-- .../plugin/ui/GenerateTestsDialogWindow.kt | 5 ++-- .../framework/codegen/domain/SpringModule.kt | 3 +++ .../api/utils/SpringDependencyPatterns.kt | 23 ++++++++++++++++--- 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/models/ExternalLibraryDescriptors.kt b/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/models/ExternalLibraryDescriptors.kt index 9db3487d0a..23448876c7 100644 --- a/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/models/ExternalLibraryDescriptors.kt +++ b/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/models/ExternalLibraryDescriptors.kt @@ -51,11 +51,21 @@ fun springBootTestLibraryDescriptor(versionInProject: Version?): ExternalLibrary ) } +private const val MIN_SUPPORTED_SPRING_VERSION = "2.5" + fun springTestLibraryDescriptor(versionInProject: Version?): ExternalLibraryDescriptor { - val preferredVersion = if (versionInProject?.hasNumericOrEmptyPatch() == true) versionInProject?.plainText else "6.0.8" + val preferredVersion = if (versionInProject?.hasNumericOrEmptyPatch() == true) versionInProject.plainText else "6.0.8" return ExternalLibraryDescriptor( "org.springframework", "spring-test", - "2.5", null, preferredVersion + MIN_SUPPORTED_SPRING_VERSION, null, preferredVersion + ) +} + +fun springSecurityLibraryDescriptor(versionInProject: Version?): ExternalLibraryDescriptor { + val preferredVersion = if (versionInProject?.hasNumericOrEmptyPatch() == true) versionInProject.plainText else "6.0.8" + return ExternalLibraryDescriptor( + "org.springframework.security", "spring-security-test", + MIN_SUPPORTED_SPRING_VERSION, null, preferredVersion ) } diff --git a/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/GenerateTestsDialogWindow.kt b/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/GenerateTestsDialogWindow.kt index 887050cff7..8076c2208a 100644 --- a/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/GenerateTestsDialogWindow.kt +++ b/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/GenerateTestsDialogWindow.kt @@ -86,8 +86,7 @@ import org.utbot.framework.codegen.domain.MockitoStaticMocking import org.utbot.framework.codegen.domain.NoStaticMocking import org.utbot.framework.codegen.domain.ParametrizedTestSource import org.utbot.framework.codegen.domain.ProjectType -import org.utbot.framework.codegen.domain.SpringModule.SPRING_BEANS -import org.utbot.framework.codegen.domain.SpringModule.SPRING_BOOT +import org.utbot.framework.codegen.domain.SpringModule.* import org.utbot.framework.codegen.domain.StaticsMocking import org.utbot.framework.codegen.domain.TestFramework import org.utbot.framework.codegen.domain.TestNg @@ -112,6 +111,7 @@ import org.utbot.intellij.plugin.models.jUnit5ParametrizedTestsLibraryDescriptor import org.utbot.intellij.plugin.models.mockitoCoreLibraryDescriptor import org.utbot.intellij.plugin.models.packageName import org.utbot.intellij.plugin.models.springBootTestLibraryDescriptor +import org.utbot.intellij.plugin.models.springSecurityLibraryDescriptor import org.utbot.intellij.plugin.models.springTestLibraryDescriptor import org.utbot.intellij.plugin.models.testNgNewLibraryDescriptor import org.utbot.intellij.plugin.models.testNgOldLibraryDescriptor @@ -1020,6 +1020,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m val libraryDescriptor = when (springModule) { SPRING_BOOT -> springBootTestLibraryDescriptor(frameworkVersionInProject) SPRING_BEANS -> springTestLibraryDescriptor(frameworkVersionInProject) + SPRING_SECURITY -> springSecurityLibraryDescriptor(frameworkVersionInProject) } model.preClasspathCollectionPromises += addDependency(model.testModule, libraryDescriptor) diff --git a/utbot-spring-framework/src/main/kotlin/org/utbot/framework/codegen/domain/SpringModule.kt b/utbot-spring-framework/src/main/kotlin/org/utbot/framework/codegen/domain/SpringModule.kt index aedace2ef1..8ada81ee7f 100644 --- a/utbot-spring-framework/src/main/kotlin/org/utbot/framework/codegen/domain/SpringModule.kt +++ b/utbot-spring-framework/src/main/kotlin/org/utbot/framework/codegen/domain/SpringModule.kt @@ -8,6 +8,9 @@ enum class SpringModule( ), SPRING_BOOT( testFrameworkDisplayName = "spring-boot-test", + ), + SPRING_SECURITY( + testFrameworkDisplayName = "spring-security-test" ); var isInstalled = false diff --git a/utbot-spring-framework/src/main/kotlin/org/utbot/framework/plugin/api/utils/SpringDependencyPatterns.kt b/utbot-spring-framework/src/main/kotlin/org/utbot/framework/plugin/api/utils/SpringDependencyPatterns.kt index 8b83c23c1b..8f18fda134 100644 --- a/utbot-spring-framework/src/main/kotlin/org/utbot/framework/plugin/api/utils/SpringDependencyPatterns.kt +++ b/utbot-spring-framework/src/main/kotlin/org/utbot/framework/plugin/api/utils/SpringDependencyPatterns.kt @@ -1,17 +1,18 @@ package org.utbot.framework.plugin.api.utils -import org.utbot.framework.codegen.domain.SpringModule.SPRING_BEANS -import org.utbot.framework.codegen.domain.SpringModule.SPRING_BOOT import org.utbot.framework.codegen.domain.SpringModule +import org.utbot.framework.codegen.domain.SpringModule.* fun SpringModule.patterns(): Patterns { val moduleLibraryPatterns = when (this) { SPRING_BOOT -> springBootModulePatterns SPRING_BEANS -> springBeansModulePatterns + SPRING_SECURITY -> springSecurityModulePatterns } val libraryPatterns = when (this) { SPRING_BOOT -> springBootPatterns SPRING_BEANS -> springBeansPatterns + SPRING_SECURITY -> springSecurityPatterns } return Patterns(moduleLibraryPatterns, libraryPatterns) @@ -21,10 +22,12 @@ fun SpringModule.testPatterns(): Patterns { val moduleLibraryPatterns = when (this) { SPRING_BOOT -> springBootTestModulePatterns SPRING_BEANS -> springBeansTestModulePatterns + SPRING_SECURITY -> springSecurityTestModulePatterns } val libraryPatterns = when (this) { SPRING_BOOT -> springBootTestPatterns SPRING_BEANS -> springBeansTestPatterns + SPRING_SECURITY -> springSecurityTestPatterns } return Patterns(moduleLibraryPatterns, libraryPatterns) @@ -57,4 +60,18 @@ val SPRING_BOOT_TEST_MVN_PATTERN = Regex("org\\.springframework\\.boot:spring-bo val springBootTestPatterns = listOf(SPRING_BOOT_TEST_JAR_PATTERN, SPRING_BOOT_TEST_MVN_PATTERN) val SPRING_BOOT_TEST_BASIC_MODULE_PATTERN = Regex("spring-boot-test") -val springBootTestModulePatterns = listOf(SPRING_BOOT_TEST_BASIC_MODULE_PATTERN) \ No newline at end of file +val springBootTestModulePatterns = listOf(SPRING_BOOT_TEST_BASIC_MODULE_PATTERN) + +val SPRING_SECURITY_JAR_PATTERN = Regex("spring-security-core-([0-9]+)(\\.[0-9]+){1,2}") +val SPRING_SECURITY_MVN_PATTERN = Regex("org\\.springframework\\.security:spring-security-core:([0-9]+)(\\.[0-9]+){1,2}") +val springSecurityPatterns = listOf(SPRING_SECURITY_JAR_PATTERN, SPRING_SECURITY_MVN_PATTERN) + +val SPRING_SECURITY_BASIC_MODULE_PATTERN = Regex("spring-security-core") +val springSecurityModulePatterns = listOf(SPRING_SECURITY_BASIC_MODULE_PATTERN) + +val SPRING_SECURITY_TEST_JAR_PATTERN = Regex("spring-security-test-([0-9]+)(\\.[0-9]+){1,2}") +val SPRING_SECURITY_TEST_MVN_PATTERN = Regex("org\\.springframework\\.security:spring-security-test:([0-9]+)(\\.[0-9]+){1,2}") +val springSecurityTestPatterns = listOf(SPRING_SECURITY_TEST_JAR_PATTERN, SPRING_SECURITY_TEST_MVN_PATTERN) + +val SPRING_SECURITY_TEST_BASIC_MODULE_PATTERN = Regex("spring-security-test") +val springSecurityTestModulePatterns = listOf(SPRING_SECURITY_TEST_BASIC_MODULE_PATTERN) \ No newline at end of file