Here, we’ll be using XMLUnit from www.xmlunit.org in a Maven project.
[wp_ad_camp_1]
Include the following in your pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <!-- https://mvnrepository.com/artifact/xmlunit/xmlunit --> <dependency> <groupId>xmlunit</groupId> <artifactId>xmlunit</artifactId> <version>1.6</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.2</version> </dependency> |
Unit Test
Create a unit test with two test methods. We’ll test 2 scenarios – same and different XMLs.
[wp_ad_camp_2]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | package com.turreta.xml; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringWriter; import java.nio.charset.Charset; import org.apache.commons.io.IOUtils; import org.apache.commons.io.input.BOMInputStream; import org.custommonkey.xmlunit.Diff; import org.custommonkey.xmlunit.XMLUnit; import org.junit.Assert; import org.junit.Test; import org.xml.sax.SAXException; public class XmlCompare { @Test public void testSame() throws SAXException, IOException { String xmlOutput; try (InputStream outFileInputStream = new BOMInputStream(new FileInputStream("C:\\myxml.xml"))) { xmlOutput = getData(outFileInputStream); } try (Reader r = new InputStreamReader(new BOMInputStream(new FileInputStream("C:\\expected_myxml.xml")), Charset.forName("UTF-8"))) { XMLUnit.setIgnoreComments(true); XMLUnit.setIgnoreWhitespace(true); XMLUnit.setNormalize(true); XMLUnit.setIgnoreAttributeOrder(true); Diff diff = XMLUnit.compareXML(r, xmlOutput); StringBuffer sb = new StringBuffer(); diff.appendMessage(sb); Assert.assertTrue(sb.toString(), diff.identical()); } } @Test public void testNotSame() throws SAXException, IOException { String xmlOutput; try (InputStream outFileInputStream = new BOMInputStream(new FileInputStream("C:\\myxml.xml"))) { xmlOutput = getData(outFileInputStream); } try (Reader r = new InputStreamReader(new BOMInputStream(new FileInputStream("C:\\expected_myxml_new.xml")), Charset.forName("UTF-8"))) { XMLUnit.setIgnoreComments(true); XMLUnit.setIgnoreWhitespace(true); XMLUnit.setNormalize(true); XMLUnit.setIgnoreAttributeOrder(true); Diff diff = XMLUnit.compareXML(r, xmlOutput); StringBuffer sb = new StringBuffer(); diff.appendMessage(sb); Assert.assertFalse(sb.toString(), diff.identical()); } } private String getData(InputStream in) throws IOException { StringWriter writer = new StringWriter(); IOUtils.copy(in, writer, "UTF-8"); return writer.toString(); } } |
Test Files
[wp_ad_camp_3]
The test files are as follow.
myxml.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <?xml version="1.0" encoding="UTF-8" standalone="no" ?> <breakfast-menu> <food> <name>Belgian Waffles</name> <price>$5.95</price> <description> two of our famous Belgian Waffles with plenty of real maple syrup </description> <calories>650</calories> </food> <food> <name>Strawberry Belgian Waffles</name> <price>$7.95</price> <description> light Belgian waffles covered with strawberrys and whipped cream </description> <calories>900</calories> </food> <food> <name>Berry-Berry Belgian Waffles</name> <price>$8.95</price> <description> light Belgian waffles covered with an assortment of fresh berries and whipped cream </description> <calories>900</calories> </food> <food> <name>French Toast</name> <price>$4.50</price> <description> thick slices made from our homemade sourdough bread </description> <calories>600</calories> </food> <food> <name>Homestyle Breakfast</name> <price>$6.95</price> <description> two eggs, bacon or sausage, toast, and our ever-popular hash browns </description> <calories>950</calories> </food> </breakfast-menu> |
expected_myxml.xml
[wp_ad_camp_4]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <?xml version="1.0" encoding="UTF-8" standalone="no" ?> <breakfast-menu> <food> <name>Belgian Waffles</name> <price>$5.95</price> <description> two of our famous Belgian Waffles with plenty of real maple syrup </description> <calories>650</calories> </food> <food> <name>Strawberry Belgian Waffles</name> <price>$7.95</price> <description> light Belgian waffles covered with strawberrys and whipped cream </description> <calories>900</calories> </food> <food> <name>Berry-Berry Belgian Waffles</name> <price>$8.95</price> <description> light Belgian waffles covered with an assortment of fresh berries and whipped cream </description> <calories>900</calories> </food> <food> <name>French Toast</name> <price>$4.50</price> <description> thick slices made from our homemade sourdough bread </description> <calories>600</calories> </food> <food> <name>Homestyle Breakfast</name> <price>$6.95</price> <description> two eggs, bacon or sausage, toast, and our ever-popular hash browns </description> <calories>950</calories> </food> </breakfast-menu> |
expected_myxml_new.xml
This is the file that does not match myxml.xml
[wp_ad_camp_5]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <?xml version="1.0" encoding="UTF-8" standalone="no" ?> <breakfast-menu> <food> <name>Belgian Waffles</name> <price>$5.95</price> <description> two of our famous Belgian Waffles with plenty of real maple syrup </description> <calories>650</calories> </food> <food> <name>Strawberry Belgian Waffles Canada</name> <price>$7.95</price> <description> light Belgian waffles covered with strawberrys and whipped cream </description> <calories>900</calories> </food> <food> <name>Berry-Berry Belgian Waffles</name> <price>$8.95</price> <description> light Belgian waffles covered with an assortment of fresh berries and whipped cream </description> <calories>900</calories> </food> <food> <name>French Toast</name> <price>$4.50</price> <description> thick slices made from our homemade sourdough bread </description> <calories>600</calories> </food> <food> <name>Homestyle Breakfast</name> <price>$6.95</price> <description> two eggs, bacon or sausage, toast, and our ever-popular hash browns </description> <calories>950</calories> </food> </breakfast-menu> |