Skip to content

Commit

Permalink
If invalid value is set for a property in settings.properties, defaul…
Browse files Browse the repository at this point in the history
…t value should be applied #1437

Add tests
  • Loading branch information
Vassiliy-Kudryashov committed Dec 6, 2022
1 parent a47bc27 commit 509ae72
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 12 deletions.
29 changes: 17 additions & 12 deletions utbot-core/src/main/kotlin/org/utbot/common/AbstractSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,6 +18,8 @@ interface SettingsContainer {
converter: (String) -> T
): PropertyDelegateProvider<Any?, ReadWriteProperty<Any?, T>>

fun getInputStream() : InputStream? = null

// Returns true iff some properties have non-default values
fun isCustomized() = false
}
Expand All @@ -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,
Expand All @@ -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<KProperty<*>, Any?> = mutableMapOf()
private var customized: Boolean = false

Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 509ae72

Please sign in to comment.