Skip to content

Commit 038286d

Browse files
tepa46EgorkaKulikov
authored andcommitted
Added the ability to shorten the paths to SpringConfigClasses and SpringConfigXml in the UI
1 parent 7b5463c commit 038286d

File tree

2 files changed

+114
-5
lines changed

2 files changed

+114
-5
lines changed

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,11 @@ import org.utbot.intellij.plugin.ui.utils.parseVersion
152152
import org.utbot.intellij.plugin.ui.utils.testResourceRootTypes
153153
import org.utbot.intellij.plugin.ui.utils.testRootType
154154
import org.utbot.intellij.plugin.util.IntelliJApiHelper
155+
import org.utbot.intellij.plugin.util.SpringConfigurationsHelper
155156
import org.utbot.intellij.plugin.util.extractFirstLevelMembers
156157
import org.utbot.intellij.plugin.util.findSdkVersion
158+
import java.awt.Component
159+
import java.io.File
157160
import java.time.LocalDateTime
158161
import java.time.format.DateTimeFormatter
159162
import java.util.*
@@ -169,6 +172,8 @@ private const val NO_SPRING_CONFIGURATION_OPTION = "No configuration"
169172
private const val ACTION_GENERATE = "Generate Tests"
170173
private const val ACTION_GENERATE_AND_RUN = "Generate and Run"
171174

175+
private const val JAVA_CONFIG_SEPARATOR = "."
176+
172177
class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(model.project) {
173178
companion object {
174179
const val minSupportedSdkVersion = 8
@@ -192,12 +197,25 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
192197
private val codegenLanguages = createComboBox(CodegenLanguage.values())
193198
private val testFrameworks = createComboBox(TestFramework.allItems.toTypedArray())
194199

195-
private val modelSpringConfigs = setOf(
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(
196213
null to listOf(NO_SPRING_CONFIGURATION_OPTION),
197-
"Java-based configurations" to model.getSortedSpringConfigurationClasses(),
198-
"XML-based configurations" to model.getSpringXMLConfigurationFiles()
214+
"Java-based configurations" to shortenedSortedSpringConfigurationClasses,
215+
"XML-based configurations" to shortenedSpringXMLConfigurationFiles
199216
)
200-
private val springConfig = createComboBoxWithSeparatorsForSpringConfigs(modelSpringConfigs)
217+
218+
private val springConfig = createComboBoxWithSeparatorsForSpringConfigs(shortenedSpringConfigNames)
201219

202220
private val mockStrategies = createComboBox(MockStrategyApi.values())
203221
private val staticsMocking = JCheckBox("Mock static methods")
@@ -1116,7 +1134,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
11161134
private fun updateSpringConfigurationEnabled() {
11171135
// We check for > 1 because there is already extra-dummy NO_SPRING_CONFIGURATION_OPTION option
11181136
springConfig.isEnabled = model.projectType == ProjectType.Spring
1119-
&& modelSpringConfigs.size > 1
1137+
&& shortenedSpringConfigNames.size > 1
11201138
}
11211139

11221140
private fun staticsMockingConfigured(): Boolean {
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package org.utbot.intellij.plugin.util
2+
3+
/**
4+
* Getting from spring Configuration Classes and Spring XML Configuration Files shortened paths
5+
*
6+
* How is this done:
7+
* - Parent directories are appended to the file name until the path becomes unique
8+
*
9+
* Example:
10+
* - [["config.web.WebConfig", "config.web2.WebConfig", "config.web.AnotherConfig"]] ->
11+
* [["web.WebConfig", "web2.WebConfig", "AnotherConfig"]]
12+
*/
13+
object SpringConfigurationsHelper {
14+
15+
private var separator = ""
16+
17+
data class PathData(private var shortenedPath: String) {
18+
19+
private val pathConstructor: MutableList<String> = shortenedPath.split(separator).toMutableList()
20+
21+
init {
22+
try {
23+
shortenedPath = pathConstructor.last()
24+
pathConstructor.removeLast()
25+
} catch (e: Exception) {
26+
println("Path [$shortenedPath] can't be extended")
27+
}
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+
}
43+
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)
51+
}
52+
pathsData.addAll(pathsDataMap.values)
53+
}
54+
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+
}
62+
63+
fun shortenSpringConfigNames(paths: Set<String>, separator: String): Set<String> {
64+
SpringConfigurationsHelper.separator = separator
65+
66+
val pathsDataMap = mutableMapOf<String, PathData>()
67+
var pathsData = mutableListOf<PathData>()
68+
69+
preparePathsData(paths, pathsData, pathsDataMap)
70+
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()
75+
76+
var maxIndWithSamePath = ind
77+
while (maxIndWithSamePath < pathsData.size) {
78+
if (pathsData[maxIndWithSamePath].getShortenedPath() == curShortenedPath) maxIndWithSamePath++
79+
else break
80+
}
81+
82+
if (ind == maxIndWithSamePath - 1) continue
83+
for (i in ind until maxIndWithSamePath) {
84+
if (!pathsData[i].addPathParentDir()) return paths
85+
}
86+
break
87+
}
88+
}
89+
return getMinimizedPaths(pathsDataMap)
90+
}
91+
}

0 commit comments

Comments
 (0)