Skip to content

Commit f0957a2

Browse files
Settings cannot be loaded because of NullPointerException #951
1 parent fcc4d81 commit f0957a2

File tree

5 files changed

+86
-32
lines changed

5 files changed

+86
-32
lines changed

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/Api.kt

+17-8
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,7 @@ open class TypeParameters(val parameters: List<ClassId> = emptyList())
10451045
class WildcardTypeParameter : TypeParameters(emptyList())
10461046

10471047
interface CodeGenerationSettingItem {
1048+
val id : String
10481049
val displayName: String
10491050
val description: String
10501051
}
@@ -1057,20 +1058,23 @@ interface CodeGenerationSettingBox {
10571058
}
10581059

10591060
enum class MockStrategyApi(
1061+
override val id : String,
10601062
override val displayName: String,
10611063
override val description: String
10621064
) : CodeGenerationSettingItem {
1063-
NO_MOCKS("Do not mock", "Do not use mock frameworks at all"),
1065+
NO_MOCKS("No mocks", "Do not mock", "Do not use mock frameworks at all"),
10641066
OTHER_PACKAGES(
1067+
"Other packages: Mockito",
10651068
"Mock package environment",
10661069
"Mock all classes outside the current package except system ones"
10671070
),
10681071
OTHER_CLASSES(
1072+
"Other classes: Mockito",
10691073
"Mock class environment",
10701074
"Mock all classes outside the class under test except system ones"
10711075
);
10721076

1073-
override fun toString() = displayName
1077+
override fun toString() = id
10741078

10751079
// Get is mandatory because of the initialization order of the inheritors.
10761080
// Otherwise, in some cases we could get an incorrect value
@@ -1081,20 +1085,23 @@ enum class MockStrategyApi(
10811085
}
10821086

10831087
enum class TreatOverflowAsError(
1088+
override val id : String,
10841089
override val displayName: String,
10851090
override val description: String,
10861091
) : CodeGenerationSettingItem {
10871092
AS_ERROR(
1093+
id = "Treat overflows as errors",
10881094
displayName = "Treat overflows as errors",
10891095
description = "Generate tests that treat possible overflows in arithmetic operations as errors " +
10901096
"that throw Arithmetic Exception",
10911097
),
10921098
IGNORE(
1099+
id = "Ignore overflows",
10931100
displayName = "Ignore overflows",
10941101
description = "Ignore possible overflows in arithmetic operations",
10951102
);
10961103

1097-
override fun toString(): String = displayName
1104+
override fun toString(): String = id
10981105

10991106
// Get is mandatory because of the initialization order of the inheritors.
11001107
// Otherwise, in some cases we could get an incorrect value
@@ -1105,13 +1112,14 @@ enum class TreatOverflowAsError(
11051112
}
11061113

11071114
enum class MockFramework(
1115+
override val id: String = "Mockito",
11081116
override val displayName: String,
11091117
override val description: String = "Use $displayName as mock framework",
11101118
var isInstalled: Boolean = false
11111119
) : CodeGenerationSettingItem {
1112-
MOCKITO("Mockito");
1120+
MOCKITO(displayName = "Mockito");
11131121

1114-
override fun toString() = displayName
1122+
override fun toString() = id
11151123

11161124
companion object : CodeGenerationSettingBox {
11171125
override val defaultItem: MockFramework = MOCKITO
@@ -1120,11 +1128,12 @@ enum class MockFramework(
11201128
}
11211129

11221130
enum class CodegenLanguage(
1131+
override val id: String,
11231132
override val displayName: String,
11241133
@Suppress("unused") override val description: String = "Generate unit tests in $displayName"
11251134
) : CodeGenerationSettingItem {
1126-
JAVA(displayName = "Java"),
1127-
KOTLIN(displayName = "Kotlin (experimental)");
1135+
JAVA(id = "Java", displayName = "Java"),
1136+
KOTLIN(id = "Kotlin", displayName = "Kotlin (experimental)");
11281137

11291138
enum class OperatingSystem {
11301139
WINDOWS,
@@ -1163,7 +1172,7 @@ enum class CodegenLanguage(
11631172
KOTLIN -> listOf(System.getenv("JAVA_HOME"), "bin", "java")
11641173
}.joinToString(File.separator)
11651174

1166-
override fun toString(): String = displayName
1175+
override fun toString(): String = id
11671176

11681177
fun getCompilationCommand(buildDirectory: String, classPath: String, sourcesFiles: List<String>): List<String> {
11691178
val arguments = when (this) {

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/Domain.kt

+21-9
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,11 @@ fun testFrameworkByName(testFramework: String): TestFramework =
103103
*/
104104
sealed class StaticsMocking(
105105
var isConfigured: Boolean = false,
106+
override val id: String,
106107
override val displayName: String,
107108
override val description: String = "Use static methods mocking"
108109
) : CodeGenerationSettingItem {
109-
override fun toString(): String = displayName
110+
override fun toString(): String = id
110111

111112
// Get is mandatory because of the initialization order of the inheritors.
112113
// Otherwise, in some cases we could get an incorrect value
@@ -119,11 +120,12 @@ sealed class StaticsMocking(
119120
}
120121

121122
object NoStaticMocking : StaticsMocking(
123+
id = "No static mocking",
122124
displayName = "No static mocking",
123125
description = "Do not use additional settings to mock static fields"
124126
)
125127

126-
object MockitoStaticMocking : StaticsMocking(displayName = "Mockito static mocking") {
128+
object MockitoStaticMocking : StaticsMocking(id = "Mockito static mocking", displayName = "Mockito static mocking") {
127129

128130
val mockedStaticClassId = BuiltinClassId(
129131
name = "org.mockito.MockedStatic",
@@ -170,6 +172,7 @@ object MockitoStaticMocking : StaticsMocking(displayName = "Mockito static mocki
170172
}
171173

172174
sealed class TestFramework(
175+
override val id: String,
173176
override val displayName: String,
174177
override val description: String = "Use $displayName as test framework",
175178
) : CodeGenerationSettingItem {
@@ -235,7 +238,7 @@ sealed class TestFramework(
235238
additionalArguments: List<String>
236239
): List<String>
237240

238-
override fun toString() = displayName
241+
override fun toString() = id
239242

240243
// Get is mandatory because of the initialization order of the inheritors.
241244
// Otherwise, in some cases we could get an incorrect value, i.e. allItems = [null, JUnit5, TestNg]
@@ -246,7 +249,7 @@ sealed class TestFramework(
246249
}
247250
}
248251

249-
object TestNg : TestFramework(displayName = "TestNG") {
252+
object TestNg : TestFramework(id = "TestNG",displayName = "TestNG") {
250253
override val mainPackage: String = TEST_NG_PACKAGE
251254
override val testAnnotation: String = "@$mainPackage.Test"
252255
override val testAnnotationFqn: String = "$mainPackage.Test"
@@ -375,7 +378,7 @@ object TestNg : TestFramework(displayName = "TestNG") {
375378
""".trimIndent()
376379
}
377380

378-
object Junit4 : TestFramework("JUnit4") {
381+
object Junit4 : TestFramework(id = "JUnit4",displayName = "JUnit4") {
379382
private val parametrizedTestsNotSupportedError: Nothing
380383
get() = error("Parametrized tests are not supported for JUnit4")
381384

@@ -448,7 +451,7 @@ object Junit4 : TestFramework("JUnit4") {
448451
}
449452
}
450453

451-
object Junit5 : TestFramework("JUnit5") {
454+
object Junit5 : TestFramework(id = "JUnit5", displayName = "JUnit5") {
452455
override val mainPackage: String = JUNIT5_PACKAGE
453456
override val testAnnotation = "@$mainPackage.Test"
454457
override val testAnnotationFqn: String = "$mainPackage.Test"
@@ -589,20 +592,23 @@ object Junit5 : TestFramework("JUnit5") {
589592
}
590593

591594
enum class RuntimeExceptionTestsBehaviour(
595+
override val id: String,
592596
override val displayName: String,
593597
override val description: String
594598
) : CodeGenerationSettingItem {
595599
PASS(
600+
id = "Passing",
596601
displayName = "Pass",
597602
description = "Tests that produce Runtime exceptions should pass (by inserting throwable assertion)"
598603
),
599604
FAIL(
605+
id = "Failing",
600606
displayName = "Fail",
601607
description = "Tests that produce Runtime exceptions should fail" +
602608
"(WARNING!: failing tests may appear in testing class)"
603609
);
604610

605-
override fun toString(): String = displayName
611+
override fun toString(): String = id
606612

607613
// Get is mandatory because of the initialization order of the inheritors.
608614
// Otherwise, in some cases we could get an incorrect value
@@ -623,11 +629,13 @@ data class HangingTestsTimeout(val timeoutMs: Long) {
623629
}
624630

625631
enum class ForceStaticMocking(
632+
override val id: String,
626633
override val displayName: String,
627634
override val description: String,
628635
val warningMessage: List<String>,
629636
) : CodeGenerationSettingItem {
630637
FORCE(
638+
id = "Force static mocking",
631639
displayName = "Force static mocking",
632640
description = "Use mocks for static methods and constructors invocations even if static mocking is disabled" +
633641
"(WARNING!: can add imports from missing dependencies)",
@@ -638,6 +646,7 @@ enum class ForceStaticMocking(
638646
)
639647
),
640648
DO_NOT_FORCE(
649+
id = "Do not force static mocking",
641650
displayName = "Do not force static mocking",
642651
description = "Do not force static mocking if static mocking setting is disabled" +
643652
"(WARNING!: flaky tests can appear)",
@@ -647,7 +656,7 @@ enum class ForceStaticMocking(
647656
)
648657
);
649658

650-
override fun toString(): String = displayName
659+
override fun toString(): String = id
651660

652661
// Get is mandatory because of the initialization order of the inheritors.
653662
// Otherwise, in some cases we could get an incorrect value
@@ -658,19 +667,22 @@ enum class ForceStaticMocking(
658667
}
659668

660669
enum class ParametrizedTestSource(
670+
override val id: String,
661671
override val displayName: String,
662672
override val description: String = "Use $displayName for parametrized tests"
663673
) : CodeGenerationSettingItem {
664674
DO_NOT_PARAMETRIZE(
675+
id = "Not parametrized",
665676
displayName = "Not parametrized",
666677
description = "Do not generate parametrized tests"
667678
),
668679
PARAMETRIZE(
680+
id = "Parametrized",
669681
displayName = "Parametrized",
670682
description = "Generate parametrized tests"
671683
);
672684

673-
override fun toString(): String = displayName
685+
override fun toString(): String = id
674686

675687
// Get is mandatory because of the initialization order of the inheritors.
676688
// Otherwise, in some cases we could get an incorrect value

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/settings/SettingsWindow.kt

+6-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.utbot.framework.codegen.RuntimeExceptionTestsBehaviour
2424
import org.utbot.framework.plugin.api.CodeGenerationSettingItem
2525
import org.utbot.framework.plugin.api.CodegenLanguage
2626
import org.utbot.framework.plugin.api.TreatOverflowAsError
27+
import org.utbot.intellij.plugin.ui.components.CodeGenerationSettingItemRenderer
2728

2829
class SettingsWindow(val project: Project) {
2930
private val settings = project.service<Settings>()
@@ -36,7 +37,7 @@ class SettingsWindow(val project: Project) {
3637
val valuesComboBox: LayoutBuilder.(KClass<*>, Array<*>) -> Unit = { loader, values ->
3738
val serviceLabels = mapOf(
3839
CodegenLanguage::class to "Generated test language:",
39-
RuntimeExceptionTestsBehaviour::class to "Test with exceptions:",
40+
RuntimeExceptionTestsBehaviour::class to "Tests with exceptions:",
4041
TreatOverflowAsError::class to "Overflow detection:",
4142
)
4243
val tooltipLabels = mapOf(
@@ -49,7 +50,10 @@ class SettingsWindow(val project: Project) {
4950
DefaultComboBoxModel(values),
5051
getter = { settings.providerNameByServiceLoader(loader) },
5152
setter = { settings.setProviderByLoader(loader, it as CodeGenerationSettingItem) },
52-
).apply { ContextHelpLabel.create(tooltipLabels[loader] ?: return@apply)() }
53+
).apply {
54+
component.renderer = CodeGenerationSettingItemRenderer()
55+
ContextHelpLabel.create(tooltipLabels[loader] ?: return@apply)()
56+
}
5357
}
5458
}
5559
}

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

+20-13
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ import org.utbot.intellij.plugin.models.mockitoCoreLibraryDescriptor
124124
import org.utbot.intellij.plugin.models.packageName
125125
import org.utbot.intellij.plugin.models.testNgLibraryDescriptor
126126
import org.utbot.intellij.plugin.settings.Settings
127+
import org.utbot.intellij.plugin.ui.components.CodeGenerationSettingItemRenderer
127128
import org.utbot.intellij.plugin.ui.components.TestFolderComboWithBrowseButton
128129
import org.utbot.intellij.plugin.ui.utils.LibrarySearchScope
129130
import org.utbot.intellij.plugin.ui.utils.addSourceRootIfAbsent
@@ -168,9 +169,9 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
168169

169170
private val testSourceFolderField = TestFolderComboWithBrowseButton(model)
170171

171-
private val codegenLanguages = ComboBox(DefaultComboBoxModel(CodegenLanguage.values()))
172-
private val testFrameworks = ComboBox(DefaultComboBoxModel(TestFramework.allItems.toTypedArray()))
173-
private val mockStrategies = ComboBox(DefaultComboBoxModel(MockStrategyApi.values()))
172+
private val codegenLanguages = createComboBox(CodegenLanguage.values())
173+
private val testFrameworks = createComboBox(TestFramework.allItems.toTypedArray())
174+
private val mockStrategies = createComboBox(MockStrategyApi.values())
174175
private val staticsMocking = JCheckBox("Mock static methods")
175176
private val timeoutSpinner =
176177
JBIntSpinner(
@@ -191,6 +192,12 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
191192
parametrizedTestSources to null
192193
)
193194

195+
private fun <T> createComboBox(values: Array<T>) : ComboBox<T> {
196+
return ComboBox<T>(DefaultComboBoxModel(values)).also {
197+
it.renderer = CodeGenerationSettingItemRenderer()
198+
}
199+
}
200+
194201
private fun createHelpLabel(commonTooltip: String? = null) = JBLabel(AllIcons.General.ContextHelp).apply {
195202
if (!commonTooltip.isNullOrEmpty()) toolTipText = commonTooltip
196203
}
@@ -239,22 +246,22 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
239246
row("Test source root:") {
240247
component(testSourceFolderField)
241248
}
242-
row("Code generation language:") {
243-
makePanelWithHelpTooltip(
244-
codegenLanguages as ComboBox<CodeGenerationSettingItem>,
245-
itemsToHelpTooltip[codegenLanguages]
246-
)
247-
}.visible = false
249+
// row("Code generation language:") {
250+
// makePanelWithHelpTooltip(
251+
// codegenLanguages,
252+
// itemsToHelpTooltip[codegenLanguages]
253+
// )
254+
// }
248255
row("Testing framework:") {
249256
makePanelWithHelpTooltip(
250-
testFrameworks as ComboBox<CodeGenerationSettingItem>,
257+
testFrameworks,
251258
null
252259
)
253260
}
254261
row { component(parametrizedTestSources) }
255262
row("Mock strategy:") {
256263
makePanelWithHelpTooltip(
257-
mockStrategies as ComboBox<CodeGenerationSettingItem>,
264+
mockStrategies,
258265
ContextHelpLabel.create("Mock everything around the target class or the whole package except the system classes. Otherwise mock nothing.")
259266
)
260267
}
@@ -959,7 +966,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
959966
list: JList<out TestFramework>, value: TestFramework?,
960967
index: Int, selected: Boolean, hasFocus: Boolean
961968
) {
962-
this.append(value.toString(), SimpleTextAttributes.REGULAR_ATTRIBUTES)
969+
this.append(value?.displayName?:"<null>", SimpleTextAttributes.REGULAR_ATTRIBUTES)
963970
if (value == null || !value.isInstalled) {
964971
this.append(WILL_BE_INSTALLED_LABEL, SimpleTextAttributes.ERROR_ATTRIBUTES)
965972
}
@@ -984,7 +991,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
984991
list: JList<out MockStrategyApi>, value: MockStrategyApi?,
985992
index: Int, selected: Boolean, hasFocus: Boolean
986993
) {
987-
this.append(value.toString(), SimpleTextAttributes.REGULAR_ATTRIBUTES)
994+
this.append(value?.displayName?:"<null>", SimpleTextAttributes.REGULAR_ATTRIBUTES)
988995
if (value != MockStrategyApi.NO_MOCKS && !MOCKITO.isInstalled) {
989996
this.append(WILL_BE_INSTALLED_LABEL, SimpleTextAttributes.ERROR_ATTRIBUTES)
990997
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.utbot.intellij.plugin.ui.components
2+
3+
import java.awt.Component
4+
import javax.swing.DefaultListCellRenderer
5+
import javax.swing.JList
6+
import org.utbot.framework.plugin.api.CodeGenerationSettingItem
7+
8+
open class CodeGenerationSettingItemRenderer : DefaultListCellRenderer() {
9+
override fun getListCellRendererComponent(
10+
list: JList<*>?,
11+
value: Any?,
12+
index: Int,
13+
isSelected: Boolean,
14+
cellHasFocus: Boolean
15+
): Component {
16+
return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus).apply {
17+
if (value is CodeGenerationSettingItem) {
18+
text = value.displayName
19+
}
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)