Skip to content

Commit 8d3b838

Browse files
committed
First refactoring of SpringConfigurationsHelper
1 parent 038286d commit 8d3b838

File tree

2 files changed

+81
-85
lines changed

2 files changed

+81
-85
lines changed

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

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ import com.intellij.ui.layout.ComboBoxPredicate
6565
import com.intellij.ui.layout.Row
6666
import com.intellij.ui.layout.enableIf
6767
import com.intellij.ui.layout.panel
68-
import com.intellij.ui.layout.selectedValueMatches
6968
import com.intellij.util.IncorrectOperationException
7069
import com.intellij.util.io.exists
7170
import com.intellij.util.lang.JavaVersion
@@ -120,7 +119,6 @@ import org.utbot.framework.plugin.api.MockFramework
120119
import org.utbot.framework.plugin.api.MockFramework.MOCKITO
121120
import org.utbot.framework.plugin.api.MockStrategyApi
122121
import org.utbot.framework.plugin.api.TreatOverflowAsError
123-
import org.utbot.framework.plugin.api.isSummarizationCompatible
124122
import org.utbot.framework.plugin.api.utils.MOCKITO_EXTENSIONS_FILE_CONTENT
125123
import org.utbot.framework.plugin.api.utils.MOCKITO_EXTENSIONS_FOLDER
126124
import org.utbot.framework.plugin.api.utils.MOCKITO_MOCKMAKER_FILE_NAME
@@ -155,7 +153,6 @@ import org.utbot.intellij.plugin.util.IntelliJApiHelper
155153
import org.utbot.intellij.plugin.util.SpringConfigurationsHelper
156154
import org.utbot.intellij.plugin.util.extractFirstLevelMembers
157155
import org.utbot.intellij.plugin.util.findSdkVersion
158-
import java.awt.Component
159156
import java.io.File
160157
import java.time.LocalDateTime
161158
import java.time.format.DateTimeFormatter
@@ -172,8 +169,6 @@ private const val NO_SPRING_CONFIGURATION_OPTION = "No configuration"
172169
private const val ACTION_GENERATE = "Generate Tests"
173170
private const val ACTION_GENERATE_AND_RUN = "Generate and Run"
174171

