/*
* Copyright 2003-2009 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 groovy.xml
/**
* Tests for MarkupBuilder. The tests directly in this file are specific
* to MarkupBuilder. Functionality in common with StreamingMarkupBuilder
* is tested in the BuilderTestSupport parent class.
*
* @author Scott Stirling
* @author Pilho Kim
* @author Paul King
*/
class MarkupBuilderTest extends BuilderTestSupport {
private StringWriter writer
private MarkupBuilder xml
protected void setUp() {
writer = new StringWriter()
xml = new MarkupBuilder(writer)
}
private assertExpectedXmlDefault(expectedXml) {
checkXml expectedXml, writer
}
/**
* It is not recommended practice to use the value attribute
* when also using nested content as there is no way to specify
* the ordering of such mixed content. The default behaviour is
* to include the value as the first node in the resulting xml.
*
* StreamingMarkupBuilder excludes this behavior and requires
* yield or yieldUnescaped exclusively. MarkupBuilder also supports
* the yield approach but retains this style for backwards compatibility.
*/
void testSmallTreeWithTextAndAttributes() {
def m = {
root1('hello1', a: 5, b: 7) {
elem1('hello2', c: 4) {
elem2('hello3', d: 4)
}
elem1('hello2', c: 4) {
elem2('hello3')
elem2('hello3', d: 4)
}
elem1('hello2', c: 4) {
elem2('hello3', d: 4)
elem2('hello3')
}
elem1('hello2', c: 4) {
elem2(d: 4)
elem2('hello3', d: 4)
}
elem1('hello2', c: 4) {
elem2('hello3', d: 4)
elem2(d: 4)
}
elem1('hello2') {
elem2('hello3', d: 4)
elem2(d: 4)
}
}
}
assertExpectedXml m, '''\
"
"
call to outside
' } void testOmitAttributeSettingsKeepBothDefaultCase() { xml.element(att1:null, att2:'') assertExpectedXmlDefault "