Made the database config option typesafe, rather than relying on String properties

This commit is contained in:
Shams Asari
2017-11-25 15:55:31 +00:00
parent c4b333c50c
commit 1705df4d1f
26 changed files with 155 additions and 139 deletions

View File

@ -131,7 +131,13 @@ private fun Config.defaultToOldPath(property: KProperty<*>): String {
private fun parseEnum(enumType: Class<*>, name: String): Enum<*> = enumBridge<Proxy.Type>(uncheckedCast(enumType), name) // Any enum will do
private fun <T : Enum<T>> enumBridge(clazz: Class<T>, name: String): T = java.lang.Enum.valueOf(clazz, name)
private fun <T : Enum<T>> enumBridge(clazz: Class<T>, name: String): T {
try {
return java.lang.Enum.valueOf(clazz, name)
} catch (e: IllegalArgumentException) {
throw IllegalArgumentException("$name is not one of { ${clazz.enumConstants.joinToString()} }")
}
}
/**
* Convert the receiver object into a [Config]. This does the inverse action of [parseAs].

View File

@ -8,6 +8,7 @@ import net.corda.core.identity.CordaX500Name
import net.corda.core.internal.div
import net.corda.core.utilities.NetworkHostAndPort
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.Test
import java.net.URL
import java.nio.file.Path
@ -47,6 +48,14 @@ class ConfigParsingTest {
testPropertyType<EnumData, EnumListData, TestEnum>(TestEnum.Value2, TestEnum.Value1, valuesToString = true)
}
@Test
fun `unknown Enum`() {
val config = config("value" to "UnknownValue")
assertThatThrownBy { config.parseAs<EnumData>() }
.hasMessageContaining(TestEnum.Value1.name)
.hasMessageContaining(TestEnum.Value2.name)
}
@Test
fun `LocalDate`() {
testPropertyType<LocalDateData, LocalDateListData, LocalDate>(LocalDate.now(), LocalDate.now().plusDays(1), valuesToString = true)