From 509ae72c67c0c36333bfadb80e0fd33267b1d04c Mon Sep 17 00:00:00 2001 From: "Vassiliy.Kudryashov" Date: Tue, 6 Dec 2022 16:33:58 +0300 Subject: [PATCH] If invalid value is set for a property in settings.properties, default value should be applied #1437 Add tests --- .../org/utbot/common/AbstractSettings.kt | 29 ++++++----- .../org/utbot/common/AbstractSettingsTest.kt | 51 +++++++++++++++++++ 2 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 utbot-core/src/test/kotlin/org/utbot/common/AbstractSettingsTest.kt diff --git a/utbot-core/src/main/kotlin/org/utbot/common/AbstractSettings.kt b/utbot-core/src/main/kotlin/org/utbot/common/AbstractSettings.kt index 4b292cccdd..3c5e4bb78f 100644 --- a/utbot-core/src/main/kotlin/org/utbot/common/AbstractSettings.kt +++ b/utbot-core/src/main/kotlin/org/utbot/common/AbstractSettings.kt @@ -2,6 +2,7 @@ package org.utbot.common import java.io.FileInputStream import java.io.IOException +import java.io.InputStream import java.util.* import kotlin.Comparator import mu.KLogger @@ -17,6 +18,8 @@ interface SettingsContainer { converter: (String) -> T ): PropertyDelegateProvider> + fun getInputStream() : InputStream? = null + // Returns true iff some properties have non-default values fun isCustomized() = false } @@ -28,10 +31,10 @@ interface SettingsContainerFactory { defaultSettingsPath: String? = null) : SettingsContainer } -class PropertiesSettingsContainer( +internal open class PropertiesSettingsContainer( private val logger: KLogger, - defaultKeyForSettingsPath: String, - defaultSettingsPath: String? = null): SettingsContainer { + val defaultKeyForSettingsPath: String, + val defaultSettingsPath: String? = null): SettingsContainer { companion object: SettingsContainerFactory { override fun createSettingsContainer( logger: KLogger, @@ -41,19 +44,21 @@ class PropertiesSettingsContainer( } private val properties = Properties().also { props -> - val settingsPath = System.getProperty(defaultKeyForSettingsPath) ?: defaultSettingsPath - val settingsPathFile = settingsPath?.toPath()?.toFile() - if (settingsPathFile?.exists() == true) { - try { - FileInputStream(settingsPathFile).use { reader -> - props.load(reader) - } - } catch (e: IOException) { - logger.info(e) { e.message } + try { + getInputStream()?.use { + props.load(it) } + } catch (e: IOException) { + logger.info(e) { e.message } } } + override fun getInputStream() : InputStream? { + val settingsPath = System.getProperty(defaultKeyForSettingsPath) ?: defaultSettingsPath + val settingsPathFile = settingsPath?.toPath()?.toFile() + return if (settingsPathFile?.exists() == true) FileInputStream(settingsPathFile) else null + } + private val settingsValues: MutableMap, Any?> = mutableMapOf() private var customized: Boolean = false diff --git a/utbot-core/src/test/kotlin/org/utbot/common/AbstractSettingsTest.kt b/utbot-core/src/test/kotlin/org/utbot/common/AbstractSettingsTest.kt new file mode 100644 index 0000000000..8290771693 --- /dev/null +++ b/utbot-core/src/test/kotlin/org/utbot/common/AbstractSettingsTest.kt @@ -0,0 +1,51 @@ +package org.utbot.common + +import java.io.InputStream +import mu.KLogger +import mu.KotlinLogging +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test + + +internal class AbstractSettingsTest { + @Test + fun testParsing() { + var settings = createSettings("testBoolean=false\ntestInt=3\ntestIntRange=2\ntestLong=333\ntestLongRange=222") + Assertions.assertFalse(settings.testBoolean) + Assertions.assertEquals(3, settings.testInt) + Assertions.assertEquals(2, settings.testIntRange) + Assertions.assertEquals(333, settings.testLong) + Assertions.assertEquals(222, settings.testLongRange) + + settings = createSettings("testBoolean=invalid\ntestInt=-1\ntestIntRange=-1\ntestLong=-111\ntestLongRange=-111") + Assertions.assertTrue(settings.testBoolean) + Assertions.assertEquals(-1, settings.testInt) + Assertions.assertEquals(1, settings.testIntRange) + Assertions.assertEquals(-111, settings.testLong) + Assertions.assertEquals(111, settings.testLongRange) + + settings = createSettings("") + Assertions.assertTrue(settings.testBoolean) + Assertions.assertEquals(3, settings.testInt) + Assertions.assertEquals(333, settings.testLongRange) + } + + private fun createSettings(propertiesText: String): TestSettings { + AbstractSettings.setupFactory(object : SettingsContainerFactory { + override fun createSettingsContainer( + logger: KLogger, defaultKeyForSettingsPath: String, defaultSettingsPath: String? + ) = object : PropertiesSettingsContainer(logger, defaultKeyForSettingsPath, defaultSettingsPath) { + override fun getInputStream(): InputStream = propertiesText.byteInputStream() + } + }) + return TestSettings() + } + + internal class TestSettings : AbstractSettings(KotlinLogging.logger {}, "") { + val testBoolean: Boolean by getBooleanProperty(true) + val testInt: Int by getIntProperty(3) + val testIntRange: Int by getIntProperty(3, 1, 5) + val testLong: Long by getLongProperty(333) + val testLongRange: Long by getLongProperty(333, 111, 555) + } +} \ No newline at end of file