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()); } } }
/** * 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()); } }
private String buildDifferenceMessage(Diff diff) { DetailedDiff detailedDiff = new DetailedDiff(diff); @SuppressWarnings("unchecked") List<Difference> differences = detailedDiff.getAllDifferences(); StringBuilder message = new StringBuilder( "Some differences has been found between database data and expected data:"); message.append(EOL); for (Difference difference : differences) { message.append("************************"); message.append(difference); message.append("************************"); } String differenceMessage = message.toString(); return differenceMessage; }
private String buildDifferenceMessage(Diff diff) { DetailedDiff detailedDiff = new DetailedDiff(diff); @SuppressWarnings("unchecked") List<Difference> differences = detailedDiff.getAllDifferences(); StringBuilder message = new StringBuilder( "Some differences has been found between database data and expected data:"); message.append(EOL); for (Difference difference : differences) { message.append("************************"); message.append(difference); message.append("************************"); } String differenceMessage = message.toString(); return differenceMessage; }
/** * Compare the controlDOM and testDOM and save and return the differences in a list. * * @return list with differences */ @SuppressWarnings("unchecked") public List<Difference> compare() { Diff diff = new Diff(this.controlDOM, this.testDOM); DetailedDiff detDiff = new DetailedDiff(diff); return detDiff.getAllDifferences(); }
protected void assertThatXmlHaveNoDifferences(final String xmlPrettyFormatExpected, final String xmlPrettyFormatExported) throws SAXException, IOException { XMLUnit.setIgnoreWhitespace(true); XMLUnit.setIgnoreAttributeOrder(true); final DetailedDiff diff = new DetailedDiff(XMLUnit.compareXML(xmlPrettyFormatExported, xmlPrettyFormatExpected)); final List<?> allDifferences = diff.getAllDifferences(); assertThat(allDifferences).as("should have no differences between:\n%s\n and:\n%s\n", xmlPrettyFormatExpected, xmlPrettyFormatExported).isEmpty(); }
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); } }
@SuppressWarnings("unchecked") private static DetailedDiff getDiff(StringWriter objectXML, StringBuilder exampleXML) throws SAXException, IOException { DetailedDiff myDiff = new DetailedDiff(XMLUnit.compareXML(exampleXML.toString(), objectXML.toString())); List<Difference> allDifferences = myDiff.getAllDifferences(); if (allDifferences.size() > 0) { for (Difference d : allDifferences) { System.err.println(d); } } return myDiff; }
DetailedDiff myDiff; try { myDiff = new DetailedDiff(XMLUnit.compareXML(xmlA, xmlB)); } catch (final Exception e) { throw new RuntimeException(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; } }
try { Diff d = new Diff(Helper.getDocument(controlDom), Helper.getDocument(testDom)); DetailedDiff dd = new DetailedDiff(d); dd.overrideDifferenceListener(new DifferenceListener() {
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); } }
@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 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); } }
System.err.println(new DetailedDiff(diff));
System.err.println(new DetailedDiff(diff));
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()); }
private void compareFileContent(File expectedFile, File givenFile) throws Exception { final String givenFileAbsolutePath = givenFile.getAbsolutePath(); final String expectedFileExtension = getExtension(expectedFile.getName()); final String expectedFileAbsolutePath = expectedFile.getAbsolutePath(); assertThat(new ExtensionFileComparator().compare(expectedFile, givenFile)) .as(expectedFileAbsolutePath + " and " + givenFileAbsolutePath + " should have same extension").isEqualTo(ARE_EQUALS); switch (expectedFileExtension) { case PROPERTIES: assertThat(getProperties(expectedFile)) .as(expectedFileAbsolutePath + " and " + givenFileAbsolutePath + " should contain same properties") .isEqualTo(getProperties(givenFile)); break; case XML: final List allDifferences = new DetailedDiff( XMLUnit.compareXML(new FileReader(givenFile), new FileReader(expectedFile))) .getAllDifferences(); assertThat(allDifferences).as("should xml file be equals").isEmpty(); break; default: fail("unexpected file:" + expectedFile.getAbsolutePath()); break; } }
Reader actualReader = new FileReader(outputFile); DetailedDiff xmlDiff = new DetailedDiff(new Diff(expectedReader, actualReader));
@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()); }