Skip to content

Commit 4b69047

Browse files
committed
Introduce autowired class variables
1 parent f2c6c56 commit 4b69047

7 files changed

Lines changed: 53 additions & 15 deletions

File tree

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/builtin/SpringBuiltins.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@ import org.utbot.framework.plugin.api.BuiltinClassId
55
internal val autowiredClassId = BuiltinClassId(
66
canonicalName = "org.springframework.beans.factory.annotation",
77
simpleName = "Autowired",
8+
)
9+
10+
internal val repositoryClassId = BuiltinClassId(
11+
canonicalName = "org.springframework.data.repository.Repository",
12+
simpleName = "Repository"
813
)

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.utbot.framework.codegen.domain.models
22

3+
import org.utbot.framework.codegen.domain.UtModelWrapper
34
import org.utbot.framework.codegen.domain.models.builders.TypedModelWrappers
45
import org.utbot.framework.plugin.api.ClassId
56

@@ -22,15 +23,17 @@ class SimpleTestClassModel(
2223

2324
/**
2425
* Extended [SimpleTestClassModel] for Spring analysis reasons
25-
*
26-
* @param injectingMocksClass a class to inject other mocks into
27-
* @param mockedClasses variables of test class to represent mocked instances
2826
*/
2927
class SpringTestClassModel(
3028
classUnderTest: ClassId,
3129
methodTestSets: List<CgMethodTestSet>,
3230
nestedClasses: List<SimpleTestClassModel>,
33-
val thisInstanceModels: TypedModelWrappers = mapOf(),
34-
val thisInstanceDependentMocks: TypedModelWrappers = mapOf(),
31+
val springSpecificInformation: SpringSpecificInformation,
3532
): TestClassModel(classUnderTest, methodTestSets, nestedClasses)
3633

34+
35+
class SpringSpecificInformation(
36+
val thisInstanceModels: TypedModelWrappers = mapOf(),
37+
val thisInstanceDependentMocks: TypedModelWrappers = mapOf(),
38+
val repositoryModels: TypedModelWrappers = mapOf(),
39+
)

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package org.utbot.framework.codegen.domain.models.builders
22

3+
import com.jetbrains.rd.framework.base.deepClonePolymorphic
34
import org.utbot.framework.codegen.domain.UtModelWrapper
5+
import org.utbot.framework.codegen.domain.builtin.repositoryClassId
46
import org.utbot.framework.codegen.domain.context.CgContext
57
import org.utbot.framework.codegen.domain.models.CgMethodTestSet
8+
import org.utbot.framework.codegen.domain.models.SpringSpecificInformation
69
import org.utbot.framework.codegen.domain.models.SpringTestClassModel
710
import org.utbot.framework.plugin.api.ClassId
811
import org.utbot.framework.plugin.api.UtArrayModel
@@ -25,20 +28,24 @@ class SpringTestClassModelBuilder(val context: CgContext): TestClassModelBuilder
2528

2629
override fun createTestClassModel(classUnderTest: ClassId, testSets: List<CgMethodTestSet>): SpringTestClassModel {
2730
val baseModel = SimpleTestClassModelBuilder(context).createTestClassModel(classUnderTest, testSets)
28-
val (thisInstanceModels, dependentMockModels) = collectThisInstanceAndDependentModels(testSets)
31+
val collectedModels = collectModelsForClassVariables(testSets)
2932

3033
return SpringTestClassModel(
3134
classUnderTest = baseModel.classUnderTest,
3235
methodTestSets = baseModel.methodTestSets,
3336
nestedClasses = baseModel.nestedClasses,
34-
thisInstanceModels = thisInstanceModels,
35-
thisInstanceDependentMocks = dependentMockModels
37+
springSpecificInformation = SpringSpecificInformation(
38+
thisInstanceModels = collectedModels.thisInstanceModels,
39+
thisInstanceDependentMocks = collectedModels.thisInstanceDependentMocks,
40+
repositoryModels = collectedModels.repositoryModels,
41+
)
3642
)
3743
}
3844

39-
private fun collectThisInstanceAndDependentModels(testSets: List<CgMethodTestSet>): Pair<TypedModelWrappers, TypedModelWrappers> {
45+
private fun collectModelsForClassVariables(testSets: List<CgMethodTestSet>): SpringSpecificInformation {
4046
val thisInstanceModels = mutableSetOf<UtModelWrapper>()
4147
val thisInstancesDependentModels = mutableSetOf<UtModelWrapper>()
48+
val repositoryModels = mutableSetOf<UtModelWrapper>()
4249

4350
with(context) {
4451
for ((testSetIndex, testSet) in testSets.withIndex()) {
@@ -51,6 +58,7 @@ class SpringTestClassModelBuilder(val context: CgContext): TestClassModelBuilder
5158
thisInstanceModels += model.wrap()
5259
thisInstancesDependentModels += collectByThisInstanceModel(model)
5360
}
61+
// TODO: collect repository models from execution after merging prototype branch
5462
}
5563
}
5664
}
@@ -63,7 +71,11 @@ class SpringTestClassModelBuilder(val context: CgContext): TestClassModelBuilder
6371
cgModel.model.isMockModel() && cgModel !in thisInstanceModels
6472
}
6573

66-
return thisInstanceModels.groupByClassId() to dependentMockModels.groupByClassId()
74+
return SpringSpecificInformation(
75+
thisInstanceModels.groupByClassId(),
76+
dependentMockModels.groupByClassId(),
77+
mapOf(repositoryClassId to repositoryModels),
78+
)
6779
}
6880

