Skip to content

Commit

Permalink
feat: add standalone setting to the XML builder #1414
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Aug 21, 2021
1 parent 883be2d commit 4db1f83
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ private fun matcherKey(path: List<String>, vararg key: String): String {
}

class PactXmlBuilder @JvmOverloads constructor (
val rootName: String,
val rootNameSpace: String? = null,
val namespaces: Map<String, String> = emptyMap(),
val version: String? = null,
val charset: String? = null
var rootName: String,
var rootNameSpace: String? = null,
var namespaces: Map<String, String> = emptyMap(),
var version: String? = null,
var charset: String? = null,
var standalone: Boolean = false
) {
val generators: Generators = Generators()
val matchingRules: Category = Category("body")
Expand All @@ -53,6 +54,7 @@ class PactXmlBuilder @JvmOverloads constructor (
if (version != null) {
doc.xmlVersion = version
}
doc.xmlStandalone = standalone
val root = if (doc.documentElement == null) {
val element = doc.createElement(rootName)
doc.appendChild(element)
Expand Down Expand Up @@ -87,6 +89,54 @@ class PactXmlBuilder @JvmOverloads constructor (
}

override fun toString() = String(asBytes())

/**
* Sets the name of the root name
*/
fun withRootName(name: String): PactXmlBuilder {
this.rootName = name
return this
}

/**
* Sets the namespace of the root node
*/
fun withRootNameSpace(nameSpace: String): PactXmlBuilder {
this.rootNameSpace = nameSpace
return this
}

/**
* Namespaces to define on the root name
*/
fun withNamespaces(namespaces: Map<String, String>): PactXmlBuilder {
this.namespaces = namespaces
return this
}

/**
* Sets the version on the XML descriptor. Defaults to '1.0'.
*/
fun withVersion(version: String): PactXmlBuilder {
this.version = version
return this
}

/**
* Sets the charset on the XML descriptor. Defaults to 'UTF-8'
*/
fun withCharset(charset: String): PactXmlBuilder {
this.charset = charset
return this
}

/**
* Sets the standalone flag on the XML descriptor. Default is set ('yes')
*/
fun withStandalone(standalone: Boolean): PactXmlBuilder {
this.standalone = standalone
return this
}
}

class XmlNode(private val builder: PactXmlBuilder, private val element: Element, private val path: List<String>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,26 @@ class PactXmlBuilderSpec extends Specification {
['$', 'one'] | ['two', "['@id']"] | "\$.one.two['@id']"
['$', 'one'] | ['two', '#text'] | "\$.one.two.#text"
}

@Unroll
def 'standalone declaration - #standalone'() {
given:
def builder = new PactXmlBuilder('projects')
.withStandalone(standalone)
.build { node ->
node.setAttributes([id: '1234'])
}

when:
def result = builder.toString()

then:
result.startsWith(value)

where:

standalone | value
true | '<?xml version="1.0" encoding="UTF-8"?>'
false | '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'
}
}

0 comments on commit 4db1f83

Please sign in to comment.