Skip to content

Commit b801da6

Browse files
committed
Extract ConcreteExecutionContext
1 parent a2c969c commit b801da6

File tree

7 files changed

+76
-23
lines changed

7 files changed

+76
-23
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package org.utbot.framework.context
22

33
import org.utbot.framework.plugin.api.CodeGenerationContext
4-
import org.utbot.framework.plugin.api.UtError
54

65
interface ApplicationContext : CodeGenerationContext {
76
val mockerContext: MockerContext
87
val typeReplacer: TypeReplacer
98
val nonNullSpeculator: NonNullSpeculator
109

11-
fun preventsFurtherTestGeneration(): Boolean
12-
13-
fun getErrors(): List<UtError>
10+
fun createConcreteExecutionContext(
11+
fullClasspath: String,
12+
classpathWithoutDependencies: String
13+
): ConcreteExecutionContext
1414
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.utbot.framework.context
2+
3+
import org.utbot.framework.plugin.api.UtError
4+
5+
interface ConcreteExecutionContext {
6+
fun preventsFurtherTestGeneration(): Boolean
7+
8+
fun getErrors(): List<UtError>
9+
10+
// TODO refactor, so this interface only includes the following:
11+
// val instrumentation: Instrumentation<UtConcreteExecutionResult>
12+
// fun createValueProviderOrThrow(classUnderTest: ClassId, idGenerator: IdentityPreservingIdGenerator<Int>): JavaValueProvider
13+
// fun loadContext(): ContextLoadingResult
14+
// fun Coverage.filterCoveredInstructions(classUnderTestId: ClassId): Coverage
15+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package org.utbot.framework.context.simple
22

33
import org.utbot.framework.context.ApplicationContext
4+
import org.utbot.framework.context.ConcreteExecutionContext
45
import org.utbot.framework.context.MockerContext
56
import org.utbot.framework.context.NonNullSpeculator
67
import org.utbot.framework.context.TypeReplacer
7-
import org.utbot.framework.plugin.api.UtError
88

99
/**
1010
* A context to use when no specific data is required.
@@ -14,8 +14,8 @@ class SimpleApplicationContext(
1414
override val typeReplacer: TypeReplacer = SimpleTypeReplacer(),
1515
override val nonNullSpeculator: NonNullSpeculator = SimpleNonNullSpeculator()
1616
) : ApplicationContext {
17-
18-
override fun preventsFurtherTestGeneration(): Boolean = false
19-
20-
override fun getErrors(): List<UtError> = emptyList()
17+
override fun createConcreteExecutionContext(
18+
fullClasspath: String,
19+
classpathWithoutDependencies: String
20+
): ConcreteExecutionContext = SimpleConcreteExecutionContext(fullClasspath, classpathWithoutDependencies)
2121
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.utbot.framework.context.simple
2+
3+
import org.utbot.framework.context.ConcreteExecutionContext
4+
import org.utbot.framework.plugin.api.UtError
5+
6+
class SimpleConcreteExecutionContext(
7+
// TODO these properties will be used later (to fulfill TODO in ConcreteExecutionContext)
8+
val fullClassPath: String,
9+
val classpathWithoutDependencies: String
10+
) : ConcreteExecutionContext {
11+
override fun preventsFurtherTestGeneration(): Boolean = false
12+
13+
override fun getErrors(): List<UtError> = emptyList()
14+
}

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import mu.KotlinLogging
44
import org.utbot.common.isAbstract
55
import org.utbot.common.isStatic
66
import org.utbot.framework.context.ApplicationContext
7+
import org.utbot.framework.context.ConcreteExecutionContext
78
import org.utbot.framework.context.NonNullSpeculator
89
import org.utbot.framework.context.TypeReplacer
910
import org.utbot.framework.plugin.api.BeanDefinitionData
@@ -32,16 +33,13 @@ class SpringApplicationContextImpl(
3233

3334
override var springContextLoadingResult: SpringContextLoadingResult? = null
3435

35-
override fun preventsFurtherTestGeneration(): Boolean =
36-
delegateContext.preventsFurtherTestGeneration() || springContextLoadingResult?.contextLoaded == false
37-
38-
override fun getErrors(): List<UtError> =
39-
springContextLoadingResult?.exceptions?.map { exception ->
40-
UtError(
41-
"Failed to load Spring application context",
42-
exception
43-
)
44-
}.orEmpty() + delegateContext.getErrors()
36+
override fun createConcreteExecutionContext(
37+
fullClasspath: String,
38+
classpathWithoutDependencies: String
39+
): ConcreteExecutionContext = SpringConcreteExecutionContext(
40+
delegateContext.createConcreteExecutionContext(fullClasspath, classpathWithoutDependencies),
41+
this
42+
)
4543

4644
override fun getBeansAssignableTo(classId: ClassId): List<BeanDefinitionData> = beanDefinitions.filter { beanDef ->
4745
// some bean classes may fail to load
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.utbot.framework.context.spring
2+
3+
import org.utbot.framework.context.ConcreteExecutionContext
4+
import org.utbot.framework.plugin.api.UtError
5+
6+
class SpringConcreteExecutionContext(
7+
private val delegateContext: ConcreteExecutionContext,
8+
private val springApplicationContext: SpringApplicationContext,
9+
) : ConcreteExecutionContext {
10+
override fun preventsFurtherTestGeneration(): Boolean =
11+
delegateContext.preventsFurtherTestGeneration() ||
12+
springApplicationContext.springContextLoadingResult?.contextLoaded == false
13+
14+
override fun getErrors(): List<UtError> =
15+
springApplicationContext.springContextLoadingResult?.exceptions?.map { exception ->
16+
UtError(
17+
"Failed to load Spring application context",
18+
exception
19+
)
20+
}.orEmpty() + delegateContext.getErrors()
21+
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ open class TestCaseGenerator(
8181
) {
8282
private val logger: KLogger = KotlinLogging.logger {}
8383
private val timeoutLogger: KLogger = KotlinLogging.logger(logger.name + ".timeout")
84+
private val concreteExecutionContext = applicationContext.createConcreteExecutionContext(
85+
fullClasspath = classpathForEngine,
86+
classpathWithoutDependencies = buildDirs.joinToString(File.pathSeparator)
87+
)
88+
8489
private val executionInstrumentation by lazy {
8590
when (applicationContext) {
8691
is SpringApplicationContext -> when (val settings = applicationContext.springSettings) {
@@ -158,8 +163,8 @@ open class TestCaseGenerator(
158163
return@flow
159164

160165
doContextDependentPreparationForTestGeneration()
161-
applicationContext.getErrors().forEach { emit(it) }
162-
if (applicationContext.preventsFurtherTestGeneration())
166+
concreteExecutionContext.getErrors().forEach { emit(it) }
167+
if (concreteExecutionContext.preventsFurtherTestGeneration())
163168
return@flow
164169

165170
try {
@@ -194,10 +199,10 @@ open class TestCaseGenerator(
194199
doContextDependentPreparationForTestGeneration()
195200

196201
val method2errors: Map<ExecutableId, MutableMap<String, Int>> = methods.associateWith {
197-
applicationContext.getErrors().associateTo(mutableMapOf()) { it.description to 1 }
202+
concreteExecutionContext.getErrors().associateTo(mutableMapOf()) { it.description to 1 }
198203
}
199204

200-
if (applicationContext.preventsFurtherTestGeneration())
205+
if (concreteExecutionContext.preventsFurtherTestGeneration())
201206
return@use methods.map { method -> UtMethodTestSet(method, errors = method2errors.getValue(method)) }
202207

203208
val executionStartInMillis = System.currentTimeMillis()

0 commit comments

Comments
 (0)