Skip to content

Commit 4e9bac9

Browse files
Improve profile selection options on UI (#2190)
* Improve profile selection options on UI #2190
1 parent c03c24b commit 4e9bac9

File tree

4 files changed

+67
-58
lines changed

4 files changed

+67
-58
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-
model.profileExpression,
255+
model.profileNames,
256256
)
257257
}
258258
}

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

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

5757
lateinit var typeReplacementApproach: TypeReplacementApproach
58-
lateinit var profileExpression: String
58+
lateinit var profileNames: String
5959

6060
val conflictTriggers: ConflictTriggers = ConflictTriggers()
6161

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

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ import com.intellij.ui.SimpleTextAttributes
5757
import com.intellij.ui.components.CheckBox
5858
import com.intellij.ui.components.JBLabel
5959
import com.intellij.ui.components.JBScrollPane
60+
import com.intellij.ui.components.JBTextField
6061
import com.intellij.ui.components.panels.HorizontalLayout
6162
import com.intellij.ui.components.panels.NonOpaquePanel
6263
import com.intellij.ui.components.panels.OpaquePanel
6364
import com.intellij.ui.dsl.builder.Align
64-
import com.intellij.ui.layout.ComboBoxPredicate
6565
import com.intellij.ui.dsl.builder.panel
66-
import com.intellij.ui.layout.selected
66+
import com.intellij.ui.layout.ComboBoxPredicate
6767
import com.intellij.util.IncorrectOperationException
6868
import com.intellij.util.lang.JavaVersion
6969
import com.intellij.util.ui.JBUI
@@ -74,43 +74,23 @@ import com.intellij.util.ui.JBUI.size
7474
import com.intellij.util.ui.UIUtil
7575
import com.intellij.util.ui.components.BorderLayoutPanel
7676
import mu.KotlinLogging
77-
import java.awt.BorderLayout
78-
import java.awt.Color
79-
import java.awt.Component
80-
import java.awt.Dimension
81-
import java.awt.event.ActionEvent
82-
import java.nio.file.Files
83-
import java.nio.file.Path
84-
import java.nio.file.Paths
85-
import java.text.ParseException
86-
import java.util.concurrent.TimeUnit
87-
import javax.swing.AbstractAction
88-
import javax.swing.Action
89-
import javax.swing.DefaultComboBoxModel
90-
import javax.swing.JButton
91-
import javax.swing.JCheckBox
92-
import javax.swing.JComboBox
93-
import javax.swing.JComponent
94-
import javax.swing.JList
95-
import javax.swing.JSpinner
96-
import javax.swing.text.DefaultFormatter
9777
import org.jetbrains.concurrency.Promise
9878
import org.jetbrains.concurrency.thenRun
9979
import org.utbot.common.PathUtil.toPath
10080
import org.utbot.framework.UtSettings
101-
import org.utbot.framework.codegen.domain.ProjectType
10281
import org.utbot.framework.codegen.domain.DependencyInjectionFramework
10382
import org.utbot.framework.codegen.domain.ForceStaticMocking
10483
import org.utbot.framework.codegen.domain.Junit4
10584
import org.utbot.framework.codegen.domain.Junit5
10685
import org.utbot.framework.codegen.domain.MockitoStaticMocking
10786
import org.utbot.framework.codegen.domain.NoStaticMocking
10887
import org.utbot.framework.codegen.domain.ParametrizedTestSource
88+
import org.utbot.framework.codegen.domain.ProjectType
10989
import org.utbot.framework.codegen.domain.SpringBeans
110-
import org.utbot.framework.codegen.domain.TypeReplacementApproach
11190
import org.utbot.framework.codegen.domain.StaticsMocking
11291
import org.utbot.framework.codegen.domain.TestFramework
11392
import org.utbot.framework.codegen.domain.TestNg
93+
import org.utbot.framework.codegen.domain.TypeReplacementApproach
11494
import org.utbot.framework.plugin.api.CodeGenerationSettingItem
11595
import org.utbot.framework.plugin.api.CodegenLanguage
11696
import org.utbot.framework.plugin.api.MockFramework
@@ -150,12 +130,32 @@ import org.utbot.intellij.plugin.util.IntelliJApiHelper
150130
import org.utbot.intellij.plugin.util.SpringConfigurationsHelper
151131
import org.utbot.intellij.plugin.util.extractFirstLevelMembers
152132
import org.utbot.intellij.plugin.util.findSdkVersion
133+
import java.awt.BorderLayout
134+
import java.awt.Color
135+
import java.awt.Component
136+
import java.awt.Dimension
137+
import java.awt.event.ActionEvent
153138
import java.io.File
139+
import java.nio.file.Files
140+
import java.nio.file.Path
141+
import java.nio.file.Paths
142+
import java.text.ParseException
154143
import java.time.LocalDateTime
155144
import java.time.format.DateTimeFormatter
156-
import javax.swing.JTextField
145+
import java.util.concurrent.TimeUnit
146+
import javax.swing.AbstractAction
147+
import javax.swing.Action
148+
import javax.swing.DefaultComboBoxModel
149+
import javax.swing.JButton
150+
import javax.swing.JCheckBox
151+
import javax.swing.JComboBox
152+
import javax.swing.JComponent
153+
import javax.swing.JList
154+
import javax.swing.JSpinner
155+
import javax.swing.text.DefaultFormatter
157156
import kotlin.io.path.notExists
158157

