"Fossies" - the Fresh Open Source Software Archive

Member "groovy-3.0.9/subprojects/groovy-xml/src/test/groovy/groovy/xml/XmlUtilTest.groovy" (3 Sep 2021, 3897 Bytes) of package /linux/misc/apache-groovy-src-3.0.9.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Java source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "XmlUtilTest.groovy": 3.0.8_vs_3.0.9.

    1 /*
    2  *  Licensed to the Apache Software Foundation (ASF) under one
    3  *  or more contributor license agreements.  See the NOTICE file
    4  *  distributed with this work for additional information
    5  *  regarding copyright ownership.  The ASF licenses this file
    6  *  to you under the Apache License, Version 2.0 (the
    7  *  "License"); you may not use this file except in compliance
    8  *  with the License.  You may obtain a copy of the License at
    9  *
   10  *    http://www.apache.org/licenses/LICENSE-2.0
   11  *
   12  *  Unless required by applicable law or agreed to in writing,
   13  *  software distributed under the License is distributed on an
   14  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15  *  KIND, either express or implied.  See the License for the
   16  *  specific language governing permissions and limitations
   17  *  under the License.
   18  */
   19 package groovy.xml
   20 
   21 import groovy.test.GroovyTestCase
   22 import org.xml.sax.ErrorHandler
   23 import org.xml.sax.InputSource
   24 
   25 import javax.xml.transform.stream.StreamSource
   26 
   27 import static groovy.util.XmlAssert.assertXmlEquals
   28 import static groovy.xml.XmlUtil.escapeControlCharacters
   29 import static groovy.xml.XmlUtil.escapeXml
   30 import static groovy.xml.XmlUtil.newSAXParser
   31 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI
   32 
   33 class XmlUtilTest extends GroovyTestCase {
   34     def xml = """\
   35     <?xml version="1.0" encoding="UTF-8"?>
   36     <Schl\u00FCssel>
   37     text content
   38     </Schl\u00FCssel>
   39     """.stripIndent()
   40 
   41     // GROOVY-5158
   42     void testSerializeOfGPathResultShouldRoundTrip() {
   43         def source = new InputSource(new StringReader(xml))
   44         source.encoding = "UTF-8"
   45         assertXmlEquals(xml, XmlUtil.serialize(new XmlSlurper().parse(source)))
   46     }
   47 
   48     def xml2 = '<?xml version="1.0" encoding="UTF-8"?><Name>🎁4</Name>'
   49 
   50     // GROOVY-10132
   51     void testSerializeOfSurrogate() {
   52         def source = new InputSource(new StringReader(xml2))
   53         source.encoding = "UTF-8"
   54         assertXmlEquals(xml2, XmlUtil.serialize(new XmlSlurper().parse(source)))
   55     }
   56 
   57     // GROOVY-5361
   58     void testSchemaValidationUtilityMethod() {
   59         Locale dl = Locale.getDefault()
   60         Locale.setDefault(Locale.ENGLISH)
   61 
   62         def cases = [
   63             "<person><first>James</first><last>Kirk</last></person>",
   64             "<person><first>James</first><middle>T.</middle><last>Kirk</last></person>",
   65             "<person title='Captain'><first>James</first><last>Kirk</last></person>"
   66         ]
   67 
   68         def xsd = '''<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   69           <xs:element name="person">
   70             <xs:complexType>
   71               <xs:sequence>
   72                 <xs:element name="first" type="xs:NCName"/>
   73                 <xs:element name="last" type="xs:NCName"/>
   74               </xs:sequence>
   75             </xs:complexType>
   76           </xs:element>
   77         </xs:schema>'''
   78 
   79         def expected = [
   80             /Kirk, James No Error/,
   81             /Kirk, James .*Invalid content.*middle.*last.*expected/,
   82             /Kirk, James .*title.*not allowed.*in element.*person/
   83         ]
   84 
   85         try {
   86             def message
   87             def parser = new XmlParser(newSAXParser(W3C_XML_SCHEMA_NS_URI, new StreamSource(new StringReader(xsd))))
   88             def results = []
   89             parser.errorHandler = { message = it.message } as ErrorHandler
   90             cases.each {
   91                 message = 'No Error'
   92                 def p = parser.parseText(it)
   93                 results << "${p.last.text()}, ${p.first.text()} $message"
   94             }
   95             assert results.size() == 3
   96             (0..2).each {
   97                 assert results[it] =~ expected[it]
   98             }
   99         } finally {
  100             Locale.setDefault(dl)
  101         }
  102     }
  103 
  104     // GROOVY-5775
  105     void testEscaping() {
  106       def ans = escapeControlCharacters(escapeXml('"bread" & "butter"\r\n'))
  107       assert ans == '&quot;bread&quot; &amp; &quot;butter&quot;&#13;&#10;'
  108     }
  109 }