Skip to content

Commit c1d22e7

Browse files
committed
Extract ApplicationContext interface
1 parent 6e65b46 commit c1d22e7

File tree

6 files changed

+76
-50
lines changed

6 files changed

+76
-50
lines changed
Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,39 @@
11
package org.utbot.framework.context
22

3-
import org.utbot.framework.UtSettings
4-
import org.utbot.framework.isFromTrustedLibrary
53
import org.utbot.framework.plugin.api.ClassId
64
import org.utbot.framework.plugin.api.CodeGenerationContext
75
import org.utbot.framework.plugin.api.TypeReplacementMode
86
import org.utbot.framework.plugin.api.UtError
97
import soot.RefType
108
import soot.SootField
119

12-
/**
13-
* A context to use when no specific data is required.
14-
*
15-
* @param mockFrameworkInstalled shows if we have installed framework dependencies
16-
* @param staticsMockingIsConfigured shows if we have installed static mocking tools
17-
*/
18-
open class ApplicationContext(
19-
val mockFrameworkInstalled: Boolean = true,
20-
staticsMockingIsConfigured: Boolean = true,
21-
) : CodeGenerationContext {
22-
var staticsMockingIsConfigured = staticsMockingIsConfigured
23-
private set
24-
25-
init {
26-
/**
27-
* Situation when mock framework is not installed but static mocking is configured is semantically incorrect.
28-
*
29-
* However, it may be obtained in real application after this actions:
30-
* - fully configure mocking (dependency installed + resource file created)
31-
* - remove mockito-core dependency from project
32-
* - forget to remove mock-maker file from resource directory
33-
*
34-
* Here we transform this configuration to semantically correct.
35-
*/
36-
if (!mockFrameworkInstalled && staticsMockingIsConfigured) {
37-
this.staticsMockingIsConfigured = false
38-
}
39-
}
10+
interface ApplicationContext : CodeGenerationContext {
11+
val mockFrameworkInstalled: Boolean
12+
val staticsMockingIsConfigured: Boolean
4013

4114
/**
4215
* Shows if there are any restrictions on type implementors.
4316
*/
44-
open val typeReplacementMode: TypeReplacementMode = TypeReplacementMode.AnyImplementor
17+
val typeReplacementMode: TypeReplacementMode
4518

4619
/**
4720
* Finds a type to replace the original abstract type
4821
* if it is guided with some additional information.
4922
*/
50-
open fun replaceTypeIfNeeded(type: RefType): ClassId? = null
23+
fun replaceTypeIfNeeded(type: RefType): ClassId?
5124

5225
/**
5326
* Checks whether accessing [field] (with a method invocation or field access) speculatively
5427
* cannot produce [NullPointerException] (according to its finality or accessibility).
5528
*
5629
* @see docs/SpeculativeFieldNonNullability.md for more information.
5730
*/
58-
open fun speculativelyCannotProduceNullPointerException(
31+
fun speculativelyCannotProduceNullPointerException(
5932
field: SootField,
6033
classUnderTest: ClassId,
61-
): Boolean =
62-
!UtSettings.maximizeCoverageUsingReflection &&
63-
field.declaringClass.isFromTrustedLibrary() &&
64-
(field.isFinal || !field.isPublic)
34+
): Boolean
6535

66-
open fun preventsFurtherTestGeneration(): Boolean = false
36+
fun preventsFurtherTestGeneration(): Boolean
6737

68-
open fun getErrors(): List<UtError> = emptyList()
38+
fun getErrors(): List<UtError>
6939
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.utbot.framework.context
2+
3+
import org.utbot.framework.UtSettings
4+
import org.utbot.framework.isFromTrustedLibrary
5+
import org.utbot.framework.plugin.api.ClassId
6+
import org.utbot.framework.plugin.api.TypeReplacementMode
7+
import org.utbot.framework.plugin.api.UtError
8+
import soot.RefType
9+
import soot.SootField
10+
11+
/**
12+
* A context to use when no specific data is required.
13+
*
14+
* @param mockFrameworkInstalled shows if we have installed framework dependencies
15+
* @param staticsMockingIsConfigured shows if we have installed static mocking tools
16+
*/
17+
class SimpleApplicationContext(
18+
override val mockFrameworkInstalled: Boolean = true,
19+
staticsMockingIsConfigured: Boolean = true,
20+
) : ApplicationContext {
21+
override var staticsMockingIsConfigured = staticsMockingIsConfigured
22+
private set
23+
24+
init {
25+
/**
26+
* Situation when mock framework is not installed but static mocking is configured is semantically incorrect.
27+
*
28+
* However, it may be obtained in real application after this actions:
29+
* - fully configure mocking (dependency installed + resource file created)
30+
* - remove mockito-core dependency from project
31+
* - forget to remove mock-maker file from resource directory
32+
*
33+
* Here we transform this configuration to semantically correct.
34+
*/
35+
if (!mockFrameworkInstalled && staticsMockingIsConfigured) {
36+
this.staticsMockingIsConfigured = false
37+
}
38+
}
39+
40+
override val typeReplacementMode: TypeReplacementMode = TypeReplacementMode.AnyImplementor
41+
42+
override fun replaceTypeIfNeeded(type: RefType): ClassId? = null
43+
44+
override fun speculativelyCannotProduceNullPointerException(
45+
field: SootField,
46+
classUnderTest: ClassId,
47+
): Boolean =
48+
!UtSettings.maximizeCoverageUsingReflection &&
49+
field.declaringClass.isFromTrustedLibrary() &&
50+
(field.isFinal || !field.isPublic)
51+
52+
override fun preventsFurtherTestGeneration(): Boolean = false
53+
54+
override fun getErrors(): List<UtError> = emptyList()
55+
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,12 @@ import soot.SootField
4141
// Right now this refactoring is blocked because some interfaces need to get extracted and moved to utbot-framework-api
4242
// As an alternative we can just move ApplicationContext itself to utbot-framework
4343
class SpringApplicationContext(
44-
mockInstalled: Boolean,
45-
staticsMockingIsConfigured: Boolean,
44+
private val delegateContext: ApplicationContext,
4645
val beanDefinitions: List<BeanDefinitionData> = emptyList(),
4746
private val shouldUseImplementors: Boolean,
4847
override val springTestType: SpringTestType,
4948
override val springSettings: SpringSettings,
50-
): ApplicationContext(mockInstalled, staticsMockingIsConfigured), SpringCodeGenerationContext {
49+
): ApplicationContext by delegateContext, SpringCodeGenerationContext {
5150

5251
override var springContextLoadingResult: SpringContextLoadingResult? = null
5352

@@ -136,15 +135,15 @@ class SpringApplicationContext(
136135
): Boolean = field.fieldId in classUnderTest.allDeclaredFieldIds && field.type.classId !in allInjectedTypes
137136

138137
override fun preventsFurtherTestGeneration(): Boolean =
139-
super.preventsFurtherTestGeneration() || springContextLoadingResult?.contextLoaded == false
138+
delegateContext.preventsFurtherTestGeneration() || springContextLoadingResult?.contextLoaded == false
140139

141140
override fun getErrors(): List<UtError> =
142141
springContextLoadingResult?.exceptions?.map { exception ->
143142
UtError(
144143
"Failed to load Spring application context",
145144
exception
146145
)
147-
}.orEmpty() + super.getErrors()
146+
}.orEmpty() + delegateContext.getErrors()
148147

149148
fun getBeansAssignableTo(classId: ClassId): List<BeanDefinitionData> = beanDefinitions.filter { beanDef ->
150149
// some bean classes may fail to load

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import org.utbot.framework.UtSettings.disableCoroutinesDebug
2626
import org.utbot.framework.UtSettings.utBotGenerationTimeoutInMillis
2727
import org.utbot.framework.UtSettings.warmupConcreteExecution
2828
import org.utbot.framework.context.ApplicationContext
29+
import org.utbot.framework.context.SimpleApplicationContext
2930
import org.utbot.framework.context.SpringApplicationContext
3031
import org.utbot.framework.plugin.api.utils.checkFrameworkDependencies
3132
import org.utbot.framework.minimization.minimizeTestCase
@@ -70,7 +71,7 @@ open class TestCaseGenerator(
7071
val engineActions: MutableList<(UtBotSymbolicEngine) -> Unit> = mutableListOf(),
7172
val isCanceled: () -> Boolean = { false },
7273
val forceSootReload: Boolean = true,
73-
val applicationContext: ApplicationContext = ApplicationContext(),
74+
val applicationContext: ApplicationContext = SimpleApplicationContext(),
7475
) {
7576
private val logger: KLogger = KotlinLogging.logger {}
7677
private val timeoutLogger: KLogger = KotlinLogging.logger(logger.name + ".timeout")

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import org.utbot.framework.CancellationStrategyType.SAVE_PROCESSED_RESULTS
4949
import org.utbot.framework.UtSettings
5050
import org.utbot.framework.codegen.domain.ProjectType.*
5151
import org.utbot.framework.context.ApplicationContext
52+
import org.utbot.framework.context.SimpleApplicationContext
5253
import org.utbot.framework.context.SpringApplicationContext
5354
import org.utbot.framework.plugin.api.*
5455
import org.utbot.framework.plugin.api.SpringSettings.*
@@ -262,6 +263,7 @@ object UtTestsDialogProcessor {
262263
process.terminateOnException { _ ->
263264
val classpathForClassLoader = buildDirs + classpathList
264265
process.setupUtContext(classpathForClassLoader)
266+
val simpleApplicationContext = SimpleApplicationContext(mockFrameworkInstalled, staticMockingConfigured)
265267
val applicationContext = when (model.projectType) {
266268
Spring -> {
267269
val beanDefinitions =
@@ -281,15 +283,14 @@ object UtTestsDialogProcessor {
281283
clarifyBeanDefinitionReturnTypes(beanDefinitions, project)
282284

283285
SpringApplicationContext(
284-
mockFrameworkInstalled,
285-
staticMockingConfigured,
286+
simpleApplicationContext,
286287
clarifiedBeanDefinitions,
287288
shouldUseImplementors,
288289
model.springTestType,
289290
model.springSettings,
290291
)
291292
}
292-
else -> ApplicationContext(mockFrameworkInstalled, staticMockingConfigured)
293+
else -> simpleApplicationContext
293294
}
294295
process.createTestGenerator(
295296
buildDirs,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import org.utbot.framework.codegen.generator.SpringCodeGenerator
1616
import org.utbot.framework.codegen.services.language.CgLanguageAssistant
1717
import org.utbot.framework.codegen.tree.ututils.UtilClassKind
1818
import org.utbot.framework.codegen.tree.ututils.UtilClassKind.Companion.UT_UTILS_INSTANCE_NAME
19+
import org.utbot.framework.context.SimpleApplicationContext
1920
import org.utbot.framework.context.SpringApplicationContext
2021
import org.utbot.framework.plugin.api.*
2122
import org.utbot.framework.plugin.api.util.UtContext
@@ -269,8 +270,7 @@ class TestCodeGeneratorPipeline(private val testInfrastructureConfiguration: Tes
269270
runtimeExceptionTestsBehaviour = runtimeExceptionTestsBehaviour,
270271
enableTestsTimeout = enableTestsTimeout,
271272
springCodeGenerationContext = SpringApplicationContext(
272-
mockInstalled = true,
273-
staticsMockingIsConfigured = true,
273+
SimpleApplicationContext(mockFrameworkInstalled = true, staticsMockingIsConfigured = true),
274274
shouldUseImplementors = false,
275275
springTestType = SpringTestType.UNIT_TEST,
276276
springSettings = SpringSettings.AbsentSpringSettings(),

0 commit comments

Comments
 (0)