[BAEL-2898] Working with XML in Groovy (#7083)
This commit is contained in:
parent
413ffde3db
commit
f6a9d02b37
|
@ -0,0 +1,40 @@
|
||||||
|
package com.baeldung.xml
|
||||||
|
|
||||||
|
import groovy.xml.MarkupBuilder
|
||||||
|
import groovy.xml.XmlUtil
|
||||||
|
import spock.lang.Specification
|
||||||
|
|
||||||
|
class MarkupBuilderUnitTest extends Specification {
|
||||||
|
|
||||||
|
def xmlFile = getClass().getResource("articles_short_formatted.xml")
|
||||||
|
|
||||||
|
def "Should create XML properly"() {
|
||||||
|
given: "Node structures"
|
||||||
|
|
||||||
|
when: "Using MarkupBuilderUnitTest to create com.baeldung.xml structure"
|
||||||
|
def writer = new StringWriter()
|
||||||
|
new MarkupBuilder(writer).articles {
|
||||||
|
article {
|
||||||
|
title('First steps in Java')
|
||||||
|
author(id: '1') {
|
||||||
|
firstname('Siena')
|
||||||
|
lastname('Kerr')
|
||||||
|
}
|
||||||
|
'release-date'('2018-12-01')
|
||||||
|
}
|
||||||
|
article {
|
||||||
|
title('Dockerize your SpringBoot application')
|
||||||
|
author(id: '2') {
|
||||||
|
firstname('Jonas')
|
||||||
|
lastname('Lugo')
|
||||||
|
}
|
||||||
|
'release-date'('2018-12-01')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
then: "Xml is created properly"
|
||||||
|
XmlUtil.serialize(writer.toString()) == XmlUtil.serialize(xmlFile.text)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,94 @@
|
||||||
|
package com.baeldung.xml
|
||||||
|
|
||||||
|
|
||||||
|
import spock.lang.Shared
|
||||||
|
import spock.lang.Specification
|
||||||
|
|
||||||
|
class XmlParserUnitTest extends Specification {
|
||||||
|
|
||||||
|
def xmlFile = getClass().getResourceAsStream("articles.xml")
|
||||||
|
|
||||||
|
@Shared
|
||||||
|
def parser = new XmlParser()
|
||||||
|
|
||||||
|
def "Should read XML file properly"() {
|
||||||
|
given: "XML file"
|
||||||
|
|
||||||
|
when: "Using XmlParser to read file"
|
||||||
|
def articles = parser.parse(xmlFile)
|
||||||
|
|
||||||
|
then: "Xml is loaded properly"
|
||||||
|
articles.'*'.size() == 4
|
||||||
|
articles.article[0].author.firstname.text() == "Siena"
|
||||||
|
articles.article[2].'release-date'.text() == "2018-06-12"
|
||||||
|
articles.article[3].title.text() == "Java 12 insights"
|
||||||
|
articles.article.find { it.author.'@id'.text() == "3" }.author.firstname.text() == "Daniele"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def "Should add node to existing com.baeldung.xml using NodeBuilder"() {
|
||||||
|
given: "XML object"
|
||||||
|
def articles = parser.parse(xmlFile)
|
||||||
|
|
||||||
|
when: "Adding node to com.baeldung.xml"
|
||||||
|
def articleNode = new NodeBuilder().article(id: '5') {
|
||||||
|
title('Traversing XML in the nutshell')
|
||||||
|
author {
|
||||||
|
firstname('Martin')
|
||||||
|
lastname('Schmidt')
|
||||||
|
}
|
||||||
|
'release-date'('2019-05-18')
|
||||||
|
}
|
||||||
|
articles.append(articleNode)
|
||||||
|
|
||||||
|
then: "Node is added to com.baeldung.xml properly"
|
||||||
|
articles.'*'.size() == 5
|
||||||
|
articles.article[4].title.text() == "Traversing XML in the nutshell"
|
||||||
|
}
|
||||||
|
|
||||||
|
def "Should replace node"() {
|
||||||
|
given: "XML object"
|
||||||
|
def articles = parser.parse(xmlFile)
|
||||||
|
|
||||||
|
when: "Adding node to com.baeldung.xml"
|
||||||
|
def articleNode = new NodeBuilder().article(id: '5') {
|
||||||
|
title('Traversing XML in the nutshell')
|
||||||
|
author {
|
||||||
|
firstname('Martin')
|
||||||
|
lastname('Schmidt')
|
||||||
|
}
|
||||||
|
'release-date'('2019-05-18')
|
||||||
|
}
|
||||||
|
articles.article[0].replaceNode(articleNode)
|
||||||
|
|
||||||
|
then: "Node is added to com.baeldung.xml properly"
|
||||||
|
articles.'*'.size() == 4
|
||||||
|
articles.article[0].title.text() == "Traversing XML in the nutshell"
|
||||||
|
}
|
||||||
|
|
||||||
|
def "Should modify node"() {
|
||||||
|
given: "XML object"
|
||||||
|
def articles = parser.parse(xmlFile)
|
||||||
|
|
||||||
|
when: "Changing value of one of the nodes"
|
||||||
|
articles.article.each { it.'release-date'[0].value = "2019-05-18" }
|
||||||
|
|
||||||
|
then: "XML is updated"
|
||||||
|
articles.article.findAll { it.'release-date'.text() != "2019-05-18" }.isEmpty()
|
||||||
|
}
|
||||||
|
|
||||||
|
def "Should remove article from com.baeldung.xml"() {
|
||||||
|
given: "XML object"
|
||||||
|
def articles = parser.parse(xmlFile)
|
||||||
|
|
||||||
|
when: "Removing all articles but with id==3"
|
||||||
|
articles.article
|
||||||
|
.findAll { it.author.'@id'.text() != "3" }
|
||||||
|
.each { articles.remove(it) }
|
||||||
|
|
||||||
|
then: "There is only one article left"
|
||||||
|
articles.children().size() == 1
|
||||||
|
articles.article[0].author.'@id'.text() == "3"
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,102 @@
|
||||||
|
package com.baeldung.xml
|
||||||
|
|
||||||
|
|
||||||
|
import groovy.xml.XmlUtil
|
||||||
|
import spock.lang.Shared
|
||||||
|
import spock.lang.Specification
|
||||||
|
|
||||||
|
class XmlSlurperUnitTest extends Specification {
|
||||||
|
|
||||||
|
def xmlFile = getClass().getResourceAsStream("articles.xml")
|
||||||
|
|
||||||
|
@Shared
|
||||||
|
def parser = new XmlSlurper()
|
||||||
|
|
||||||
|
def "Should read XML file properly"() {
|
||||||
|
given: "XML file"
|
||||||
|
|
||||||
|
when: "Using XmlSlurper to read file"
|
||||||
|
def articles = parser.parse(xmlFile)
|
||||||
|
|
||||||
|
then: "Xml is loaded properly"
|
||||||
|
articles.'*'.size() == 4
|
||||||
|
articles.article[0].author.firstname == "Siena"
|
||||||
|
articles.article[2].'release-date' == "2018-06-12"
|
||||||
|
articles.article[3].title == "Java 12 insights"
|
||||||
|
articles.article.find { it.author.'@id' == "3" }.author.firstname == "Daniele"
|
||||||
|
}
|
||||||
|
|
||||||
|
def "Should add node to existing com.baeldung.xml"() {
|
||||||
|
given: "XML object"
|
||||||
|
def articles = parser.parse(xmlFile)
|
||||||
|
|
||||||
|
when: "Adding node to com.baeldung.xml"
|
||||||
|
articles.appendNode {
|
||||||
|
article(id: '5') {
|
||||||
|
title('Traversing XML in the nutshell')
|
||||||
|
author {
|
||||||
|
firstname('Martin')
|
||||||
|
lastname('Schmidt')
|
||||||
|
}
|
||||||
|
'release-date'('2019-05-18')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
articles = parser.parseText(XmlUtil.serialize(articles))
|
||||||
|
|
||||||
|
then: "Node is added to com.baeldung.xml properly"
|
||||||
|
articles.'*'.size() == 5
|
||||||
|
articles.article[4].title == "Traversing XML in the nutshell"
|
||||||
|
}
|
||||||
|
|
||||||
|
def "Should modify node"() {
|
||||||
|
given: "XML object"
|
||||||
|
def articles = parser.parse(xmlFile)
|
||||||
|
|
||||||
|
when: "Changing value of one of the nodes"
|
||||||
|
articles.article.each { it.'release-date' = "2019-05-18" }
|
||||||
|
|
||||||
|
then: "XML is updated"
|
||||||
|
articles.article.findAll { it.'release-date' != "2019-05-18" }.isEmpty()
|
||||||
|
}
|
||||||
|
|
||||||
|
def "Should replace node"() {
|
||||||
|
given: "XML object"
|
||||||
|
def articles = parser.parse(xmlFile)
|
||||||
|
|
||||||
|
when: "Replacing node"
|
||||||
|
articles.article[0].replaceNode {
|
||||||
|
article(id: '5') {
|
||||||
|
title('Traversing XML in the nutshell')
|
||||||
|
author {
|
||||||
|
firstname('Martin')
|
||||||
|
lastname('Schmidt')
|
||||||
|
}
|
||||||
|
'release-date'('2019-05-18')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
articles = parser.parseText(XmlUtil.serialize(articles))
|
||||||
|
|
||||||
|
then: "Node is replaced properly"
|
||||||
|
articles.'*'.size() == 4
|
||||||
|
articles.article[0].title == "Traversing XML in the nutshell"
|
||||||
|
}
|
||||||
|
|
||||||
|
def "Should remove article from com.baeldung.xml"() {
|
||||||
|
given: "XML object"
|
||||||
|
def articles = parser.parse(xmlFile)
|
||||||
|
|
||||||
|
when: "Removing all articles but with id==3"
|
||||||
|
articles.article
|
||||||
|
.findAll { it.author.'@id' != "3" }
|
||||||
|
.replaceNode {}
|
||||||
|
|
||||||
|
articles = parser.parseText(XmlUtil.serialize(articles))
|
||||||
|
|
||||||
|
then: "There is only one article left"
|
||||||
|
articles.children().size() == 1
|
||||||
|
articles.article[0].author.'@id' == "3"
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
<articles>
|
||||||
|
<article>
|
||||||
|
<title>First steps in Java</title>
|
||||||
|
<author id='1'>
|
||||||
|
<firstname>Siena</firstname>
|
||||||
|
<lastname>Kerr</lastname>
|
||||||
|
</author>
|
||||||
|
<release-date>2018-12-01</release-date>
|
||||||
|
</article>
|
||||||
|
<article>
|
||||||
|
<title>Dockerize your SpringBoot application</title>
|
||||||
|
<author id='2'>
|
||||||
|
<firstname>Jonas</firstname>
|
||||||
|
<lastname>Lugo</lastname>
|
||||||
|
</author>
|
||||||
|
<release-date>2018-12-01</release-date>
|
||||||
|
</article>
|
||||||
|
<article>
|
||||||
|
<title>SpringBoot tutorial</title>
|
||||||
|
<author id='3'>
|
||||||
|
<firstname>Daniele</firstname>
|
||||||
|
<lastname>Ferguson</lastname>
|
||||||
|
</author>
|
||||||
|
<release-date>2018-06-12</release-date>
|
||||||
|
</article>
|
||||||
|
<article>
|
||||||
|
<title>Java 12 insights</title>
|
||||||
|
<author id='1'>
|
||||||
|
<firstname>Siena</firstname>
|
||||||
|
<lastname>Kerr</lastname>
|
||||||
|
</author>
|
||||||
|
<release-date>2018-07-22</release-date>
|
||||||
|
</article>
|
||||||
|
</articles>
|
|
@ -0,0 +1,18 @@
|
||||||
|
<articles>
|
||||||
|
<article>
|
||||||
|
<title>First steps in Java</title>
|
||||||
|
<author id='1'>
|
||||||
|
<firstname>Siena</firstname>
|
||||||
|
<lastname>Kerr</lastname>
|
||||||
|
</author>
|
||||||
|
<release-date>2018-12-01</release-date>
|
||||||
|
</article>
|
||||||
|
<article>
|
||||||
|
<title>Dockerize your SpringBoot application</title>
|
||||||
|
<author id='2'>
|
||||||
|
<firstname>Jonas</firstname>
|
||||||
|
<lastname>Lugo</lastname>
|
||||||
|
</author>
|
||||||
|
<release-date>2018-12-01</release-date>
|
||||||
|
</article>
|
||||||
|
</articles>
|
Loading…
Reference in New Issue