📄 markupbuildertest.groovy
字号:
package groovy.xml;import groovy.util.GroovyTestCase/** * Tests that special XML chars are entitized by MarkupBuilder. * * @author <a href="mailto:scottstirling@rcn.com">Scott Stirling</a> * * @version $Revision: 1.4 $ * * Fix the cr lf handling of multiline stringon both of linux and Windows XP. * This test should success on Windows XP. * * @author Pilho Kim */class MarkupBuilderTest extends GroovyTestCase { private StringWriter writer private MarkupBuilder xml protected void setUp() { writer = new StringWriter() xml = new MarkupBuilder(writer) } /** * Main test method. Checks that well-formed XML is generated * and that the appropriate characters are escaped with the * correct entities. */ void testBuilder() { String expectedXml = '''<chars> <ampersand a='&'>&</ampersand> <quote attr='"'>"</quote> <apostrophe attr='''>'</apostrophe> <lessthan attr='value'>chars: & < > '</lessthan> <element attr='value 1 & 2'>chars: & < > " in middle</element> <greaterthan>></greaterthan> <emptyElement /></chars>''' // Generate the markup. xml.chars { ampersand(a: "&", "&") quote(attr: "\"", "\"") apostrophe(attr: "'", "'") lessthan(attr: "value", "chars: & < > '") element(attr: "value 1 & 2", "chars: & < > \" in middle") greaterthan(">") emptyElement() } // Compare the MarkupBuilder generated XML with the 'expectedXml' // string. assertEquals(expectedXml, fixEOLs(writer.toString())) } /** * Tests the builder with double quotes for attribute values. */ void testBuilderWithDoubleQuotes() { String expectedXml = '''<chars> <ampersand a="&">&</ampersand> <quote attr=""">"</quote> <apostrophe attr="'">'</apostrophe> <lessthan attr="value">chars: & < > '</lessthan> <element attr="value 1 & 2">chars: & < > " in middle</element> <greaterthan>></greaterthan> <emptyElement /></chars>''' // Generate the markup. xml.doubleQuotes = true xml.chars { ampersand(a: "&", "&") quote(attr: "\"", "\"") apostrophe(attr: "'", "'") lessthan(attr: "value", "chars: & < > '") element(attr: "value 1 & 2", "chars: & < > \" in middle") greaterthan(">") emptyElement() } // Compare the MarkupBuilder generated XML with the 'expectedXml' // string. assertEquals(expectedXml, fixEOLs(writer.toString())) } /** * Tests that MarkupBuilder escapes element content correctly, even * when the content contains line-endings. */ void testEscapingMultiLineContent() { def expectedXml = '''<element>This is multi-line content with characters, such as <, thatrequire escaping. The other characters consist of: * > - greater than * & - ampersand</element>''' // Generate the markup. xml.element('''This is multi-line content with characters, such as <, thatrequire escaping. The other characters consist of: * > - greater than * & - ampersand''') // Compare the generated markup with the 'expectedXml' string. assertEquals(expectedXml, fixEOLs(writer.toString())) } /** * Checks against a regression bug whereby some empty elements were * not closed. */ void testMarkupForClosingTags() { def expectedXml ='''<ELEM1> <ELEM2 type='2' id='first'> <ELEM3A id='first' /> <ELEM3B type='3'>text</ELEM3B> </ELEM2> <ELEM2 type='2' id='second'> <ELEM3A id='second' /> <ELEM3B type='3'>text</ELEM3B> </ELEM2> <ELEM2 type='2' id='third'> <ELEM3A id='third' /> <ELEM3B type='3'>text</ELEM3B> </ELEM2></ELEM1>''' // Generate the XML. def list = ['first', 'second', 'third'] xml.ELEM1() { list.each(){ r -> xml.ELEM2(id:r, type:'2') { xml.ELEM3A(id:r) xml.ELEM3B(type:'3', 'text') } } } // Check that the MarkupBuilder has generated the expected XML. assertEquals(expectedXml, fixEOLs(writer.toString())) } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -