org.gradle.internal.xml.XmlTransformerTest.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-api Show documentation
Show all versions of gradle-api Show documentation
Gradle 6.9.1 API redistribution.
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.internal.xml
import groovy.xml.XmlParser
import org.gradle.api.Action
import org.gradle.api.XmlProvider
import org.gradle.api.internal.DomNode
import org.gradle.internal.UncheckedException
import org.gradle.test.fixtures.file.TestNameTestDirectoryProvider
import org.gradle.util.internal.TextUtil
import org.junit.Rule
import spock.lang.Specification
import javax.xml.parsers.DocumentBuilderFactory
class XmlTransformerTest extends Specification {
final XmlTransformer transformer = new XmlTransformer()
@Rule TestNameTestDirectoryProvider tmpDir = new TestNameTestDirectoryProvider(getClass())
def "returns original string when no actions are provided"() {
expect:
looksLike ' ', transformer.transform(' ')
}
def "action can access XML as StringBuilder"() {
Action action = Mock()
transformer.addAction(action)
when:
def result = transformer.transform(' ')
then:
action.execute(_) >> { XmlProvider provider ->
def builder = provider.asString()
builder.insert(builder.indexOf("root"), 'some-')
}
looksLike ' ', result
}
def "action can access XML as Node"() {
Action action = Mock()
transformer.addAction(action)
when:
def result = transformer.transform(' ')
then:
action.execute(_) >> { XmlProvider provider ->
provider.asNode().appendNode('child1')
}
looksLike '\n \n \n', result
}
def "action can access XML as DOM element"() {
Action action = Mock()
transformer.addAction(action)
when:
def result = transformer.transform(' ')
then:
action.execute(_) >> { XmlProvider provider ->
def document = provider.asElement().ownerDocument
provider.asElement().appendChild(document.createElement('child1'))
}
looksLike '\n \n \n', result
}
def "can transform String to a Writer"() {
Action action = Mock()
transformer.addAction(action)
StringWriter writer = new StringWriter()
when:
transformer.transform(' ', writer)
then:
action.execute(_) >> { XmlProvider provider ->
provider.asNode().appendNode('child1')
}
looksLike '\n \n \n', writer.toString()
}
def "can transform String to an OutputStream"() {
Action action = Mock()
transformer.addAction(action)
def outputStream = new ByteArrayOutputStream()
when:
transformer.transform(' ', outputStream)
then:
action.execute(_) >> { XmlProvider provider ->
provider.asNode().appendNode('child\u03b1')
}
looksLike '\n \n \n', outputStream.toByteArray()
}
def "can transform Node to a Writer"() {
Action action = Mock()
transformer.addAction(action)
StringWriter writer = new StringWriter()
Node node = new XmlParser().parseText(' ')
when:
transformer.transform(node, writer)
then:
action.execute(_) >> { XmlProvider provider ->
provider.asNode().appendNode('child1')
}
looksLike '\n \n \n', writer.toString()
}
def "can transform Node to an OutputStream"() {
Action action = Mock()
transformer.addAction(action)
def outputStream = new ByteArrayOutputStream()
Node node = new XmlParser().parseText(' ')
when:
transformer.transform(node, outputStream)
then:
action.execute(_) >> { XmlProvider provider ->
provider.asNode().appendNode('child\u03b1')
}
looksLike '\n \n \n', outputStream.toByteArray()
}
def "can transform Node to a File"() {
Action action = Mock()
transformer.addAction(action)
File file = tmpDir.file("out.xml")
Node node = new XmlParser().parseText(' ')
when:
transformer.transform(node, file)
then:
action.execute(_) >> { XmlProvider provider ->
provider.asNode().appendNode('child\u03b1')
}
looksLike '\n \n \n', file.bytes
}
def "can use a closure as an action"() {
transformer.addAction { provider ->
provider.asNode().appendNode('child1')
}
StringWriter writer = new StringWriter()
when:
transformer.transform(' ', writer)
then:
looksLike '\n \n \n', writer.toString()
}
def "can chain actions"() {
Action stringAction = Mock()
Action nodeAction = Mock()
Action elementAction = Mock()
Action stringAction2 = Mock()
transformer.addAction(stringAction)
transformer.addAction(elementAction)
transformer.addAction(nodeAction)
transformer.addAction(stringAction2)
when:
def result = transformer.transform(' ')
then:
stringAction.execute(_) >> { XmlProvider provider ->
def builder = provider.asString()
builder.insert(builder.indexOf("root"), 'some-')
}
nodeAction.execute(_) >> { XmlProvider provider ->
provider.asNode().appendNode('child2')
}
elementAction.execute(_) >> { XmlProvider provider ->
def document = provider.asElement().ownerDocument
provider.asElement().appendChild(document.createElement('child1'))
}
stringAction2.execute(_) >> { XmlProvider provider ->
provider.asString().append('')
}
looksLike '\n \n \n \n', result
}
def "can chain node actions"() {
Action nodeAction = Mock()
Action nodeAction2 = Mock()
transformer.addAction(nodeAction)
transformer.addAction(nodeAction2)
when:
def result = transformer.transform(' ')
then:
nodeAction.execute(_) >> { XmlProvider provider ->
provider.asNode().appendNode('child1')
}
nodeAction2.execute(_) >> { XmlProvider provider ->
provider.asNode().appendNode('child2')
}
looksLike '\n \n \n \n', result
}
def "indentation correct when writing out Node"() {
transformer.indentation = "\t"
transformer.addAction { XmlProvider provider -> provider.asNode().children()[0].appendNode("grandchild") }
when:
def result = transformer.transform("\n \n \n")
then:
looksLike "\n\t\n\t\t \n\t \n \n", result
}
def "can add DOCTYPE along with nodes"() {
transformer.addAction { it.asNode().appendNode('someChild') }
transformer.addAction {
def s = it.asString()
s.insert(s.indexOf("?>") + 2, '\n')
}
when:
def result = transformer.transform(" ")
then:
looksLike "\n\n \n \n", result
}
def "can specify DOCTYPE when using DomNode"() {
StringWriter writer = new StringWriter()
def node = new DomNode('root')
node.publicId = 'public-id'
node.systemId = 'system-id'
when:
transformer.transform(node, writer)
then:
looksLike '''
''', writer.toString()
}
def "DOCTYPE is preserved when transformed as a Node"() {
StringWriter writer = new StringWriter()
def node = new DomNode('root')
node.publicId = 'public-id'
node.systemId = 'system-id'
transformer.addAction { it.asNode().appendNode('someChild') }
when:
transformer.transform(node, writer)
then:
looksLike '''
''', writer.toString()
}
def "DOCTYPE with DTD is not allowed when transformed as a DOM element"() {
StringWriter writer = new StringWriter()
def node = new DomNode('root')
node.publicId = 'public-id'
node.systemId = tmpDir.createFile("thing.dtd").toURI()
transformer.addAction { it.asElement().appendChild(it.asElement().ownerDocument.createElement('someChild')) }
when:
transformer.transform(node, writer)
then:
def e = thrown(UncheckedException)
e.message.contains("External DTD: Failed to read external DTD 'thing.dtd', because 'file' access is not allowed")
}
def "indentation correct when writing out DOM element (only) if indenting with spaces"() {
transformer.indentation = expected
transformer.addAction { XmlProvider provider ->
def document = provider.asElement().ownerDocument
document.getElementsByTagName("child").item(0).appendChild(document.createElement("grandchild"))
}
when:
def result = transformer.transform("\n \n \n")
then:
looksLike("\n$actual\n$actual$actual \n$actual \n \n", result)
where:
expected | actual
" " | " "
"\t" | " " // tabs not supported, two spaces used instead
}
def "empty text nodes are removed when writing out DOM element"() {
transformer.addAction { XmlProvider provider ->
def document = provider.asElement().ownerDocument
document.getElementsByTagName("child").item(0).appendChild(document.createElement("grandchild"))
document.getElementsByTagName("child").item(0).appendChild(document.createTextNode(" "))
document.getElementsByTagName("child").item(0).appendChild(document.createElement("grandchild"))
}
when:
def result = transformer.transform("\n \n \n")
then:
looksLike("\n \n \n \n \n \n", result)
}
def "can use with action api"() {
given:
def writer = new StringWriter()
def input = " "
def generator = new Action() {
void execute(Writer t) {
t.write(input)
}
}
when:
transformer.transform(writer, generator)
then:
looksLike(input, writer.toString())
when:
writer.buffer.setLength(0)
transformer.addAction(new Action() {
void execute(XmlProvider xml) {
xml.asNode().thing[0].@foo = "bar"
}
})
transformer.transform(writer, generator)
then:
looksLike('\n \n ', writer.toString())
}
private void looksLike(String expected, String actual) {
assert removeTrailingWhitespace(actual) == removeTrailingWhitespace(TextUtil.toPlatformLineSeparators("\n" + expected))
}
private void looksLike(String expected, byte[] actual) {
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(actual))
assert removeTrailingWhitespace(new String(actual, "utf-8")) == removeTrailingWhitespace(TextUtil.toPlatformLineSeparators("\n" + expected))
}
private String removeTrailingWhitespace(String value) {
return value.replaceFirst('(?s)\\s+$', "")
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy