public boolean matches(final HttpRequest context, NottableString matched) { boolean result = false; if (diffBuilder != null) { try { Diff diff = diffBuilder.withTest(Input.fromString(normaliseXmlString(matched.getValue()))).build(); result = !diff.hasDifferences(); if (!result) { mockServerLogger.trace("Failed to match [{}] with schema [{}] because [{}]", matched, this.matcher, diff.toString()); } } catch (Exception e) { mockServerLogger.trace(context, "Failed to match [{}] with schema [{}] because [{}]", matched, this.matcher, e.getMessage()); } } return matcher.isNot() != (not != result); }
public boolean hasDifferences() { return this.diff.hasDifferences(); }
public String toString(ComparisonFormatter formatter) { if (!hasDifferences()) { return "[identical]"; } return getDifferences().iterator().next().getComparison().toString(formatter); }
@Override public void describeTo(Description description) { if (diffResult == null || !diffResult.hasDifferences()) { description.appendText(" is ") .appendText(checkFor == ComparisonResult.EQUAL ? "equal" : "similar") .appendText(" to the control document"); return; } final Comparison difference = firstComparison(); final String reason = createReasonPrefix(diffResult.getControlSource().getSystemId(), difference); final String testString = comparisonFormatter.getDetails(difference.getControlDetails(), difference.getType(), formatXml); description.appendText(String.format("%s:\n%s", reason, testString)); }
@Override public String toString() { return this.diff.toString(); }
private Comparison firstComparison() { return diffResult.getDifferences().iterator().next().getComparison(); } }
public boolean hasDifferences() { return this.diff.hasDifferences(); }
@Override public String toString() { return toString(formatter); }
public void run( final HtmlContentOutput output, final SemanticDiffFormatter semanticDiff ) { this.output = output; this.semanticDiff = semanticDiff; for( final Difference diff : diffs.getDifferences() ) { final Comparison comparison = diff.getComparison(); if( isAdded( comparison ) ) { printAddedNode( comparison ); } else if( isDeleted( comparison ) ) { printDeletedNode( comparison ); } else { printModifiedNode( comparison ); } } }
public static void similar(InputStream input, InputStream expected) throws IOException, SAXException { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); IOUtils.copy(input, bytes); ByteArrayOutputStream expectedBytes = new ByteArrayOutputStream(); IOUtils.copy(expected, expectedBytes); Diff diff = DiffBuilder .compare(expected) .withTest(input) .checkForSimilar() .build(); if (diff.hasDifferences()) { throw new ComparisonFailure(diff.toString(), expectedBytes.toString(), bytes.toString()); } }
import org.w3c.dom.Element; import org.xmlunit.builder.DiffBuilder; import org.xmlunit.util.Nodes; import org.xmlunit.diff.*; public class Test { public static void main(String[] args) { Diff d = DiffBuilder.compare("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<response>\n" + "<bih-metadata>\n" + "<result>Error</result>\n" + "<correlation-id>ID:925977d0-83cd-11e6-b94d-c135e6c73218</correlation-id>\n" + "<error-message>SAXParseException: The entity name must immediately follow the '&' in the entity reference.</error-message>\n" + "</bih-metadata>\n" + "</response>") .withTest("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<response>\n" + "<bih-metadata>\n" + "<result>Error</result>\n" + "<correlation-id>ID:134345d0-83cd-11e6-b94d-c135e6c73218</correlation-id>\n" + "<error-message>SAXParseException: The entity name must immediately follow the '&' in the entity reference.</error-message>\n" + "</bih-metadata>\n" + "</response>") .withNodeFilter(n -> !(n instanceof Element && "correlation-id".equals(Nodes.getQName(n).getLocalPart()))) .build(); System.err.println("Different? " + d.hasDifferences()); } }
@Override public String toString() { return this.diff.toString(); }
Joiner.on("\n").join(diff.getDifferences()) );
public static void similar(InputStream input, String expected) throws IOException, SAXException { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); Diff diff = DiffBuilder .compare(expected) .withTest(input) .ignoreComments() .checkForSimilar() .build(); if (diff.hasDifferences()) { throw new ComparisonFailure(diff.toString(), expected, bytes.toString()); } }
@Override public boolean matches(Object item) { if (checkFor == ComparisonResult.EQUAL) { diffBuilder.withComparisonController(ComparisonControllers.StopWhenSimilar); } else if (checkFor == ComparisonResult.SIMILAR) { diffBuilder.withComparisonController(ComparisonControllers.StopWhenDifferent); } diffResult = diffBuilder.withTest(item).build(); if (!diffResult.hasDifferences()) { return true; } if (throwComparisonFailure) { AssertionError assertionError = createComparisonFailure(); if (assertionError != null) throw assertionError; } return false; }
public static void similar(String input, String expected, Consumer<DiffBuilder>... build) throws IOException, SAXException { DiffBuilder builder = DiffBuilder .compare(expected) .withTest(input) .ignoreWhitespace() .checkForSimilar() ; for (Consumer<DiffBuilder> b : build) { b.accept(builder); } try { Diff diff = builder.build(); if (diff.hasDifferences()) { throw new ComparisonFailure(diff.toString(), expected, input); } else { assertThat(diff.hasDifferences()).isFalse(); } } catch (XMLUnitException xue) { throw new ComparisonFailure(xue.getMessage(), expected, input); } }
@Test public void test() throws Exception { final ForceUniqueFilter f = new ForceUniqueFilter(); f.setJob(job); f.setTempFileNameScheme(tempFileNameScheme); f.setCurrentFile(new File(srcDir, "test.ditamap").toURI()); f.setParent(SAXParserFactory.newInstance().newSAXParser().getXMLReader()); final DOMResult dst = new DOMResult(); TransformerFactory.newInstance().newTransformer().transform(new SAXSource(f, new InputSource(new File(srcDir, "test.ditamap").toURI().toString())), dst); final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); builderFactory.setIgnoringComments(true); final Document exp = builderFactory.newDocumentBuilder().parse(new InputSource(new File(expDir, "test.ditamap").toURI().toString())); final Diff d = DiffBuilder .compare(exp) .withTest(dst.getNode()) .ignoreWhitespace() .build(); assertFalse(d.hasDifferences()); assertEquals(new HashMap<FileInfo, FileInfo>(ImmutableMap.of( createFileInfo("test.dita", "test_3.dita"), createFileInfo("test.dita", "test.dita"), createFileInfo("test.dita", "test_2.dita"), createFileInfo("test.dita", "test.dita"), createFileInfo(null, "copy-to_2.dita"), createFileInfo(null, "copy-to.dita") )), f.copyToMap); }
private void assertXMLEqual(Document exp, Document act) { final Diff d = DiffBuilder .compare(exp) .withTest(act) .build(); if (d.hasDifferences()) { throw new AssertionError(d.toString()); } }
.withTest(Input.fromString(actualDataConfig)).ignoreWhitespace().checkForSimilar().build(); Assert.assertFalse(xmlDiff.hasDifferences());