use of org.xmlunit.diff.Diff in project irontest by zheng-wang.
the class XMLUtils method compareXML.
/**
* @param control
* @param test
* @param namespaceAware
* @return differences found, in a format for print
*/
public static String compareXML(String control, String test, boolean namespaceAware) {
Object controlObject = control;
Object testObject = test;
// DiffBuilder.withDocumentBuilderFactory(factory);
if (!namespaceAware) {
try {
controlObject = xmlStringToDOM(control);
testObject = xmlStringToDOM(test);
} catch (Exception e) {
throw new RuntimeException(e.getCause().getMessage(), e);
}
}
StringBuilder differencesSB = new StringBuilder();
Diff diff;
try {
diff = DiffBuilder.compare(controlObject).withTest(testObject).normalizeWhitespace().withDifferenceEvaluator(DifferenceEvaluators.chain(DifferenceEvaluators.Default, new PlaceholderDifferenceEvaluator("#\\{", null))).build();
} catch (XMLUnitException e) {
throw new RuntimeException(e.getCause().getMessage(), e);
}
if (diff.hasDifferences()) {
Iterator it = diff.getDifferences().iterator();
while (it.hasNext()) {
Difference difference = (Difference) it.next();
if (difference.getResult() == ComparisonResult.DIFFERENT) {
// ignore SIMILAR comparison results
if (differencesSB.length() > 0) {
differencesSB.append("\n");
}
differencesSB.append(difference.getComparison().toString());
}
}
}
return differencesSB.toString();
}
use of org.xmlunit.diff.Diff in project pom-manipulation-ext by release-engineering.
the class XMLIOTest method modifyFile.
@Test
public void modifyFile() throws ManipulationException, IOException, XPathExpressionException {
String updatePath = "/assembly/includeBaseDirectory";
String newBaseDirectory = "/home/MYNEWBASEDIR";
Document doc = xmlIO.parseXML(xmlFile);
XPath xPath = XPathFactory.newInstance().newXPath();
Node node = (Node) xPath.evaluate(updatePath, doc, XPathConstants.NODE);
node.setTextContent(newBaseDirectory);
File target = tf.newFile();
xmlIO.writeXML(target, doc);
Diff diff = DiffBuilder.compare(fromFile(xmlFile)).withTest(Input.fromFile(target)).build();
logger.debug("Difference {} ", diff.toString());
String targetXML = FileUtils.readFileToString(target);
// XMLUnit only seems to support XPath 1.0 so modify the expression to find the value.
String xpathForHamcrest = "/*/*[local-name() = '" + updatePath.substring(updatePath.lastIndexOf('/') + 1) + "']";
assertThat(targetXML, hasXPath(xpathForHamcrest));
assertThat(targetXML, EvaluateXPathMatcher.hasXPath(xpathForHamcrest, equalTo(newBaseDirectory)));
assertTrue(diff.toString(), diff.hasDifferences());
}
use of org.xmlunit.diff.Diff in project pom-manipulation-ext by release-engineering.
the class XMLIOTest method readFile.
@Test
public void readFile() throws IOException, ManipulationException, TransformerException, InstantiationException, IllegalAccessException, ClassNotFoundException {
Document doc = xmlIO.parseXML(xmlFile);
String strResult = xmlIO.convert(doc);
Diff diff = DiffBuilder.compare(fromFile(xmlFile)).withTest(Input.fromString(strResult)).build();
assertFalse(diff.toString(), diff.hasDifferences());
}
use of org.xmlunit.diff.Diff in project pom-manipulation-ext by release-engineering.
the class XMLManipulatorTest method alterFile.
@Test
public void alterFile() throws Exception {
String replacementGA = "com.rebuild:servlet-api";
String tomcatPath = "//include[starts-with(.,'org.apache.tomcat')]";
File target = tf.newFile();
FileUtils.copyFile(xmlFile, target);
Project project = new Project(null, target, null);
xmlManipulator.internalApplyChanges(project, new XMLState.XMLOperation(target.getName(), tomcatPath, replacementGA));
Diff diff = DiffBuilder.compare(fromFile(xmlFile)).withTest(fromFile(target)).build();
assertTrue(diff.toString(), diff.hasDifferences());
String xpathForHamcrest = "/*/*/*/*/*[starts-with(.,'com.rebuild') and local-name() = 'include']";
Iterable<Node> i = new JAXPXPathEngine().selectNodes(xpathForHamcrest, fromFile(target).build());
int count = 0;
for (Node anI : i) {
count++;
assertTrue(anI.getTextContent().startsWith("com.rebuild:servlet-api"));
}
assertEquals(1, count);
}
use of org.xmlunit.diff.Diff in project atlasmap by atlasmap.
the class AtlasMapSMTTest method test.
@Test
public void test() throws Exception {
File f = new File("target/target.xml");
for (int i = 0; !f.exists() && i < 5; i++) {
Thread.sleep(1000);
f = new File("target/target.xml");
}
assertTrue(f.exists());
try (BufferedReader reader = new BufferedReader(new FileReader(f))) {
String line = reader.readLine();
for (int i = 0; line == null && i < 5; i++) {
Thread.sleep(1000);
line = reader.readLine();
}
assertNotNull(line);
Diff d = DiffBuilder.compare(Input.fromFile("src/test/resources/target.xml").build()).withTest(Input.fromString(line).build()).ignoreWhitespace().build();
assertFalse(d.hasDifferences(), d.toString() + ": " + line);
}
}
Aggregations