Skip to content

Commit acc2fff

Browse files
committed
Added ForceStaticMockListener to run "configure mockito-inline" action.
Refactored url listener in notifications.
1 parent 80c2328 commit acc2fff

File tree

15 files changed

+185
-134
lines changed

15 files changed

+185
-134
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class Mocker(
168168
fun shouldMock(
169169
type: RefType,
170170
mockInfo: UtMockInfo,
171-
): Boolean = checkIfShouldMock(type, mockInfo).also { if (it) mockListenerController?.onShouldMock(strategy) }
171+
): Boolean = checkIfShouldMock(type, mockInfo).also { if (it) mockListenerController?.onShouldMock(strategy, mockInfo) }
172172

173173
private fun checkIfShouldMock(
174174
type: RefType,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.utbot.engine.util.mockListeners
2+
3+
import kotlinx.coroutines.CancellationException
4+
5+
/**
6+
* Exception used in [org.utbot.engine.util.mockListeners.ForceMockListener].
7+
*/
8+
class ForceMockCancellationException: CancellationException("Forced mocks without Mockito")
9+
10+
/**
11+
* Exception used in [org.utbot.engine.util.mockListeners.ForceStaticMockListener].
12+
*/
13+
class ForceStaticMockCancellationException: CancellationException("Forced static mocks without Mockito-inline")

utbot-framework/src/main/kotlin/org/utbot/engine/util/mockListeners/ForceMockListener.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.utbot.engine.util.mockListeners
22
import org.utbot.engine.EngineController
33
import org.utbot.engine.MockStrategy
4-
import org.utbot.engine.util.mockListeners.exceptions.ForceMockCancellationException
4+
import org.utbot.engine.UtMockInfo
55

66
/**
77
* Listener for mocker events in [org.utbot.engine.UtBotSymbolicEngine].
@@ -13,7 +13,7 @@ class ForceMockListener: MockListener {
1313
var forceMockHappened = false
1414
private set
1515

16-
override fun onShouldMock(controller: EngineController, strategy: MockStrategy) {
16+
override fun onShouldMock(controller: EngineController, strategy: MockStrategy, mockInfo: UtMockInfo) {
1717
// If force mocking happened -- сancel engine job
1818
controller.job?.cancel(ForceMockCancellationException())
1919
forceMockHappened = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.utbot.engine.util.mockListeners
2+
3+
import org.utbot.engine.EngineController
4+
import org.utbot.engine.MockStrategy
5+
import org.utbot.engine.UtMockInfo
6+
import org.utbot.engine.UtNewInstanceMockInfo
7+
import org.utbot.engine.UtStaticMethodMockInfo
8+
import org.utbot.engine.UtStaticObjectMockInfo
9+
10+
/**
11+
* Listener for mocker events in [org.utbot.engine.UtBotSymbolicEngine].
12+
* If forced static mock happened, cancels the engine job.
13+
*
14+
* Supposed to be created only if Mockito inline is not installed.
15+
*/
16+
class ForceStaticMockListener: MockListener {
17+
var forceStaticMockHappened = false
18+
private set
19+
20+
override fun onShouldMock(controller: EngineController, strategy: MockStrategy, mockInfo: UtMockInfo) {
21+
if (mockInfo is UtNewInstanceMockInfo
22+
|| mockInfo is UtStaticMethodMockInfo
23+
|| mockInfo is UtStaticObjectMockInfo) {
24+
// If force static mocking happened -- сancel engine job
25+
controller.job?.cancel(ForceStaticMockCancellationException())
26+
forceStaticMockHappened = true
27+
}
28+
}
29+
}

utbot-framework/src/main/kotlin/org/utbot/engine/util/mockListeners/MockListener.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package org.utbot.engine.util.mockListeners
22

33
import org.utbot.engine.EngineController
44
import org.utbot.engine.MockStrategy
5+
import org.utbot.engine.UtMockInfo
56

67
/**
78
* Listener that can be attached using [MockListenerController] to mocker in [org.utbot.engine.UtBotSymbolicEngine].
89
*/
910
interface MockListener {
10-
fun onShouldMock(controller: EngineController, strategy: MockStrategy)
11+
fun onShouldMock(controller: EngineController, strategy: MockStrategy, mockInfo: UtMockInfo)
1112
}

utbot-framework/src/main/kotlin/org/utbot/engine/util/mockListeners/MockListenerController.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.utbot.engine.util.mockListeners
22

33
import org.utbot.engine.EngineController
44
import org.utbot.engine.MockStrategy
5+
import org.utbot.engine.UtMockInfo
56

67
/**
78
* Controller that allows to attach listeners to mocker in [org.utbot.engine.UtBotSymbolicEngine].
@@ -13,7 +14,7 @@ class MockListenerController(private val controller: EngineController) {
1314
listeners += listener
1415
}
1516

16-
fun onShouldMock(strategy: MockStrategy) {
17-
listeners.map { it.onShouldMock(controller, strategy) }
17+
fun onShouldMock(strategy: MockStrategy, mockInfo: UtMockInfo) {
18+
listeners.map { it.onShouldMock(controller, strategy, mockInfo) }
1819
}
1920
}

utbot-framework/src/main/kotlin/org/utbot/engine/util/mockListeners/exceptions/ForceMockCancellationException.kt

-8
This file was deleted.

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ object UtBotTestCaseGenerator : TestCaseGenerator {
5858
private val logger = KotlinLogging.logger {}
5959
private val timeoutLogger = KotlinLogging.logger(logger.name + ".timeout")
6060

61-
lateinit var configureEngine: (UtBotSymbolicEngine) -> Unit
61+
lateinit var engineActions: MutableList<(UtBotSymbolicEngine) -> Unit>
6262
lateinit var isCanceled: () -> Boolean
6363

6464
//properties to save time on soot initialization
@@ -76,19 +76,19 @@ object UtBotTestCaseGenerator : TestCaseGenerator {
7676
buildDir,
7777
classpath,
7878
dependencyPaths,
79-
configureEngine = {},
79+
engineActions = mutableListOf(),
8080
isCanceled
8181
)
8282

8383
fun init(
8484
buildDir: Path,
8585
classpath: String?,
8686
dependencyPaths: String,
87-
configureEngine: (UtBotSymbolicEngine) -> Unit,
87+
engineActions: MutableList<(UtBotSymbolicEngine) -> Unit>,
8888
isCanceled: () -> Boolean
8989
) {
9090
this.isCanceled = isCanceled
91-
this.configureEngine = configureEngine
91+
this.engineActions = engineActions
9292
if (isCanceled()) return
9393

9494
checkFrameworkDependencies(dependencyPaths)
@@ -293,7 +293,9 @@ object UtBotTestCaseGenerator : TestCaseGenerator {
293293
mockStrategy,
294294
chosenClassesToMockAlways,
295295
executionTimeEstimator
296-
).apply(configureEngine)
296+
)
297+
298+
engineActions.map { engine.apply(it) }
297299

298300
generate(engine).collect {
299301
when (it) {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ class CodeGenerator(
3333
buildDir: String,
3434
classpath: String,
3535
pluginJarsPath: String,
36-
configureEngine: (UtBotSymbolicEngine) -> Unit = {},
36+
engineActions: MutableList<(UtBotSymbolicEngine) -> Unit> = mutableListOf(),
3737
isCanceled: () -> Boolean,
3838
) {
3939
init {
4040
UtSettings.testMinimizationStrategyType = TestSelectionStrategyType.COVERAGE_STRATEGY
4141
}
4242

4343
val generator = (project.service<Settings>().testCasesGenerator as UtBotTestCaseGenerator).apply {
44-
init(Paths.get(buildDir), classpath, pluginJarsPath, configureEngine, isCanceled)
44+
init(Paths.get(buildDir), classpath, pluginJarsPath, engineActions, isCanceled)
4545
}
4646

4747
private val settingsState = project.service<Settings>().state

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,11 @@ object TestGenerator {
405405
""".trimIndent()
406406
appendHtmlLine(savedFileMessage)
407407
}
408-
TestsReportNotifier.notify(notifyMessage, model.project, model.testModule)
408+
TestsReportNotifier.notify(notifyMessage)
409409
}
410410

411411
private fun processInitialWarnings(testsCodeWithTestReport: TestsCodeWithTestReport, model: GenerateTestsModel) {
412-
val hasInitialWarnings = model.forceMockHappened || model.hasTestFrameworkConflict
412+
val hasInitialWarnings = model.forceMockHappened || model.forceStaticMockHappened || model.hasTestFrameworkConflict
413413
if (!hasInitialWarnings) {
414414
return
415415
}
@@ -425,6 +425,14 @@ object TestGenerator {
425425
""".trimIndent()
426426
}
427427
}
428+
if (model.forceStaticMockHappened) {
429+
initialWarnings.add {
430+
"""
431+
<b>Warning</b>: Some test cases were ignored, because mockito-inline is not installed in the project.<br>
432+
Better results could be achieved by <a href="${TestReportUrlOpeningListener.prefix}${TestReportUrlOpeningListener.mockitoInlineSuffix}">configuring mockito-inline</a>.
433+
""".trimIndent()
434+
}
435+
}
428436
if (model.hasTestFrameworkConflict) {
429437
initialWarnings.add {
430438
"""

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/ConfigureWindowCommon.kt

-47
This file was deleted.

0 commit comments

Comments
 (0)