Skip to content

Commit 5f9233b

Browse files
committed
Extract MockerContext interface
1 parent c1d22e7 commit 5f9233b

File tree

8 files changed

+47
-33
lines changed

8 files changed

+47
-33
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import kotlinx.collections.immutable.persistentListOf
1919
import org.utbot.common.nameOfPackage
2020
import org.utbot.engine.types.OBJECT_TYPE
2121
import org.utbot.engine.util.mockListeners.MockListenerController
22-
import org.utbot.framework.context.ApplicationContext
22+
import org.utbot.framework.context.mocker.MockerContext
2323
import org.utbot.framework.plugin.api.util.isInaccessibleViaReflection
2424
import soot.BooleanType
2525
import soot.RefType
@@ -168,7 +168,7 @@ class Mocker(
168168
private val hierarchy: Hierarchy,
169169
chosenClassesToMockAlways: Set<ClassId>,
170170
internal val mockListenerController: MockListenerController? = null,
171-
private val applicationContext: ApplicationContext,
171+
private val mockerContext: MockerContext,
172172
) {
173173
private val mocksAreDesired: Boolean = strategy != MockStrategy.NO_MOCKS
174174

@@ -227,10 +227,10 @@ class Mocker(
227227

228228
val mockingIsPossible = when (mockInfo) {
229229
is UtFieldMockInfo,
230-
is UtObjectMockInfo -> applicationContext.mockFrameworkInstalled
230+
is UtObjectMockInfo -> mockerContext.mockFrameworkInstalled
231231
is UtNewInstanceMockInfo,
232232
is UtStaticMethodMockInfo,
233-
is UtStaticObjectMockInfo -> applicationContext.staticsMockingIsConfigured
233+
is UtStaticObjectMockInfo -> mockerContext.staticsMockingIsConfigured
234234
}
235235
val mockingIsForcedAndPossible = mockAlways(mockedValue.type) && mockingIsPossible
236236

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class UtBotSymbolicEngine(
143143
hierarchy,
144144
chosenClassesToMockAlways,
145145
MockListenerController(controller),
146-
applicationContext = applicationContext,
146+
mockerContext = applicationContext.mockerContext,
147147
)
148148

149149
fun attachMockListener(mockListener: MockListener) = mocker.mockListenerController?.attach(mockListener)

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

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

3+
import org.utbot.framework.context.mocker.MockerContext
34
import org.utbot.framework.plugin.api.ClassId
45
import org.utbot.framework.plugin.api.CodeGenerationContext
56
import org.utbot.framework.plugin.api.TypeReplacementMode
@@ -8,8 +9,7 @@ import soot.RefType
89
import soot.SootField
910

1011
interface ApplicationContext : CodeGenerationContext {
11-
val mockFrameworkInstalled: Boolean
12-
val staticsMockingIsConfigured: Boolean
12+
val mockerContext: MockerContext
1313

1414
/**
1515
* Shows if there are any restrictions on type implementors.

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

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

33
import org.utbot.framework.UtSettings
4+
import org.utbot.framework.context.mocker.MockerContext
45
import org.utbot.framework.isFromTrustedLibrary
56
import org.utbot.framework.plugin.api.ClassId
67
import org.utbot.framework.plugin.api.TypeReplacementMode
@@ -10,33 +11,10 @@ import soot.SootField
1011

1112
/**
1213
* 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
1614
*/
1715
class SimpleApplicationContext(
18-
override val mockFrameworkInstalled: Boolean = true,
19-
staticsMockingIsConfigured: Boolean = true,
16+
override val mockerContext: MockerContext
2017
) : 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-
4018
override val typeReplacementMode: TypeReplacementMode = TypeReplacementMode.AnyImplementor
4119

4220
override fun replaceTypeIfNeeded(type: RefType): ClassId? = null
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.utbot.framework.context.mocker
2+
3+
interface MockerContext {
4+
/**
5+
* Shows if we have installed framework dependencies
6+
*/
7+
val mockFrameworkInstalled: Boolean
8+
9+
/**
10+
* Shows if we have installed static mocking tools
11+
*/
12+
val staticsMockingIsConfigured: Boolean
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.utbot.framework.context.mocker
2+
3+
class SimpleMockerContext(
4+
override val mockFrameworkInstalled: Boolean,
5+
staticsMockingIsConfigured: Boolean
6+
) : MockerContext {
7+
/**
8+
* NOTE: Can only be `true` when [mockFrameworkInstalled], because
9+
* situation when mock framework is not installed but static mocking
10+
* is configured is semantically incorrect.
11+
*/
12+
override val staticsMockingIsConfigured: Boolean =
13+
mockFrameworkInstalled && staticsMockingIsConfigured
14+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import org.utbot.framework.codegen.domain.ProjectType.*
5151
import org.utbot.framework.context.ApplicationContext
5252
import org.utbot.framework.context.SimpleApplicationContext
5353
import org.utbot.framework.context.SpringApplicationContext
54+
import org.utbot.framework.context.mocker.SimpleMockerContext
5455
import org.utbot.framework.plugin.api.*
5556
import org.utbot.framework.plugin.api.SpringSettings.*
5657
import org.utbot.framework.plugin.api.SpringConfiguration.*
@@ -263,7 +264,9 @@ object UtTestsDialogProcessor {
263264
process.terminateOnException { _ ->
264265
val classpathForClassLoader = buildDirs + classpathList
265266
process.setupUtContext(classpathForClassLoader)
266-
val simpleApplicationContext = SimpleApplicationContext(mockFrameworkInstalled, staticMockingConfigured)
267+
val simpleApplicationContext = SimpleApplicationContext(
268+
SimpleMockerContext(mockFrameworkInstalled, staticMockingConfigured)
269+
)
267270
val applicationContext = when (model.projectType) {
268271
Spring -> {
269272
val beanDefinitions =

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import org.utbot.framework.codegen.tree.ututils.UtilClassKind
1818
import org.utbot.framework.codegen.tree.ututils.UtilClassKind.Companion.UT_UTILS_INSTANCE_NAME
1919
import org.utbot.framework.context.SimpleApplicationContext
2020
import org.utbot.framework.context.SpringApplicationContext
21+
import org.utbot.framework.context.mocker.SimpleMockerContext
2122
import org.utbot.framework.plugin.api.*
2223
import org.utbot.framework.plugin.api.util.UtContext
2324
import org.utbot.framework.plugin.api.util.description
@@ -270,7 +271,12 @@ class TestCodeGeneratorPipeline(private val testInfrastructureConfiguration: Tes
270271
runtimeExceptionTestsBehaviour = runtimeExceptionTestsBehaviour,
271272
enableTestsTimeout = enableTestsTimeout,
272273
springCodeGenerationContext = SpringApplicationContext(
273-
SimpleApplicationContext(mockFrameworkInstalled = true, staticsMockingIsConfigured = true),
274+
SimpleApplicationContext(
275+
SimpleMockerContext(
276+
mockFrameworkInstalled = true,
277+
staticsMockingIsConfigured = true
278+
)
279+
),
274280
shouldUseImplementors = false,
275281
springTestType = SpringTestType.UNIT_TEST,
276282
springSettings = SpringSettings.AbsentSpringSettings(),

0 commit comments

Comments
 (0)