[java-support] 01/02: JSPT-75 Compare the parsed nodes as well as their textual representation

Rod Widdowson rdw at steadingsoftware.com
Sun Oct 8 09:40:16 EDT 2017


This is an automated email from the git hooks/post-receive script.

rdw pushed a commit to branch maint-7
in repository java-support.

View the commit online:
http://git.shibboleth.net/view/?p=java-support.git;a=commit;h=6461e0592fdcc0e2bc907393948f96a1429cc9f9

commit 6461e0592fdcc0e2bc907393948f96a1429cc9f9
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Sun Oct 8 14:03:17 2017 +0100

    JSPT-75 Compare the parsed nodes as well as their textual representation
    
    https://issues.shibboleth.net/jira/browse/JSPT-75
    
    Repurpose from code for comparing XML structures and add to all the
    serial support tests.
---
 .../java/support/xml/SerializeSupportTest.java     | 134 ++++++++++++++++++---
 1 file changed, 115 insertions(+), 19 deletions(-)

diff --git a/src/test/java/net/shibboleth/utilities/java/support/xml/SerializeSupportTest.java b/src/test/java/net/shibboleth/utilities/java/support/xml/SerializeSupportTest.java
index 58ce9da..a6c0866 100644
--- a/src/test/java/net/shibboleth/utilities/java/support/xml/SerializeSupportTest.java
+++ b/src/test/java/net/shibboleth/utilities/java/support/xml/SerializeSupportTest.java
@@ -23,21 +23,26 @@ import java.io.IOException;
 import java.io.StringReader;
 
 import javax.xml.parsers.DocumentBuilder;
-
-import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
+import javax.xml.soap.Text;
 
 import org.springframework.core.io.ClassPathResource;
 import org.springframework.core.io.Resource;
 import org.testng.Assert;
 import org.testng.annotations.BeforeTest;
 import org.testng.annotations.Test;
+import org.w3c.dom.Attr;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 import org.w3c.dom.ls.DOMImplementationLS;
 import org.w3c.dom.ls.LSOutput;
 import org.w3c.dom.ls.LSSerializer;
 import org.xml.sax.SAXException;
 
+import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
+
 /**
  * Tests for {@link NamespaceSupport}
  */
