Add config loading tests
This commit is contained in:
parent
009ddd3d16
commit
d4635f82ae
@ -1,13 +1,10 @@
|
||||
package starter
|
||||
|
||||
import org.koin.core.context.startKoin
|
||||
import starter.modules.mainModule
|
||||
import starter.modules.pebbleModule
|
||||
import starter.modules.routesModule
|
||||
import starter.modules.templateModule
|
||||
import starter.modules.*
|
||||
|
||||
fun main() {
|
||||
startKoin {
|
||||
modules(mainModule, pebbleModule, templateModule, routesModule)
|
||||
modules(mainModule, pebbleModule, templateModule, configModule, routesModule)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
package starter.config
|
||||
|
||||
import com.electronwill.nightconfig.core.UnmodifiableConfig
|
||||
import com.electronwill.nightconfig.core.file.FileConfig
|
||||
import starter.*
|
||||
import com.electronwill.nightconfig.core.Config as NightConfig
|
||||
|
||||
@ -10,17 +9,19 @@ data class StarterConfig(
|
||||
val inputs: List<Input>,
|
||||
)
|
||||
|
||||
class Config {
|
||||
class Config(private val cfg: NightConfig) {
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
private fun FileConfig.configMap(key: String) = this.get<NightConfig>(key).valueMap() as Map<String, NightConfig>
|
||||
private fun NightConfig.configMap(key: String) = this.get<NightConfig>(key)
|
||||
?.valueMap() as Map<String, NightConfig>?
|
||||
?: emptyMap()
|
||||
|
||||
fun load(): StarterConfig {
|
||||
val cfg = FileConfig.of("config.toml")
|
||||
cfg.load()
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val versions = cfg.get<UnmodifiableConfig>("versions").valueMap() as Map<String, String>
|
||||
val versions = cfg.get<UnmodifiableConfig>("versions")
|
||||
?.valueMap() as Map<String, String>?
|
||||
?: emptyMap()
|
||||
|
||||
val repositories = cfg.configMap("repositories")
|
||||
.map { (name, values) ->
|
||||
|
||||
11
src/main/kotlin/starter/modules/ConfigModule.kt
Normal file
11
src/main/kotlin/starter/modules/ConfigModule.kt
Normal file
@ -0,0 +1,11 @@
|
||||
package starter.modules
|
||||
|
||||
import com.electronwill.nightconfig.core.file.FileConfig
|
||||
import org.koin.dsl.module
|
||||
import starter.config.Config
|
||||
import com.electronwill.nightconfig.core.Config as NightConfig
|
||||
|
||||
val configModule = module {
|
||||
single<NightConfig> { FileConfig.of("config.toml").apply { load() } }
|
||||
single { Config(get()).load() }
|
||||
}
|
||||
@ -17,7 +17,6 @@ val mainModule = module {
|
||||
get<RoutingHttpHandler>().asServer(SunHttp(7000)).start()
|
||||
} onClose { it?.stop() }
|
||||
|
||||
single { Config().load() }
|
||||
single { LoggerFactory.getLogger("Starter") }
|
||||
single { Views(get(), get()) }
|
||||
single { ProjectZip(getAll()) }
|
||||
|
||||
114
src/test/kotlin/starter/config/ConfigTest.kt
Normal file
114
src/test/kotlin/starter/config/ConfigTest.kt
Normal file
@ -0,0 +1,114 @@
|
||||
package starter.config
|
||||
|
||||
import com.electronwill.nightconfig.toml.TomlParser
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.intellij.lang.annotations.Language
|
||||
import org.junit.jupiter.api.Test
|
||||
import starter.*
|
||||
import java.io.StringReader
|
||||
|
||||
internal class ConfigTest {
|
||||
private val parser = TomlParser()
|
||||
private fun parse(config: String) = parser.parse(StringReader(config))
|
||||
|
||||
@Test
|
||||
fun inputs() {
|
||||
@Language("toml")
|
||||
val toml = """
|
||||
[inputs]
|
||||
|
||||
[inputs.name]
|
||||
default = "example"
|
||||
display = "Project Name"
|
||||
|
||||
[inputs.basePackage]
|
||||
default = "org.example"
|
||||
display = "Base package"
|
||||
""".trimIndent()
|
||||
|
||||
val starterConfig = Config(parse(toml)).load()
|
||||
|
||||
val expected = listOf(
|
||||
Input("basePackage", "Base package", "org.example"),
|
||||
Input("name", "Project Name", "example"),
|
||||
)
|
||||
|
||||
assertThat(starterConfig.inputs).containsExactlyInAnyOrderElementsOf(expected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun dependencies() {
|
||||
@Language("toml")
|
||||
val toml = """
|
||||
[versions]
|
||||
assertj = "3.17.2"
|
||||
koin = "2.1.6"
|
||||
h2 = "1.4.200"
|
||||
|
||||
[repositories]
|
||||
|
||||
[repositories.jcenter]
|
||||
url = "https://jcenter.bintray.com"
|
||||
|
||||
[dependencies]
|
||||
|
||||
[dependencies.assertj]
|
||||
groupId = "org.assertj"
|
||||
artifactId = "assertj-core"
|
||||
scope = "test"
|
||||
category = "test"
|
||||
default = true
|
||||
|
||||
[dependencies.koin]
|
||||
groupId = "org.koin"
|
||||
artifactId = "koin-core"
|
||||
category = "injection"
|
||||
repository = "jcenter"
|
||||
default = true
|
||||
|
||||
[dependencies.h2]
|
||||
groupId = "com.h2database"
|
||||
category = "database"
|
||||
""".trimIndent()
|
||||
|
||||
val starterConfig = Config(parse(toml)).load()
|
||||
|
||||
val expected = listOf(
|
||||
Dependency(
|
||||
name = "h2",
|
||||
groupId = "com.h2database",
|
||||
artifactId = "h2",
|
||||
version = Version("h2", "1.4.200"),
|
||||
default = false,
|
||||
category = Category.Database,
|
||||
scope = Scope.Compile,
|
||||
logger = null,
|
||||
repository = null,
|
||||
),
|
||||
Dependency(
|
||||
name = "assertj",
|
||||
groupId = "org.assertj",
|
||||
artifactId = "assertj-core",
|
||||
version = Version("assertj", "3.17.2"),
|
||||
default = true,
|
||||
category = Category.Test,
|
||||
scope = Scope.Test,
|
||||
logger = null,
|
||||
repository = null,
|
||||
),
|
||||
Dependency(
|
||||
name = "koin",
|
||||
groupId = "org.koin",
|
||||
artifactId = "koin-core",
|
||||
version = Version("koin", "2.1.6"),
|
||||
default = true,
|
||||
category = Category.Injection,
|
||||
scope = Scope.Compile,
|
||||
logger = null,
|
||||
repository = Repository("jcenter", "https://jcenter.bintray.com"),
|
||||
),
|
||||
)
|
||||
|
||||
assertThat(starterConfig.dependencies).containsExactlyInAnyOrderElementsOf(expected)
|
||||
}
|
||||
}
|
||||
@ -9,7 +9,7 @@ import org.w3c.dom.Node
|
||||
import org.w3c.dom.NodeList
|
||||
import starter.Project
|
||||
import starter.config.StarterConfig
|
||||
import starter.modules.mainModule
|
||||
import starter.modules.configModule
|
||||
import starter.modules.pebbleModule
|
||||
import starter.modules.templateModule
|
||||
import javax.xml.parsers.DocumentBuilderFactory
|
||||
@ -20,7 +20,7 @@ import javax.xml.xpath.XPathFactory
|
||||
internal class PomTemplateTest {
|
||||
|
||||
private val koin = koinApplication {
|
||||
modules(mainModule, pebbleModule, templateModule)
|
||||
modules(configModule, pebbleModule, templateModule)
|
||||
}.koin
|
||||
|
||||
private val pomTemplate = koin.get<PomTemplate>()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user