Skip to content

Activate selected profiles in Spring #2174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ private fun EngineProcessModel.setup(kryoHelper: KryoHelper, watchdog: IdleWatch
val springAnalyzerProcess = SpringAnalyzerProcess.createBlocking(params.classpath.toList())
val beans = springAnalyzerProcess.terminateOnException { _ ->
springAnalyzerProcess.getBeanQualifiedNames(
params.classpath.toList(),
params.config,
params.fileStorage,
params.profileExpression,
).toTypedArray()
}
springAnalyzerProcess.terminate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class EngineProcessModel private constructor(

private val __StringArraySerializer = FrameworkMarshallers.String.array()

const val serializationHash = -8371458556124482010L
const val serializationHash = 6076922965808046990L

}
override val serializersOwner: ISerializersOwner get() = EngineProcessModel
Expand Down Expand Up @@ -180,7 +180,7 @@ val IProtocol.engineProcessModel get() = getOrCreateExtension(EngineProcessModel


/**
* #### Generated from [EngineProcessModel.kt:104]
* #### Generated from [EngineProcessModel.kt:105]
*/
data class FindMethodParamNamesArguments (
val classId: ByteArray,
Expand Down Expand Up @@ -243,7 +243,7 @@ data class FindMethodParamNamesArguments (


/**
* #### Generated from [EngineProcessModel.kt:108]
* #### Generated from [EngineProcessModel.kt:109]
*/
data class FindMethodParamNamesResult (
val paramNames: ByteArray
Expand Down Expand Up @@ -300,7 +300,7 @@ data class FindMethodParamNamesResult (


/**
* #### Generated from [EngineProcessModel.kt:97]
* #### Generated from [EngineProcessModel.kt:98]
*/
data class FindMethodsInClassMatchingSelectedArguments (
val classId: ByteArray,
Expand Down Expand Up @@ -363,7 +363,7 @@ data class FindMethodsInClassMatchingSelectedArguments (


/**
* #### Generated from [EngineProcessModel.kt:101]
* #### Generated from [EngineProcessModel.kt:102]
*/
data class FindMethodsInClassMatchingSelectedResult (
val executableIds: ByteArray
Expand Down Expand Up @@ -588,7 +588,7 @@ data class GenerateResult (


/**
* #### Generated from [EngineProcessModel.kt:116]
* #### Generated from [EngineProcessModel.kt:117]
*/
data class GenerateTestReportArgs (
val eventLogMessage: String?,
Expand Down Expand Up @@ -681,7 +681,7 @@ data class GenerateTestReportArgs (


/**
* #### Generated from [EngineProcessModel.kt:125]
* #### Generated from [EngineProcessModel.kt:126]
*/
data class GenerateTestReportResult (
val notifyMessage: String,
Expand Down Expand Up @@ -755,7 +755,8 @@ data class GenerateTestReportResult (
data class GetSpringBeanQualifiedNamesParams (
val classpath: Array<String>,
val config: String,
val fileStorage: String?
val fileStorage: Array<String>,
val profileExpression: String?
) : IPrintable {
//companion

Expand All @@ -766,14 +767,16 @@ data class GetSpringBeanQualifiedNamesParams (
override fun read(ctx: SerializationCtx, buffer: AbstractBuffer): GetSpringBeanQualifiedNamesParams {
val classpath = buffer.readArray {buffer.readString()}
val config = buffer.readString()
val fileStorage = buffer.readNullable { buffer.readString() }
return GetSpringBeanQualifiedNamesParams(classpath, config, fileStorage)
val fileStorage = buffer.readArray {buffer.readString()}
val profileExpression = buffer.readNullable { buffer.readString() }
return GetSpringBeanQualifiedNamesParams(classpath, config, fileStorage, profileExpression)
}

override fun write(ctx: SerializationCtx, buffer: AbstractBuffer, value: GetSpringBeanQualifiedNamesParams) {
buffer.writeArray(value.classpath) { buffer.writeString(it) }
buffer.writeString(value.config)
buffer.writeNullable(value.fileStorage) { buffer.writeString(it) }
buffer.writeArray(value.fileStorage) { buffer.writeString(it) }
buffer.writeNullable(value.profileExpression) { buffer.writeString(it) }
}


Expand All @@ -791,7 +794,8 @@ data class GetSpringBeanQualifiedNamesParams (

if (!(classpath contentDeepEquals other.classpath)) return false
if (config != other.config) return false
if (fileStorage != other.fileStorage) return false
if (!(fileStorage contentDeepEquals other.fileStorage)) return false
if (profileExpression != other.profileExpression) return false

return true
}
Expand All @@ -800,7 +804,8 @@ data class GetSpringBeanQualifiedNamesParams (
var __r = 0
__r = __r*31 + classpath.contentDeepHashCode()
__r = __r*31 + config.hashCode()
__r = __r*31 + if (fileStorage != null) fileStorage.hashCode() else 0
__r = __r*31 + fileStorage.contentDeepHashCode()
__r = __r*31 + if (profileExpression != null) profileExpression.hashCode() else 0
return __r
}
//pretty print
Expand All @@ -810,6 +815,7 @@ data class GetSpringBeanQualifiedNamesParams (
print("classpath = "); classpath.print(printer); println()
print("config = "); config.print(printer); println()
print("fileStorage = "); fileStorage.print(printer); println()
print("profileExpression = "); profileExpression.print(printer); println()
}
printer.print(")")
}
Expand Down Expand Up @@ -882,7 +888,7 @@ data class JdkInfo (


/**
* #### Generated from [EngineProcessModel.kt:92]
* #### Generated from [EngineProcessModel.kt:93]
*/
data class MethodDescription (
val name: String,
Expand Down Expand Up @@ -1299,7 +1305,7 @@ data class TestGeneratorParams (


/**
* #### Generated from [EngineProcessModel.kt:111]
* #### Generated from [EngineProcessModel.kt:112]
*/
data class WriteSarifReportArguments (
val testSetsId: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,13 @@ object UtTestsDialogProcessor {
ModuleRootManager.getInstance(module).contentRoots.toList()
}
}

val fileStorage = contentRoots.map { root -> root.url }.toTypedArray()
process.getSpringBeanQualifiedNames(
classpathForClassLoader,
approach.config,
// TODO: consider passing it as an array
contentRoots.joinToString(File.pathSeparator),
fileStorage,
profileExpression = null,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,14 @@ class EngineProcess private constructor(val project: Project, private val classN
engineModel.setupUtContext.startBlocking(SetupContextParams(classpathForUrlsClassloader))
}

fun getSpringBeanQualifiedNames(classpathList: List<String>, config: String, fileStorage: String?): List<String> {
fun getSpringBeanQualifiedNames(
classpathList: List<String>,
config: String,
fileStorage: Array<String>,
profileExpression: String?): List<String> {
assertReadAccessNotAllowed()
return engineModel.getSpringBeanQualifiedNames.startBlocking(
GetSpringBeanQualifiedNamesParams(classpathList.toTypedArray(), config, fileStorage)
GetSpringBeanQualifiedNamesParams(classpathList.toTypedArray(), config, fileStorage, profileExpression)
).toList()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ object EngineProcessModel : Ext(EngineProcessRoot) {
val getSpringBeanQualifiedNamesParams = structdef {
field("classpath", array(PredefinedType.string))
field("config", PredefinedType.string)
field("fileStorage", PredefinedType.string.nullable)
field("fileStorage", array(PredefinedType.string))
field("profileExpression", PredefinedType.string.nullable)
}
val methodDescription = structdef {
field("name", PredefinedType.string)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ object SpringAnalyzerRoot : Root()

object SpringAnalyzerProcessModel : Ext(SpringAnalyzerRoot) {
val springAnalyzerParams = structdef {
field("classpath", array(PredefinedType.string))
field("configuration", PredefinedType.string)
field("fileStorage", PredefinedType.string.nullable)
field("fileStorage", array(PredefinedType.string))
field("profileExpression", PredefinedType.string.nullable)
}

val springAnalyzerResult = structdef {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.utbot.spring.api
import java.net.URL

class ApplicationData(
val classpath: Array<URL>,
val configurationFile: String,
val fileStorage: String?,
val fileStorage: List<URL>,
val profileExpression: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package org.utbot.spring.configurators
import com.jetbrains.rd.util.getLogger
import com.jetbrains.rd.util.info
import org.springframework.boot.builder.SpringApplicationBuilder
import org.utbot.spring.config.TestApplicationConfiguration
import org.springframework.core.env.StandardEnvironment
import org.utbot.spring.api.ApplicationData
import org.utbot.spring.config.TestApplicationConfiguration
import org.utbot.spring.utils.ConfigurationManager
import java.io.File
import kotlin.io.path.Path

const val DEFAULT_PROFILE_NAME = "default"
private val logger = getLogger<ApplicationConfigurator>()

open class ApplicationConfigurator(
Expand Down Expand Up @@ -38,8 +40,22 @@ open class ApplicationConfigurator(
)
}
}

setActiveProfile(applicationData.profileExpression ?: DEFAULT_PROFILE_NAME)
}

private fun setActiveProfile(profileExpression: String) {
val profilesToActivate = parseProfileExpression(profileExpression)

val environment = StandardEnvironment()
environment.setActiveProfiles(*profilesToActivate)

applicationBuilder.environment(environment)
}

//TODO: implement this, e.g. 'prod|web' -> listOf(prod, web)
private fun parseProfileExpression(profileExpression: String) : Array<String> = arrayOf(profileExpression)

private fun findConfigurationType(applicationData: ApplicationData): ApplicationConfigurationType {
//TODO: support Spring Boot Applications here.
val fileExtension = File(applicationData.configurationFile).extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class SpringAnalyzerProcessModel private constructor(
}


const val serializationHash = 476832059519556525L
const val serializationHash = 1208896755601889441L

}
override val serializersOwner: ISerializersOwner get() = SpringAnalyzerProcessModel
Expand Down Expand Up @@ -98,9 +98,9 @@ val IProtocol.springAnalyzerProcessModel get() = getOrCreateExtension(SpringAnal
* #### Generated from [SpringAnalyzerModel.kt:9]
*/
data class SpringAnalyzerParams (
val classpath: Array<String>,
val configuration: String,
val fileStorage: String?
val fileStorage: Array<String>,
val profileExpression: String?
) : IPrintable {
//companion

Expand All @@ -109,16 +109,16 @@ data class SpringAnalyzerParams (

@Suppress("UNCHECKED_CAST")
override fun read(ctx: SerializationCtx, buffer: AbstractBuffer): SpringAnalyzerParams {
val classpath = buffer.readArray {buffer.readString()}
val configuration = buffer.readString()
val fileStorage = buffer.readNullable { buffer.readString() }
return SpringAnalyzerParams(classpath, configuration, fileStorage)
val fileStorage = buffer.readArray {buffer.readString()}
val profileExpression = buffer.readNullable { buffer.readString() }
return SpringAnalyzerParams(configuration, fileStorage, profileExpression)
}

override fun write(ctx: SerializationCtx, buffer: AbstractBuffer, value: SpringAnalyzerParams) {
buffer.writeArray(value.classpath) { buffer.writeString(it) }
buffer.writeString(value.configuration)
buffer.writeNullable(value.fileStorage) { buffer.writeString(it) }
buffer.writeArray(value.fileStorage) { buffer.writeString(it) }
buffer.writeNullable(value.profileExpression) { buffer.writeString(it) }
}


Expand All @@ -134,27 +134,27 @@ data class SpringAnalyzerParams (

other as SpringAnalyzerParams

if (!(classpath contentDeepEquals other.classpath)) return false
if (configuration != other.configuration) return false
if (fileStorage != other.fileStorage) return false
if (!(fileStorage contentDeepEquals other.fileStorage)) return false
if (profileExpression != other.profileExpression) return false

return true
}
//hash code trait
override fun hashCode(): Int {
var __r = 0
__r = __r*31 + classpath.contentDeepHashCode()
__r = __r*31 + configuration.hashCode()
__r = __r*31 + if (fileStorage != null) fileStorage.hashCode() else 0
__r = __r*31 + fileStorage.contentDeepHashCode()
__r = __r*31 + if (profileExpression != null) profileExpression.hashCode() else 0
return __r
}
//pretty print
override fun print(printer: PrettyPrinter) {
printer.println("SpringAnalyzerParams (")
printer.indent {
print("classpath = "); classpath.print(printer); println()
print("configuration = "); configuration.print(printer); println()
print("fileStorage = "); fileStorage.print(printer); println()
print("profileExpression = "); profileExpression.print(printer); println()
}
printer.print(")")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ class SpringAnalyzerProcess private constructor(
private val loggerModel: LoggerModel = onSchedulerBlocking { protocol.loggerModel }

fun getBeanQualifiedNames(
classpath: List<String>,
configuration: String,
fileStorage: String?,
fileStorage: Array<String>,
profileExpression: String?,
): List<String> {
val params = SpringAnalyzerParams(classpath.toTypedArray(), configuration, fileStorage)
val params = SpringAnalyzerParams(configuration, fileStorage, profileExpression)
val result = springAnalyzerModel.analyze.startBlocking(params)
return result.beanTypes.toList()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ suspend fun main(args: Array<String>) =
private fun SpringAnalyzerProcessModel.setup(watchdog: IdleWatchdog, realProtocol: IProtocol) {
watchdog.measureTimeForActiveCall(analyze, "Analyzing Spring Application") { params ->
val applicationData = ApplicationData(
params.classpath.toList().map { File(it).toURI().toURL() }.toTypedArray(),
params.configuration,
params.fileStorage,
params.fileStorage.map { File(it).toURI().toURL() },
params.profileExpression,
)
SpringAnalyzerResult(
SpringApplicationAnalyzer(applicationData).analyze().toTypedArray()
Expand Down