Skip to content

Commit a6842f7

Browse files
authored
Recursive collecting of Autowired models returned #2473 (#2478)
1 parent f28ce77 commit a6842f7

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/models/builders/SpringTestClassModelBuilder.kt

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import org.utbot.framework.plugin.api.UtNullModel
1919
import org.utbot.framework.plugin.api.UtPrimitiveModel
2020
import org.utbot.framework.plugin.api.UtVoidModel
2121
import org.utbot.framework.plugin.api.isMockModel
22+
import org.utbot.framework.plugin.api.UtStatementCallModel
23+
import org.utbot.framework.plugin.api.UtDirectSetFieldModel
2224
import org.utbot.framework.plugin.api.util.SpringModelUtils.isAutowiredFromContext
2325
import org.utbot.framework.plugin.api.canBeSpied
2426

@@ -59,7 +61,7 @@ class SpringTestClassModelBuilder(val context: CgContext) :
5961

6062
(execution.stateBefore.parameters + execution.stateBefore.thisInstance)
6163
.filterNotNull()
62-
.forEach { model -> stateBeforeDependentModels += collectDependentModels(model) }
64+
.forEach { model -> stateBeforeDependentModels += collectAutowiredModels(model) }
6365
}
6466
}
6567
}
@@ -88,11 +90,24 @@ class SpringTestClassModelBuilder(val context: CgContext) :
8890
)
8991
}
9092

93+
private fun collectAutowiredModels(model: UtModel): Set<UtModelWrapper> {
94+
val allDependentModels = mutableSetOf<UtModelWrapper>()
95+
96+
collectRecursively(model, allDependentModels)
97+
98+
return allDependentModels
99+
}
100+
101+
private fun collectRecursively(model: UtModel, allDependentModels: MutableSet<UtModelWrapper>){
102+
if(!allDependentModels.add(model.wrap())){
103+
return
104+
}
105+
collectDependentModels(model).forEach { collectRecursively(it.model, allDependentModels) }
106+
}
107+
91108
private fun collectDependentModels(model: UtModel): Set<UtModelWrapper> {
92109
val dependentModels = mutableSetOf<UtModelWrapper>()
93110

94-
dependentModels.add(model.wrap())
95-
96111
when (model) {
97112
is UtNullModel,
98113
is UtPrimitiveModel,
@@ -118,6 +133,16 @@ class SpringTestClassModelBuilder(val context: CgContext) :
118133
is UtAssembleModel -> {
119134
model.instantiationCall.instance?.let { dependentModels.add(it.wrap()) }
120135
model.instantiationCall.params.forEach { dependentModels.add(it.wrap()) }
136+
137+
if(model.isAutowiredFromContext()) {
138+
model.modificationsChain.forEach { stmt ->
139+
stmt.instance?.let { dependentModels.add(it.wrap()) }
140+
when (stmt) {
141+
is UtStatementCallModel -> stmt.params.forEach { dependentModels.add(it.wrap()) }
142+
is UtDirectSetFieldModel -> dependentModels.add(stmt.fieldModel.wrap())
143+
}
144+
}
145+
}
121146
}
122147
}
123148

0 commit comments

Comments
 (0)