Skip to content

Commit 2c59289

Browse files
Support profiles selection for Spring-guided test generation (#2183)
1 parent 79201d3 commit 2c59289

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ object UtTestsDialogProcessor {
252252
classpathForClassLoader,
253253
approach.config,
254254
fileStorage,
255-
profileExpression = null,
255+
model.profileExpression,
256256
)
257257
}
258258
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class GenerateTestsModel(
5555
lateinit var commentStyle: JavaDocCommentStyle
5656

5757
lateinit var typeReplacementApproach: TypeReplacementApproach
58+
lateinit var profileExpression: String
5859

5960
val conflictTriggers: ConflictTriggers = ConflictTriggers()
6061

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

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import com.intellij.ui.components.panels.OpaquePanel
6363
import com.intellij.ui.dsl.builder.Align
6464
import com.intellij.ui.layout.ComboBoxPredicate
6565
import com.intellij.ui.dsl.builder.panel
66+
import com.intellij.ui.layout.selected
6667
import com.intellij.util.IncorrectOperationException
6768
import com.intellij.util.lang.JavaVersion
6869
import com.intellij.util.ui.JBUI
@@ -152,7 +153,7 @@ import org.utbot.intellij.plugin.util.findSdkVersion
152153
import java.io.File
153154
import java.time.LocalDateTime
154155
import java.time.format.DateTimeFormatter
155-
import java.util.*
156+
import javax.swing.JTextField
156157
import kotlin.io.path.notExists
157158

158159
private const val RECENTS_KEY = "org.utbot.recents"
@@ -162,6 +163,7 @@ private const val SAME_PACKAGE_LABEL = "same as for sources"
162163
private const val WILL_BE_INSTALLED_LABEL = " (will be installed)"
163164

164165
private const val NO_SPRING_CONFIGURATION_OPTION = "No configuration"
166+
private const val DEFAULT_SPRING_PROFILE_NAME = "default"
165167

166168
private const val ACTION_GENERATE = "Generate Tests"
167169
private const val ACTION_GENERATE_AND_RUN = "Generate and Run"
@@ -192,10 +194,13 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
192194
private val javaConfigurationHelper = SpringConfigurationsHelper(".")
193195
private val xmlConfigurationHelper = SpringConfigurationsHelper(File.separator)
194196

195-
private val springConfig = createComboBoxWithSeparatorsForSpringConfigs(shortenConfigurationNames())
196-
197197
private val mockStrategies = createComboBox(MockStrategyApi.values())
198198
private val staticsMocking = JCheckBox("Mock static methods")
199+
200+
private val springConfig = createComboBoxWithSeparatorsForSpringConfigs(shortenConfigurationNames())
201+
private val selectProfile = JCheckBox("Select active profiles")
202+
private val profileExpression = JTextField(DEFAULT_SPRING_PROFILE_NAME, 23)
203+
199204
private val timeoutSpinner =
200205
JBIntSpinner(TimeUnit.MILLISECONDS.toSeconds(model.timeout).toInt(), 1, Int.MAX_VALUE, 1).also {
201206
when(val editor = it.editor) {
@@ -388,17 +393,33 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
388393
row("Spring configuration:") {
389394
cell(springConfig)
390395
contextHelp(
391-
"100% Symbolic execution mode.\n" +
396+
"100% Symbolic execution mode.<br>" +
392397
"Classes defined in Spring configuration will be used instead " +
393-
"of interfaces and abstract classes.\n" +
398+
"of interfaces and abstract classes.<br>" +
394399
"Mocks will be used when necessary."
395400
)
396401
}
402+
row {
403+
cell(selectProfile)
404+
contextHelp(
405+
"Only selected profile will be active.<br>" +
406+
"Otherwise, profile from the configuration class or default one is used."
407+
)
408+
}
409+
indent {
410+
row("Profile name(s):") {
411+
cell(profileExpression).align(Align.FILL)
412+
contextHelp(
413+
"Profile name or expression like \"prod|web\" may be passed here.<br>" +
414+
"If expression is incorrect, default profile will be used"
415+
)
416+
}.enabledIf(selectProfile.selected)
417+
}
397418
}
398419
row("Mocking strategy:") {
399420
cell(mockStrategies)
400421
contextHelp(
401-
"Mock everything around the target class or the whole package except the system classes. " +
422+
"Mock everything around the target class or the whole package except the system classes.<br> " +
402423
"Otherwise, mock nothing. Mockito will be installed, if you don't have one."
403424
)
404425
}.enabledIf(ComboBoxPredicate(springConfig) {
@@ -638,6 +659,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
638659
TypeReplacementApproach.ReplaceIfPossible(fullConfigName)
639660
}
640661
}
662+
model.profileExpression = profileExpression.text
641663

642664
val settings = model.project.service<Settings>()
643665
with(settings) {
@@ -1034,16 +1056,26 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
10341056
mockStrategies.item = MockStrategyApi.springDefaultItem
10351057
mockStrategies.isEnabled = false
10361058
updateMockStrategyListForConfigGuidedTypeReplacements()
1059+
1060+
selectProfile.isEnabled = true
1061+
selectProfile.isSelected = false
10371062
} else {
10381063
mockStrategies.item = when (model.projectType) {
10391064
ProjectType.Spring -> MockStrategyApi.springDefaultItem
10401065
else -> MockStrategyApi.defaultItem
10411066
}
10421067
mockStrategies.isEnabled = true
10431068
updateMockStrategyList()
1069+
1070+
selectProfile.isEnabled = false
1071+
selectProfile.isSelected = false
10441072
}
10451073
}
10461074

1075+
selectProfile.addActionListener { _ ->
1076+
profileExpression.text = if (selectProfile.isSelected) "" else DEFAULT_SPRING_PROFILE_NAME
1077+
}
1078+
10471079
cbSpecifyTestPackage.addActionListener {
10481080
val testPackageName = findTestPackageComboValue()
10491081
val packageNameIsNeeded = testPackageField.isEnabled || testPackageName != SAME_PACKAGE_LABEL
@@ -1120,6 +1152,8 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
11201152
private fun updateSpringConfigurationEnabled() {
11211153
// We check for > 1 because there is already extra-dummy NO_SPRING_CONFIGURATION_OPTION option
11221154
springConfig.isEnabled = model.projectType == ProjectType.Spring && springConfig.itemCount > 1
1155+
selectProfile.isEnabled = false
1156+
selectProfile.isSelected = false
11231157
}
11241158

11251159
private fun staticsMockingConfigured(): Boolean {

0 commit comments

Comments
 (0)