158+
159159
private const val RECENTS_KEY = "org.utbot.recents"
160160

161161
private const val SAME_PACKAGE_LABEL = "same as for sources"
@@ -198,8 +198,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
198198
private val staticsMocking = JCheckBox("Mock static methods")
199199

200200
private val springConfig = createComboBoxWithSeparatorsForSpringConfigs(shortenConfigurationNames())
201-
private val selectProfile = JCheckBox("Select active profiles")
202-
private val profileExpression = JTextField(DEFAULT_SPRING_PROFILE_NAME, 23)
201+
private val profileNames = JBTextField(23).apply { emptyText.text = DEFAULT_SPRING_PROFILE_NAME }
203202

204203
private val timeoutSpinner =
205204
JBIntSpinner(TimeUnit.MILLISECONDS.toSeconds(model.timeout).toInt(), 1, Int.MAX_VALUE, 1).also {
@@ -394,6 +393,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
394393
row("Testing framework:") {
395394
cell(testFrameworks)
396395
}
396+
397397
if (model.projectType == ProjectType.Spring) {
398398
row("Spring configuration:") {
399399
cell(springConfig)
@@ -404,23 +404,17 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
404404
"Mocks will be used when necessary."
405405
)
406406
}
407-
row {
408-
cell(selectProfile)
407+
row("Active profile(s):") {
408+
cell(profileNames)
409409
contextHelp(
410-
"Only selected profile will be active.<br>" +
411-
"Otherwise, profile from the configuration class or default one is used."
410+
"One or several comma-separated names.<br>" +
411+
"If all names are incorrect, default profile is used"
412412
)
413-
}
414-
indent {
415-
row("Profile name(s):") {
416-
cell(profileExpression).align(Align.FILL)
417-
contextHelp(
418-
"Profile name or expression like \"prod|web\" may be passed here.<br>" +
419-
"If expression is incorrect, default profile will be used"
420-
)
421-
}.enabledIf(selectProfile.selected)
422-
}
413+
}.enabledIf(
414+
ComboBoxPredicate(springConfig) { springConfig.item != NO_SPRING_CONFIGURATION_OPTION }
415+
)
423416
}
417+
424418
row("Mocking strategy:") {
425419
cell(mockStrategies)
426420
contextHelp(
@@ -664,7 +658,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
664658
TypeReplacementApproach.ReplaceIfPossible(fullConfigName)
665659
}
666660
}
667-
model.profileExpression = profileExpression.text
661+
model.profileNames = profileNames.text.let { it.ifEmpty { DEFAULT_SPRING_PROFILE_NAME } }
668662

