Skip to content

Commit eabd6d5

Browse files
committed
Address comments in #2447
1 parent 8787427 commit eabd6d5

File tree

25 files changed

+134
-105
lines changed

25 files changed

+134
-105
lines changed

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/Api.kt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -576,9 +576,20 @@ data class UtArrayModel(
576576
override fun hashCode(): Int = id
577577
}
578578

579-
interface UtModelWithOrigin {
580-
val origin: UtCompositeModel?
581-
}
579+
/**
580+
* Wrapper of [origin] model, that can be handled in a different
581+
* way in some situations (e.g. during value construction).
582+
*/
583+
sealed class UtModelWithCompositeOrigin(
584+
id: Int?,
585+
classId: ClassId,
586+
modelName: String = id.toString(),
587+
open val origin: UtCompositeModel?,
588+
) : UtReferenceModel(
589+
id = id,
590+
classId = classId,
591+
modelName = modelName
592+
)
582593

583594
/**
584595
* Model for complex objects with assemble instructions.
@@ -595,7 +606,7 @@ data class UtAssembleModel private constructor(
595606
val instantiationCall: UtStatementCallModel,
596607
val modificationsChain: List<UtStatementModel>,
597608
override val origin: UtCompositeModel?
598-
) : UtReferenceModel(id, classId, modelName), UtModelWithOrigin {
609+
) : UtModelWithCompositeOrigin(id, classId, modelName, origin) {
599610

600611
/**
601612
* Creates a new [UtAssembleModel].
@@ -737,11 +748,11 @@ class UtLambdaModel(
737748
* Common parent of all framework-specific models (e.g. Spring-specific models)
738749
*/
739750
abstract class UtCustomModel(
740-
override val origin: UtCompositeModel? = null,
741751
id: Int?,
742752
classId: ClassId,
743-
modelName: String = id.toString()
744-
) : UtReferenceModel(id, classId, modelName), UtModelWithOrigin
753+
modelName: String = id.toString(),
754+
override val origin: UtCompositeModel? = null,
755+
) : UtModelWithCompositeOrigin(id, classId, modelName, origin)
745756

