mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
122 lines
4.6 KiB
Kotlin
122 lines
4.6 KiB
Kotlin
package com.beust.kobalt
|
|
|
|
import com.beust.kobalt.misc.KFiles
|
|
import com.beust.kobalt.misc.kobaltLog
|
|
import org.testng.Assert
|
|
import org.testng.annotations.Test
|
|
import java.io.File
|
|
import java.io.FileInputStream
|
|
import java.io.FileReader
|
|
import java.io.InputStream
|
|
import java.util.*
|
|
import java.util.jar.JarFile
|
|
import java.util.jar.JarInputStream
|
|
|
|
class VerifyKobaltZipTest : KobaltTest() {
|
|
@Test
|
|
fun verifySourceJarFile() {
|
|
assertExistsInJar("kobalt-$KOBALT_VERSION-sources.jar", "com/beust/kobalt/Main.kt")
|
|
}
|
|
|
|
@Test
|
|
fun verifyZipFile() {
|
|
var foundKobaltw = false
|
|
var foundJar = false
|
|
var foundWrapperJar = false
|
|
|
|
val mainJarFilePath = "kobalt-$KOBALT_VERSION.jar"
|
|
val zipFilePath = KFiles.joinDir("kobaltBuild", "libs", "kobalt-$KOBALT_VERSION.zip")
|
|
if (File(zipFilePath).exists()) {
|
|
val zipFile = JarFile(zipFilePath)
|
|
val stream = JarInputStream(FileInputStream(zipFilePath))
|
|
var entry = stream.nextEntry
|
|
while (entry != null) {
|
|
if (entry.name.endsWith("kobaltw")) {
|
|
foundKobaltw = true
|
|
} else if (entry.name.endsWith(mainJarFilePath)) {
|
|
val ins = zipFile.getInputStream(entry)
|
|
if (ins.available() < 20000000) {
|
|
throw KobaltException(mainJarFilePath + " is too small: " + mainJarFilePath)
|
|
}
|
|
verifyMainJarFile(ins)
|
|
foundJar = true
|
|
} else if (entry.name.endsWith("kobalt-wrapper.jar")) {
|
|
val ins = zipFile.getInputStream(entry)
|
|
foundWrapperJar = true
|
|
assertExistsInJar(jarContents(JarInputStream(ins)), "kobalt.properties")
|
|
}
|
|
entry = stream.nextEntry
|
|
}
|
|
if (!foundKobaltw) {
|
|
throw KobaltException("Couldn't find kobaltw in $zipFilePath")
|
|
}
|
|
if (!foundJar) {
|
|
throw KobaltException("Couldn't find jar in $zipFilePath")
|
|
}
|
|
if (!foundWrapperJar) {
|
|
throw KobaltException("Couldn't find wrapper jar in $zipFilePath")
|
|
}
|
|
kobaltLog(1, "$zipFilePath looks correct")
|
|
} else {
|
|
kobaltLog(1, "Couldn't find $zipFilePath, skipping test")
|
|
}
|
|
}
|
|
|
|
// Can't use Kobalt.version since the tests have their own src/test/resources/kobalt.properties
|
|
val KOBALT_VERSION: String
|
|
get() {
|
|
val p = Properties()
|
|
p.load(FileReader("src/main/resources/kobalt.properties"))
|
|
val result = p.getProperty("kobalt.version")
|
|
return result
|
|
}
|
|
|
|
private fun verifyMainJarFile(ins: InputStream) {
|
|
JarInputStream(ins).let { jar ->
|
|
val setContent = jarContents(jar)
|
|
assertExistsInJar(setContent, "com/beust/kobalt/MainKt.class",
|
|
"templates/kobaltPlugin/kobaltPlugin.jar", "com/beust/kobalt/Args.class",
|
|
"com/beust/kobalt/wrapper/Main.class")
|
|
assertDoesNotExistInJar(setContent, "BuildKt.class")
|
|
}
|
|
}
|
|
|
|
private fun assertExistsInJar(content: Set<String>, vararg fileNames: String)
|
|
= assertExistence(content, true, *fileNames)
|
|
|
|
private fun assertDoesNotExistInJar(content: Set<String>, vararg fileNames: String)
|
|
= assertExistence(content, false, *fileNames)
|
|
|
|
private fun assertExistence(content: Set<String>, verifyExistence: Boolean, vararg fileNames: String) {
|
|
with(content) {
|
|
fileNames.forEach { fileName ->
|
|
if (verifyExistence) {
|
|
Assert.assertTrue(contains(fileName), "Couldn't find $fileName")
|
|
} else {
|
|
val exists = content.contains(fileName)
|
|
Assert.assertFalse(exists, "The jar file should not contain $fileName")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private fun assertExistsInJar(jarName: String, vararg fileNames: String) {
|
|
val sourceJarPath = KFiles.joinDir("kobaltBuild", "libs", jarName)
|
|
val file = File(sourceJarPath)
|
|
if (file.exists()) {
|
|
assertExistsInJar(jarContents(JarInputStream(FileInputStream(file))), *fileNames)
|
|
} else {
|
|
kobaltLog(1, "Couldn't find $file, skipping test")
|
|
}
|
|
}
|
|
|
|
private fun jarContents(stream: JarInputStream) : Set<String> {
|
|
val result = hashSetOf<String>()
|
|
var entry = stream.nextEntry
|
|
while (entry != null) {
|
|
result.add(entry.name)
|
|
entry = stream.nextEntry
|
|
}
|
|
return result
|
|
}
|
|
}
|