Skip to content

Commit

Permalink
Add a unit test class and documentation
Browse files Browse the repository at this point in the history
Also, the method `DependencyHandler.createJosm()` has been updated to take only one argument instead of two to three.
  • Loading branch information
floscher committed Oct 12, 2018
1 parent 011d724 commit 554c279
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 19 deletions.
8 changes: 7 additions & 1 deletion src/main/kotlin/ConfigurationsSetup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ExternalModuleDependency

/**
* Configure this [Configuration] as the main one:
* * derive `packIntoJar` and `requiredPlugin` configurations from this one
* * add the JOSM dependency
* @param [project] the project to which the configuration belongs
*/
fun Configuration.setupAsMainConfiguration(project: Project) {
// Configuration for JOSM plugins that are required for this plugin. Normally there's no need to set these manually, these are set based on the manifest configuration
val requiredPluginConfiguration = project.configurations.create("requiredPlugin") {
Expand All @@ -17,7 +23,7 @@ fun Configuration.setupAsMainConfiguration(project: Project) {
val josmCompileVersion = project.extensions.josm.josmCompileVersion ?: throw GradleException("JOSM compile version not set!")

// Adding dependencies for JOSM and the required plugins
val josmDependency = project.dependencies.createJosm(project, josmCompileVersion)
val josmDependency = project.dependencies.createJosm(josmCompileVersion)
if (josmDependency is ExternalModuleDependency && josmDependency.isChanging) {
project.logger.info("Compile against the variable JOSM version $josmCompileVersion")
} else {
Expand Down
29 changes: 15 additions & 14 deletions src/main/kotlin/Project.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fun Project.getNextJosmVersion(startVersion: String?): Dependency {
}

private fun Project.resolveJosm(version: String): Dependency {
val dep = dependencies.create("org.openstreetmap.josm:josm:$version")
val dep = dependencies.createJosm(version)
val conf = configurations.detachedConfiguration(dep)
try {
conf.resolve()
Expand Down Expand Up @@ -128,21 +128,22 @@ private fun Project.getAllRequiredJosmPlugins(recursionDepth: Int, alreadyResolv
return result
}

fun DependencyHandler.createJosm(project: Project, version: String, findNextAvailable: Boolean = false): Dependency {
return if (findNextAvailable) {
project.getNextJosmVersion(version)
} else {
when (version) {
"latest", "tested" -> {
(create("org.openstreetmap.josm:josm:$version") as ExternalModuleDependency).setChanging(true)
}
else -> {
create("org.openstreetmap.josm:josm:${version.toInt()}")
}
}
/**
* Creates a dependency onto JOSM using the given version number
* @param [version] the version number for JOSM (latest and tested are special versions that are [ExternalModuleDependency.isChanging])
* @return the dependency as created by [DependencyHandler.create]
*/
fun DependencyHandler.createJosm(version: String) =
when (version) {
"latest", "tested" ->
(create("org.openstreetmap.josm:josm:$version") as ExternalModuleDependency).setChanging(true)
else ->
create("org.openstreetmap.josm:josm:$version")
}
}

/**
* @return true iff the given dependency contains JOSM (the group and name of the dependency are checked to determine this)
*/
fun DependencyHandler.isJosmDependency(dep: Dependency) = dep.group == "org.openstreetmap.josm" && dep.name == "josm"

/**
Expand Down
9 changes: 8 additions & 1 deletion src/main/kotlin/SourceSetSetup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@ import org.gradle.api.internal.plugins.DslObject
import org.gradle.api.tasks.SourceSet
import org.gradle.language.jvm.tasks.ProcessResources
import org.openstreetmap.josm.gradle.plugin.i18n.DefaultI18nSourceSet
import org.openstreetmap.josm.gradle.plugin.i18n.I18nSourceSet
import org.openstreetmap.josm.gradle.plugin.task.LangCompile
import org.openstreetmap.josm.gradle.plugin.task.MoCompile
import org.openstreetmap.josm.gradle.plugin.task.PoCompile
import org.openstreetmap.josm.gradle.plugin.task.ShortenPoFiles

/**
* Add the [I18nSourceSet] and create the associated tasks ([PoCompile], [MoCompile], [LangCompile], [ShortenPoFiles]).
* This allows this source set to also contain files of types `*.po`, `*.mo` and `*.lang`.
* @param [project] the [Project] to which this source set belongs
* @param [sdsf] the [SourceDirectorySetFactory] of the project
*/
fun SourceSet.setup(project: Project, sdsf: SourceDirectorySetFactory) {
if (name.isNotEmpty()) {
// Inspired by https://github.com/gradle/gradle/blob/9d86f98b01acb6496d05e05deddbc88c1e35d038/subprojects/plugins/src/main/java/org/gradle/api/plugins/GroovyBasePlugin.java#L88-L113
val i18nSourceSet = DefaultI18nSourceSet(this, sdsf)
val i18nSourceSet: I18nSourceSet = DefaultI18nSourceSet(this, sdsf)
DslObject(this).convention.plugins["i18n"] = i18nSourceSet

// Create "shortenPoFiles" task for the current i18n source set
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/i18n/io/InputStream.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import java.io.InputStream
* @throws IOException if reading from the [InputStream] fails (see [InputStream.read]) or if the number of bytes that
* can be read is below the parameter [b].
*/
@Throws(IOException::class)
fun InputStream.readAllOrException(b: ByteArray): Int {
val numBytes = this.read(b)
if (numBytes != b.size) {
Expand All @@ -34,6 +35,7 @@ fun InputStream.skipAllOrException(n: Long): Long {
* @return -1 if no bytes can be read, otherwise the number that
* @throws IOException if only one byte can be read from the [InputStream]
*/
@Throws(IOException::class)
fun InputStream.readTwoBytesAsInt(): Int {
val bytes = Pair(read(), read())
if (bytes.first < 0) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/kotlin/i18n/io/LangReader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class LangReader {
}.toMap().plus(baseLang to baseStrings.associate { it to it.id })
}

/**
* Reads the strings of the base language from the input stream.
* @return a [List] of all [MsgId]s that can be read from the [InputStream]
* @throws [IOException] if the data in the [InputStream] is not in the *.lang format
*/
@Throws(IOException::class)
fun readBaseLangStream(stream: InputStream) = readLangStream(stream) {
if (it.isEmpty()) {
throw IOException("A MsgId must have one or more strings (${it.size} given)!")
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/task/CustomJosmVersionCompile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.compile.JavaCompile
import org.openstreetmap.josm.gradle.plugin.createJosm
import org.openstreetmap.josm.gradle.plugin.getNextJosmVersion
import org.openstreetmap.josm.gradle.plugin.isJosmDependency
import java.io.File
import javax.inject.Inject
Expand All @@ -22,9 +23,9 @@ open class CustomJosmVersionCompile
source.add(sourceSet.java)
destinationDir = File(project.buildDir, "classes/java/${sourceSet.name}_${customVersion}")

project.gradle.taskGraph.whenReady {
if (it.hasTask(this)) {
customJosm = project.dependencies.createJosm(project, customVersion, findNextVersion)
project.gradle.taskGraph.whenReady { graph ->
if (graph.hasTask(this)) {
customJosm = if (findNextVersion) { project.getNextJosmVersion(customVersion) } else { project.dependencies.createJosm(customVersion) }
val customConfig = project.configurations.detachedConfiguration(*
project.configurations.getByName(sourceSet.compileClasspathConfigurationName).dependencies
.filterNot { project.dependencies.isJosmDependency(it) }
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/task/MarkdownToHtml.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ import java.io.InputStreamReader
import java.nio.charset.StandardCharsets
import java.util.Locale

/**
* Convert all `*.md` files found among the source files of this task to *.html files.
*/
open class MarkdownToHtml: SourceTask() {

/**
* The directory where the *.html files will be created in.
* All files will be created directly inside the directory (not in subdirectories)
* using the name of the source file, replacing the ".md" extension with ".html".
*/
var destDir: File? = null

init {
Expand Down
63 changes: 63 additions & 0 deletions src/test/kotlin/i18n/io/InputStreamTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.openstreetmap.josm.gradle.plugin.i18n.io

import org.junit.Test
import org.junit.jupiter.api.Assertions.assertEquals
import java.io.IOException

class InputStreamTest {

@Test(expected = IOException::class)
fun testReadAllOrExceptionFail1() {
// too short input stream
ByteArray(0).inputStream().readAllOrException(ByteArray(2))
}

@Test(expected = IOException::class)
fun testReadAllOrExceptionFail2() {
// too short input stream
ByteArray(1).inputStream().readAllOrException(ByteArray(2))
}

@Test
fun testReadAllOrException() {
// check values are read correctly
val resultArray = ByteArray(2)
assertEquals(2, ByteArray(2) { 42 }.inputStream().readAllOrException(resultArray))
assertEquals(42, resultArray[0])
assertEquals(42, resultArray[1])

// too long stream does not hurt
assertEquals(2, ByteArray(42).inputStream().readAllOrException(ByteArray(2)))
}

@Test(expected = IOException::class)
fun testSkipAllOrExceptionFail1() {
ByteArray(0).inputStream().skipAllOrException(2)
}

@Test(expected = IOException::class)
fun testSkipAllOrExceptionFail2() {
ByteArray(1).inputStream().skipAllOrException(2)
}

@Test
fun testSkipAllOrException() {
assertEquals(2, ByteArray(2).inputStream().skipAllOrException(2))
assertEquals(2, ByteArray(3).inputStream().skipAllOrException(2))
assertEquals(2, ByteArray(72).inputStream().skipAllOrException(2))
}

@Test(expected = IOException::class)
fun testReadTwoBytesFail() {
ByteArray(1).inputStream().readTwoBytesAsInt()
}

@Test
fun testReadTwoBytes() {
assertEquals(-1, ByteArray(0).inputStream().readTwoBytesAsInt())
assertEquals(
3370 /* = 13 * 256 + 42 */,
arrayOf(13.toByte(), 42.toByte(), 72.toByte()).toByteArray().inputStream().readTwoBytesAsInt()
)
}
}

0 comments on commit 554c279

Please sign in to comment.