Skip to content

Commit

Permalink
Add saving Spring related settings selected by User on action dialog #…
Browse files Browse the repository at this point in the history
…2643 (#2731)

* Add saving Spring related settings selected by User on action dialog

* Refactoring

* Little code cleanup

---------

Co-authored-by: Egor Kulikov <[email protected]>
  • Loading branch information
tepa46 and EgorkaKulikov authored Dec 21, 2023
1 parent c0235d4 commit fb60ec0
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,30 @@ enum class SpringTestType(
}
}

class SpringProfileNames(
override val defaultItem: String,
) : CodeGenerationSettingTextField {

override fun toString() = defaultItem

companion object : CodeGenerationSettingTextField {
override val defaultItem = "default"
}
}

const val NO_SPRING_CONFIGURATION_OPTION = "No configuration"

class SpringConfig(
override val defaultItem: String,
) : CodeGenerationSettingTextField {

override fun toString() = defaultItem

companion object : CodeGenerationSettingTextField {
override val defaultItem = NO_SPRING_CONFIGURATION_OPTION
}
}

/**
* Describes information about beans obtained from Spring analysis process.
*
Expand Down Expand Up @@ -1568,6 +1592,10 @@ interface CodeGenerationSettingBox {
fun labels(): Array<String> = allItems.map { it.displayName }.toTypedArray()
}

interface CodeGenerationSettingTextField {
val defaultItem: String
}

enum class MockStrategyApi(
override val id: String,
override val displayName: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class GenerateTestsModel(

lateinit var springSettings: SpringSettings
lateinit var springTestType: SpringTestType
lateinit var springConfig: String
lateinit var springProfileNames: String

val conflictTriggers: ConflictTriggers = ConflictTriggers()
val preClasspathCollectionPromises: MutableList<Promise<*>> = mutableListOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ private fun fromGenerateTestsModel(model: GenerateTestsModel): Settings.State {
parametrizedTestSource = model.parametrizedTestSource,
classesToMockAlways = model.chosenClassesToMockAlways.mapTo(mutableSetOf()) { it.name }.toTypedArray(),
springTestType = model.springTestType,
springConfig = model.springConfig,
springProfileNames = model.springProfileNames,
fuzzingValue = model.fuzzingValue,
runGeneratedTestsWithCoverage = model.runGeneratedTestsWithCoverage,
commentStyle = model.commentStyle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,11 @@ import org.utbot.framework.plugin.api.CodegenLanguage
import org.utbot.framework.plugin.api.CodeGenerationSettingItem
import org.utbot.framework.plugin.api.SpringConfiguration
import org.utbot.framework.plugin.api.SpringTestType.*
import org.utbot.framework.plugin.api.SpringProfileNames
import org.utbot.framework.plugin.api.utils.MOCKITO_EXTENSIONS_FILE_CONTENT
import org.utbot.framework.plugin.api.utils.MOCKITO_EXTENSIONS_FOLDER
import org.utbot.framework.plugin.api.utils.MOCKITO_MOCKMAKER_FILE_NAME
import org.utbot.framework.plugin.api.NO_SPRING_CONFIGURATION_OPTION
import org.utbot.framework.util.Conflict
import org.utbot.intellij.plugin.models.GenerateTestsModel
import org.utbot.intellij.plugin.models.id
Expand Down Expand Up @@ -165,9 +167,6 @@ private const val SAME_PACKAGE_LABEL = "same as for sources"

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

private const val NO_SPRING_CONFIGURATION_OPTION = "No configuration"
private const val DEFAULT_SPRING_PROFILE_NAME = "default"

private const val ACTION_GENERATE = "Generate Tests"
private const val ACTION_GENERATE_AND_RUN = "Generate and Run"

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

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

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

private fun shortenConfigurationNameByFullname(fullname: String): String? {
val allShortenConfigurationNames = shortenConfigurationNames().flatMap { it.second }
return allShortenConfigurationNames.firstOrNull { fullname.endsWith(it) }
}

private fun <T : CodeGenerationSettingItem> createComboBox(values: Array<T>) : ComboBox<T> {
val comboBox = object:ComboBox<T>(DefaultComboBoxModel(values)) {
var maxWidth = 0
Expand Down Expand Up @@ -428,7 +432,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
ComboBoxPredicate(springConfig) { isSpringConfigSelected() && !isXmlSpringConfigUsed() }
)
row("Active profile(s):") {
cell(profileNames)
cell(springProfileNames)
contextHelp(
"One or several comma-separated names.<br>" +
"If all names are incorrect, default profile is used"
Expand Down Expand Up @@ -736,12 +740,14 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m

PresentSpringSettings(
configuration = config,
profiles = parseProfileExpression(profileNames.text, DEFAULT_SPRING_PROFILE_NAME).toList()
profiles = parseProfileExpression(springProfileNames.text, SpringProfileNames.defaultItem).toList()
)
}
}

model.springTestType = springTestType.item
model.springConfig = springConfig.item.toString()
model.springProfileNames = springProfileNames.text

val settings = model.project.service<Settings>()
with(settings) {
Expand Down Expand Up @@ -883,21 +889,20 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
if (settings.fuzzingValue == 0.0) 0.0
else settings.fuzzingValue.coerceAtLeast(0.3)
}
springConfig.item = settings.springConfig
}
else -> {}
}

mockStrategies.item = when (model.projectType) {
ProjectType.Spring -> MockStrategyApi.springDefaultItem
ProjectType.Spring ->
if (isSpringConfigSelected()) MockStrategyApi.springDefaultItem else settings.mockStrategy
else -> settings.mockStrategy
}
staticsMocking.isSelected = settings.staticsMocking == MockitoStaticMocking
parametrizedTestSources.isSelected = (settings.parametrizedTestSource == ParametrizedTestSource.PARAMETRIZE
&& model.projectType == ProjectType.PureJvm)

mockStrategies.isEnabled = true
staticsMocking.isEnabled = mockStrategies.item != MockStrategyApi.NO_MOCKS

codegenLanguages.item = model.codegenLanguage

val installedTestFramework = TestFramework.allItems.singleOrNull { it.isInstalled }
Expand All @@ -914,15 +919,19 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
updateParametrizationEnabled()
}
ProjectType.Spring -> {
springProfileNames.text = settings.springProfileNames
springTestType.item =
if (isSpringConfigSelected()) settings.springTestType else SpringTestType.defaultItem
updateMockStrategy(springTestType.item)
updateSpringSettings()
updateTestFrameworksList(springTestType.item)
}
ProjectType.Python,
ProjectType.JavaScript -> { }
}

mockStrategies.isEnabled = !isSpringConfigSelected()
updateStaticMockEnabled()
updateMockStrategyList()

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

private fun configureSpringTestFrameworkIfRequired() {
if (springConfig.item != NO_SPRING_CONFIGURATION_OPTION) {
if (isSpringConfigSelected()) {

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

springTestType.item = SpringTestType.defaultItem

profileNames.text = ""
springProfileNames.text = ""
}

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

updateTestFrameworksList(item)

when (item) {
UNIT_TEST -> {
mockStrategies.item = MockStrategyApi.springDefaultItem
staticsMocking.isSelected = true
}
INTEGRATION_TEST -> {
mockStrategies.item = MockStrategyApi.springIntegrationTestItem
staticsMocking.isSelected = false
}
}
updateMockStrategy(item)
updateMockStrategyList()
updateControlsEnabledStatus()
}
Expand All @@ -1232,6 +1231,21 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
}
}

private fun updateMockStrategy(springTestType: SpringTestType){
when (springTestType) {
UNIT_TEST -> {
if(isSpringConfigSelected()){
mockStrategies.item = MockStrategyApi.springDefaultItem
staticsMocking.isSelected = true
}
}
INTEGRATION_TEST -> {
mockStrategies.item = MockStrategyApi.springIntegrationTestItem
staticsMocking.isSelected = false
}
}
}

private lateinit var currentFrameworkItem: TestFramework

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

if (isSpringConfigSelected()) {
mockStrategies.isEnabled = false
profileNames.isEnabled = true
springProfileNames.isEnabled = true
springTestType.isEnabled = !isXmlSpringConfigUsed()
} else {
profileNames.isEnabled = false
springProfileNames.isEnabled = false
springTestType.isEnabled = false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import org.utbot.framework.SummariesGenerationType
import org.utbot.framework.codegen.domain.UnknownTestFramework
import org.utbot.framework.plugin.api.SpringTestType
import org.utbot.framework.plugin.api.isSummarizationCompatible
import org.utbot.framework.plugin.api.SpringProfileNames
import org.utbot.framework.plugin.api.SpringConfig

@State(
name = "UtBotSettings",
Expand All @@ -66,6 +68,8 @@ class Settings(val project: Project) : PersistentStateComponent<Settings.State>
var parametrizedTestSource: ParametrizedTestSource = ParametrizedTestSource.defaultItem,
var classesToMockAlways: Array<String> = Mocker.defaultSuperClassesToMockAlwaysNames.toTypedArray(),
var springTestType: SpringTestType = SpringTestType.defaultItem,
var springConfig: String = SpringConfig.defaultItem,
var springProfileNames: String = SpringProfileNames.defaultItem,
var fuzzingValue: Double = 0.05,
var runGeneratedTestsWithCoverage: Boolean = false,
var commentStyle: JavaDocCommentStyle = JavaDocCommentStyle.defaultItem,
Expand Down Expand Up @@ -96,6 +100,8 @@ class Settings(val project: Project) : PersistentStateComponent<Settings.State>
if (parametrizedTestSource != other.parametrizedTestSource) return false
if (!classesToMockAlways.contentEquals(other.classesToMockAlways)) return false
if (springTestType != other.springTestType) return false
if (springConfig != other.springConfig) return false
if (springProfileNames != other.springProfileNames) return false
if (fuzzingValue != other.fuzzingValue) return false
if (runGeneratedTestsWithCoverage != other.runGeneratedTestsWithCoverage) return false
if (commentStyle != other.commentStyle) return false
Expand All @@ -120,6 +126,8 @@ class Settings(val project: Project) : PersistentStateComponent<Settings.State>
result = 31 * result + parametrizedTestSource.hashCode()
result = 31 * result + classesToMockAlways.contentHashCode()
result = 31 * result + springTestType.hashCode()
result = 31 * result + springConfig.hashCode()
result = 31 * result + springProfileNames.hashCode()
result = 31 * result + fuzzingValue.hashCode()
result = 31 * result + if (runGeneratedTestsWithCoverage) 1 else 0
result = 31 * result + summariesGenerationType.hashCode()
Expand Down Expand Up @@ -170,6 +178,10 @@ class Settings(val project: Project) : PersistentStateComponent<Settings.State>

val springTestType: SpringTestType get() = state.springTestType

val springConfig: String get() = state.springConfig

val springProfileNames: String get() = state.springProfileNames

val javaDocCommentStyle: JavaDocCommentStyle get() = state.commentStyle

var fuzzingValue: Double
Expand Down

0 comments on commit fb60ec0

Please sign in to comment.