Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ object UtTestsDialogProcessor {
classpathForClassLoader,
approach.config,
fileStorage,
model.profileExpression,
model.profileNames,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class GenerateTestsModel(
lateinit var commentStyle: JavaDocCommentStyle

lateinit var typeReplacementApproach: TypeReplacementApproach
lateinit var profileExpression: String
lateinit var profileNames: String

val conflictTriggers: ConflictTriggers = ConflictTriggers()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ import com.intellij.ui.SimpleTextAttributes
import com.intellij.ui.components.CheckBox
import com.intellij.ui.components.JBLabel
import com.intellij.ui.components.JBScrollPane
import com.intellij.ui.components.JBTextField
import com.intellij.ui.components.panels.HorizontalLayout
import com.intellij.ui.components.panels.NonOpaquePanel
import com.intellij.ui.components.panels.OpaquePanel
import com.intellij.ui.dsl.builder.Align
import com.intellij.ui.layout.ComboBoxPredicate
import com.intellij.ui.dsl.builder.panel
import com.intellij.ui.layout.selected
import com.intellij.ui.layout.ComboBoxPredicate
import com.intellij.util.IncorrectOperationException
import com.intellij.util.lang.JavaVersion
import com.intellij.util.ui.JBUI
Expand All @@ -74,43 +74,23 @@ import com.intellij.util.ui.JBUI.size
import com.intellij.util.ui.UIUtil
import com.intellij.util.ui.components.BorderLayoutPanel
import mu.KotlinLogging
import java.awt.BorderLayout
import java.awt.Color
import java.awt.Component
import java.awt.Dimension
import java.awt.event.ActionEvent
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.text.ParseException
import java.util.concurrent.TimeUnit
import javax.swing.AbstractAction
import javax.swing.Action
import javax.swing.DefaultComboBoxModel
import javax.swing.JButton
import javax.swing.JCheckBox
import javax.swing.JComboBox
import javax.swing.JComponent
import javax.swing.JList
import javax.swing.JSpinner
import javax.swing.text.DefaultFormatter
import org.jetbrains.concurrency.Promise
import org.jetbrains.concurrency.thenRun
import org.utbot.common.PathUtil.toPath
import org.utbot.framework.UtSettings
import org.utbot.framework.codegen.domain.ProjectType
import org.utbot.framework.codegen.domain.DependencyInjectionFramework
import org.utbot.framework.codegen.domain.ForceStaticMocking
import org.utbot.framework.codegen.domain.Junit4
import org.utbot.framework.codegen.domain.Junit5
import org.utbot.framework.codegen.domain.MockitoStaticMocking
import org.utbot.framework.codegen.domain.NoStaticMocking
import org.utbot.framework.codegen.domain.ParametrizedTestSource
import org.utbot.framework.codegen.domain.ProjectType
import org.utbot.framework.codegen.domain.SpringBeans
import org.utbot.framework.codegen.domain.TypeReplacementApproach
import org.utbot.framework.codegen.domain.StaticsMocking
import org.utbot.framework.codegen.domain.TestFramework
import org.utbot.framework.codegen.domain.TestNg
import org.utbot.framework.codegen.domain.TypeReplacementApproach
import org.utbot.framework.plugin.api.CodeGenerationSettingItem
import org.utbot.framework.plugin.api.CodegenLanguage
import org.utbot.framework.plugin.api.MockFramework
Expand Down Expand Up @@ -150,12 +130,32 @@ import org.utbot.intellij.plugin.util.IntelliJApiHelper
import org.utbot.intellij.plugin.util.SpringConfigurationsHelper
import org.utbot.intellij.plugin.util.extractFirstLevelMembers
import org.utbot.intellij.plugin.util.findSdkVersion
import java.awt.BorderLayout
import java.awt.Color
import java.awt.Component
import java.awt.Dimension
import java.awt.event.ActionEvent
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.text.ParseException
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import javax.swing.JTextField
import java.util.concurrent.TimeUnit
import javax.swing.AbstractAction
import javax.swing.Action
import javax.swing.DefaultComboBoxModel
import javax.swing.JButton
import javax.swing.JCheckBox
import javax.swing.JComboBox
import javax.swing.JComponent
import javax.swing.JList
import javax.swing.JSpinner
import javax.swing.text.DefaultFormatter
import kotlin.io.path.notExists


private const val RECENTS_KEY = "org.utbot.recents"

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

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

private val timeoutSpinner =
JBIntSpinner(TimeUnit.MILLISECONDS.toSeconds(model.timeout).toInt(), 1, Int.MAX_VALUE, 1).also {
Expand Down Expand Up @@ -394,6 +393,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
row("Testing framework:") {
cell(testFrameworks)
}

if (model.projectType == ProjectType.Spring) {
row("Spring configuration:") {
cell(springConfig)
Expand All @@ -404,23 +404,17 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
"Mocks will be used when necessary."
)
}
row {
cell(selectProfile)
row("Active profile(s):") {
cell(profileNames)
contextHelp(
"Only selected profile will be active.<br>" +
"Otherwise, profile from the configuration class or default one is used."
"One or several comma-separated names.<br>" +
"If all names are incorrect, default profile is used"
)
}
indent {
row("Profile name(s):") {
cell(profileExpression).align(Align.FILL)
contextHelp(
"Profile name or expression like \"prod|web\" may be passed here.<br>" +
"If expression is incorrect, default profile will be used"
)
}.enabledIf(selectProfile.selected)
}
}.enabledIf(
ComboBoxPredicate(springConfig) { springConfig.item != NO_SPRING_CONFIGURATION_OPTION }
)
}

row("Mocking strategy:") {
cell(mockStrategies)
contextHelp(
Expand Down Expand Up @@ -664,7 +658,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
TypeReplacementApproach.ReplaceIfPossible(fullConfigName)
}
}
model.profileExpression = profileExpression.text
model.profileNames = profileNames.text.let { it.ifEmpty { DEFAULT_SPRING_PROFILE_NAME } }

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

selectProfile.isEnabled = true
selectProfile.isSelected = false
profileNames.isEnabled = true
} else {
mockStrategies.item = when (model.projectType) {
ProjectType.Spring -> MockStrategyApi.springDefaultItem
Expand All @@ -1072,15 +1065,11 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
mockStrategies.isEnabled = true
updateMockStrategyList()

selectProfile.isEnabled = false
selectProfile.isSelected = false
profileNames.isEnabled = false
profileNames.text = ""
}
}

selectProfile.addActionListener { _ ->
profileExpression.text = if (selectProfile.isSelected) "" else DEFAULT_SPRING_PROFILE_NAME
}

cbSpecifyTestPackage.addActionListener {
val testPackageName = findTestPackageComboValue()
val packageNameIsNeeded = testPackageField.isEnabled || testPackageName != SAME_PACKAGE_LABEL
Expand Down Expand Up @@ -1157,8 +1146,6 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
private fun updateSpringConfigurationEnabled() {
// We check for > 1 because there is already extra-dummy NO_SPRING_CONFIGURATION_OPTION option
springConfig.isEnabled = model.projectType == ProjectType.Spring && springConfig.itemCount > 1
selectProfile.isEnabled = false
selectProfile.isSelected = false
}

private fun staticsMockingConfigured(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.utbot.spring.utils

import com.jetbrains.rd.util.getLogger
import com.jetbrains.rd.util.info
import org.springframework.core.env.ConfigurableEnvironment
import org.springframework.core.env.StandardEnvironment
import org.utbot.spring.api.ApplicationData

private val logger = getLogger<EnvironmentFactory>()

class EnvironmentFactory(
private val applicationData: ApplicationData
) {
Expand All @@ -12,15 +16,33 @@ class EnvironmentFactory(
}

fun createEnvironment(): ConfigurableEnvironment {
val profilesToActivate = parseProfileExpression(applicationData.profileExpression ?: DEFAULT_PROFILE_NAME)
val profilesToActivate = parseProfileExpression(applicationData.profileExpression)

val environment = StandardEnvironment()
environment.setActiveProfiles(*profilesToActivate)

try {
environment.setActiveProfiles(*profilesToActivate)
} catch (e: Exception) {
logger.info { "Setting ${applicationData.profileExpression} as active profiles failed with exception $e" }
}

return environment
}

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

return profileExpression
.filter { !it.isWhitespace() }
.split(',')
.toTypedArray()
}
}