175-
private const val JAVA_CONFIG_SEPARATOR = "."
176-
177172
class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(model.project) {
178173
companion object {
179174
const val minSupportedSdkVersion = 8
@@ -197,25 +192,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
197192
private val codegenLanguages = createComboBox(CodegenLanguage.values())
198193
private val testFrameworks = createComboBox(TestFramework.allItems.toTypedArray())
199194

200-
private val shortenedSortedSpringConfigurationClasses =
201-
SpringConfigurationsHelper.shortenSpringConfigNames(
202-
model.getSortedSpringConfigurationClasses(),
203-
JAVA_CONFIG_SEPARATOR
204-
)
205-
206-
private val shortenedSpringXMLConfigurationFiles =
207-
SpringConfigurationsHelper.shortenSpringConfigNames(
208-
model.getSpringXMLConfigurationFiles(),
209-
File.separator
210-
)
211-
212-
private val shortenedSpringConfigNames = setOf(
213-
null to listOf(NO_SPRING_CONFIGURATION_OPTION),
214-
"Java-based configurations" to shortenedSortedSpringConfigurationClasses,
215-
"XML-based configurations" to shortenedSpringXMLConfigurationFiles
216-
)
217-
218-
private val springConfig = createComboBoxWithSeparatorsForSpringConfigs(shortenedSpringConfigNames)
195+
private val springConfig = createComboBoxWithSeparatorsForSpringConfigs(shortenConfigurationNames())
219196

220197
private val mockStrategies = createComboBox(MockStrategyApi.values())
221198
private val staticsMocking = JCheckBox("Mock static methods")
@@ -241,6 +218,24 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
241218
parametrizedTestSources to null
242219
)
243220

221+
222+
private val javaConfigurationHelper = SpringConfigurationsHelper(".")
223+
private val xmlConfigurationHelper = SpringConfigurationsHelper(File.separator)
224+
225+
private fun shortenConfigurationNames(): Set<Pair<String?, Collection<String>>> {
226+
val shortenedSortedSpringConfigurationClasses =
227+
javaConfigurationHelper.shortenSpringConfigNames(model.getSortedSpringConfigurationClasses())
228+
229+
val shortenedSpringXMLConfigurationFiles =
230+
xmlConfigurationHelper.shortenSpringConfigNames(model.getSpringXMLConfigurationFiles())
231+
232+
return setOf(
233+
null to listOf(NO_SPRING_CONFIGURATION_OPTION),
234+
"Java-based configurations" to shortenedSortedSpringConfigurationClasses,
235+
"XML-based configurations" to shortenedSpringXMLConfigurationFiles
236+
)
237+
}
238+
244239
private fun <T : CodeGenerationSettingItem> createComboBox(values: Array<T>) : ComboBox<T> {
245240
val comboBox = object:ComboBox<T>(DefaultComboBoxModel(values)) {
246241
var maxWidth = 0
@@ -598,7 +593,17 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
598593
model.typeReplacementApproach =
599594
when (springConfig.item.getItem()) {
600595
NO_SPRING_CONFIGURATION_OPTION -> TypeReplacementApproach.DoNotReplace
601-
else -> TypeReplacementApproach.ReplaceIfPossible(springConfig.item.getItem().toString())
596+
else -> {
597+
val shortConfigName = springConfig.item.getItem().toString()
598+
//TODO: avoid this check on xml here, merge two helpers into one
599+
val fullConfigName = if (shortConfigName.endsWith(".xml")) {
600+
xmlConfigurationHelper.restoreFullName(shortConfigName)
601+
} else {
602+
javaConfigurationHelper.restoreFullName(shortConfigName)
603+
}
604+
605+
TypeReplacementApproach.ReplaceIfPossible(fullConfigName)
606+
}
602607
}
603608

604609
val settings = model.project.service<Settings>()
@@ -1133,8 +1138,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
11331138

11341139
private fun updateSpringConfigurationEnabled() {
11351140
// We check for > 1 because there is already extra-dummy NO_SPRING_CONFIGURATION_OPTION option
1136-
springConfig.isEnabled = model.projectType == ProjectType.Spring
1137-
&& shortenedSpringConfigNames.size > 1
1141+
springConfig.isEnabled = model.projectType == ProjectType.Spring && springConfig.itemCount > 1
11381142
}
11391143

11401144
private fun staticsMockingConfigured(): Boolean {

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/util/SpringConfigurationsHelper.kt

Lines changed: 50 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,82 +10,74 @@ package org.utbot.intellij.plugin.util
1010
* - [["config.web.WebConfig", "config.web2.WebConfig", "config.web.AnotherConfig"]] ->
1111
* [["web.WebConfig", "web2.WebConfig", "AnotherConfig"]]
1212
*/
13-
object SpringConfigurationsHelper {
13+
class SpringConfigurationsHelper(val separator: String) {
1414

15-
private var separator = ""
15+
private val nameToInfo = mutableMapOf<String, NameInfo>()
1616

17-
data class PathData(private var shortenedPath: String) {
17+
inner class NameInfo(val fullName: String) {
18+
val shortenedName: String
19+
get() = innerShortName
1820

19-
private val pathConstructor: MutableList<String> = shortenedPath.split(separator).toMutableList()
21+
private val pathFragments: MutableList<String> = fullName.split(separator).toMutableList()
22+
private var innerShortName = pathFragments.removeLast()
2023

21-
init {
22-
try {
23-
shortenedPath = pathConstructor.last()
24-
pathConstructor.removeLast()
25-
} catch (e: Exception) {
26-
println("Path [$shortenedPath] can't be extended")
24+
fun enlargeShortName(): Boolean {
25+
if (pathFragments.isEmpty()) {
26+
return false
2727
}
28-
}
29-
30-
fun getShortenedPath() = shortenedPath
31-
32-
fun addPathParentDir(): Boolean {
33-
return try {
34-
shortenedPath = pathConstructor.last() + separator + shortenedPath
35-
pathConstructor.removeLast()
36-
true
37-
} catch (e: Exception) {
38-
println("Path [$shortenedPath] can't be extended")
39-
false
40-
}
41-
}
42-
}
4328

44-
private fun preparePathsData(
45-
paths: Set<String>,
46-
pathsData: MutableList<PathData>,
47-
pathsDataMap: MutableMap<String, PathData>
48-
) {
49-
for (path in paths) {
50-
pathsDataMap[path] = PathData(path)
29+
val lastElement = pathFragments.removeLast()
30+
innerShortName = "${lastElement}$separator$innerShortName"
31+
return true
5132
}
52-
pathsData.addAll(pathsDataMap.values)
5333
}
5434

55-
private fun getMinimizedPaths(pathDataMap: MutableMap<String, PathData>): Set<String> {
56-
val shortenedPaths = mutableSetOf<String>()
57-
for (elem in pathDataMap.values) {
58-
shortenedPaths.add(elem.getShortenedPath())
59-
}
60-
return shortenedPaths.toSet()
61-
}
35+
fun restoreFullName(shortenedName: String): String =
36+
nameToInfo
37+
.values
38+
.singleOrNull { it.shortenedName == shortenedName }
39+
?.fullName
40+
?: error("Full name of configuration file cannot be restored by shortened name $shortenedName")
6241

63-
fun shortenSpringConfigNames(paths: Set<String>, separator: String): Set<String> {
64-
SpringConfigurationsHelper.separator = separator
42+
fun shortenSpringConfigNames(fullNames: Set<String>): Set<String> {
43+
fullNames.forEach { nameToInfo[it] = NameInfo(it) }
44+
var nameInfoCollection = nameToInfo.values
6545

66-
val pathsDataMap = mutableMapOf<String, PathData>()
67-
var pathsData = mutableListOf<PathData>()
46+
while (nameInfoCollection.size != nameInfoCollection.distinct().size) {
47+
nameInfoCollection = nameInfoCollection.sortedBy { it.shortenedName }.toMutableList()
6848

69-
preparePathsData(paths, pathsData, pathsDataMap)
49+
for (index in nameInfoCollection.indices) {
50+
val curShortenedPath = nameInfoCollection[index].shortenedName
7051

71-
while (pathsData.size != pathsData.distinct().size) {
72-
pathsData = pathsData.sortedBy { it.getShortenedPath() }.toMutableList()
73-
for (ind in pathsData.indices) {
74-
val curShortenedPath = pathsData[ind].getShortenedPath()
52+
// here we search a block of shortened paths that are equivalent
53+
// and must be enlarged with new fragment so on.
54+
var maxIndexWithSamePath = index
55+
while (maxIndexWithSamePath < nameInfoCollection.size) {
56+
if (nameInfoCollection[maxIndexWithSamePath].shortenedName == curShortenedPath) {
57+
maxIndexWithSamePath++
58+
}
59+
else {
60+
break
61+
}
62+
}
7563

76-
var maxIndWithSamePath = ind
77-
while (maxIndWithSamePath < pathsData.size) {
78-
if (pathsData[maxIndWithSamePath].getShortenedPath() == curShortenedPath) maxIndWithSamePath++
79-
else break
64+
//if the size of this block is one, we should not enlarge it
65+
if (index == maxIndexWithSamePath - 1) {
66+
continue
8067
}
8168

82-
if (ind == maxIndWithSamePath - 1) continue
83-
for (i in ind until maxIndWithSamePath) {
84-
if (!pathsData[i].addPathParentDir()) return paths
69+
// otherwise, enlarge the block of shortened names with one new fragment
70+
for (i in index until maxIndexWithSamePath) {
71+
if (!nameInfoCollection[i].enlargeShortName()) {
72+
return collectShortenedNames()
73+
}
8574
}
86-
break
8775
}
8876
}
89-
return getMinimizedPaths(pathsDataMap)
77+
78+
return collectShortenedNames()
9079
}
80+
81+
private fun collectShortenedNames() = nameToInfo.values.mapTo(mutableSetOf()) { it.shortenedName }
82+
9183
}

0 commit comments

Comments
 (0)