From 26a2f7235f19ac704cd4f59c4854771b26dd7676 Mon Sep 17 00:00:00 2001 From: Kirill Shishin Date: Thu, 3 Aug 2023 17:43:12 +0300 Subject: [PATCH 1/2] Add recursively collecting of Autowiring dependent models --- .../builders/SpringTestClassModelBuilder.kt | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/models/builders/SpringTestClassModelBuilder.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/models/builders/SpringTestClassModelBuilder.kt index edf3ea6593..e4b51333fb 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/models/builders/SpringTestClassModelBuilder.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/models/builders/SpringTestClassModelBuilder.kt @@ -59,7 +59,7 @@ class SpringTestClassModelBuilder(val context: CgContext) : (execution.stateBefore.parameters + execution.stateBefore.thisInstance) .filterNotNull() - .forEach { model -> stateBeforeDependentModels += collectDependentModels(model) } + .forEach { model -> stateBeforeDependentModels += collectAutowiredModels(model) } } } } @@ -88,11 +88,24 @@ class SpringTestClassModelBuilder(val context: CgContext) : ) } + private fun collectAutowiredModels(model: UtModel): Set { + val allDependentModels = mutableSetOf() + + collectRecursively(model, allDependentModels) + + return allDependentModels + } + + private fun collectRecursively(model: UtModel, allDependentModels: MutableSet){ + if(!allDependentModels.add(model.wrap())){ + return + } + collectDependentModels(model).forEach { collectRecursively(it.model, allDependentModels) } + } + private fun collectDependentModels(model: UtModel): Set { val dependentModels = mutableSetOf() - dependentModels.add(model.wrap()) - when (model) { is UtNullModel, is UtPrimitiveModel, From caeb7632a61670e89a196df569f0a7b6e7f65ff2 Mon Sep 17 00:00:00 2001 From: Kirill Shishin Date: Fri, 4 Aug 2023 00:42:17 +0300 Subject: [PATCH 2/2] Add collecting modificationsChain fields for `UtAssembleModel`s autowired from context --- .../models/builders/SpringTestClassModelBuilder.kt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/models/builders/SpringTestClassModelBuilder.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/models/builders/SpringTestClassModelBuilder.kt index e4b51333fb..a890671e6a 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/models/builders/SpringTestClassModelBuilder.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/models/builders/SpringTestClassModelBuilder.kt @@ -19,6 +19,8 @@ import org.utbot.framework.plugin.api.UtNullModel import org.utbot.framework.plugin.api.UtPrimitiveModel import org.utbot.framework.plugin.api.UtVoidModel import org.utbot.framework.plugin.api.isMockModel +import org.utbot.framework.plugin.api.UtStatementCallModel +import org.utbot.framework.plugin.api.UtDirectSetFieldModel import org.utbot.framework.plugin.api.util.SpringModelUtils.isAutowiredFromContext import org.utbot.framework.plugin.api.canBeSpied @@ -131,6 +133,16 @@ class SpringTestClassModelBuilder(val context: CgContext) : is UtAssembleModel -> { model.instantiationCall.instance?.let { dependentModels.add(it.wrap()) } model.instantiationCall.params.forEach { dependentModels.add(it.wrap()) } + + if(model.isAutowiredFromContext()) { + model.modificationsChain.forEach { stmt -> + stmt.instance?.let { dependentModels.add(it.wrap()) } + when (stmt) { + is UtStatementCallModel -> stmt.params.forEach { dependentModels.add(it.wrap()) } + is UtDirectSetFieldModel -> dependentModels.add(stmt.fieldModel.wrap()) + } + } + } } }