[java-metadata-aggregator] branch master updated: MDA-204 - migrate away from legacy xmlunit API
Ian Young
ian at iay.org.uk
Tue Mar 13 08:00:52 EDT 2018
This is an automated email from the git hooks/post-receive script.
iay pushed a commit to branch master
in repository java-metadata-aggregator.
View the commit online:
http://git.shibboleth.net/view/?p=java-metadata-aggregator.git;a=commit;h=d0da9d1675c2a8c070f117c77c67f5f712446477
The following commit(s) were added to refs/heads/master by this push:
new d0da9d1 MDA-204 - migrate away from legacy xmlunit API
d0da9d1 is described below
commit d0da9d1675c2a8c070f117c77c67f5f712446477
Author: Ian Young <ian at iay.org.uk>
AuthorDate: Tue Mar 13 12:00:43 2018 +0000
MDA-204 - migrate away from legacy xmlunit API
Rework assertXMLIdentical to use new XMLUnit API.
assertXMLIdentical now compares the provided nodes, not their associated documents.
Replace assertXMLEqual with assertXMLIdentical.
XML parser pool used to load test resources no longer drops comments and white space.
Added explicit tests for BaseDOMTest itself to nail down some of its less obvious behaviour.
Adjusted tests and test resources as appropriate.
---
aggregator-parent/pom.xml | 2 +-
.../net/shibboleth/metadata/dom/BaseDOMTest.java | 102 ++++++++++++---------
.../shibboleth/metadata/dom/BaseDOMTestTest.java | 61 ++++++++++++
.../net/shibboleth/metadata/dom/ContainerTest.java | 22 ++---
.../metadata/dom/XSLTtransformationStageTest.java | 5 +-
.../mdattr/EntityAttributeAddingStageTest.java | 10 +-
.../shibboleth/metadata/dom/BaseDOMTest-prolog.xml | 2 +
.../metadata/dom/ElementsStrippingStage-out-sw.xml | 1 +
.../dom/XSLTransformationStage-transform1.xml | 5 +
9 files changed, 146 insertions(+), 64 deletions(-)
diff --git a/aggregator-parent/pom.xml b/aggregator-parent/pom.xml
index 5078b7e..38adc0d 100644
--- a/aggregator-parent/pom.xml
+++ b/aggregator-parent/pom.xml
@@ -77,7 +77,7 @@
<!-- Test Dependencies -->
<dependency>
<groupId>org.xmlunit</groupId>
- <artifactId>xmlunit-legacy</artifactId>
+ <artifactId>xmlunit-core</artifactId>
<scope>test</scope>
</dependency>
diff --git a/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/BaseDOMTest.java b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/BaseDOMTest.java
index 3904a1e..dd455bb 100644
--- a/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/BaseDOMTest.java
+++ b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/BaseDOMTest.java
@@ -18,10 +18,19 @@
package net.shibboleth.metadata.dom;
import java.io.InputStream;
-import java.io.StringReader;
import java.util.List;
import javax.annotation.Nonnull;
+import javax.xml.transform.Source;
+
+import org.testng.Assert;
+import org.testng.annotations.BeforeClass;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.xmlunit.builder.DiffBuilder;
+import org.xmlunit.builder.Input;
+import org.xmlunit.diff.Diff;
+import org.xmlunit.input.NormalizedSource;
import net.shibboleth.metadata.BaseTest;
import net.shibboleth.metadata.ErrorStatus;
@@ -36,17 +45,22 @@ import net.shibboleth.utilities.java.support.xml.ParserPool;
import net.shibboleth.utilities.java.support.xml.SerializeSupport;
import net.shibboleth.utilities.java.support.xml.XMLParserException;
-import org.custommonkey.xmlunit.Diff;
-import org.custommonkey.xmlunit.XMLUnit;
-import org.testng.Assert;
-import org.testng.annotations.BeforeClass;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-
/** A base class for DOM related tests. */
public abstract class BaseDOMTest extends BaseTest {
- /** Initialized parser pool used to parser data. */
+ /**
+ * Initialized parser pool used to parse data.
+ *
+ * Parsers produced by this pool are set up slightly differently
+ * than the default Shibboleth {@link BasicParserPool}.
+ * In particular, they do <i>not</i> ignore either comment
+ * nodes or "ignoreable" whitespace in element content.
+ *
+ * This means that tests have access to <i>all</i> of the
+ * contents of test resources. It is the responsibility of
+ * each test to perform appropriate XMLUnit source wrapping
+ * or comparisons if that is appropriate in any given case.
+ */
private BasicParserPool parserPool;
/** Constructor */
@@ -61,9 +75,13 @@ public abstract class BaseDOMTest extends BaseTest {
*/
@BeforeClass
public void setUp() throws ComponentInitializationException {
- XMLUnit.setIgnoreWhitespace(true);
-
+ // Use defaults of BasicParserPool as a basis.
parserPool = new BasicParserPool();
+
+ // Override defaults to present ALL of the XML in the resource.
+ parserPool.setIgnoreComments(false);
+ parserPool.setIgnoreElementContentWhitespace(false);
+
parserPool.initialize();
}
@@ -117,49 +135,43 @@ public abstract class BaseDOMTest extends BaseTest {
}
/**
- * Checks whether two nodes are identical based on {@link Diff#identical()}.
- *
+ * Checks whether two nodes are identical.
+ *
+ * The only variation that is permitted for the purpose of comparison is that
+ * adjacent text nodes will be coalesced.
+ *
+ * Tests requiring other semantics should call XMLUnit directly.
+ *
* @param expected the expected node against which the actual node will be tested, never null
* @param actual the actual node tested against the expected node, never null
*/
- public void assertXMLIdentical(Node expected, Node actual) {
+ public void assertXMLIdentical(@Nonnull final Node expected, @Nonnull final Node actual) {
Constraint.isNotNull(expected, "Expected Node may not be null");
Constraint.isNotNull(actual, "Actual Node may not be null");
- Diff diff = new Diff(expected.getOwnerDocument(), actual.getOwnerDocument());
- if (!diff.identical()) {
- org.testng.Assert.fail(diff.toString());
+ /*
+ * Normalize empty and adjacent text nodes within the source nodes.
+ *
+ * Don't try to simplify this by passing expected and actual directly to the
+ * NormalizedSource(Node) constructor. That's much faster, as the constructor just
+ * normalizes the provided node, but by the same token it causes the original
+ * node to be changed, and side-effects are undesirable in a general-use method
+ * like this one.
+ */
+ final Source expectedSource = new NormalizedSource(Input.fromNode(expected).build());
+ final Source actualSource = new NormalizedSource(Input.fromNode(actual).build());
+
+ final Diff diff = DiffBuilder.compare(expectedSource).withTest(actualSource)
+ .checkForIdentical()
+ .build();
+
+ if (diff.hasDifferences()) {
+ System.out.println("Expected:\n" + SerializeSupport.nodeToString(expected));
+ System.out.println("Actual:\n" + SerializeSupport.nodeToString(actual));
+ Assert.fail(diff.toString());
}
}
- /**
- * Checks whether two nodes are equal based on {@link Node#isEqualNode(Node)}. Both nodes are serialized, re-parsed,
- * and then compared for equality. This forces any changes made to the document that haven't yet been represented in
- * the DOM (e.g., declaration of used namespaces) to be flushed to the DOM.
- *
- * @param expected the expected node against which the actual node will be tested, never null
- * @param actual the actual node tested against the expected node, never null
- *
- * @throws XMLParserException thrown if there is a problem serializing and re-parsing the nodes
- */
- public void assertXMLEqual(@Nonnull final Node expected, @Nonnull final Node actual) throws XMLParserException {
- Constraint.isNotNull(actual, "Actual Node may not be null");
- final String serializedActual = SerializeSupport.nodeToString(actual);
- Element deserializedActual = parserPool.parse(new StringReader(serializedActual)).getDocumentElement();
-
- Constraint.isNotNull(expected, "Expected Node may not be null");
- final String serializedExpected = SerializeSupport.nodeToString(expected);
- Element deserializedExpected = parserPool.parse(new StringReader(serializedExpected)).getDocumentElement();
-
- final boolean ok = deserializedExpected.isEqualNode(deserializedActual);
- if (!ok) {
- System.out.println("Expected:\n" + serializedExpected);
- System.out.println("Actual:\n" + serializedActual);
- }
-
- Assert.assertTrue(ok, "Actual Node does not equal expected Node");
- }
-
protected int countErrors(final Item<Element> item) {
final ClassToInstanceMultiMap<ItemMetadata> metadata = item.getItemMetadata();
final List<ErrorStatus> errors = metadata.get(ErrorStatus.class);
diff --git a/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/BaseDOMTestTest.java b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/BaseDOMTestTest.java
new file mode 100644
index 0000000..f677dee
--- /dev/null
+++ b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/BaseDOMTestTest.java
@@ -0,0 +1,61 @@
+package net.shibboleth.metadata.dom;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.xmlunit.builder.DiffBuilder;
+import org.xmlunit.diff.Diff;
+
+public class BaseDOMTestTest extends BaseDOMTest {
+
+ protected BaseDOMTestTest() {
+ super(BaseDOMTest.class);
+ }
+
+ // Check that readXMLData can read the prolog from a resource.
+ @Test
+ public void readXMLData_prolog() throws Exception {
+ final Element docElement = readXMLData("prolog.xml");
+ final Document doc = docElement.getOwnerDocument();
+ Assert.assertEquals(doc.getChildNodes().getLength(), 2);
+
+ final Node com = doc.getChildNodes().item(0);
+ Assert.assertEquals(com.getNodeType(), Node.COMMENT_NODE);
+ Assert.assertEquals(com.getNodeValue(), "comment in prolog");
+
+ final Node root = doc.getChildNodes().item(1);
+ Assert.assertEquals(root.getNodeType(), Node.ELEMENT_NODE);
+ }
+
+ // Check that assertXMLIdentical normalizes consecutive text nodes
+ @Test
+ public void assertXMLIdentical_normalized() throws Exception {
+ final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+
+ final Element el1 = doc.createElement("tag");
+ el1.appendChild(doc.createTextNode("foo"));
+ el1.appendChild(doc.createTextNode(""));
+ el1.appendChild(doc.createTextNode("bar"));
+
+ final Element el2 = doc.createElement("tag");
+ el2.appendChild(doc.createTextNode("foobar"));
+
+ // Verify that the nodes are *different* according to XMLUnit.
+ final Diff diff = DiffBuilder.compare(el1).withTest(el2)
+ .checkForIdentical().build();
+ Assert.assertTrue(diff.hasDifferences());
+
+ // Check that the nodes *look* identical to assertXMLIdentical.
+ assertXMLIdentical(el1, el2);
+
+ // Verify that the original nodes are *still* different according to XMLUnit.
+ // (assertXMLIdentical should not have side-effects)
+ final Diff diff2 = DiffBuilder.compare(el1).withTest(el2)
+ .checkForIdentical().build();
+ Assert.assertTrue(diff2.hasDifferences(), "side effect detected");
+ }
+}
diff --git a/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/ContainerTest.java b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/ContainerTest.java
index 5fbb0b4..bc2ac4d 100644
--- a/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/ContainerTest.java
+++ b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/ContainerTest.java
@@ -56,13 +56,13 @@ public class ContainerTest extends BaseDOMTest {
c1.addChild(e2, Container.FIRST_CHILD);
final Element ok = readXMLData("add1.xml");
- assertXMLEqual(ok, e1);
+ assertXMLIdentical(ok, e1);
final Element e3 = doc.createElementNS("ns", "child2");
e3.setTextContent("child 2 value");
c1.addChild(e3, Container.FIRST_CHILD);
final Element ok2 = readXMLData("addFirst.xml");
- assertXMLEqual(ok2, e1);
+ assertXMLIdentical(ok2, e1);
}
@Test
@@ -74,13 +74,13 @@ public class ContainerTest extends BaseDOMTest {
c1.addChild(e2, Container.LAST_CHILD);
final Element ok = readXMLData("add1.xml");
- assertXMLEqual(ok, e1);
+ assertXMLIdentical(ok, e1);
final Element e3 = doc.createElementNS("ns", "child2");
e3.setTextContent("child 2 value");
c1.addChild(e3, Container.LAST_CHILD);
final Element ok2 = readXMLData("addLast.xml");
- assertXMLEqual(ok2, e1);
+ assertXMLIdentical(ok2, e1);
}
@Test
@@ -97,7 +97,7 @@ public class ContainerTest extends BaseDOMTest {
midContainer.addChild(leaf2, Container.LAST_CHILD);
final Element ok = readXMLData("nested.xml");
- assertXMLEqual(ok, root);
+ assertXMLIdentical(ok, root);
}
@Test
@@ -117,7 +117,7 @@ public class ContainerTest extends BaseDOMTest {
}, Container.FIRST_CHILD);
final Element ok = readXMLData("add1.xml");
- assertXMLEqual(ok, e1);
+ assertXMLIdentical(ok, e1);
c1.addChild(new Function<Container, Element>(){
@@ -131,7 +131,7 @@ public class ContainerTest extends BaseDOMTest {
}, Container.FIRST_CHILD);
final Element ok2 = readXMLData("addFirst.xml");
- assertXMLEqual(ok2, e1);
+ assertXMLIdentical(ok2, e1);
}
@Test
@@ -151,7 +151,7 @@ public class ContainerTest extends BaseDOMTest {
}, Container.LAST_CHILD);
final Element ok = readXMLData("add1.xml");
- assertXMLEqual(ok, e1);
+ assertXMLIdentical(ok, e1);
c1.addChild(new Function<Container, Element>(){
@@ -165,7 +165,7 @@ public class ContainerTest extends BaseDOMTest {
}, Container.LAST_CHILD);
final Element ok2 = readXMLData("addLast.xml");
- assertXMLEqual(ok2, e1);
+ assertXMLIdentical(ok2, e1);
}
@Test
@@ -242,7 +242,7 @@ public class ContainerTest extends BaseDOMTest {
}, Container.LAST_CHILD);
final Element ok = readXMLData("add1.xml");
- assertXMLEqual(ok, e1);
+ assertXMLIdentical(ok, e1);
// same again should NOT change the result for locate
@@ -266,7 +266,7 @@ public class ContainerTest extends BaseDOMTest {
}, Container.LAST_CHILD);
final Element ok2 = readXMLData("add1.xml");
- assertXMLEqual(ok2, e1);
+ assertXMLIdentical(ok2, e1);
}
}
diff --git a/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/XSLTtransformationStageTest.java b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/XSLTtransformationStageTest.java
index 3d76b19..9c86fcc 100644
--- a/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/XSLTtransformationStageTest.java
+++ b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/XSLTtransformationStageTest.java
@@ -232,8 +232,9 @@ public class XSLTtransformationStageTest extends BaseDOMTest {
AssertSupport.assertValidComponentInfo(result, 1, XSLTransformationStage.class, "test");
Assert.assertEquals(result.getItemMetadata().get(TestInfo.class).size(), 1);
- final Element expected = readXMLData("output.xml");
- assertXMLIdentical(expected, result.unwrap());
+ final Element expected = readXMLData("transform1.xml");
+ // Compare *documents* here so that we include the prolog
+ assertXMLIdentical(expected.getOwnerDocument(), result.unwrap().getOwnerDocument());
// peek at the first node in the document; should be a comment
final Node firstNode = result.unwrap().getOwnerDocument().getFirstChild();
diff --git a/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/saml/mdattr/EntityAttributeAddingStageTest.java b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/saml/mdattr/EntityAttributeAddingStageTest.java
index 03fced0..cb410b9 100644
--- a/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/saml/mdattr/EntityAttributeAddingStageTest.java
+++ b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/saml/mdattr/EntityAttributeAddingStageTest.java
@@ -79,7 +79,7 @@ public class EntityAttributeAddingStageTest extends BaseDOMTest {
pipeline.execute(itemCollection);
final Element result = itemCollection.get(0).unwrap();
final Element expected = readXMLData("added1.xml");
- assertXMLEqual(expected, result);
+ assertXMLIdentical(expected, result);
}
/*
@@ -98,7 +98,7 @@ public class EntityAttributeAddingStageTest extends BaseDOMTest {
pipeline.execute(itemCollection);
final Element result = itemCollection.get(0).unwrap();
final Element expected = readXMLData("added2.xml");
- assertXMLEqual(expected, result);
+ assertXMLIdentical(expected, result);
}
/*
@@ -122,7 +122,7 @@ public class EntityAttributeAddingStageTest extends BaseDOMTest {
pipeline.execute(itemCollection);
final Element result = itemCollection.get(0).unwrap();
final Element expected = readXMLData("added3.xml");
- assertXMLEqual(expected, result);
+ assertXMLIdentical(expected, result);
}
/*
@@ -141,7 +141,7 @@ public class EntityAttributeAddingStageTest extends BaseDOMTest {
pipeline.execute(itemCollection);
final Element result = itemCollection.get(0).unwrap();
final Element expected = readXMLData("added2.xml");
- assertXMLEqual(expected, result);
+ assertXMLIdentical(expected, result);
}
/*
@@ -160,6 +160,6 @@ public class EntityAttributeAddingStageTest extends BaseDOMTest {
pipeline.execute(itemCollection);
final Element result = itemCollection.get(0).unwrap();
final Element expected = readXMLData("added2.xml");
- assertXMLEqual(expected, result);
+ assertXMLIdentical(expected, result);
}
}
diff --git a/aggregator-pipeline/src/test/resources/net/shibboleth/metadata/dom/BaseDOMTest-prolog.xml b/aggregator-pipeline/src/test/resources/net/shibboleth/metadata/dom/BaseDOMTest-prolog.xml
new file mode 100644
index 0000000..87fc75c
--- /dev/null
+++ b/aggregator-pipeline/src/test/resources/net/shibboleth/metadata/dom/BaseDOMTest-prolog.xml
@@ -0,0 +1,2 @@
+<!--comment in prolog-->
+<root/>
diff --git a/aggregator-pipeline/src/test/resources/net/shibboleth/metadata/dom/ElementsStrippingStage-out-sw.xml b/aggregator-pipeline/src/test/resources/net/shibboleth/metadata/dom/ElementsStrippingStage-out-sw.xml
index c897e32..52b37b0 100644
--- a/aggregator-pipeline/src/test/resources/net/shibboleth/metadata/dom/ElementsStrippingStage-out-sw.xml
+++ b/aggregator-pipeline/src/test/resources/net/shibboleth/metadata/dom/ElementsStrippingStage-out-sw.xml
@@ -9,5 +9,6 @@
<beta:StripMe>
<nested/>
</beta:StripMe>
+
</alpha:keepMe>
</rootElement>
diff --git a/aggregator-pipeline/src/test/resources/net/shibboleth/metadata/dom/XSLTransformationStage-transform1.xml b/aggregator-pipeline/src/test/resources/net/shibboleth/metadata/dom/XSLTransformationStage-transform1.xml
new file mode 100644
index 0000000..3c0d9f3
--- /dev/null
+++ b/aggregator-pipeline/src/test/resources/net/shibboleth/metadata/dom/XSLTransformationStage-transform1.xml
@@ -0,0 +1,5 @@
+<!--this is a comment--><values>
+ <firstValue>17 bananas</firstValue>
+ <secondValue value="99"/>
+ <thirdValue>33</thirdValue>
+</values>
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list