746757
object UtSpringContextModel : UtCustomModel(
747758
id = null,

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/SpringModelUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ object SpringModelUtils {
109109
private val mockMvcRequestBuildersClassId = ClassId("org.springframework.test.web.servlet.request.MockMvcRequestBuilders")
110110
private val requestBuilderClassId = ClassId("org.springframework.test.web.servlet.RequestBuilder")
111111
val resultActionsClassId = ClassId("org.springframework.test.web.servlet.ResultActions")
112-
private val mockMvcClassId = ClassId("org.springframework.test.web.servlet.MockMvc")
112+
val mockMvcClassId = ClassId("org.springframework.test.web.servlet.MockMvc")
113113
private val mvcResultClassId = ClassId("org.springframework.test.web.servlet.MvcResult")
114114
private val resultHandlerClassId = ClassId("org.springframework.test.web.servlet.ResultHandler")
115115
val mockMvcResultHandlersClassId = ClassId("org.springframework.test.web.servlet.result.MockMvcResultHandlers")

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import org.utbot.framework.plugin.api.ClassId
1111
import org.utbot.framework.plugin.api.UtAssembleModel
1212
import org.utbot.framework.plugin.api.UtCompositeModel
1313
import org.utbot.framework.plugin.api.UtModel
14-
import org.utbot.framework.plugin.api.UtModelWithOrigin
14+
import org.utbot.framework.plugin.api.UtModelWithCompositeOrigin
1515
import org.utbot.framework.plugin.api.isMockModel
1616
import org.utbot.framework.plugin.api.util.SpringModelUtils.autowiredClassId
1717
import org.utbot.framework.plugin.api.util.SpringModelUtils.isAutowiredFromContext
@@ -48,7 +48,7 @@ class CgInjectingMocksFieldsManager(val context: CgContext) : CgClassFieldManage
4848
override fun constructVariableForField(model: UtModel, modelVariable: CgValue): CgValue {
4949
val modelFields = when (model) {
5050
is UtCompositeModel -> model.fields
51-
is UtModelWithOrigin -> model.origin?.fields
51+
is UtModelWithCompositeOrigin -> model.origin?.fields
5252
else -> null
5353
}
5454

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ import org.utbot.framework.plugin.api.UtExecutionSuccess
100100
import org.utbot.framework.plugin.api.UtExplicitlyThrownException
101101
import org.utbot.framework.plugin.api.UtLambdaModel
102102
import org.utbot.framework.plugin.api.UtModel
103-
import org.utbot.framework.plugin.api.UtModelWithOrigin
103+
import org.utbot.framework.plugin.api.UtModelWithCompositeOrigin
104104
import org.utbot.framework.plugin.api.UtNewInstanceInstrumentation
105105
import org.utbot.framework.plugin.api.UtNullModel
106106
import org.utbot.framework.plugin.api.UtOverflowFailure
@@ -1069,7 +1069,7 @@ open class CgMethodConstructor(val context: CgContext) : CgContextOwner by conte
10691069
when (model) {
10701070
is UtCompositeModel -> collectExecutionsResultFieldsRecursively(model, 0)
10711071

1072-
is UtModelWithOrigin -> model.origin?.let {
1072+
is UtModelWithCompositeOrigin -> model.origin?.let {
10731073
collectExecutionsResultFieldsRecursively(it, 0)
10741074
}
10751075

@@ -1109,7 +1109,7 @@ open class CgMethodConstructor(val context: CgContext) : CgContextOwner by conte
11091109
when (fieldModel) {
11101110
is UtCompositeModel -> collectExecutionsResultFieldsRecursively(fieldModel, depth + 1)
11111111

1112-
is UtModelWithOrigin -> fieldModel.origin?.let {
1112+
is UtModelWithCompositeOrigin -> fieldModel.origin?.let {
11131113
collectExecutionsResultFieldsRecursively(it, depth + 1)
11141114
}
11151115

@@ -1201,7 +1201,10 @@ open class CgMethodConstructor(val context: CgContext) : CgContextOwner by conte
12011201
expectedVariableName: String = "expected",
12021202
emptyLineIfNeeded: Boolean = false,
12031203
) {
1204-
if (expected !is UtCustomModel || !customAssertConstructor.tryConstructCustomAssert(expected, actual)) {
1204+
val successfullyConstructedCustomAssert = expected is UtCustomModel &&
1205+
customAssertConstructor.tryConstructCustomAssert(expected, actual)
1206+
1207+
if (!successfullyConstructedCustomAssert) {
12051208
val expectedVariable = variableConstructor.getOrCreateVariable(expected, expectedVariableName)
12061209
if (emptyLineIfNeeded) emptyLineIfNeeded()
12071210
assertEquality(expectedVariable, actual)

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ package org.utbot.framework.codegen.tree
22

33
import org.utbot.framework.codegen.domain.context.CgContext
44
import org.utbot.framework.codegen.domain.context.CgContextOwner
5-
import org.utbot.framework.codegen.domain.models.AnnotationTarget
65
import org.utbot.framework.codegen.domain.models.CgVariable
76
import org.utbot.framework.codegen.services.access.CgCallableAccessManager
87
import org.utbot.framework.codegen.services.access.CgCallableAccessManagerImpl
98
import org.utbot.framework.codegen.tree.CgComponents.getStatementConstructorBy
109
import org.utbot.framework.plugin.api.UtCustomModel
1110
import org.utbot.framework.plugin.api.UtSpringMockMvcResultActionsModel
12-
import org.utbot.framework.plugin.api.util.SpringModelUtils.autoConfigureMockMvcClassId
1311
import org.utbot.framework.plugin.api.util.SpringModelUtils.contentMatchersStringMethodId
1412
import org.utbot.framework.plugin.api.util.SpringModelUtils.mockMvcResultHandlersClassId
1513
import org.utbot.framework.plugin.api.util.SpringModelUtils.mockMvcResultMatchersClassId
@@ -34,20 +32,18 @@ class CgMockMvcResultActionsAssertConstructor(
3432
CgCallableAccessManager by CgCallableAccessManagerImpl(context) {
3533
override fun tryConstructCustomAssert(expected: UtCustomModel, actual: CgVariable): Boolean {
3634
if (expected is UtSpringMockMvcResultActionsModel) {
37-
addAnnotation(autoConfigureMockMvcClassId, AnnotationTarget.Class)
38-
var expr = actual[resultActionsAndDoMethodId](mockMvcResultHandlersClassId[resultHandlersPrintMethodId]())
39-
expr = expr[resultActionsAndExpectMethodId](
35+
+actual[resultActionsAndDoMethodId](mockMvcResultHandlersClassId[resultHandlersPrintMethodId]())
36+
+actual[resultActionsAndExpectMethodId](
4037
mockMvcResultMatchersClassId[resultMatchersStatusMethodId]()[statusMatchersIsMethodId](expected.status)
4138
)
4239
expected.viewName?.let { viewName ->
43-
expr = expr[resultActionsAndExpectMethodId](
40+
+actual[resultActionsAndExpectMethodId](
4441
mockMvcResultMatchersClassId[resultMatchersViewMethodId]()[viewMatchersNameMethodId](viewName)
4542
)
4643
}
47-
expr = expr[resultActionsAndExpectMethodId](
44+
+actual[resultActionsAndExpectMethodId](
4845
mockMvcResultMatchersClassId[resultMatchersContentMethodId]()[contentMatchersStringMethodId](expected.contentAsString)
4946
)
50-
+expr
5147
return true
5248
} else
5349
return delegateAssertConstructor.tryConstructCustomAssert(expected, actual)

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import org.utbot.framework.plugin.api.ConcreteContextLoadingResult
1717
import org.utbot.framework.plugin.api.SpringSettings.*
1818
import org.utbot.framework.plugin.api.SpringConfiguration.*
1919
import org.utbot.framework.plugin.api.util.IndentUtil.TAB
20+
import org.utbot.framework.plugin.api.util.SpringModelUtils
2021
import org.utbot.framework.plugin.api.util.SpringModelUtils.activeProfilesClassId
2122
import org.utbot.framework.plugin.api.util.SpringModelUtils.autoConfigureTestDbClassId
2223
import org.utbot.framework.plugin.api.util.SpringModelUtils.bootstrapWithClassId
@@ -25,6 +26,7 @@ import org.utbot.framework.plugin.api.util.SpringModelUtils.crudRepositoryClassI
2526
import org.utbot.framework.plugin.api.util.SpringModelUtils.dirtiesContextClassId
2627
import org.utbot.framework.plugin.api.util.SpringModelUtils.dirtiesContextClassModeClassId
2728
import org.utbot.framework.plugin.api.util.SpringModelUtils.extendWithClassId
29+
import org.utbot.framework.plugin.api.util.SpringModelUtils.mockMvcClassId
2830
import org.utbot.framework.plugin.api.util.SpringModelUtils.runWithClassId
2931
import org.utbot.framework.plugin.api.util.SpringModelUtils.springBootTestClassId
3032
import org.utbot.framework.plugin.api.util.SpringModelUtils.springBootTestContextBootstrapperClassId
@@ -47,7 +49,7 @@ class CgSpringIntegrationTestClassConstructor(
4749
}
4850

4951
override fun constructTestClass(testClassModel: SpringTestClassModel): CgClass {
50-
addNecessarySpringSpecificAnnotations()
52+
addNecessarySpringSpecificAnnotations(testClassModel)
5153
return super.constructTestClass(testClassModel)
5254
}
5355

@@ -102,7 +104,7 @@ class CgSpringIntegrationTestClassConstructor(
102104
.map { it.escapeControlChars() }
103105
)
104106

105-
private fun addNecessarySpringSpecificAnnotations() {
107+
private fun addNecessarySpringSpecificAnnotations(testClassModel: SpringTestClassModel) {
106108
val isSpringBootTestAccessible = utContext.classLoader.tryLoadClass(springBootTestClassId.name) != null
107109
if (isSpringBootTestAccessible) {
108110
addAnnotation(springBootTestClassId, Class)
@@ -193,5 +195,8 @@ class CgSpringIntegrationTestClassConstructor(
193195
// generated tests will fail with `ClassNotFoundException: org.springframework.dao.DataAccessException`.
194196
if (utContext.classLoader.tryLoadClass(crudRepositoryClassId.name) != null)
195197
addAnnotation(autoConfigureTestDbClassId, Class)
198+
199+
if (mockMvcClassId in testClassModel.springSpecificInformation.autowiredFromContextModels)
200+
addAnnotation(SpringModelUtils.autoConfigureMockMvcClassId, Class)
196201
}
197202
}

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

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

3+
import mu.KotlinLogging
34
import org.utbot.common.isStatic
45
import org.utbot.framework.codegen.domain.builtin.forName
56
import org.utbot.framework.codegen.domain.builtin.setArrayElement
@@ -68,6 +69,10 @@ open class CgVariableConstructor(val context: CgContext) :
6869
CgCallableAccessManager by getCallableAccessManagerBy(context),
6970
CgStatementConstructor by getStatementConstructorBy(context) {
7071

72+
companion object {
73+
private val logger = KotlinLogging.logger {}
74+
}
75+
7176
private val nameGenerator = getNameGeneratorBy(context)
7277
val mockFrameworkManager = getMockFrameworkManagerBy(context)
7378

@@ -99,7 +104,7 @@ open class CgVariableConstructor(val context: CgContext) :
99104
constructValueByModel(model, name)
100105
}
101106

102-
open fun constructValueByModel(model: UtModel, name: String?): CgValue {
107+
private fun constructValueByModel(model: UtModel, name: String?): CgValue {
103108
// name could be taken from existing names, or be specified manually, or be created from generator
104109
val baseName = name ?: nameGenerator.nameFrom(model.classId)
105110

@@ -112,7 +117,12 @@ open class CgVariableConstructor(val context: CgContext) :
112117
is UtLambdaModel -> constructLambda(model, baseName)
113118
is UtNullModel -> nullLiteral()
114119
is UtPrimitiveModel -> CgLiteral(model.classId, model.value)
115-
is UtCustomModel -> constructValueByModel(model.origin ?: error("Can't construct value for custom model without origin [$model]"), name)
120+
is UtCustomModel -> {
121+
logger.error { "Unexpected behaviour: value for UtCustomModel [$model] is constructed by base CgVariableConstructor" }
122+
constructValueByModel(
123+
model.origin ?: error("Can't construct value for UtCustomModel without origin [$model]"), name
124+
)
125+
}
116126
is UtReferenceModel -> error("Unexpected UtReferenceModel: ${model::class}")
117127
is UtVoidModel -> error("Unexpected UtVoidModel: ${model::class}")
118128
else -> error("Unexpected UtModel: ${model::class}")

utbot-framework/src/main/kotlin/org/utbot/framework/fields/ExecutionStateAnalyzer.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import org.utbot.framework.plugin.api.UtEnumConstantModel
1515
import org.utbot.framework.plugin.api.UtExecution
1616
import org.utbot.framework.plugin.api.UtLambdaModel
1717
import org.utbot.framework.plugin.api.UtModel
18-
import org.utbot.framework.plugin.api.UtModelWithOrigin
18+
import org.utbot.framework.plugin.api.UtModelWithCompositeOrigin
1919
import org.utbot.framework.plugin.api.UtNullModel
2020
import org.utbot.framework.plugin.api.UtPrimitiveModel
2121
import org.utbot.framework.plugin.api.UtReferenceModel
@@ -106,15 +106,15 @@ class ExecutionStateAnalyzer(val execution: UtExecution) {
106106
var modelBefore = before
107107

108108
if (before::class != after::class) {
109-
if (before is UtModelWithOrigin && after is UtModelWithOrigin && before.origin != null) {
109+
if (before is UtModelWithCompositeOrigin && after is UtModelWithCompositeOrigin && before.origin != null) {
110110
modelBefore = before.origin ?: unreachableBranch("We have already checked the origin for a null value")
111111
} else {
112112
doNotRun {
113113
// it is ok because we might have modelBefore with some absent fields (i.e. statics), but
114114
// modelAfter (constructed by concrete executor) will consist all these fields,
115115
// therefore, AssembleModelGenerator won't be able to transform the given composite model
116116

117-
val reason = if (before is UtModelWithOrigin && after is UtCompositeModel) {
117+
val reason = if (before is UtModelWithCompositeOrigin && after is UtCompositeModel) {
118118
"ModelBefore is an UtModelWithOrigin and ModelAfter " +
119119
"is a CompositeModel, but modelBefore doesn't have an origin model."
120120
} else {

utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/SimpleUtExecutionInstrumentation.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class SimpleUtExecutionInstrumentation(
131131
delegateInstrumentation.getStaticField(fieldId).map { value ->
132132
UtModelConstructor.createOnlyUserClassesConstructor(
133133
pathsToUserClasses = pathsToUserClasses,
134-
utCustomModelConstructorFinder = instrumentationContext::findUtCustomModelConstructor
134+
utModelWithCompositeOriginConstructorFinder = instrumentationContext::findUtModelWithCompositeOriginConstructor
135135
).construct(value, fieldId.type)
136136
}
137137

utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/constructors/JavaStdLibCustomModelConstructors.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import org.utbot.framework.plugin.api.util.jClass
44
import org.utbot.framework.plugin.api.util.primitiveWrappers
55
import org.utbot.framework.plugin.api.util.voidWrapperClassId
66

7-
val javaStdLibCustomModelConstructors: Map<Class<*>, () -> UtCustomModelConstructor> =
7+
val javaStdLibModelWithCompositeOriginConstructors: Map<Class<*>, () -> UtModelWithCompositeOriginConstructor> =
88
mutableMapOf<Class<*>, () -> UtAssembleModelConstructorBase>(
99
/**
1010
* Optionals

utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/constructors/UtAssembleModelConstructors.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ internal fun findStreamConstructor(stream: BaseStream<*, *>): UtAssembleModelCon
1818
else -> BaseStreamConstructor()
1919
}
2020

21-
internal abstract class UtAssembleModelConstructorBase : UtCustomModelConstructor {
22-
override fun constructCustomModel(
21+
internal abstract class UtAssembleModelConstructorBase : UtModelWithCompositeOriginConstructor {
22+
override fun constructModelWithCompositeOrigin(
2323
internalConstructor: UtModelConstructorInterface,
2424
value: Any,
2525
valueClassId: ClassId,

utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/constructors/UtCustomModelConstructor.kt

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

0 commit comments

Comments
 (0)