version = '0.9.6'
buildscript {
+ ext.kotlinVersion = '1.1.51'
repositories {
mavenCentral()
}
dependencies {
classpath group: 'info.solidsoft.gradle.pitest', name: 'gradle-pitest-plugin', version: '1.1.11'
- classpath group: 'org.jetbrains.kotlin', name: 'kotlin-gradle-plugin', version: '1.1.51'
+ classpath group: 'org.jetbrains.kotlin', name: 'kotlin-gradle-plugin', version: kotlinVersion
+ classpath group: 'org.jetbrains.kotlin', name: 'kotlin-noarg', version: kotlinVersion
}
}
compile group: 'com.google.inject', name: 'guice', version: '3.0'
compile group: 'com.google.guava', name: 'guava', version: '14.0.1'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.1'
+ compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: '2.9.1'
compile group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.2'
compile group: 'org.jsoup', name: 'jsoup', version: '1.10.2'
dependsOn tasks.countLinesMain
dependsOn tasks.countLinesTest
}
+
+apply plugin: 'kotlin-noarg'
+
+noArg {
+ annotation('net.pterodactylus.sone.main.NoArg')
+}
--- /dev/null
+package net.pterodactylus.sone.main
+
+/**
+ * Annotation for class that will have a no-argument constructor artificially generated by
+ * the no-arg Kotlin compiler plugin.
+ */
+annotation class NoArg
--- /dev/null
+package net.pterodactylus.sone.main
+
+import com.fasterxml.jackson.databind.ObjectMapper
+import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
+
+fun parseVersion(file: String = "/version.yaml"): Version? =
+ Version::class.java.getResourceAsStream(file)?.use {
+ objectMapper.readValue(it, Version::class.java)
+ }
+
+private val objectMapper = ObjectMapper(YAMLFactory())
+
+@NoArg
+data class Version(val id: String, val nice: String)
--- /dev/null
+package net.pterodactylus.sone.main
+
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.hamcrest.Matchers.nullValue
+import org.junit.Test
+
+/**
+ * Unit test for [parseVersion].
+ */
+class VersionParserTest {
+
+ @Test
+ fun `version from missing file can not be read`() {
+ assertThat(parseVersion("does-not-exist.yaml"), nullValue())
+ }
+
+ @Test
+ fun `custom version file can be parsed`() {
+ val version = parseVersion("custom-version.yaml")!!
+ assertThat(version.id, equalTo("some-id"))
+ assertThat(version.nice, equalTo("some-nice"))
+ }
+
+ @Test
+ fun `default version file is parsed`() {
+ val version = parseVersion()!!
+ assertThat(version.id, equalTo("43f3e1c3a0f487e37e5851a2cc72756d271c7571"))
+ assertThat(version.nice, equalTo("0.9.6-466-g43f3e1c"))
+ }
+
+}
--- /dev/null
+id: some-id
+nice: some-nice
--- /dev/null
+id: 43f3e1c3a0f487e37e5851a2cc72756d271c7571
+nice: 0.9.6-466-g43f3e1c