6981
private fun collectByThisInstanceModel(model: UtModel): Set<UtModelWrapper> {

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgAbstractSpringTestClassConstructor.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.utbot.framework.codegen.tree
22

33
import org.utbot.framework.codegen.domain.builtin.TestClassUtilMethodProvider
4+
import org.utbot.framework.codegen.domain.builtin.autowiredClassId
45
import org.utbot.framework.codegen.domain.builtin.injectMocksClassId
56
import org.utbot.framework.codegen.domain.builtin.mockClassId
67
import org.utbot.framework.codegen.domain.context.CgContext
@@ -84,8 +85,8 @@ abstract class CgAbstractSpringTestClassConstructor(context: CgContext):
8485
abstract fun constructAdditionalMethods(): CgMethodsCluster
8586

8687
protected fun constructFieldsWithAnnotation(
88+
annotationClassId: ClassId,
8789
groupedModelsByClassId: TypedModelWrappers,
88-
annotationClassId: ClassId
8990
): List<CgFieldDeclaration> {
9091
require(annotationClassId == injectMocksClassId || annotationClassId == mockClassId) {
9192
error("Unexpected annotation classId -- $annotationClassId")
@@ -109,6 +110,7 @@ abstract class CgAbstractSpringTestClassConstructor(context: CgContext):
109110
when (annotationClassId) {
110111
injectMocksClassId -> variableConstructor.injectedMocksModelsVariables += listOfUtModels
111112
mockClassId -> variableConstructor.mockedModelsVariables += listOfUtModels
113+
autowiredClassId -> variableConstructor.autowiredVariables += listOfUtModels
112114
}
113115
}
114116

@@ -129,7 +131,8 @@ abstract class CgAbstractSpringTestClassConstructor(context: CgContext):
129131
val whiteListOfModels =
130132
listOf(
131133
variableConstructor.mockedModelsVariables,
132-
variableConstructor.injectedMocksModelsVariables
134+
variableConstructor.injectedMocksModelsVariables,
135+
variableConstructor.autowiredVariables,
133136
).flatten()
134137

135138
valueByUtModelWrapper

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgSpringIntegrationTestClassConstructor.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ import org.utbot.framework.codegen.domain.models.SpringTestClassModel
99
class CgSpringIntegrationTestClassConstructor(context: CgContext) : CgAbstractSpringTestClassConstructor(context) {
1010

1111
override fun constructClassFields(testClassModel: SpringTestClassModel): List<CgFieldDeclaration> {
12-
return constructFieldsWithAnnotation(testClassModel.thisInstanceModels, autowiredClassId)
12+
val thisIntances = testClassModel.springSpecificInformation.thisInstanceModels
13+
val repositories = testClassModel.springSpecificInformation.repositoryModels
14+
15+
val fields = mutableListOf<CgFieldDeclaration>()
16+
fields += constructFieldsWithAnnotation(autowiredClassId, thisIntances)
17+
fields += constructFieldsWithAnnotation(autowiredClassId, repositories)
18+
19+
return fields
1320
}
1421

1522
override fun constructAdditionalMethods() = CgMethodsCluster(

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgSpringUnitTestClassConstructor.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ class CgSpringUnitTestClassConstructor(context: CgContext) : CgAbstractSpringTes
2727

2828
override fun constructClassFields(testClassModel: SpringTestClassModel): List<CgFieldDeclaration> {
2929
val fields = mutableListOf<CgFieldDeclaration>()
30+
val mocks = testClassModel.springSpecificInformation.thisInstanceDependentMocks
31+
val mockedFields = constructFieldsWithAnnotation(mockClassId, mocks)
3032

3133
if (testClassModel.thisInstanceDependentMocks.isNotEmpty()) {
32-
val mockedFields = constructFieldsWithAnnotation(testClassModel.thisInstanceDependentMocks, mockClassId)
33-
val injectingMocksFields = constructFieldsWithAnnotation(testClassModel.thisInstanceModels, injectMocksClassId)
34+
val mockedFields = constructFieldsWithAnnotation(mockClassId, mocks)
35+
val injectingMocksFields = constructFieldsWithAnnotation(injectMocksClassId, thisInstances)
3436

3537
fields += injectingMocksFields
3638
fields += mockedFields

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgSpringVariableConstructor.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import org.utbot.framework.plugin.api.isMockModel
1212
class CgSpringVariableConstructor(context: CgContext) : CgVariableConstructor(context) {
1313
val injectedMocksModelsVariables: MutableSet<UtModelWrapper> = mutableSetOf()
1414
val mockedModelsVariables: MutableSet<UtModelWrapper> = mutableSetOf()
15+
val autowiredVariables: MutableSet<UtModelWrapper> = mutableSetOf()
1516

1617
override fun getOrCreateVariable(model: UtModel, name: String?): CgValue {
1718
val alreadyCreatedInjectMocks = findCgValueByModel(model, injectedMocksModelsVariables)
@@ -39,6 +40,11 @@ class CgSpringVariableConstructor(context: CgContext) : CgVariableConstructor(co
3940
return alreadyCreatedMock
4041
}
4142

43+
val alreadyCreatedAutowired = findCgValueByModel(model, autowiredVariables)
44+
if (alreadyCreatedAutowired != null) {
45+
return alreadyCreatedAutowired
46+
}
47+
4248
return super.getOrCreateVariable(model, name)
4349
}
4450

0 commit comments

Comments
 (0)