Skip to content

Commit fb60ec0

Browse files
Add saving Spring related settings selected by User on action dialog #2643 (#2731)
* Add saving Spring related settings selected by User on action dialog * Refactoring * Little code cleanup --------- Co-authored-by: Egor Kulikov <[email protected]>
1 parent c0235d4 commit fb60ec0

File tree

5 files changed

+84
-26
lines changed

5 files changed

+84
-26
lines changed

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

+28
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,30 @@ enum class SpringTestType(
15251525
}
15261526
}
15271527

1528+
class SpringProfileNames(
1529+
override val defaultItem: String,
1530+
) : CodeGenerationSettingTextField {
1531+
1532+
override fun toString() = defaultItem
1533+
1534+
companion object : CodeGenerationSettingTextField {
1535+
override val defaultItem = "default"
1536+
}
1537+
}
1538+
1539+
const val NO_SPRING_CONFIGURATION_OPTION = "No configuration"
1540+
1541+
class SpringConfig(
1542+
override val defaultItem: String,
1543+
) : CodeGenerationSettingTextField {
1544+
1545+
override fun toString() = defaultItem
1546+
1547+
companion object : CodeGenerationSettingTextField {
1548+
override val defaultItem = NO_SPRING_CONFIGURATION_OPTION
1549+
}
1550+
}
1551+
15281552
/**
15291553
* Describes information about beans obtained from Spring analysis process.
15301554
*
@@ -1568,6 +1592,10 @@ interface CodeGenerationSettingBox {
15681592
fun labels(): Array<String> = allItems.map { it.displayName }.toTypedArray()
15691593
}
15701594

1595+
interface CodeGenerationSettingTextField {
1596+
val defaultItem: String
1597+
}
1598+
15711599
enum class MockStrategyApi(
15721600
override val id: String,
15731601
override val displayName: String,

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/models/GenerateTestsModel.kt

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class GenerateTestsModel(
8787

8888
lateinit var springSettings: SpringSettings
8989
lateinit var springTestType: SpringTestType
90+
lateinit var springConfig: String
91+
lateinit var springProfileNames: String
9092

9193
val conflictTriggers: ConflictTriggers = ConflictTriggers()
9294
val preClasspathCollectionPromises: MutableList<Promise<*>> = mutableListOf()

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/settings/Settings.kt

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ private fun fromGenerateTestsModel(model: GenerateTestsModel): Settings.State {
2525
parametrizedTestSource = model.parametrizedTestSource,
2626
classesToMockAlways = model.chosenClassesToMockAlways.mapTo(mutableSetOf()) { it.name }.toTypedArray(),
2727
springTestType = model.springTestType,
28+
springConfig = model.springConfig,
29+
springProfileNames = model.springProfileNames,
2830
fuzzingValue = model.fuzzingValue,
2931
runGeneratedTestsWithCoverage = model.runGeneratedTestsWithCoverage,
3032
commentStyle = model.commentStyle,

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

+40-26
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ import org.utbot.framework.plugin.api.CodegenLanguage
9999
import org.utbot.framework.plugin.api.CodeGenerationSettingItem
100100
import org.utbot.framework.plugin.api.SpringConfiguration
101101
import org.utbot.framework.plugin.api.SpringTestType.*
102+
import org.utbot.framework.plugin.api.SpringProfileNames
102103
import org.utbot.framework.plugin.api.utils.MOCKITO_EXTENSIONS_FILE_CONTENT
103104
import org.utbot.framework.plugin.api.utils.MOCKITO_EXTENSIONS_FOLDER
104105
import org.utbot.framework.plugin.api.utils.MOCKITO_MOCKMAKER_FILE_NAME
106+
import org.utbot.framework.plugin.api.NO_SPRING_CONFIGURATION_OPTION
105107
import org.utbot.framework.util.Conflict
106108
import org.utbot.intellij.plugin.models.GenerateTestsModel
107109
import org.utbot.intellij.plugin.models.id
@@ -165,9 +167,6 @@ private const val SAME_PACKAGE_LABEL = "same as for sources"
165167

166168
private const val WILL_BE_INSTALLED_LABEL = " (will be installed)"
167169

168-
private const val NO_SPRING_CONFIGURATION_OPTION = "No configuration"
169-
private const val DEFAULT_SPRING_PROFILE_NAME = "default"
170-
171170
private const val ACTION_GENERATE = "Generate Tests"
172171
private const val ACTION_GENERATE_AND_RUN = "Generate and Run"
173172

@@ -202,7 +201,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
202201

203202
private val springTestType = createComboBox(SpringTestType.values()).also { it.setMinimumAndPreferredWidth(300) }
204203
private val springConfig = createComboBoxWithSeparatorsForSpringConfigs(shortenConfigurationNames())
205-
private val profileNames = JBTextField(23).apply { emptyText.text = DEFAULT_SPRING_PROFILE_NAME }
204+
private val springProfileNames = JBTextField(23).apply { emptyText.text = SpringProfileNames.defaultItem }
206205

207206
private val timeoutSpinner =
208207
JBIntSpinner(TimeUnit.MILLISECONDS.toSeconds(model.timeout).toInt(), 1, Int.MAX_VALUE, 1).also {
@@ -245,6 +244,11 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
245244
)
246245
}
247246

247+
private fun shortenConfigurationNameByFullname(fullname: String): String? {
248+
val allShortenConfigurationNames = shortenConfigurationNames().flatMap { it.second }
249+
return allShortenConfigurationNames.firstOrNull { fullname.endsWith(it) }
250+
}
251+
248252
private fun <T : CodeGenerationSettingItem> createComboBox(values: Array<T>) : ComboBox<T> {
249253
val comboBox = object:ComboBox<T>(DefaultComboBoxModel(values)) {
250254
var maxWidth = 0
@@ -428,7 +432,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
428432
ComboBoxPredicate(springConfig) { isSpringConfigSelected() && !isXmlSpringConfigUsed() }
429433
)
430434
row("Active profile(s):") {
431-
cell(profileNames)
435+
cell(springProfileNames)
432436
contextHelp(
433437
"One or several comma-separated names.<br>" +
434438
"If all names are incorrect, default profile is used"
@@ -736,12 +740,14 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
736740

737741
PresentSpringSettings(
738742
configuration = config,
739-
profiles = parseProfileExpression(profileNames.text, DEFAULT_SPRING_PROFILE_NAME).toList()
743+
profiles = parseProfileExpression(springProfileNames.text, SpringProfileNames.defaultItem).toList()
740744
)
741745
}
742746
}
743747

744748
model.springTestType = springTestType.item
749+
model.springConfig = springConfig.item.toString()
750+
model.springProfileNames = springProfileNames.text
745751

746752
val settings = model.project.service<Settings>()
747753
with(settings) {
@@ -883,21 +889,20 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
883889
if (settings.fuzzingValue == 0.0) 0.0
884890
else settings.fuzzingValue.coerceAtLeast(0.3)
885891
}
892+
springConfig.item = settings.springConfig
886893
}
887894
else -> {}
888895
}
889896

890897
mockStrategies.item = when (model.projectType) {
891-
ProjectType.Spring -> MockStrategyApi.springDefaultItem
898+
ProjectType.Spring ->
899+
if (isSpringConfigSelected()) MockStrategyApi.springDefaultItem else settings.mockStrategy
892900
else -> settings.mockStrategy
893901
}
894902
staticsMocking.isSelected = settings.staticsMocking == MockitoStaticMocking
895903
parametrizedTestSources.isSelected = (settings.parametrizedTestSource == ParametrizedTestSource.PARAMETRIZE
896904
&& model.projectType == ProjectType.PureJvm)
897905

898-
mockStrategies.isEnabled = true
899-
staticsMocking.isEnabled = mockStrategies.item != MockStrategyApi.NO_MOCKS
900-
901906
codegenLanguages.item = model.codegenLanguage
902907

903908
val installedTestFramework = TestFramework.allItems.singleOrNull { it.isInstalled }
@@ -914,15 +919,19 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
914919
updateParametrizationEnabled()
915920
}
916921
ProjectType.Spring -> {
922+
springProfileNames.text = settings.springProfileNames
917923
springTestType.item =
918924
if (isSpringConfigSelected()) settings.springTestType else SpringTestType.defaultItem
925+
updateMockStrategy(springTestType.item)
919926
updateSpringSettings()
920927
updateTestFrameworksList(springTestType.item)
921928
}
922929
ProjectType.Python,
923930
ProjectType.JavaScript -> { }
924931
}
925932

933+
mockStrategies.isEnabled = !isSpringConfigSelected()
934+
updateStaticMockEnabled()
926935
updateMockStrategyList()
927936

928937
itemsToHelpTooltip.forEach { (box, tooltip) ->
@@ -976,7 +985,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
976985
}
977986

978987
private fun configureSpringTestFrameworkIfRequired() {
979-
if (springConfig.item != NO_SPRING_CONFIGURATION_OPTION) {
988+
if (isSpringConfigSelected()) {
980989

981990
SpringModule.installedItems
982991
.forEach { configureSpringTestDependency(it) }
@@ -1192,7 +1201,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
11921201

11931202
springTestType.item = SpringTestType.defaultItem
11941203

1195-
profileNames.text = ""
1204+
springProfileNames.text = ""
11961205
}
11971206

11981207
if (isSpringConfigSelected() && springTestType.item == UNIT_TEST) {
@@ -1208,17 +1217,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
12081217
val item = comboBox.item as SpringTestType
12091218

12101219
updateTestFrameworksList(item)
1211-
1212-
when (item) {
1213-
UNIT_TEST -> {
1214-
mockStrategies.item = MockStrategyApi.springDefaultItem
1215-
staticsMocking.isSelected = true
1216-
}
1217-
INTEGRATION_TEST -> {
1218-
mockStrategies.item = MockStrategyApi.springIntegrationTestItem
1219-
staticsMocking.isSelected = false
1220-
}
1221-
}
1220+
updateMockStrategy(item)
12221221
updateMockStrategyList()
12231222
updateControlsEnabledStatus()
12241223
}
@@ -1232,6 +1231,21 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
12321231
}
12331232
}
12341233

1234+
private fun updateMockStrategy(springTestType: SpringTestType){
1235+
when (springTestType) {
1236+
UNIT_TEST -> {
1237+
if(isSpringConfigSelected()){
1238+
mockStrategies.item = MockStrategyApi.springDefaultItem
1239+
staticsMocking.isSelected = true
1240+
}
1241+
}
1242+
INTEGRATION_TEST -> {
1243+
mockStrategies.item = MockStrategyApi.springIntegrationTestItem
1244+
staticsMocking.isSelected = false
1245+
}
1246+
}
1247+
}
1248+
12351249
private lateinit var currentFrameworkItem: TestFramework
12361250

12371251
private fun updateTestFrameworksList(parametrizedTestSource: ParametrizedTestSource) {
@@ -1321,7 +1335,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
13211335
index: Int, selected: Boolean, hasFocus: Boolean
13221336
) {
13231337
this.append(value.displayName, SimpleTextAttributes.REGULAR_ATTRIBUTES)
1324-
if (springConfig.item != NO_SPRING_CONFIGURATION_OPTION) {
1338+
if (isSpringConfigSelected()) {
13251339
SpringModule.installedItems
13261340
// only first missing test framework is shown to avoid overflowing ComboBox
13271341
.firstOrNull { !it.testFrameworkInstalled }
@@ -1384,10 +1398,10 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
13841398

13851399
if (isSpringConfigSelected()) {
13861400
mockStrategies.isEnabled = false
1387-
profileNames.isEnabled = true
1401+
springProfileNames.isEnabled = true
13881402
springTestType.isEnabled = !isXmlSpringConfigUsed()
13891403
} else {
1390-
profileNames.isEnabled = false
1404+
springProfileNames.isEnabled = false
13911405
springTestType.isEnabled = false
13921406
}
13931407
}

utbot-ui-commons/src/main/kotlin/org/utbot/intellij/plugin/settings/CommonSettings.kt

+12
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ import org.utbot.framework.SummariesGenerationType
4040
import org.utbot.framework.codegen.domain.UnknownTestFramework
4141
import org.utbot.framework.plugin.api.SpringTestType
4242
import org.utbot.framework.plugin.api.isSummarizationCompatible
43+
import org.utbot.framework.plugin.api.SpringProfileNames
44+
import org.utbot.framework.plugin.api.SpringConfig
4345

4446
@State(
4547
name = "UtBotSettings",
@@ -66,6 +68,8 @@ class Settings(val project: Project) : PersistentStateComponent<Settings.State>
6668
var parametrizedTestSource: ParametrizedTestSource = ParametrizedTestSource.defaultItem,
6769
var classesToMockAlways: Array<String> = Mocker.defaultSuperClassesToMockAlwaysNames.toTypedArray(),
6870
var springTestType: SpringTestType = SpringTestType.defaultItem,
71+
var springConfig: String = SpringConfig.defaultItem,
72+
var springProfileNames: String = SpringProfileNames.defaultItem,
6973
var fuzzingValue: Double = 0.05,
7074
var runGeneratedTestsWithCoverage: Boolean = false,
7175
var commentStyle: JavaDocCommentStyle = JavaDocCommentStyle.defaultItem,
@@ -96,6 +100,8 @@ class Settings(val project: Project) : PersistentStateComponent<Settings.State>
96100
if (parametrizedTestSource != other.parametrizedTestSource) return false
97101
if (!classesToMockAlways.contentEquals(other.classesToMockAlways)) return false
98102
if (springTestType != other.springTestType) return false
103+
if (springConfig != other.springConfig) return false
104+
if (springProfileNames != other.springProfileNames) return false
99105
if (fuzzingValue != other.fuzzingValue) return false
100106
if (runGeneratedTestsWithCoverage != other.runGeneratedTestsWithCoverage) return false
101107
if (commentStyle != other.commentStyle) return false
@@ -120,6 +126,8 @@ class Settings(val project: Project) : PersistentStateComponent<Settings.State>
120126
result = 31 * result + parametrizedTestSource.hashCode()
121127
result = 31 * result + classesToMockAlways.contentHashCode()
122128
result = 31 * result + springTestType.hashCode()
129+
result = 31 * result + springConfig.hashCode()
130+
result = 31 * result + springProfileNames.hashCode()
123131
result = 31 * result + fuzzingValue.hashCode()
124132
result = 31 * result + if (runGeneratedTestsWithCoverage) 1 else 0
125133
result = 31 * result + summariesGenerationType.hashCode()
@@ -170,6 +178,10 @@ class Settings(val project: Project) : PersistentStateComponent<Settings.State>
170178

171179
val springTestType: SpringTestType get() = state.springTestType
172180

181+
val springConfig: String get() = state.springConfig
182+
183+
val springProfileNames: String get() = state.springProfileNames
184+
173185
val javaDocCommentStyle: JavaDocCommentStyle get() = state.commentStyle
174186

175187
var fuzzingValue: Double

0 commit comments

Comments
 (0)