Fix KotlinVersionsTest, use SAX parser instead of DOM
This commit is contained in:
committed by
Dmitry Jemerov
parent
33e9e660c4
commit
449d1f6ad2
@@ -24,10 +24,11 @@ import org.jetbrains.kotlin.config.LanguageVersion
|
||||
import org.jetbrains.kotlin.test.testFramework.KtUsefulTestCase
|
||||
import org.jetbrains.kotlin.utils.addIfNotNull
|
||||
import org.junit.Assert
|
||||
import org.w3c.dom.Element
|
||||
import org.xml.sax.Attributes
|
||||
import org.xml.sax.helpers.DefaultHandler
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
import javax.xml.parsers.DocumentBuilderFactory
|
||||
import javax.xml.parsers.SAXParserFactory
|
||||
|
||||
class KotlinVersionsTest : KtUsefulTestCase() {
|
||||
fun testVersionsAreConsistent() {
|
||||
@@ -72,13 +73,13 @@ class KotlinVersionsTest : KtUsefulTestCase() {
|
||||
)
|
||||
|
||||
versions.add(
|
||||
loadValueFromPomXml("libraries/pom.xml", listOf("properties", "kotlin.language.version"))
|
||||
loadValueFromPomXml("libraries/pom.xml", listOf("project", "properties", "kotlin.language.version"))
|
||||
?.toVersion("kotlin.language.version in pom.xml")
|
||||
?: error("No kotlin.language.version in libraries/pom.xml")
|
||||
)
|
||||
|
||||
versions.add(
|
||||
loadValueFromPomXml("libraries/pom.xml", listOf("version"))
|
||||
loadValueFromPomXml("libraries/pom.xml", listOf("project", "version"))
|
||||
?.toVersion("version in pom.xml")
|
||||
?: error("No version in libraries/pom.xml")
|
||||
)
|
||||
@@ -102,8 +103,8 @@ class KotlinVersionsTest : KtUsefulTestCase() {
|
||||
|
||||
FileUtil.processFilesRecursively(File("libraries"), Processor { file ->
|
||||
if (file.name == "pom.xml") {
|
||||
if (loadValueFromPomXml(file.path, listOf("parent", "artifactId")) == "kotlin-project") {
|
||||
val version = loadValueFromPomXml(file.path, listOf("version"))
|
||||
if (loadValueFromPomXml(file.path, listOf("project", "parent", "artifactId")) == "kotlin-project") {
|
||||
val version = loadValueFromPomXml(file.path, listOf("project", "parent", "version"))
|
||||
?: error("No version found in pom.xml at $file")
|
||||
poms.add(Pom(file.path, version))
|
||||
}
|
||||
@@ -127,12 +128,29 @@ class KotlinVersionsTest : KtUsefulTestCase() {
|
||||
private fun loadValueFromPomXml(filePath: String, query: List<String>): String? {
|
||||
assert(filePath.endsWith("pom.xml")) { filePath }
|
||||
|
||||
val docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
|
||||
val input = docBuilder.parse(File(filePath).inputStream())
|
||||
var result: String? = null
|
||||
|
||||
return query.fold(input.documentElement) { element, tagName ->
|
||||
element?.getElementsByTagName(tagName)?.item(0) as? Element
|
||||
}?.textContent
|
||||
SAXParserFactory.newInstance().newSAXParser().parse(File(filePath), object : DefaultHandler() {
|
||||
val currentPath = mutableListOf<String>()
|
||||
|
||||
override fun startElement(uri: String, localName: String, qName: String, attributes: Attributes) {
|
||||
currentPath.add(qName)
|
||||
}
|
||||
|
||||
override fun endElement(uri: String, localName: String, qName: String) {
|
||||
assert(currentPath.lastOrNull() == qName) { "Invalid XML at $filePath: mismatched tag '$qName'" }
|
||||
currentPath.removeAt(currentPath.lastIndex)
|
||||
}
|
||||
|
||||
override fun characters(ch: CharArray, start: Int, length: Int) {
|
||||
if (currentPath == query) {
|
||||
assert(result == null) { "More than one value found for $query in $filePath" }
|
||||
result = String(ch, start, length).trim()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
private fun Collection<Any>.areEqual(): Boolean = all(first()::equals)
|
||||
|
||||
Reference in New Issue
Block a user