Skip to content

Commit a04676f

Browse files
IlyaMuravjovEgorkaKulikov
authored andcommitted
Use user classpath and Spring Boot in utbot-spring-analyzer
1 parent 7011887 commit a04676f

File tree

22 files changed

+275
-351
lines changed

22 files changed

+275
-351
lines changed

utbot-framework-api/src/main/kotlin/org/utbot/framework/UtSettings.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ object UtSettings : AbstractSettings(logger, defaultKeyForSettingsPath, defaultS
301301
* The property is useful only for the IntelliJ IDEs.
302302
* If the property is set in true the spring analyzer process opens a debug port.
303303
* @see runInstrumentedProcessWithDebug
304-
* @see org.utbot.framework.process.SpringAnalyzerProcess
304+
* @see org.utbot.spring.process.SpringAnalyzerProcess
305305
*/
306306
var runSpringAnalyzerProcessWithDebug by getBooleanProperty(false)
307307

utbot-framework/src/main/kotlin/org/utbot/framework/process/EngineProcessMain.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,12 @@ private fun EngineProcessModel.setup(kryoHelper: KryoHelper, watchdog: IdleWatch
7777
}.toTypedArray())))
7878
}
7979
watchdog.measureTimeForActiveCall(getSpringBeanQualifiedNames, "Getting Spring bean definitions") { params ->
80-
val springAnalyzerProcess = SpringAnalyzerProcess.createBlocking()
80+
val springAnalyzerProcess = SpringAnalyzerProcess.createBlocking(params.classpath.toList())
8181
val beans = springAnalyzerProcess.terminateOnException { _ ->
8282
springAnalyzerProcess.getBeanQualifiedNames(
8383
params.classpath.toList(),
8484
params.config,
85-
// TODO remove once spring-analyzer learns to find resources on its own, temporarily leaving it here for testing with hardcoded absolute paths
86-
propertyFilesPaths = emptyList(),
87-
xmlConfigurationPaths = emptyList(),
88-
params.useSpringAnalyzer
85+
params.fileStorage,
8986
).toTypedArray()
9087
}
9188
springAnalyzerProcess.terminate()

utbot-framework/src/main/kotlin/org/utbot/framework/process/generated/EngineProcessModel.Generated.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class EngineProcessModel private constructor(
7373

7474
private val __StringArraySerializer = FrameworkMarshallers.String.array()
7575

76-
const val serializationHash = 1955031277042475752L
76+
const val serializationHash = -8371458556124482010L
7777

7878
}
7979
override val serializersOwner: ISerializersOwner get() = EngineProcessModel
@@ -755,7 +755,7 @@ data class GenerateTestReportResult (
755755
data class GetSpringBeanQualifiedNamesParams (
756756
val classpath: Array<String>,
757757
val config: String,
758-
val useSpringAnalyzer: Boolean
758+
val fileStorage: String?
759759
) : IPrintable {
760760
//companion
761761

@@ -766,14 +766,14 @@ data class GetSpringBeanQualifiedNamesParams (
766766
override fun read(ctx: SerializationCtx, buffer: AbstractBuffer): GetSpringBeanQualifiedNamesParams {
767767
val classpath = buffer.readArray {buffer.readString()}
768768
val config = buffer.readString()
769-
val useSpringAnalyzer = buffer.readBool()
770-
return GetSpringBeanQualifiedNamesParams(classpath, config, useSpringAnalyzer)
769+
val fileStorage = buffer.readNullable { buffer.readString() }
770+
return GetSpringBeanQualifiedNamesParams(classpath, config, fileStorage)
771771
}
772772

773773
override fun write(ctx: SerializationCtx, buffer: AbstractBuffer, value: GetSpringBeanQualifiedNamesParams) {
774774
buffer.writeArray(value.classpath) { buffer.writeString(it) }
775775
buffer.writeString(value.config)
776-
buffer.writeBool(value.useSpringAnalyzer)
776+
buffer.writeNullable(value.fileStorage) { buffer.writeString(it) }
777777
}
778778

779779

@@ -791,7 +791,7 @@ data class GetSpringBeanQualifiedNamesParams (
791791

792792
if (!(classpath contentDeepEquals other.classpath)) return false
793793
if (config != other.config) return false
794-
if (useSpringAnalyzer != other.useSpringAnalyzer) return false
794+
if (fileStorage != other.fileStorage) return false
795795

796796
return true
797797
}
@@ -800,7 +800,7 @@ data class GetSpringBeanQualifiedNamesParams (
800800
var __r = 0
801801
__r = __r*31 + classpath.contentDeepHashCode()
802802
__r = __r*31 + config.hashCode()
803-
__r = __r*31 + useSpringAnalyzer.hashCode()
803+
__r = __r*31 + if (fileStorage != null) fileStorage.hashCode() else 0
804804
return __r
805805
}
806806
//pretty print
@@ -809,7 +809,7 @@ data class GetSpringBeanQualifiedNamesParams (
809809
printer.indent {
810810
print("classpath = "); classpath.print(printer); println()
811811
print("config = "); config.print(printer); println()
812-
print("useSpringAnalyzer = "); useSpringAnalyzer.print(printer); println()
812+
print("fileStorage = "); fileStorage.print(printer); println()
813813
}
814814
printer.print(")")
815815
}

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/generator/UtTestsDialogProcessor.kt

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.intellij.openapi.progress.ProgressIndicator
88
import com.intellij.openapi.progress.Task
99
import com.intellij.openapi.project.DumbService
1010
import com.intellij.openapi.project.Project
11+
import com.intellij.openapi.project.guessProjectDir
1112
import com.intellij.openapi.roots.OrderEnumerator
1213
import com.intellij.openapi.roots.ProjectFileIndex
1314
import com.intellij.openapi.ui.Messages
@@ -27,6 +28,8 @@ import com.intellij.task.impl.ProjectTaskList
2728
import com.intellij.util.concurrency.AppExecutorUtil
2829
import com.intellij.util.containers.ContainerUtil
2930
import com.intellij.util.containers.nullize
31+
import com.intellij.util.io.exists
32+
import com.jetbrains.python.sdk.basePath
3033
import java.io.File
3134
import java.nio.file.Path
3235
import java.nio.file.Paths
@@ -46,7 +49,7 @@ import org.utbot.framework.CancellationStrategyType.NONE
4649
import org.utbot.framework.CancellationStrategyType.SAVE_PROCESSED_RESULTS
4750
import org.utbot.framework.UtSettings
4851
import org.utbot.framework.codegen.domain.ProjectType.*
49-
import org.utbot.framework.codegen.domain.TypeReplacementApproach
52+
import org.utbot.framework.codegen.domain.TypeReplacementApproach.*
5053
import org.utbot.framework.plugin.api.ApplicationContext
5154
import org.utbot.framework.plugin.api.ClassId
5255
import org.utbot.framework.plugin.api.JavaDocCommentStyle
@@ -168,8 +171,8 @@ object UtTestsDialogProcessor {
168171

169172
private fun createTests(project: Project, model: GenerateTestsModel) {
170173
val springConfigClass = when (val approach = model.typeReplacementApproach) {
171-
TypeReplacementApproach.DoNotReplace -> null
172-
is TypeReplacementApproach.ReplaceIfPossible ->
174+
DoNotReplace -> null
175+
is ReplaceIfPossible ->
173176
approach.config.takeUnless { it.endsWith(".xml") }?.let {
174177
JavaPsiFacade.getInstance(project).findClass(it, GlobalSearchScope.projectScope(project)) ?:
175178
error("Can't find configuration class $it")
@@ -227,25 +230,31 @@ object UtTestsDialogProcessor {
227230
val process = EngineProcess.createBlocking(project, classNameToPath)
228231

229232
process.terminateOnException { _ ->
230-
process.setupUtContext(buildDirs + classpathList)
233+
val classpathForClassLoader = buildDirs + classpathList
234+
process.setupUtContext(classpathForClassLoader)
231235
val applicationContext = when (model.projectType) {
232236
Spring -> {
233237
val beanQualifiedNames =
234238
when (val approach = model.typeReplacementApproach) {
235-
TypeReplacementApproach.DoNotReplace -> emptyList()
236-
is TypeReplacementApproach.ReplaceIfPossible ->
239+
DoNotReplace -> emptyList()
240+
is ReplaceIfPossible -> {
241+
// TODO: use common parent path for srcModule and used Spring
242+
// config module if they are different modules
243+
val projectFileStorage = model.srcModule.basePath
237244
process.getSpringBeanQualifiedNames(
238-
buildDirs + classpathList,
245+
classpathForClassLoader,
239246
approach.config,
240-
model.useSpringAnalyzer
241-
).also { logger.info { "Detected Spring Beans: $it" } }
247+
projectFileStorage,
248+
)
249+
}
242250
}
251+
val shouldUseImplementors = model.typeReplacementApproach is ReplaceIfPossible
243252

244253
SpringApplicationContext(
245254
mockFrameworkInstalled,
246255
staticMockingConfigured,
247256
beanQualifiedNames,
248-
shouldUseImplementors = beanQualifiedNames.isNotEmpty(),
257+
shouldUseImplementors,
249258
)
250259
}
251260
else -> ApplicationContext(mockFrameworkInstalled, staticMockingConfigured)

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ class GenerateTestsModel(
5656

5757
lateinit var typeReplacementApproach: TypeReplacementApproach
5858

59-
// TODO remove once issue with spring-analyzer expecting .properties and .xml file paths is resolved
60-
var useSpringAnalyzer = false
61-
6259
val conflictTriggers: ConflictTriggers = ConflictTriggers()
6360

6461
var runGeneratedTestsWithCoverage : Boolean = false

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/process/EngineProcess.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ class EngineProcess private constructor(val project: Project, private val classN
143143
engineModel.setupUtContext.startBlocking(SetupContextParams(classpathForUrlsClassloader))
144144
}
145145

146-
fun getSpringBeanQualifiedNames(classpathList: List<String>, config: String, useSpringAnalyzer: Boolean): List<String> {
146+
fun getSpringBeanQualifiedNames(classpathList: List<String>, config: String, fileStorage: String?): List<String> {
147147
assertReadAccessNotAllowed()
148148
return engineModel.getSpringBeanQualifiedNames.startBlocking(
149-
GetSpringBeanQualifiedNamesParams(classpathList.toTypedArray(), config, useSpringAnalyzer)
149+
GetSpringBeanQualifiedNamesParams(classpathList.toTypedArray(), config, fileStorage)
150150
).toList()
151151
}
152152

utbot-rd/src/main/rdgen/org/utbot/rd/models/EngineProcessModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ object EngineProcessModel : Ext(EngineProcessRoot) {
8787
val getSpringBeanQualifiedNamesParams = structdef {
8888
field("classpath", array(PredefinedType.string))
8989
field("config", PredefinedType.string)
90-
field("useSpringAnalyzer", PredefinedType.bool)
90+
field("fileStorage", PredefinedType.string.nullable)
9191
}
9292
val methodDescription = structdef {
9393
field("name", PredefinedType.string)

utbot-rd/src/main/rdgen/org/utbot/rd/models/SpringAnalyzerModel.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ object SpringAnalyzerProcessModel : Ext(SpringAnalyzerRoot) {
99
val springAnalyzerParams = structdef {
1010
field("classpath", array(PredefinedType.string))
1111
field("configuration", PredefinedType.string)
12-
field("propertyFilesPaths", array(PredefinedType.string))
13-
field("xmlConfigurationPaths", array(PredefinedType.string))
14-
field("useSpringAnalyzer", PredefinedType.bool)
12+
field("fileStorage", PredefinedType.string.nullable)
1513
}
1614

1715
val springAnalyzerResult = structdef {
Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
12
import com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCacheFileTransformer
23
import com.github.jengelman.gradle.plugins.shadow.transformers.PropertiesFileTransformer
34

@@ -18,26 +19,62 @@ java {
1819
targetCompatibility = JavaVersion.VERSION_1_8
1920
}
2021

22+
val withoutSpringConfiguration by configurations.creating {}
23+
val withSpringConfiguration by configurations.creating {
24+
extendsFrom(withoutSpringConfiguration)
25+
}
26+
configurations.implementation.get().extendsFrom(withSpringConfiguration)
27+
2128
dependencies {
2229
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot
23-
implementation("org.springframework.boot:spring-boot:$springBootVersion")
24-
25-
implementation(project(":utbot-rd"))
26-
implementation(project(":utbot-core"))
27-
implementation(project(":utbot-framework-api"))
28-
implementation("com.jetbrains.rd:rd-framework:$rdVersion")
29-
implementation("com.jetbrains.rd:rd-core:$rdVersion")
30-
implementation("commons-logging:commons-logging:$commonsLoggingVersion")
30+
withSpringConfiguration("org.springframework.boot:spring-boot:$springBootVersion")
31+
3132
implementation("io.github.microutils:kotlin-logging:$kotlinLoggingVersion")
32-
implementation("commons-io:commons-io:$commonsIOVersion")
33+
34+
fun ModuleDependency.excludeSlf4jApi() = exclude(group = "org.slf4j", module = "slf4j-api")
35+
36+
withoutSpringConfiguration(project(":utbot-rd")) { excludeSlf4jApi() }
37+
withoutSpringConfiguration(project(":utbot-core")) { excludeSlf4jApi() }
38+
withoutSpringConfiguration(project(":utbot-framework-api")) { excludeSlf4jApi() }
39+
withoutSpringConfiguration("com.jetbrains.rd:rd-framework:$rdVersion") { excludeSlf4jApi() }
40+
withoutSpringConfiguration("com.jetbrains.rd:rd-core:$rdVersion") { excludeSlf4jApi() }
41+
withoutSpringConfiguration("commons-logging:commons-logging:$commonsLoggingVersion") { excludeSlf4jApi() }
42+
withoutSpringConfiguration("commons-io:commons-io:$commonsIOVersion") { excludeSlf4jApi() }
3343
}
3444

3545
application {
3646
mainClass.set("org.utbot.spring.process.SpringAnalyzerProcessMainKt")
3747
}
3848

39-
// see more details about this task -- https://github.com/spring-projects/spring-boot/issues/1828
40-
tasks.shadowJar {
49+
val shadowWithoutSpring by tasks.register<ShadowJar>("shadowJarWithoutSpring") {
50+
configureShadowJar(withoutSpringConfiguration)
51+
archiveFileName.set("utbot-spring-analyzer-shadow.jar")
52+
}
53+
54+
val shadowWithSpring by tasks.register<ShadowJar>("shadowJarWithSpring") {
55+
configureShadowJar(withSpringConfiguration)
56+
archiveFileName.set("utbot-spring-analyzer-with-spring-shadow.jar")
57+
}
58+
59+
val springAnalyzerJar: Configuration by configurations.creating {
60+
isCanBeResolved = false
61+
isCanBeConsumed = true
62+
}
63+
64+
artifacts {
65+
add(springAnalyzerJar.name, shadowWithoutSpring)
66+
add(springAnalyzerJar.name, shadowWithSpring)
67+
}
68+
69+
fun ShadowJar.configureShadowJar(configuration: Configuration) {
70+
// see more details -- https://github.com/johnrengelman/shadow/blob/master/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy
71+
group = "shadow"
72+
from(sourceSets.main.get().output)
73+
exclude("META-INF/INDEX.LIST", "META-INF/*.SF", "META-INF/*.DSA", "META-INF/*.RSA", "module-info.class")
74+
75+
configurations = listOf(configuration)
76+
77+
// see more details -- https://github.com/spring-projects/spring-boot/issues/1828
4178
isZip64 = true
4279
// Required for Spring
4380
mergeServiceFiles()
@@ -50,14 +87,4 @@ tasks.shadowJar {
5087
})
5188

5289
transform(Log4j2PluginsCacheFileTransformer::class.java)
53-
archiveFileName.set("utbot-spring-analyzer-shadow.jar")
54-
}
55-
56-
val springAnalyzerJar: Configuration by configurations.creating {
57-
isCanBeResolved = false
58-
isCanBeConsumed = true
59-
}
60-
61-
artifacts {
62-
add(springAnalyzerJar.name, tasks.shadowJar)
6390
}

utbot-spring-analyzer/src/main/kotlin/org/utbot/spring/ApplicationRunner.kt

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)