Bug 8675: Fix a design flaw of the new XML parser
[yangtools.git] / yang / yang-data-codec-xml / src / test / java / org / opendaylight / yangtools / yang / data / codec / xml / Bug5446Test.java
index 8c7970230be51ec28353ac5816c5408523f593d3..c6cfe00cafb0c636362c91ba8109cbb6f3399636 100644 (file)
@@ -15,9 +15,6 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.StringWriter;
 import java.net.URI;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
@@ -32,6 +29,7 @@ import javax.xml.transform.stream.StreamResult;
 import org.custommonkey.xmlunit.XMLTestCase;
 import org.custommonkey.xmlunit.XMLUnit;
 import org.junit.Test;
+import org.opendaylight.yangtools.util.xml.UntrustedXML;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
@@ -53,23 +51,16 @@ import org.xml.sax.SAXException;
 
 public class Bug5446Test extends XMLTestCase {
     private static final XMLOutputFactory XML_FACTORY;
-    private static final DocumentBuilderFactory BUILDERFACTORY;
 
     static {
         XML_FACTORY = XMLOutputFactory.newFactory();
-        XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
-
-        BUILDERFACTORY = DocumentBuilderFactory.newInstance();
-        BUILDERFACTORY.setNamespaceAware(true);
-        BUILDERFACTORY.setCoalescing(true);
-        BUILDERFACTORY.setIgnoringElementContentWhitespace(true);
-        BUILDERFACTORY.setIgnoringComments(true);
+        XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.FALSE);
     }
 
-    private QNameModule fooModuleQName;
-    private QName rootQName;
-    private QName ipAddressQName;
-    private SchemaContext schemaContext;
+    private final QNameModule fooModuleQName;
+    private final QName rootQName;
+    private final QName ipAddressQName;
+    private final SchemaContext schemaContext;
 
     public Bug5446Test() throws Exception {
         fooModuleQName = QNameModule.create(new URI("foo"), SimpleDateFormatUtil.getRevisionFormat()
@@ -106,7 +97,7 @@ public class Bug5446Test extends XMLTestCase {
         XMLUnit.setIgnoreAttributeOrder(true);
         XMLUnit.setNormalize(true);
 
-        String expectedXMLString = toString(doc.getDocumentElement().getElementsByTagName("root").item(0));
+        String expectedXMLString = toString(doc.getDocumentElement());
         String serializationResultXMLString = toString(serializationResult.getNode());
 
         assertXMLEqual(expectedXMLString, serializationResultXMLString);
@@ -122,7 +113,7 @@ public class Bug5446Test extends XMLTestCase {
 
     private static DOMResult writeNormalizedNode(final ContainerNode normalized, final SchemaContext context)
             throws IOException, XMLStreamException {
-        final Document doc = getDocument();
+        final Document doc = UntrustedXML.newDocumentBuilder().newDocument();
         final DOMResult result = new DOMResult(doc);
         NormalizedNodeWriter normalizedNodeWriter = null;
         NormalizedNodeStreamWriter normalizedNodeStreamWriter = null;
@@ -160,14 +151,7 @@ public class Bug5446Test extends XMLTestCase {
     }
 
     private static Document readXmlToDocument(final InputStream xmlContent) throws IOException, SAXException {
-        final DocumentBuilder dBuilder;
-        try {
-            dBuilder = BUILDERFACTORY.newDocumentBuilder();
-        } catch (final ParserConfigurationException e) {
-            throw new RuntimeException("Failed to parse XML document", e);
-        }
-        final Document doc = dBuilder.parse(xmlContent);
-
+        final Document doc = UntrustedXML.newDocumentBuilder().parse(xmlContent);
         doc.getDocumentElement().normalize();
         return doc;
     }
@@ -187,16 +171,4 @@ public class Bug5446Test extends XMLTestCase {
             throw new RuntimeException("Unable to serialize xml element " + xml, e);
         }
     }
-
-    private static Document getDocument() {
-        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
-        Document doc = null;
-        try {
-            DocumentBuilder bob = dbf.newDocumentBuilder();
-            doc = bob.newDocument();
-        } catch (ParserConfigurationException e) {
-            throw new RuntimeException(e);
-        }
-        return doc;
-    }
 }