Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
XMLUnit Demo...
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.blep</groupId>
<artifactId>xmlunit-poc</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>xmlunit</groupId>
<artifactId>xmlunit</artifactId>
<version>1.4</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package org.blep;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.Difference;
import org.custommonkey.xmlunit.DifferenceListener;
import org.junit.Test;
import org.w3c.dom.Node;
import static junit.framework.Assert.assertFalse;
import static org.custommonkey.xmlunit.XMLAssert.*;
/**
* User: blep
*/
public class XmlUnitTest {
private static String control ="<my:myXml xmlns:my='myUri' " +
"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
"xsi:schemaLocation='myUri http://somehost/my.xsd'>" +
"<timestamp>1377064471255</timestamp>" +
"<realBusinessValue>The Babel Tower</realBusinessValue>" +
"</my:myXml>";
private static String shouldBeSimilar ="<m:myXml xmlns:m='myUri' " +
"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
"xsi:schemaLocation='myUri http://somehost/my.xsd'>" +
"<timestamp>137706447100</timestamp>" +
"<realBusinessValue>The Babel Tower</realBusinessValue>" +
"</m:myXml>";
private static String different ="<my:myXml xmlns:my='myUri' " +
"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
"xsi:schemaLocation='myUri http://somehost/my.xsd'>" +
"<timestamp>1377064471255</timestamp>" +
"<realBusinessValue>The fake Babel Tower</realBusinessValue>" +
"</my:myXml>";
@Test
public void should_be_identical() throws Exception {
assertXMLIdentical(new Diff(control, control), true);
}
@Test
public void should_be_similar() throws Exception {
assertXMLEqual(control, control);
}
@Test
public void should_not_be_equal() throws Exception {
// timestamps and ns prefix differ, so they're not equal
assertXMLNotEqual(control, shouldBeSimilar);
}
@Test
public void make_notEquals_similar() throws Exception {
final DifferenceListener dl = new DifferenceListener() {
@Override
public int differenceFound(Difference difference) {
if(difference.getControlNodeDetail() == null ||
difference.getControlNodeDetail().getNode() == null ||
difference.getControlNodeDetail().getNode().getParentNode() == null)
return RETURN_ACCEPT_DIFFERENCE;
else if (difference.getControlNodeDetail().getNode().getParentNode().getNodeName().equals("timestamp"))
return RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR;
return RETURN_ACCEPT_DIFFERENCE;
}
@Override
public void skippedComparison(Node control, Node test) { }
};
//Same business value, different timestamp, different ns prefix => similar
final Diff diff = new Diff(control, shouldBeSimilar);
diff.overrideDifferenceListener(dl);
assertXMLEqual(diff, true);
// Same timestamp, same ns prefix, different business value => different
final Diff diff1 = new Diff(control, different);
diff1.overrideDifferenceListener(dl);
assertFalse(diff1.similar());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment