From f2aa5435edc1fd20e0a8f04ab59008e0a08e87b6 Mon Sep 17 00:00:00 2001 From: evanchooly Date: Fri, 9 Oct 2015 22:21:18 -0400 Subject: [PATCH] add some more testing around pom importing --- .../com/beust/kobalt/ProjectGenerator.kt | 31 +++++------ src/main/kotlin/com/beust/kobalt/maven/Pom.kt | 16 +++--- .../kotlin/com/beust/kobalt/maven/PomTest.kt | 54 ++++++++++++++++++- .../pom-norepositories-properties.xml | 11 ++++ 4 files changed, 89 insertions(+), 23 deletions(-) create mode 100644 src/test/resources/pom-norepositories-properties.xml diff --git a/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt b/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt index dd18b415..2637d2cf 100644 --- a/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt @@ -19,6 +19,15 @@ import java.util.HashMap * Generate a new project. */ public class ProjectGenerator : KobaltLogger { + companion object { + /** + * Turns a dot property into a proper Kotlin identifier, e.g. common.version -> commonVersion + */ + fun translate(key: String): String { + return key.split('.').mapIndexed( { index, value -> if (index == 0) value else value.upperFirst() }).join("") + } + } + fun run(args: Args) { if (File(args.buildFile).exists()) { log(1, "Build file ${args.buildFile} already exists, not overwriting it") @@ -52,7 +61,7 @@ public class ProjectGenerator : KobaltLogger { map.put("mainDependencies", mainDeps) map.put("testDependencies", testDeps) File("pom.xml").let { - if (it.exists()) { + if (it.absoluteFile.exists()) { importPom(it, mainDeps, testDeps, map) } } @@ -68,13 +77,13 @@ public class ProjectGenerator : KobaltLogger { private fun importPom(pomFile: File, mainDeps: ArrayList, testDeps: ArrayList, map: HashMap) { - var pom = Pom("imported", pomFile) + var pom = Pom("imported", pomFile.absoluteFile) with(map) { put("group", pom.groupId ?: "com.example") put("artifactId", pom.artifactId ?: "com.example") put("version", pom.version ?: "0.1") put("name", pom.name ?: pom.artifactId) - put("repositories", pom.repos.map({ "\"${it}\"" }).join(",")) + put("repositories", pom.repositories.map({ "\"${it}\"" }).join(",")) } val properties = pom.properties @@ -102,18 +111,6 @@ public class ProjectGenerator : KobaltLogger { dep } - /** - * Turns a dot property into a proper Kotlin identifier, e.g. common.version -> commonVersion - */ - private fun translate(key: String) = - key.split('.').mapIndexed( { index, value -> if (index == 0) value else value.upperFirst() }).join("") - - private fun String.upperFirst() = - if (this.isBlank()) { - this - } else { - this.substring(0, 1).toUpperCase() + this.substring(1) - } /** * Detect all the languages contained in this project. @@ -130,3 +127,7 @@ public class ProjectGenerator : KobaltLogger { return result.map { it.first } } } + +private fun String.upperFirst(): String { + return if (this.isBlank()) this else this.substring(0, 1).toUpperCase() + this.substring(1) +} diff --git a/src/main/kotlin/com/beust/kobalt/maven/Pom.kt b/src/main/kotlin/com/beust/kobalt/maven/Pom.kt index fa8507a5..0ae2598a 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/Pom.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/Pom.kt @@ -19,7 +19,7 @@ public class Pom @javax.inject.Inject constructor(@Assisted val id: String, var version: String? = null var name: String? = null var properties = sortedMapOf() - var repos = listOf() + var repositories = listOf() public interface IFactory { fun create(@Assisted id: String, @Assisted documentFile : java.io.File) : Pom @@ -63,13 +63,15 @@ public class Pom @javax.inject.Inject constructor(@Assisted val id: String, artifactId = XPATH.compile("/project/artifactId").evaluate(document) version = XPATH.compile("/project/version").evaluate(document) name = XPATH.compile("/project/name").evaluate(document) - var list = XPATH.compile("/project/repositories").evaluate(document, XPathConstants.NODESET) as NodeList - var elem = list.item(0) as Element? - repos = elem.childElements() - .map({ it.getElementsByTagName("url").item(0).textContent }) + var repositoriesList = XPATH.compile("/project/repositories").evaluate(document, XPathConstants.NODESET) as NodeList +// if (repositoriesList.getLength() != 0) { + var elem = repositoriesList.item(0) as Element? + repositories = elem.childElements() + .map({ it.getElementsByTagName("url").item(0).textContent }) +// } - list = XPATH.compile("/project/properties").evaluate(document, XPathConstants.NODESET) as NodeList - elem = list.item(0) as Element? + val propertiesList = XPATH.compile("/project/properties").evaluate(document, XPathConstants.NODESET) as NodeList + /*var*/ elem = propertiesList.item(0) as Element? elem.childElements().forEach { properties.put(it.nodeName, it.textContent) } diff --git a/src/test/kotlin/com/beust/kobalt/maven/PomTest.kt b/src/test/kotlin/com/beust/kobalt/maven/PomTest.kt index 376cd79a..b840bd99 100644 --- a/src/test/kotlin/com/beust/kobalt/maven/PomTest.kt +++ b/src/test/kotlin/com/beust/kobalt/maven/PomTest.kt @@ -1,5 +1,7 @@ package com.beust.kobalt.maven +import com.beust.kobalt.Args +import com.beust.kobalt.ProjectGenerator import org.testng.Assert import org.testng.annotations.Test import java.io.File @@ -7,7 +9,8 @@ import java.io.File class PomTest { @Test fun importPom() { - val pom = Pom("testing", File("src/test/resources/pom.xml")); + val pomSrc = File("src/test/resources/pom.xml") + val pom = Pom("testing", pomSrc); Assert.assertEquals(pom.groupId, "com.foo.bob") Assert.assertEquals(pom.artifactId, "rawr") @@ -15,5 +18,54 @@ class PomTest { Assert.assertEquals(pom.version, "1.2.3") Assert.assertEquals(pom.properties.get("commons.version"), "2.1.1") Assert.assertEquals(pom.properties.get("guice.version"), "4.0") + + validateGeneratedFile(pom, pomSrc) + } + + @Test + fun importBasicPom() { + val pomSrc = File("src/test/resources/pom-norepositories-properties.xml") + val pom = Pom("testing", pomSrc); + + Assert.assertEquals(pom.groupId, "com.foo.bob") + Assert.assertEquals(pom.artifactId, "rawr") + Assert.assertEquals(pom.name, "rawr") + Assert.assertEquals(pom.version, "1.2.3") + Assert.assertTrue(pom.properties.isEmpty()) + Assert.assertTrue(pom.repositories.isEmpty()) + + validateGeneratedFile(pom, pomSrc) + } + + private fun validateGeneratedFile(pom: Pom, pomSrc: File) { + val temp = File(System.getProperty("java.io.tmpdir")) + val original = System.getProperty("user.dir") + System.setProperty("user.dir", temp.absolutePath) + val pomFile = File(temp, "pom.xml") + pomFile.deleteOnExit() + pomSrc.copyTo(pomFile, true) + try { + val file = File(temp, "Build.kt") + file.delete() + + file.deleteOnExit() + val args = Args() + args.buildFile = file.absolutePath + args.init = true + ProjectGenerator().run(args) + var contents = file.readText() + Assert.assertTrue(contents.contains("group = \"${pom.groupId}\""), "Should find the group defined") + Assert.assertTrue(contents.contains("name = \"${pom.name}\""), "Should find the name defined") + Assert.assertTrue(contents.contains("version = \"${pom.version}\""), "Should find the version defined") + pom.properties.forEach { + Assert.assertTrue(contents.contains("val ${ProjectGenerator.translate(it.key)} = \"${it.value}\""), "Should find the " + + "property defined") + } + pom.repositories.forEach { + Assert.assertTrue(contents.contains(it), "Should find the repository defined") + } + } finally { + System.getProperty("user.dir", original) + } } } \ No newline at end of file diff --git a/src/test/resources/pom-norepositories-properties.xml b/src/test/resources/pom-norepositories-properties.xml new file mode 100644 index 00000000..c467fa10 --- /dev/null +++ b/src/test/resources/pom-norepositories-properties.xml @@ -0,0 +1,11 @@ + + 4.0.0 + + com.foo.bob + rawr + jar + 1.2.3 + + rawr +