private void compareXML(String expected, String actual) throws Exception { setNormalizeWhitespace(true); setIgnoreWhitespace(true); setIgnoreComments(true); setIgnoreAttributeOrder(false); Diff diff = XMLUnit.compareXML(expected, actual); if (!(diff.similar() && diff.identical())) { System.out.println(actual); DetailedDiff detDiff = new DetailedDiff(diff); @SuppressWarnings("rawtypes") List differences = detDiff.getAllDifferences(); StringBuilder diffLines = new StringBuilder(); for (Object object : differences) { Difference difference = (Difference) object; diffLines.append(difference.toString() + '\n'); } throw new IllegalArgumentException("Actual XML differs from expected: \n" + diffLines.toString()); } } }
/** * Obtain all the differences found by this instance * @return a list of {@link Difference differences} */ public List getAllDifferences() { compare(); return allDifferences; } }
DetailedDiff diff = new DetailedDiff(XMLUnit.compareXML(oldXml, newXml)); diff.overrideDifferenceListener(new IgnoreMyAttributeElements()); List<?> allDifferences = diff.getAllDifferences(); //do something with differences
import org.custommonkey.xmlunit.DetailedDiff; import org.custommonkey.xmlunit.XMLUnit; import org.junit.Assert; public class TestXml { public static void main(String[] args) throws Exception { String result = "<abc attr=\"value1\" title=\"something\"> </abc>"; // will be ok assertXMLEquals("<abc attr=\"value1\" title=\"something\"></abc>", result); } public static void assertXMLEquals(String expectedXML, String actualXML) throws Exception { XMLUnit.setIgnoreWhitespace(true); XMLUnit.setIgnoreAttributeOrder(true); DetailedDiff diff = new DetailedDiff(XMLUnit.compareXML(expectedXML, actualXML)); List<?> allDifferences = diff.getAllDifferences(); Assert.assertEquals("Differences found: "+ diff.toString(), 0, allDifferences.size()); } }
@Test public void testParseAndFormat() throws Exception { File inputFile = new File(getClass().getResource("/test-formatted-input.xml").getFile()); File outputFile = File.createTempFile("export-test-", ".xml"); OutputStream outputStream = new FileOutputStream(outputFile); XMLReader reader = saxParserFactory.newSAXParser().getXMLReader(); DataTransporter.readFormatted(reader, inputFile, outputStream); IOUtils.closeQuietly(outputStream); Reader expectedReader = new InputStreamReader(getClass().getResourceAsStream("/test-formatted-expected.xml")); Reader actualReader = new FileReader(outputFile); DetailedDiff xmlDiff = new DetailedDiff(new Diff(expectedReader, actualReader)); IOUtils.closeQuietly(expectedReader); IOUtils.closeQuietly(actualReader); outputFile.delete(); final StringBuilder diffLog = new StringBuilder(); for (Iterator iter = xmlDiff.getAllDifferences().iterator(); iter.hasNext(); ) { Difference difference = (Difference) iter.next(); diffLog.append("expected> ").append(difference.getControlNodeDetail().getValue()).append("\n"); diffLog.append("actual > ").append(difference.getTestNodeDetail().getValue()).append("\n"); } assertTrue("Document is not formatted as expected:\n" + diffLog.toString(), xmlDiff.identical()); }
public boolean validate(Document value) { if (value == null) return false; try { Document expectedValue = getValue(); logger.debug("Expected XML Value: {},\nActual XML Value: {}", xmlUtilities.getDocumentAsString(expectedValue).trim() , xmlUtilities.getDocumentAsString(value).trim()); DetailedDiff difference = new DetailedDiff(new Diff(expectedValue, value)); if (!difference.similar()) logger.warn("Differences exist between two documents: {}", difference.getAllDifferences()); else logger.debug("No differences exist for input"); return difference.similar(); } catch (Exception e) { throw new RuntimeException(e); } }
public void testRoundTrip() throws Exception { StringWriter sw = new StringWriter(); InputStream original = getClass().getClassLoader().getResourceAsStream("testmodel_data.xml"); XMLUnit.setIgnoreWhitespace(true); Collection<FastPathObject> unmarshalled = (Collection<FastPathObject>) binding.unmarshal(original); setIds(unmarshalled); binding.marshal(unmarshalled, sw); // System.out.println(sw.toString()); String expected = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("testmodel_data.xml")); Diff diff = new Diff(expected, sw.toString()); DetailedDiff detail = new DetailedDiff(diff); detail.overrideElementQualifier(new ElementNameAndAttributeQualifier()); assertTrue(detail.getAllDifferences().toString() + ": Original: " + expected + ", Generated: " + sw.toString(), detail.similar()); }
try { Diff d = new Diff(Helper.getDocument(controlDom), Helper.getDocument(testDom)); DetailedDiff dd = new DetailedDiff(d); dd.overrideDifferenceListener(new DifferenceListener() { return dd.getAllDifferences(); } catch (Exception e) { LOGGER.error("Error with getDifferences: " + e.getMessage(), e);
private boolean isdifferent(Document testDoc, Document controlDoc) { boolean isdifferent = false; XMLUnit.setIgnoreWhitespace(true); XMLUnit.setIgnoreComments(true); XMLUnit.setNormalize(true); Diff myDiff = new Diff(controlDoc, testDoc); DetailedDiff myComparisonController = new DetailedDiff(myDiff); DifferenceEngine engine = new DifferenceEngine(myComparisonController); XmlDifferenceListener listener = new XmlDifferenceListener(); ElementNameAndAttributeQualifier myElementQualifier = new ElementNameAndAttributeQualifier(); try { //debug engine.compare(controlDoc.getDocumentElement(), testDoc.getDocumentElement(), listener, myElementQualifier); } catch (NullPointerException ne) { LOG.error(ne); } isdifferent =listener.called(); return isdifferent; } }
@Test public void testPack() throws IOException, ISOException, SAXException { isoMsg.setMTI("0800"); isoMsg.set(7, "7654321"); isoMsg.set(11, "12345678"); isoMsg.set(12, "20110224112759"); isoMsg.set(24, ""); byte[] data = isoMsg.pack(); // System.out.println(new String(data)); String expected = "<isomsg><!-- org.jpos.iso.packager.XMLPackager --><field id=\"0\" value=\"0800\"/>" + "<field id=\"7\" value=\"7654321\"/><field id=\"11\" value=\"12345678\"/>" + "<field id=\"12\" value=\"20110224112759\"/><field id=\"24\" value=\"\"/></isomsg>"; XMLUnit.setIgnoreWhitespace(true); // XMLAssert.assertXMLEqual(expected, new String(data)); DetailedDiff myDiff = new DetailedDiff(XMLUnit.compareXML(expected, new String(data))); List allDifferences = myDiff.getAllDifferences(); assertEquals(myDiff.toString(), 0, allDifferences.size()); }
List differences = detDiff.getAllDifferences(); for (Object object : differences) { Difference difference = (Difference)object;
String expectedXML = "some xml"; String actualXML = "some xml"; DetailedDiff diff1 = new DetailedDiff(XMLUnit.compareXML(expectedXML, actualXML)); diff1.overrideElementQualifier(new RecursiveElementNameAndTextQualifier()); System.out.println("Differences found: " + diff1.getAllDifferences().toString());
Reader actualReader = new FileReader(outputFile); DetailedDiff xmlDiff = new DetailedDiff(new Diff(expectedReader, actualReader)); for (Iterator iter = xmlDiff.getAllDifferences().iterator(); iter.hasNext(); ) { Difference d = (Difference) iter.next(); log.warn(d.getControlNodeDetail().getXpathLocation() assertTrue("Document " + outputFile.getAbsolutePath() + " is not formatted as expected", xmlDiff.identical());
public static boolean isEquivalent(InputStream is1, InputStream is2) { try { XMLUnit.setIgnoreAttributeOrder(true); XMLUnit.setIgnoreWhitespace(true); Diff diff = XMLUnit.compareXML(new InputStreamReader(is1), new InputStreamReader(is2)); diff.overrideDifferenceListener(new IgnoreIrrelevantNodesDifferenceListener() { @Override protected boolean isIrrelevantChildNode(Node node) { return false; } @Override protected boolean isIrrelevantAttribute(Attr att) { return (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(att.getNamespaceURI())) || (XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI.equals(att .getNamespaceURI())); } }); if (!diff.identical()) { System.err.println(new DetailedDiff(diff)); } return diff.identical(); } catch (SAXException se) { throw new UncheckedException(se); } catch (IOException ioe) { throw new UncheckedException(ioe); } }
List differences = detDiff.getAllDifferences(); for (Object object : differences) { Difference difference = (Difference)object;
public void testAllDifferences() throws Exception { String myControlXML = "<news><item id=\"1\">War</item>" + "<item id=\"2\">Plague</item><item id=\"3\">Famine</item></news>"; String myTestXML = "<news><item id=\"1\">Peace</item>" + "<item id=\"2\">Health</item><item id=\"3\">Plenty</item></news>"; DetailedDiff myDiff = new DetailedDiff(compareXML(myControlXML, myTestXML)); List allDifferences = myDiff.getAllDifferences(); assertEquals(myDiff.toString(), 0, allDifferences.size()); }
String expectedXML = "some xml"; String actualXML = "some xml"; DetailedDiff diff1 = new DetailedDiff(XMLUnit.compareXML(expectedXML, actualXML)); diff1.overrideElementQualifier(new RecursiveElementNameAndTextQualifier()); System.out.println("Differences found: " + diff1.getAllDifferences().toString());
/** * Receives to {@link String} representation of two XML files and verify that they are semantically equivalent * * @param expected the reference content * @param actual the actual content * @throws Exception if comparison fails */ public static void compareXML(String expected, String actual) throws Exception { XMLUnit.setNormalizeWhitespace(true); XMLUnit.setIgnoreWhitespace(true); XMLUnit.setIgnoreComments(true); XMLUnit.setIgnoreAttributeOrder(false); Diff diff = XMLUnit.compareXML(expected, actual); if (!(diff.similar() && diff.identical())) { System.out.println(actual); DetailedDiff detDiff = new DetailedDiff(diff); @SuppressWarnings("rawtypes") List differences = detDiff.getAllDifferences(); StringBuilder diffLines = new StringBuilder(); for (Object object : differences) { Difference difference = (Difference) object; diffLines.append(difference.toString() + '\n'); } throw new IllegalArgumentException("Actual XML differs from expected: \n" + diffLines.toString()); } }
public static boolean isEquivalent(InputStream is1, InputStream is2) { try { XMLUnit.setIgnoreAttributeOrder(true); XMLUnit.setIgnoreWhitespace(true); Diff diff = XMLUnit.compareXML(new InputStreamReader(is1), new InputStreamReader(is2)); diff.overrideDifferenceListener(new IgnoreIrrelevantNodesDifferenceListener() { @Override protected boolean isIrrelevantChildNode(Node node) { return false; } @Override protected boolean isIrrelevantAttribute(Attr att) { return (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(att.getNamespaceURI())) || (XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI.equals(att .getNamespaceURI())); } }); if (!diff.identical()) { System.err.println(new DetailedDiff(diff)); } return diff.identical(); } catch (SAXException se) { throw new UncheckedException(se); } catch (IOException ioe) { throw new UncheckedException(ioe); } }
List differences = detDiff.getAllDifferences(); for (Object object : differences) { Difference difference = (Difference)object;