Skip to content

Commit ca044a3

Browse files
committed
Refactoring
1 parent 33093e5 commit ca044a3

File tree

26 files changed

+154
-189
lines changed

26 files changed

+154
-189
lines changed

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,7 @@ interface CodeGenerationContext
13271327

13281328
interface SpringCodeGenerationContext : CodeGenerationContext {
13291329
val springTestType: SpringTestType
1330-
val springSettings: SpringSettings?
1330+
val springSettings: SpringSettings
13311331
}
13321332

13331333
/**
@@ -1396,10 +1396,21 @@ sealed interface SpringConfiguration {
13961396
class XMLConfiguration(val absolutePath: String) : SpringConfiguration
13971397
}
13981398

1399-
class SpringSettings(
1400-
val configuration: SpringConfiguration,
1401-
val profileExpression: String
1402-
)
1399+
sealed interface SpringSettings {
1400+
class AbsentSpringSettings : SpringSettings {
1401+
// Denotes no configuration and no profile setting
1402+
}
1403+
1404+
class PresentSpringSettings(
1405+
val configuration: SpringConfiguration,
1406+
val profiles: Array<String>
1407+
) : SpringSettings
1408+
}
1409+
1410+
//class SpringSettings(
1411+
// val configuration: SpringConfiguration,
1412+
// val profileExpression: String
1413+
//)
14031414

14041415
/**
14051416
* Data we get from Spring application context
@@ -1423,7 +1434,7 @@ class SpringApplicationContext(
14231434
val beanDefinitions: List<BeanDefinitionData> = emptyList(),
14241435
private val shouldUseImplementors: Boolean,
14251436
override val springTestType: SpringTestType,
1426-
override val springSettings: SpringSettings?,
1437+
override val springSettings: SpringSettings,
14271438
): ApplicationContext(mockInstalled, staticsMockingIsConfigured), SpringCodeGenerationContext {
14281439

14291440
companion object {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ inline fun <T> withStaticsSubstitutionRequired(condition: Boolean, block: () ->
1414
} finally {
1515
UtSettings.substituteStaticsWithSymbolicVariable = standardSubstitutionSetting
1616
}
17-
}
17+
}

utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import org.utbot.framework.plugin.api.util.*
3939
import org.utbot.framework.util.convertToAssemble
4040
import org.utbot.framework.util.graph
4141
import org.utbot.framework.util.sootMethod
42+
import org.utbot.framework.plugin.api.SpringSettings.*
4243
import org.utbot.fuzzer.*
4344
import org.utbot.fuzzing.*
4445
import org.utbot.fuzzing.providers.FieldValueProvider
@@ -388,7 +389,7 @@ class UtBotSymbolicEngine(
388389
var testEmittedByFuzzer = 0
389390
val valueProviders = ValueProvider.of(defaultValueProviders(defaultIdGenerator))
390391
.letIf(applicationContext is SpringApplicationContext
391-
&& applicationContext.springSettings != null
392+
&& applicationContext.springSettings is PresentSpringSettings
392393
) { provider ->
393394
val relevantRepositories = concreteExecutor.getRelevantSpringRepositories(methodUnderTest.classId)
394395
logger.info { "Detected relevant repositories for class ${methodUnderTest.classId}: $relevantRepositories" }

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import org.utbot.framework.plugin.api.ExecutableId
1919
import org.utbot.framework.plugin.api.MockFramework
2020
import org.utbot.framework.plugin.api.SpringTestType
2121
import org.utbot.framework.plugin.api.SpringCodeGenerationContext
22+
import org.utbot.framework.plugin.api.SpringSettings.*
2223

2324
class SpringCodeGenerator(
2425
val classUnderTest: ClassId,
@@ -63,9 +64,10 @@ class SpringCodeGenerator(
6364
val astConstructor = when (codeGenerationContext.springTestType) {
6465
SpringTestType.UNIT_TEST -> CgSpringUnitTestClassConstructor(context)
6566
SpringTestType.INTEGRATION_TEST ->
66-
codeGenerationContext.springSettings
67-
?.let { CgSpringIntegrationTestClassConstructor(context, it) }
68-
?: error("Error text.")
67+
when (val settings = codeGenerationContext.springSettings) {
68+
is PresentSpringSettings -> CgSpringIntegrationTestClassConstructor(context, settings)
69+
is AbsentSpringSettings -> error("No Spring settings were provided for Spring integration test generation.")
70+
}
6971
}
7072
val testClassFile = astConstructor.construct(testClassModel)
7173
logger.info { "Code generation phase finished at ${now()}" }

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import org.utbot.framework.codegen.domain.context.CgContext
88
import org.utbot.framework.codegen.domain.models.*
99
import org.utbot.framework.codegen.domain.models.AnnotationTarget.*
1010
import org.utbot.framework.plugin.api.ClassId
11-
import org.utbot.framework.plugin.api.SpringConfiguration
12-
import org.utbot.framework.plugin.api.SpringSettings
11+
import org.utbot.framework.plugin.api.SpringSettings.*
12+
import org.utbot.framework.plugin.api.SpringConfiguration.*
1313
import org.utbot.framework.plugin.api.util.SpringModelUtils
1414
import org.utbot.framework.plugin.api.util.SpringModelUtils.activeProfilesClassId
1515
import org.utbot.framework.plugin.api.util.SpringModelUtils.autoConfigureTestDbClassId
@@ -25,7 +25,7 @@ import org.utbot.framework.plugin.api.util.utContext
2525

2626
class CgSpringIntegrationTestClassConstructor(
2727
context: CgContext,
28-
private val springSettings: SpringSettings
28+
private val springSettings: PresentSpringSettings
2929
) : CgAbstractSpringTestClassConstructor(context) {
3030
override fun constructTestClass(testClassModel: SpringTestClassModel): CgClass {
3131
addNecessarySpringSpecificAnnotations()
@@ -61,12 +61,12 @@ class CgSpringIntegrationTestClassConstructor(
6161
)
6262
addAnnotation(
6363
classId = activeProfilesClassId,
64-
argument = springSettings.profileExpression, // TODO: separate by comma
64+
argument = springSettings.profiles.first(), // TODO: andrey
6565
target = Class,
6666
)
6767
addAnnotation(
6868
classId = contextConfigurationClassId,
69-
argument = (springSettings.configuration as SpringConfiguration.JavaConfiguration).classBinaryName, // TODO: unpacking
69+
argument = (springSettings.configuration as JavaConfiguration).classBinaryName, // TODO: andrey
7070
target = Class,
7171
)
7272
addAnnotation(

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import org.utbot.framework.util.ConflictTriggers
3434
import org.utbot.framework.util.SootUtils
3535
import org.utbot.framework.util.jimpleBody
3636
import org.utbot.framework.util.toModel
37+
import org.utbot.framework.plugin.api.SpringSettings.*
38+
import org.utbot.framework.plugin.api.SpringTestType.*
3739
import org.utbot.instrumentation.ConcreteExecutor
3840
import org.utbot.instrumentation.instrumentation.execution.SpringUtExecutionInstrumentation
3941
import org.utbot.instrumentation.instrumentation.execution.UtExecutionInstrumentation
@@ -69,13 +71,13 @@ open class TestCaseGenerator(
6971
private val timeoutLogger: KLogger = KotlinLogging.logger(logger.name + ".timeout")
7072
private val executionInstrumentation by lazy {
7173
when (applicationContext) {
72-
is SpringApplicationContext -> when (val springSettings = applicationContext.springSettings) {
73-
null -> UtExecutionInstrumentation
74-
else -> when (applicationContext.springTestType) {
75-
SpringTestType.UNIT_TEST -> UtExecutionInstrumentation
76-
SpringTestType.INTEGRATION_TEST -> SpringUtExecutionInstrumentation(
74+
is SpringApplicationContext -> when (val settings = applicationContext.springSettings) {
75+
is AbsentSpringSettings -> UtExecutionInstrumentation
76+
is PresentSpringSettings -> when (applicationContext.springTestType) {
77+
UNIT_TEST -> UtExecutionInstrumentation
78+
INTEGRATION_TEST -> SpringUtExecutionInstrumentation(
7779
UtExecutionInstrumentation,
78-
springSettings,
80+
settings,
7981
applicationContext.beanDefinitions,
8082
buildDirs.map { it.toURL() }.toTypedArray(),
8183
)

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ private fun EngineProcessModel.setup(kryoHelper: KryoHelper, watchdog: IdleWatch
8383
val springAnalyzerProcess = SpringAnalyzerProcess.createBlocking(params.classpath.toList())
8484
val result = springAnalyzerProcess.terminateOnException { _ ->
8585
springAnalyzerProcess.getBeanDefinitions(
86-
params.configuration,
87-
params.fileStorage,
88-
params.profileExpression,
86+
params.springSettings
8987
)
9088
}
9189
springAnalyzerProcess.terminate()
@@ -152,7 +150,7 @@ private fun EngineProcessModel.setup(kryoHelper: KryoHelper, watchdog: IdleWatch
152150
}
153151
}
154152
watchdog.measureTimeForActiveCall(render, "Rendering tests") { params ->
155-
val codeGenerator = createCodeGenerator(kryoHelper, params)
153+
val codeGenerator = createCodeGenerator(kryoHelper, params, testGenerator.applicationContext)
156154

157155
codeGenerator.generateAsStringWithTestReport(testSets[params.testSetsId]!!).let {
158156
testGenerationReports.add(it.testsGenerationReport)
@@ -291,15 +289,14 @@ private fun destinationWarningMessage(testPackageName: String?, classUnderTestPa
291289
}
292290
}
293291

294-
private fun createCodeGenerator(kryoHelper: KryoHelper, params: RenderParams): AbstractCodeGenerator {
292+
private fun createCodeGenerator(kryoHelper: KryoHelper, params: RenderParams, codeGenerationContext: CodeGenerationContext): AbstractCodeGenerator {
295293
with(params) {
296294
val classUnderTest: ClassId = kryoHelper.readObject(classUnderTest)
297295
val paramNames: MutableMap<ExecutableId, List<String>> = kryoHelper.readObject(paramNames)
298296
val testFramework = testFrameworkByName(testFramework)
299297
val staticMocking = if (staticsMocking.startsWith("No")) NoStaticMocking else MockitoStaticMocking
300298
val forceStaticMocking: ForceStaticMocking = kryoHelper.readObject(forceStaticMocking)
301299
val projectType = ProjectType.valueOf(projectType)
302-
val codeGenerationContext: CodeGenerationContext = kryoHelper.readObject(codeGenerationContext)
303300

304301
return when (codeGenerationContext) {
305302
is SpringCodeGenerationContext -> {

0 commit comments

Comments
 (0)