@@ -47,15 +52,99 @@ public class SerializeSupportTest {
 
     private BasicParserPool parserPool;
 
-    @BeforeTest public void setup() throws XMLParserException, ComponentInitializationException, SAXException,
-            IOException {
+    // Helper methods, from http://www.java2s.com/Code/Java/XML/ComparetwoDOMNodes.htm
+    private int countNonNamespaceAttributes(final NamedNodeMap attrs) {
+        int n = 0;
+        for (int i = 0; i < attrs.getLength(); i++) {
+            final Attr attr = (Attr) attrs.item(i);
+            if (!attr.getName().startsWith("xmlns")) {
+                n++;
+            }
+        }
+        return n;
+    }
+
+    private void assertEquals(final Node expected, final Node actual) {
+
+        Assert.assertEquals(expected.getNodeType(), actual.getNodeType(),
+                "Different types of nodes: " + expected + " " + actual);
+
+        if (expected instanceof Document) {
+            final Document expectedDoc = (Document) expected;
+            final Document actualDoc = (Document) actual;
+            assertEquals(expectedDoc.getDocumentElement(), actualDoc.getDocumentElement());
+        } else if (expected instanceof Element) {
+            final Element expectedElement = (Element) expected;
+            final Element actualElement = (Element) actual;
+
+            // compare element names
+            Assert.assertEquals(expectedElement.getLocalName(), actualElement.getLocalName(),
+                    "Element names do not match: " + expectedElement.getLocalName() + " "
+                            + actualElement.getLocalName());
+            // compare element ns
+            final String expectedNS = expectedElement.getNamespaceURI();
+            final String actualNS = actualElement.getNamespaceURI();
+            Assert.assertFalse(
+                    (expectedNS == null && actualNS != null) || (expectedNS != null && !expectedNS.equals(actualNS)),
+                    "Element namespaces names do not match: " + expectedNS + " " + actualNS);
+
+            final String elementName = "{" + expectedElement.getNamespaceURI() + "}" + actualElement.getLocalName();
+
+            // compare attributes
+            final NamedNodeMap expectedAttrs = expectedElement.getAttributes();
+            final NamedNodeMap actualAttrs = actualElement.getAttributes();
+            Assert.assertEquals(countNonNamespaceAttributes(expectedAttrs), countNonNamespaceAttributes(actualAttrs),
+                    ": Number of attributes do not match up: " + countNonNamespaceAttributes(expectedAttrs) + " "
+                            + countNonNamespaceAttributes(actualAttrs));
+
+            for (int i = 0; i < expectedAttrs.getLength(); i++) {
+                final Attr expectedAttr = (Attr) expectedAttrs.item(i);
+                if (expectedAttr.getName().startsWith("xmlns")) {
+                    continue;
+                }
+                Attr actualAttr = null;
+                if (expectedAttr.getNamespaceURI() == null) {
+                    actualAttr = (Attr) actualAttrs.getNamedItem(expectedAttr.getName());
+                } else {
+                    actualAttr = (Attr) actualAttrs.getNamedItemNS(expectedAttr.getNamespaceURI(),
+                            expectedAttr.getLocalName());
+                }
+                Assert.assertNotNull(actualAttr, elementName + ": No attribute found:" + expectedAttr);
+
+                Assert.assertEquals(expectedAttr.getValue(), actualAttr.getValue(), elementName
+                        + ": Attribute values do not match: " + expectedAttr.getValue() + " " + actualAttr.getValue());
+            }
+
+            // compare children
+            final NodeList expectedChildren = expectedElement.getChildNodes();
+            final NodeList actualChildren = actualElement.getChildNodes();
+
+            Assert.assertEquals(expectedChildren.getLength(), actualChildren.getLength(),
+                    elementName + ": Number of children do not match up: " + expectedChildren.getLength() + " "
+                            + actualChildren.getLength());
+
+            for (int i = 0; i < expectedChildren.getLength(); i++) {
+                final Node expectedChild = expectedChildren.item(i);
+                final Node actualChild = actualChildren.item(i);
+                assertEquals(expectedChild, actualChild);
+            }
+        } else if (expected instanceof Text) {
+            final String expectedData = ((Text) expected).getData().trim();
+            final String actualData = ((Text) actual).getData().trim();
+
+            Assert.assertEquals(expectedData, actualData, "Text does not match: " + expectedData + " " + actualData);
+        }
+    }
+
+    @BeforeTest public void setup()
+            throws XMLParserException, ComponentInitializationException, SAXException, IOException {
         parserPool = new BasicParserPool();
         parserPool.initialize();
-        DocumentBuilder builder = parserPool.getBuilder();
+        final DocumentBuilder builder = parserPool.getBuilder();
         try {
-            Resource resource =
+            final Resource resource =
                     new ClassPathResource("/net/shibboleth/utilities/java/support/xml/serializeSupportTest.xml");
-            Document testFile = builder.parse(resource.getInputStream());
+            final Document testFile = builder.parse(resource.getInputStream());
 
             parent = (Element) testFile.getFirstChild();
 
@@ -64,58 +153,65 @@ public class SerializeSupportTest {
         }
     }
 
-    @Test public void testNodeToString() throws XMLParserException {
+    @Test public void testNodeToString() throws Exception {
         //
         // Serialize then parse and serialize again
         //
-        String s = SerializeSupport.nodeToString(parent);
+        final String s = SerializeSupport.nodeToString(parent);
 
-        Document dom = parserPool.parse(new StringReader(s));
+        final Document dom = parserPool.parse(new StringReader(s));
 
         Assert.assertEquals(SerializeSupport.nodeToString(dom.getFirstChild()), s, "Should serialize to same output");
+        assertEquals(parent, dom.getFirstChild());
     }
 
     @Test(dependsOnMethods = {"testNodeToString"}) public void testWriteNode() throws XMLParserException {
         //
         // Serialize then parse and serialize again
         //
-        ByteArrayOutputStream output = new ByteArrayOutputStream();
+        final ByteArrayOutputStream output = new ByteArrayOutputStream();
 
         SerializeSupport.writeNode(parent, output);
 
-        byte data[] = output.toByteArray();
+        final byte data[] = output.toByteArray();
 
-        Document dom = parserPool.parse(new ByteArrayInputStream(data));
+        final Document dom = parserPool.parse(new ByteArrayInputStream(data));
 
         Assert.assertEquals(SerializeSupport.nodeToString(dom.getFirstChild()), SerializeSupport.nodeToString(parent),
                 "Should serialize to same output");
+
+        assertEquals(dom.getFirstChild(), parent);
     }
 
     @Test(dependsOnMethods = {"testNodeToString"}) public void testPrettyPrintXML() throws XMLParserException {
         //
         // Serialize then parse and serialize again
         //
-        String s = SerializeSupport.prettyPrintXML(parent);
+        final String s = SerializeSupport.prettyPrintXML(parent);
 
-        Document dom = parserPool.parse(new StringReader(s));
+        final Document dom = parserPool.parse(new StringReader(s));
 
         Assert.assertEquals(SerializeSupport.nodeToString(dom.getFirstChild()), SerializeSupport.nodeToString(parent),
                 "Should serialize to same output");
+
+        assertEquals(dom.getFirstChild(), parent);
     }
 
     @Test(dependsOnMethods = {"testNodeToString"}) public void testLSOps() throws XMLParserException {
 
-        DOMImplementationLS domLS = SerializeSupport.getDOMImplementationLS(parent);
-        LSSerializer serializer = SerializeSupport.getLSSerializer(domLS, null);
+        final DOMImplementationLS domLS = SerializeSupport.getDOMImplementationLS(parent);
+        final LSSerializer serializer = SerializeSupport.getLSSerializer(domLS, null);
 
         final LSOutput serializerOut = domLS.createLSOutput();
-        ByteArrayOutputStream output = new ByteArrayOutputStream();
+        final ByteArrayOutputStream output = new ByteArrayOutputStream();
         serializerOut.setByteStream(output);
 
         serializer.write(parent, serializerOut);
 
-        Document dom = parserPool.parse(new ByteArrayInputStream(output.toByteArray()));
+        final Document dom = parserPool.parse(new ByteArrayInputStream(output.toByteArray()));
         Assert.assertEquals(SerializeSupport.nodeToString(dom.getFirstChild()), SerializeSupport.nodeToString(parent),
                 "Should serialize to same output");
+        assertEquals(dom.getFirstChild(), parent);
+
     }
 }

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list