Skip to content

Commit 100c1f9

Browse files
committed
Make ApplicationContext be responsible for creating appropriate code generator
1 parent 3e9f385 commit 100c1f9

File tree

12 files changed

+76
-75
lines changed

12 files changed

+76
-75
lines changed

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,14 +1319,6 @@ enum class TypeReplacementMode {
13191319
NoImplementors,
13201320
}
13211321

1322-
interface CodeGenerationContext
1323-
1324-
interface SpringCodeGenerationContext : CodeGenerationContext {
1325-
val springTestType: SpringTestType
1326-
val springSettings: SpringSettings
1327-
val concreteContextLoadingResult: ConcreteContextLoadingResult?
1328-
}
1329-
13301322
sealed class SpringConfiguration(val fullDisplayName: String) {
13311323
class JavaConfiguration(val classBinaryName: String) : SpringConfiguration(classBinaryName)
13321324
class XMLConfiguration(val absolutePath: String) : SpringConfiguration(absolutePath)

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/generator/CodeGenerator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import org.utbot.framework.codegen.tree.ututils.UtilClassKind
77
import org.utbot.framework.plugin.api.ClassId
88

99
open class CodeGenerator(params: CodeGeneratorParams): AbstractCodeGenerator(params) {
10-
val classUnderTest: ClassId = params.classUnderTest
10+
protected val classUnderTest: ClassId = params.classUnderTest
1111

1212
override fun generate(testSets: List<CgMethodTestSet>): CodeGeneratorResult {
1313
val testClassModel = SimpleTestClassModelBuilder(context).createTestClassModel(classUnderTest, testSets)

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/generator/SpringCodeGenerator.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ import org.utbot.framework.codegen.tree.CgSpringVariableConstructor
1010
import org.utbot.framework.codegen.tree.CgVariableConstructor
1111
import org.utbot.framework.codegen.tree.ututils.UtilClassKind
1212
import org.utbot.framework.plugin.api.ClassId
13-
import org.utbot.framework.plugin.api.SpringCodeGenerationContext
13+
import org.utbot.framework.plugin.api.ConcreteContextLoadingResult
14+
import org.utbot.framework.plugin.api.SpringSettings
1415
import org.utbot.framework.plugin.api.SpringSettings.AbsentSpringSettings
1516
import org.utbot.framework.plugin.api.SpringSettings.PresentSpringSettings
1617
import org.utbot.framework.plugin.api.SpringTestType
1718

1819
class SpringCodeGenerator(
19-
val springCodeGenerationContext: SpringCodeGenerationContext,
20+
private val springTestType: SpringTestType,
21+
private val springSettings: SpringSettings,
22+
private val concreteContextLoadingResult: ConcreteContextLoadingResult?,
2023
params: CodeGeneratorParams
2124
) : AbstractCodeGenerator(
2225
params.copy(
@@ -27,17 +30,17 @@ class SpringCodeGenerator(
2730
}
2831
)
2932
) {
30-
val classUnderTest: ClassId = params.classUnderTest
33+
private val classUnderTest: ClassId = params.classUnderTest
3134

3235
override fun generate(testSets: List<CgMethodTestSet>): CodeGeneratorResult {
3336
val testClassModel = SpringTestClassModelBuilder(context).createTestClassModel(classUnderTest, testSets)
3437

3538
logger.info { "Code generation phase started at ${now()}" }
36-
val astConstructor = when (springCodeGenerationContext.springTestType) {
39+
val astConstructor = when (springTestType) {
3740
SpringTestType.UNIT_TEST -> CgSpringUnitTestClassConstructor(context)
3841
SpringTestType.INTEGRATION_TEST ->
39-
when (val settings = springCodeGenerationContext.springSettings) {
40-
is PresentSpringSettings -> CgSpringIntegrationTestClassConstructor(context, springCodeGenerationContext, settings)
42+
when (val settings = springSettings) {
43+
is PresentSpringSettings -> CgSpringIntegrationTestClassConstructor(context, concreteContextLoadingResult, settings)
4144
is AbsentSpringSettings -> error("No Spring settings were provided for Spring integration test generation.")
4245
}
4346
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ import org.utbot.framework.codegen.domain.models.CgTestMethodType.SUCCESSFUL
1313
import org.utbot.framework.codegen.util.escapeControlChars
1414
import org.utbot.framework.codegen.util.resolve
1515
import org.utbot.framework.plugin.api.ClassId
16-
import org.utbot.framework.plugin.api.SpringCodeGenerationContext
16+
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
2020
import org.utbot.framework.plugin.api.util.SpringModelUtils.activeProfilesClassId
2121
import org.utbot.framework.plugin.api.util.SpringModelUtils.autoConfigureTestDbClassId
22-
import org.utbot.framework.plugin.api.util.SpringModelUtils.autowiredClassId
2322
import org.utbot.framework.plugin.api.util.SpringModelUtils.bootstrapWithClassId
2423
import org.utbot.framework.plugin.api.util.SpringModelUtils.contextConfigurationClassId
2524
import org.utbot.framework.plugin.api.util.SpringModelUtils.crudRepositoryClassId
@@ -37,7 +36,7 @@ import org.utbot.spring.api.UTSpringContextLoadingException
3736

3837
class CgSpringIntegrationTestClassConstructor(
3938
context: CgContext,
40-
private val springCodeGenerationContext: SpringCodeGenerationContext,
39+
private val concreteContextLoadingResult: ConcreteContextLoadingResult?,
4140
private val springSettings: PresentSpringSettings,
4241
) : CgAbstractSpringTestClassConstructor(context) {
4342

@@ -64,21 +63,20 @@ class CgSpringIntegrationTestClassConstructor(
6463
)
6564

6665
private fun constructContextLoadsMethod() : CgTestMethod {
67-
val contextLoadingResult = springCodeGenerationContext.concreteContextLoadingResult
68-
if (contextLoadingResult == null)
66+
if (concreteContextLoadingResult == null)
6967
logger.error { "Missing contextLoadingResult" }
70-
val exception = contextLoadingResult?.exceptions?.firstOrNull()
68+
val exception = concreteContextLoadingResult?.exceptions?.firstOrNull()
7169
return CgTestMethod(
7270
name = "contextLoads",
7371
statements = listOfNotNull(
7472
exception?.let { e -> constructFailedContextLoadingTraceComment(e) },
75-
if (contextLoadingResult == null) CgSingleLineComment("Error: context loading result from concrete execution is missing") else null
73+
if (concreteContextLoadingResult == null) CgSingleLineComment("Error: context loading result from concrete execution is missing") else null
7674
),
7775
annotations = listOf(addAnnotation(context.testFramework.testAnnotationId, Method)),
7876
documentation = CgDocumentationComment(listOf(
7977
CgDocRegularLineStmt("This sanity check test fails if the application context cannot start.")
8078
) + exception?.let { constructFailedContextLoadingDocComment() }.orEmpty()),
81-
type = if (contextLoadingResult != null && exception == null) SUCCESSFUL else FAILING
79+
type = if (concreteContextLoadingResult != null && exception == null) SUCCESSFUL else FAILING
8280
)
8381
}
8482

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package org.utbot.framework.context
22

3-
import org.utbot.framework.plugin.api.CodeGenerationContext
3+
import org.utbot.framework.codegen.generator.AbstractCodeGenerator
4+
import org.utbot.framework.codegen.generator.CodeGeneratorParams
45

5-
interface ApplicationContext : CodeGenerationContext {
6+
interface ApplicationContext {
67
val mockerContext: MockerContext
78
val typeReplacer: TypeReplacer
89
val nonNullSpeculator: NonNullSpeculator
@@ -11,4 +12,6 @@ interface ApplicationContext : CodeGenerationContext {
1112
fullClasspath: String,
1213
classpathWithoutDependencies: String
1314
): ConcreteExecutionContext
15+
16+
fun createCodeGenerator(params: CodeGeneratorParams): AbstractCodeGenerator
1417
}

utbot-framework/src/main/kotlin/org/utbot/framework/context/simple/SimpleApplicationContext.kt

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

3+
import org.utbot.framework.codegen.generator.AbstractCodeGenerator
4+
import org.utbot.framework.codegen.generator.CodeGenerator
5+
import org.utbot.framework.codegen.generator.CodeGeneratorParams
36
import org.utbot.framework.context.ApplicationContext
47
import org.utbot.framework.context.ConcreteExecutionContext
58
import org.utbot.framework.context.MockerContext
@@ -18,4 +21,7 @@ class SimpleApplicationContext(
1821
fullClasspath: String,
1922
classpathWithoutDependencies: String
2023
): ConcreteExecutionContext = SimpleConcreteExecutionContext(fullClasspath)
24+
25+
override fun createCodeGenerator(params: CodeGeneratorParams): AbstractCodeGenerator =
26+
CodeGenerator(params)
2127
}

utbot-framework/src/main/kotlin/org/utbot/framework/context/spring/SpringApplicationContext.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@ package org.utbot.framework.context.spring
33
import org.utbot.framework.context.ApplicationContext
44
import org.utbot.framework.plugin.api.BeanDefinitionData
55
import org.utbot.framework.plugin.api.ClassId
6-
import org.utbot.framework.plugin.api.SpringCodeGenerationContext
76
import org.utbot.framework.plugin.api.ConcreteContextLoadingResult
7+
import org.utbot.framework.plugin.api.SpringSettings
88

99
/**
1010
* Data we get from Spring application context
1111
* to manage engine and code generator behaviour.
1212
*/
13-
// TODO #2358
14-
interface SpringApplicationContext : ApplicationContext, SpringCodeGenerationContext {
13+
interface SpringApplicationContext : ApplicationContext {
14+
val springSettings: SpringSettings
15+
1516
/**
1617
* Describes bean definitions (bean name, type, some optional additional data)
1718
*/
1819
val beanDefinitions: List<BeanDefinitionData>
1920
val injectedTypes: Set<ClassId>
2021
val allInjectedSuperTypes: Set<ClassId>
2122

22-
override var concreteContextLoadingResult: ConcreteContextLoadingResult?
23+
var concreteContextLoadingResult: ConcreteContextLoadingResult?
2324
fun getBeansAssignableTo(classId: ClassId): List<BeanDefinitionData>
2425
}

utbot-framework/src/main/kotlin/org/utbot/framework/context/spring/SpringApplicationContextImpl.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package org.utbot.framework.context.spring
33
import mu.KotlinLogging
44
import org.utbot.common.isAbstract
55
import org.utbot.common.isStatic
6+
import org.utbot.framework.codegen.generator.AbstractCodeGenerator
7+
import org.utbot.framework.codegen.generator.CodeGeneratorParams
8+
import org.utbot.framework.codegen.generator.SpringCodeGenerator
69
import org.utbot.framework.context.ApplicationContext
710
import org.utbot.framework.context.ConcreteExecutionContext
811
import org.utbot.framework.context.NonNullSpeculator
@@ -22,7 +25,7 @@ import org.utbot.framework.plugin.api.util.utContext
2225
class SpringApplicationContextImpl(
2326
private val delegateContext: ApplicationContext,
2427
override val beanDefinitions: List<BeanDefinitionData> = emptyList(),
25-
override val springTestType: SpringTestType,
28+
private val springTestType: SpringTestType,
2629
override val springSettings: SpringSettings,
2730
): ApplicationContext by delegateContext, SpringApplicationContext {
2831
companion object {
@@ -64,6 +67,15 @@ class SpringApplicationContextImpl(
6467
}
6568
}
6669

70+
override fun createCodeGenerator(params: CodeGeneratorParams): AbstractCodeGenerator =
71+
// TODO decorate original `delegateContext.createCodeGenerator(params)`
72+
SpringCodeGenerator(
73+
springTestType = springTestType,
74+
springSettings = springSettings,
75+
concreteContextLoadingResult = concreteContextLoadingResult,
76+
params = params,
77+
)
78+
6779
override fun getBeansAssignableTo(classId: ClassId): List<BeanDefinitionData> = beanDefinitions.filter { beanDef ->
6880
// some bean classes may fail to load
6981
runCatching {

utbot-framework/src/main/kotlin/org/utbot/framework/process/EngineProcessMain.kt

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ import org.utbot.framework.codegen.domain.ProjectType
1414
import org.utbot.framework.codegen.domain.RuntimeExceptionTestsBehaviour
1515
import org.utbot.framework.codegen.domain.testFrameworkByName
1616
import org.utbot.framework.codegen.generator.AbstractCodeGenerator
17-
import org.utbot.framework.codegen.generator.CodeGenerator
1817
import org.utbot.framework.codegen.generator.CodeGeneratorParams
19-
import org.utbot.framework.codegen.generator.SpringCodeGenerator
2018
import org.utbot.framework.codegen.reports.TestsGenerationReport
2119
import org.utbot.framework.codegen.services.language.CgLanguageAssistant
2220
import org.utbot.framework.context.ApplicationContext
@@ -291,7 +289,7 @@ private fun destinationWarningMessage(testPackageName: String?, classUnderTestPa
291289
}
292290
}
293291

294-
private fun createCodeGenerator(kryoHelper: KryoHelper, params: RenderParams, codeGenerationContext: CodeGenerationContext): AbstractCodeGenerator {
292+
private fun createCodeGenerator(kryoHelper: KryoHelper, params: RenderParams, applicationContext: ApplicationContext): AbstractCodeGenerator {
295293
with(params) {
296294
val classUnderTest: ClassId = kryoHelper.readObject(classUnderTest)
297295
val paramNames: MutableMap<ExecutableId, List<String>> = kryoHelper.readObject(paramNames)
@@ -300,7 +298,7 @@ private fun createCodeGenerator(kryoHelper: KryoHelper, params: RenderParams, co
300298
val forceStaticMocking: ForceStaticMocking = kryoHelper.readObject(forceStaticMocking)
301299
val projectType = ProjectType.valueOf(projectType)
302300

303-
val codeGeneratorParams = CodeGeneratorParams(
301+
return applicationContext.createCodeGenerator(CodeGeneratorParams(
304302
classUnderTest = classUnderTest,
305303
projectType = projectType,
306304
generateUtilClassFile = generateUtilClassFile,
@@ -323,11 +321,6 @@ private fun createCodeGenerator(kryoHelper: KryoHelper, params: RenderParams, co
323321
hangingTestsTimeout = HangingTestsTimeout(hangingTestsTimeout),
324322
enableTestsTimeout = enableTestsTimeout,
325323
testClassPackageName = testClassPackageName,
326-
)
327-
328-
return when (codeGenerationContext) {
329-
is SpringCodeGenerationContext -> SpringCodeGenerator(codeGenerationContext, codeGeneratorParams)
330-
else -> CodeGenerator(codeGeneratorParams)
331-
}
324+
))
332325
}
333326
}

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/generator/CodeGenerationController.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ import org.utbot.framework.codegen.domain.StaticImport
7171
import org.utbot.framework.codegen.tree.ututils.UtilClassKind
7272
import org.utbot.framework.codegen.tree.ututils.UtilClassKind.Companion.UT_UTILS_INSTANCE_NAME
7373
import org.utbot.framework.plugin.api.ClassId
74-
import org.utbot.framework.plugin.api.CodeGenerationContext
7574
import org.utbot.framework.plugin.api.CodegenLanguage
7675
import org.utbot.intellij.plugin.inspection.UnitTestBotInspectionManager
7776
import org.utbot.intellij.plugin.models.GenerateTestsModel
@@ -116,7 +115,6 @@ object CodeGenerationController {
116115

117116
fun generateTests(
118117
model: GenerateTestsModel,
119-
codeGenerationContext: CodeGenerationContext,
120118
classesWithTests: Map<PsiClass, RdTestGenerationResult>,
121119
psi2KClass: Map<PsiClass, ClassId>,
122120
process: EngineProcess,
@@ -163,7 +161,6 @@ object CodeGenerationController {
163161
testFilePointer,
164162
srcClassPathToSarifReport,
165163
model,
166-
codeGenerationContext,
167164
latch,
168165
utilClassListener,
169166
indicator
@@ -667,7 +664,6 @@ object CodeGenerationController {
667664
filePointer: SmartPsiElementPointer<PsiFile>,
668665
srcClassPathToSarifReport: MutableMap<Path, Sarif>,
669666
model: GenerateTestsModel,
670-
codeGenerationContext: CodeGenerationContext,
671667
reportsCountDown: CountDownLatch,
672668
utilClassListener: UtilClassListener,
673669
indicator: ProgressIndicator

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/generator/UtTestsDialogProcessor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ object UtTestsDialogProcessor {
446446
// indicator.checkCanceled()
447447

448448
invokeLater {
449-
generateTests(model, applicationContext, testSetsByClass, psi2KClass, process, indicator)
449+
generateTests(model, testSetsByClass, psi2KClass, process, indicator)
450450
logger.info { "Generation complete" }
451451
}
452452
}

utbot-testing/src/main/kotlin/org/utbot/testing/TestCodeGeneratorPipeline.kt

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ import org.utbot.framework.codegen.domain.ParametrizedTestSource
1111
import org.utbot.framework.codegen.domain.ProjectType
1212
import org.utbot.framework.codegen.domain.StaticsMocking
1313
import org.utbot.framework.codegen.domain.TestFramework
14-
import org.utbot.framework.codegen.generator.CodeGenerator
1514
import org.utbot.framework.codegen.generator.CodeGeneratorParams
16-
import org.utbot.framework.codegen.generator.SpringCodeGenerator
1715
import org.utbot.framework.codegen.services.language.CgLanguageAssistant
1816
import org.utbot.framework.codegen.tree.ututils.UtilClassKind
1917
import org.utbot.framework.codegen.tree.ututils.UtilClassKind.Companion.UT_UTILS_INSTANCE_NAME
@@ -256,39 +254,38 @@ class TestCodeGeneratorPipeline(private val testInfrastructureConfiguration: Tes
256254

257255
withUtContext(UtContext(classUnderTest.java.classLoader)) {
258256
val codeGenerator = with(testInfrastructureConfiguration) {
259-
val codeGeneratorParams = CodeGeneratorParams(
260-
classUnderTest.id,
261-
projectType = projectType,
262-
generateUtilClassFile = generateUtilClassFile,
263-
paramNames = params,
264-
testFramework = testFramework,
265-
staticsMocking = staticsMocking,
266-
forceStaticMocking = forceStaticMocking,
267-
generateWarningsForStaticMocking = false,
268-
codegenLanguage = codegenLanguage,
269-
cgLanguageAssistant = CgLanguageAssistant.getByCodegenLanguage(codegenLanguage),
270-
parameterizedTestSource = parametrizedTestSource,
271-
runtimeExceptionTestsBehaviour = runtimeExceptionTestsBehaviour,
272-
enableTestsTimeout = enableTestsTimeout
257+
val simpleApplicationContext = SimpleApplicationContext(
258+
SimpleMockerContext(
259+
mockFrameworkInstalled = true,
260+
staticsMockingIsConfigured = true
261+
)
273262
)
274263

275264
when (projectType) {
276-
ProjectType.Spring -> SpringCodeGenerator(
277-
params = codeGeneratorParams,
278-
springCodeGenerationContext = SpringApplicationContextImpl(
279-
SimpleApplicationContext(
280-
SimpleMockerContext(
281-
mockFrameworkInstalled = true,
282-
staticsMockingIsConfigured = true
283-
)
284-
),
285-
springTestType = SpringTestType.UNIT_TEST,
286-
springSettings = SpringSettings.AbsentSpringSettings(),
287-
)
265+
ProjectType.Spring -> SpringApplicationContextImpl(
266+
simpleApplicationContext,
267+
springTestType = SpringTestType.UNIT_TEST,
268+
springSettings = SpringSettings.AbsentSpringSettings(),
288269
)
289-
ProjectType.PureJvm -> CodeGenerator(codeGeneratorParams)
270+
ProjectType.PureJvm -> simpleApplicationContext
290271
else -> error("Unsupported project type $projectType in code generator instantiation")
291-
}
272+
}.createCodeGenerator(
273+
CodeGeneratorParams(
274+
classUnderTest.id,
275+
projectType = projectType,
276+
generateUtilClassFile = generateUtilClassFile,
277+
paramNames = params,
278+
testFramework = testFramework,
279+
staticsMocking = staticsMocking,
280+
forceStaticMocking = forceStaticMocking,
281+
generateWarningsForStaticMocking = false,
282+
codegenLanguage = codegenLanguage,
283+
cgLanguageAssistant = CgLanguageAssistant.getByCodegenLanguage(codegenLanguage),
284+
parameterizedTestSource = parametrizedTestSource,
285+
runtimeExceptionTestsBehaviour = runtimeExceptionTestsBehaviour,
286+
enableTestsTimeout = enableTestsTimeout
287+
)
288+
)
292289
}
293290
val testClassCustomName = "${classUnderTest.java.simpleName}GeneratedTest"
294291

0 commit comments

Comments
 (0)