669663
val settings = model.project.service<Settings>()
670664
with(settings) {
@@ -1062,8 +1056,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
10621056
mockStrategies.isEnabled = false
10631057
updateMockStrategyListForConfigGuidedTypeReplacements()
10641058

1065-
selectProfile.isEnabled = true
1066-
selectProfile.isSelected = false
1059+
profileNames.isEnabled = true
10671060
} else {
10681061
mockStrategies.item = when (model.projectType) {
10691062
ProjectType.Spring -> MockStrategyApi.springDefaultItem
@@ -1072,15 +1065,11 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
10721065
mockStrategies.isEnabled = true
10731066
updateMockStrategyList()
10741067

1075-
selectProfile.isEnabled = false
1076-
selectProfile.isSelected = false
1068+
profileNames.isEnabled = false
1069+
profileNames.text = ""
10771070
}
10781071
}
10791072

1080-
selectProfile.addActionListener { _ ->
1081-
profileExpression.text = if (selectProfile.isSelected) "" else DEFAULT_SPRING_PROFILE_NAME
1082-
}
1083-
10841073
cbSpecifyTestPackage.addActionListener {
10851074
val testPackageName = findTestPackageComboValue()
10861075
val packageNameIsNeeded = testPackageField.isEnabled || testPackageName != SAME_PACKAGE_LABEL
@@ -1157,8 +1146,6 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
11571146
private fun updateSpringConfigurationEnabled() {
11581147
// We check for > 1 because there is already extra-dummy NO_SPRING_CONFIGURATION_OPTION option
11591148
springConfig.isEnabled = model.projectType == ProjectType.Spring && springConfig.itemCount > 1
1160-
selectProfile.isEnabled = false
1161-
selectProfile.isSelected = false
11621149
}
11631150

11641151
private fun staticsMockingConfigured(): Boolean {
Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package org.utbot.spring.utils
22

3+
import com.jetbrains.rd.util.getLogger
4+
import com.jetbrains.rd.util.info
35
import org.springframework.core.env.ConfigurableEnvironment
46
import org.springframework.core.env.StandardEnvironment
57
import org.utbot.spring.api.ApplicationData
68

9+
private val logger = getLogger<EnvironmentFactory>()
10+
711
class EnvironmentFactory(
812
private val applicationData: ApplicationData
913
) {
@@ -12,15 +16,33 @@ class EnvironmentFactory(
1216
}
1317

1418
fun createEnvironment(): ConfigurableEnvironment {
15-
val profilesToActivate = parseProfileExpression(applicationData.profileExpression ?: DEFAULT_PROFILE_NAME)
19+
val profilesToActivate = parseProfileExpression(applicationData.profileExpression)
1620

1721
val environment = StandardEnvironment()
18-
environment.setActiveProfiles(*profilesToActivate)
22+
23+
try {
24+
environment.setActiveProfiles(*profilesToActivate)
25+
} catch (e: Exception) {
26+
logger.info { "Setting ${applicationData.profileExpression} as active profiles failed with exception $e" }
27+
}
1928

2029
return environment
2130
}
2231

23-
//TODO: implement this, e.g. 'prod|web' -> listOf(prod, web)
24-
private fun parseProfileExpression(profileExpression: String) : Array<String> = arrayOf(profileExpression)
32+
/*
33+
* Transforms active profile information
34+
* from the form of user input to a list of active profiles.
35+
*
36+
* Current user input form is comma-separated values, but it may be changed later.
37+
*/
38+
private fun parseProfileExpression(profileExpression: String?): Array<String> {
39+
if (profileExpression.isNullOrEmpty()) {
40+
return arrayOf(DEFAULT_PROFILE_NAME)
41+
}
2542

43+
return profileExpression
44+
.filter { !it.isWhitespace() }
45+
.split(',')
46+
.toTypedArray()
47+
}
2648
}

0 commit comments

Comments
 (0)