@Test public void compareDocuments() { DOMDifferenceEngine d = new DOMDifferenceEngine(); DiffExpecter ex = new DiffExpecter(ComparisonType.HAS_DOCTYPE_DECLARATION); d.addDifferenceListener(ex); d.setDifferenceEvaluator(new DifferenceEvaluator() { public ComparisonResult evaluate(Comparison comparison, ComparisonResult outcome) { if (comparison.getType() == ComparisonType.CHILD_NODELIST_LENGTH) { assertEquals(ComparisonResult.DIFFERENT, outcome); return ComparisonResult.EQUAL; } if (comparison.getType() == ComparisonType.HAS_DOCTYPE_DECLARATION) { assertEquals(ComparisonResult.DIFFERENT, outcome); return ComparisonResult.DIFFERENT; } assertEquals("Expected EQUAL for " + comparison.getType() + " comparison.", ComparisonResult.EQUAL, outcome); return ComparisonResult.EQUAL; } }); d.setComparisonController(ComparisonControllers.StopWhenDifferent); d.setNodeFilter(NodeFilters.AcceptAll); Document d1 = Convert.toDocument(Input.fromString("<Book/>").build()); Document d2 = Convert.toDocument(Input.fromString("<!DOCTYPE Book PUBLIC " + "\"XMLUNIT/TEST/PUB\" " + "\"" + TestResources.BOOK_DTD + "\">" + "<Book/>") .build()); assertEquals(wrapAndStop(ComparisonResult.DIFFERENT), d.compareNodes(d1, new XPathContext(), d2, new XPathContext())); assertEquals(1, ex.invoked); d = new DOMDifferenceEngine(); ex = new DiffExpecter(ComparisonType.XML_VERSION); d.addDifferenceListener(ex); d.setComparisonController(ComparisonControllers.StopWhenDifferent); d1 = Convert.toDocument(Input.fromString("<?xml version=\"1.0\"" + " encoding=\"UTF-8\"?>" + "<Book/>").build()); d2 = Convert.toDocument(Input.fromString("<?xml version=\"1.1\"" + " encoding=\"UTF-8\"?>" + "<Book/>").build()); assertEquals(wrapAndStop(ComparisonResult.DIFFERENT), d.compareNodes(d1, new XPathContext(), d2, new XPathContext())); assertEquals(1, ex.invoked); d = new DOMDifferenceEngine(); ex = new DiffExpecter(ComparisonType.XML_STANDALONE); d.addDifferenceListener(ex); d.setComparisonController(ComparisonControllers.StopWhenDifferent); d1 = Convert.toDocument(Input.fromString("<?xml version=\"1.0\"" + " standalone=\"yes\"?>" + "<Book/>").build()); d2 = Convert.toDocument(Input.fromString("<?xml version=\"1.0\"" + " standalone=\"no\"?>" + "<Book/>").build()); assertEquals(wrapAndStop(ComparisonResult.DIFFERENT), d.compareNodes(d1, new XPathContext(), d2, new XPathContext())); assertEquals(1, ex.invoked); d = new DOMDifferenceEngine(); ex = new DiffExpecter(ComparisonType.XML_ENCODING); d.addDifferenceListener(ex); d.setDifferenceEvaluator(new DifferenceEvaluator() { public ComparisonResult evaluate(Comparison comparison, ComparisonResult outcome) { if (comparison.getType() == ComparisonType.XML_ENCODING) { assertEquals(ComparisonResult.DIFFERENT, outcome); return ComparisonResult.DIFFERENT; } assertEquals(ComparisonResult.EQUAL, outcome); return ComparisonResult.EQUAL; } }); d.setComparisonController(ComparisonControllers.StopWhenDifferent); d1 = Convert.toDocument(Input.fromString("<?xml version=\"1.0\"" + " encoding=\"UTF-8\"?>" + "<Book/>").build()); d2 = Convert.toDocument(Input.fromString("<?xml version=\"1.0\"" + " encoding=\"UTF-16\"?>" + "<Book/>").build()); assertEquals(wrapAndStop(ComparisonResult.DIFFERENT), d.compareNodes(d1, new XPathContext(), d2, new XPathContext())); assertEquals(1, ex.invoked); }
private ComparisonState compareDocuments(final Document control, final XPathContext controlContext, final Document test, final XPathContext testContext) { final DocumentType controlDt = filterNode(control.getDoctype()); final DocumentType testDt = filterNode(test.getDoctype()); return compare(new Comparison(ComparisonType.HAS_DOCTYPE_DECLARATION, control, getXPath(controlContext), Boolean.valueOf(controlDt != null), getParentXPath(controlContext), test, getXPath(testContext), Boolean.valueOf(testDt != null), getParentXPath(testContext))) .andIfTrueThen(controlDt != null && testDt != null, new DeferredComparison() { @Override public ComparisonState apply() { return compareNodes(controlDt, controlContext, testDt, testContext); } }) .andThen(compareDeclarations(control, controlContext, test, testContext)); }
DOMDifferenceEngine extends AbstractDifferenceEngine { private ComparisonState compareDocuments(final Document control, final XPathContext controlContext, final Document test, final XPathContext testContext) { final DocumentType controlDt = filterNode(control.getDoctype()); final DocumentType testDt = filterNode(test.getDoctype()); return compare(new Comparison(ComparisonType.HAS_DOCTYPE_DECLARATION, control, getXPath(controlContext), Boolean.valueOf(controlDt != null), getParentXPath(controlContext), test, getXPath(testContext), Boolean.valueOf(testDt != null), getParentXPath(testContext))) .andIfTrueThen(controlDt != null && testDt != null, new DeferredComparison() { @Override public ComparisonState apply() { return compareNodes(controlDt, controlContext, testDt, testContext); } }) .andThen(compareDeclarations(control, controlContext, test, testContext)); } }
DOMDifferenceEngine extends AbstractDifferenceEngine { private ComparisonState compareDocuments(final Document control, final XPathContext controlContext, final Document test, final XPathContext testContext) { final DocumentType controlDt = filterNode(control.getDoctype()); final DocumentType testDt = filterNode(test.getDoctype()); return compare(new Comparison(ComparisonType.HAS_DOCTYPE_DECLARATION, control, getXPath(controlContext), Boolean.valueOf(controlDt != null), getParentXPath(controlContext), test, getXPath(testContext), Boolean.valueOf(testDt != null), getParentXPath(testContext))) .andIfTrueThen(controlDt != null && testDt != null, new DeferredComparison() { @Override public ComparisonState apply() { return compareNodes(controlDt, controlContext, testDt, testContext); } }) .andThen(compareDeclarations(control, controlContext, test, testContext)); } DOMDifferenceEngine();  DOMDifferenceEngine(final DocumentBuilderFactory f); }
DOMDifferenceEngine extends AbstractDifferenceEngine { private ComparisonState compareDocuments(final Document control, final XPathContext controlContext, final Document test, final XPathContext testContext) { final DocumentType controlDt = filterNode(control.getDoctype()); final DocumentType testDt = filterNode(test.getDoctype()); return compare(new Comparison(ComparisonType.HAS_DOCTYPE_DECLARATION, control, getXPath(controlContext), Boolean.valueOf(controlDt != null), getParentXPath(controlContext), test, getXPath(testContext), Boolean.valueOf(testDt != null), getParentXPath(testContext))) .andIfTrueThen(controlDt != null && testDt != null, new DeferredComparison() { @Override public ComparisonState apply() { return compareNodes(controlDt, controlContext, testDt, testContext); } }) .andThen(compareDeclarations(control, controlContext, test, testContext)); } DOMDifferenceEngine();  DOMDifferenceEngine(final DocumentBuilderFactory f); void setDocumentBuilderFactory(DocumentBuilderFactory f); @Override void compare(Source control, Source test); }
DOMDifferenceEngine extends AbstractDifferenceEngine { private ComparisonState compareDocuments(final Document control, final XPathContext controlContext, final Document test, final XPathContext testContext) { final DocumentType controlDt = filterNode(control.getDoctype()); final DocumentType testDt = filterNode(test.getDoctype()); return compare(new Comparison(ComparisonType.HAS_DOCTYPE_DECLARATION, control, getXPath(controlContext), Boolean.valueOf(controlDt != null), getParentXPath(controlContext), test, getXPath(testContext), Boolean.valueOf(testDt != null), getParentXPath(testContext))) .andIfTrueThen(controlDt != null && testDt != null, new DeferredComparison() { @Override public ComparisonState apply() { return compareNodes(controlDt, controlContext, testDt, testContext); } }) .andThen(compareDeclarations(control, controlContext, test, testContext)); } DOMDifferenceEngine();  DOMDifferenceEngine(final DocumentBuilderFactory f); void setDocumentBuilderFactory(DocumentBuilderFactory f); @Override void compare(Source control, Source test);  }