use of org.custommonkey.xmlunit.DetailedDiff in project opennms by OpenNMS.
the class PersistenceSerializationTest method generateXML.
@Test
public void generateXML() throws Exception {
// Marshal the test object to an XML string
StringWriter objectXML = new StringWriter();
m.marshal(fsw, objectXML);
// Read the example XML from src/test/resources
final StringBuilder exampleXML = new StringBuilder();
File foreignSources = new File(ClassLoader.getSystemResource("foreign-sources.xml").getFile());
assertTrue("foreign-sources.xml is readable", foreignSources.canRead());
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(foreignSources), StandardCharsets.UTF_8));
String line;
while (true) {
line = reader.readLine();
if (line == null) {
reader.close();
break;
}
exampleXML.append(line).append("\n");
}
System.err.println("========================================================================");
System.err.println("Object XML:");
System.err.println("========================================================================");
System.err.print(objectXML.toString());
System.err.println("========================================================================");
System.err.println("Example XML:");
System.err.println("========================================================================");
System.err.print(exampleXML.toString());
DetailedDiff myDiff = getDiff(objectXML, exampleXML);
assertEquals("number of XMLUnit differences between the example XML and the mock object XML is 0", 0, myDiff.getAllDifferences().size());
}
use of org.custommonkey.xmlunit.DetailedDiff in project sirix by sirixdb.
the class XMLUpdateShredderTest method test.
// /** Not working anymore due to text merging on deletes. */
// @Ignore
// @Test
// public void testAllEighth() throws Exception {
// test(XMLALLEIGHTH);
// }
//
// /** Not working anymore due to text merging on deletes. */
// @Ignore
// @Test
// public void testAllNineth() throws Exception {
// test(XMLALLNINETH);
// }
// @Test
// public void testLinguistics() throws Exception {
// test(XMLLINGUISTICS);
// }
private void test(final Path folder) throws Exception {
final Database database = TestHelper.getDatabase(PATHS.PATH1.getFile());
database.createResource(new ResourceConfiguration.Builder(TestHelper.RESOURCE, PATHS.PATH1.getConfig()).build());
final ResourceManager manager = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
int i = 2;
final List<Path> files = Files.list(folder).filter(file -> file.getFileName().endsWith(".xml")).collect(Collectors.toList());
// Sort files array according to file names.
files.sort((first, second) -> {
final String firstName = first.getFileName().toString().substring(0, second.getFileName().toString().indexOf('.'));
final String secondName = second.getFileName().toString().substring(0, second.getFileName().toString().indexOf('.'));
if (Integer.parseInt(firstName) < Integer.parseInt(secondName)) {
return -1;
} else if (Integer.parseInt(firstName) > Integer.parseInt(secondName)) {
return +1;
} else {
return 0;
}
});
boolean first = true;
// Shredder files.
for (final Path file : files) {
if (file.endsWith(".xml")) {
final XdmNodeWriteTrx wtx = manager.beginNodeWriteTrx();
if (first) {
final XMLShredder shredder = new XMLShredder.Builder(wtx, XMLShredder.createFileReader(file), Insert.ASFIRSTCHILD).commitAfterwards().build();
shredder.call();
first = false;
} else {
@SuppressWarnings("deprecation") final XMLUpdateShredder shredder = new XMLUpdateShredder(wtx, XMLShredder.createFileReader(file), Insert.ASFIRSTCHILD, file, ShredderCommit.COMMIT);
shredder.call();
}
assertEquals(i, wtx.getRevisionNumber());
i++;
final OutputStream out = new ByteArrayOutputStream();
final XMLSerializer serializer = new XMLSerializerBuilder(manager, out).prettyPrint().build();
serializer.call();
final StringBuilder sBuilder = TestHelper.readFile(file, false);
final Diff diff = new Diff(sBuilder.toString(), out.toString());
final DetailedDiff detDiff = new DetailedDiff(diff);
@SuppressWarnings("unchecked") final List<Difference> differences = detDiff.getAllDifferences();
for (final Difference difference : differences) {
System.out.println("***********************");
System.out.println(difference);
System.out.println("***********************");
}
assertTrue("pieces of XML are similar " + diff, diff.similar());
assertTrue("but are they identical? " + diff, diff.identical());
wtx.close();
}
}
}
use of org.custommonkey.xmlunit.DetailedDiff in project archiva by apache.
the class MetadataTransferTest method assertMetadataEquals.
private void assertMetadataEquals(String expectedMetadataXml, Path actualFile) throws Exception {
assertNotNull("Actual File should not be null.", actualFile);
assertTrue("Actual file exists.", Files.exists(actualFile));
StringWriter actualContents = new StringWriter();
ArchivaRepositoryMetadata metadata = MavenMetadataReader.read(actualFile);
RepositoryMetadataWriter.write(metadata, actualContents);
DetailedDiff detailedDiff = new DetailedDiff(new Diff(expectedMetadataXml, actualContents.toString()));
if (!detailedDiff.similar()) {
assertEquals(expectedMetadataXml, actualContents);
}
// assertEquals( "Check file contents.", expectedMetadataXml, actualContents );
}
use of org.custommonkey.xmlunit.DetailedDiff in project archiva by apache.
the class MetadataToolsTest method assertMetadata.
private void assertMetadata(String expectedMetadata, ManagedRepositoryContent repository, ProjectReference reference) throws LayoutException, IOException, SAXException, ParserConfigurationException {
Path metadataFile = Paths.get(repository.getRepoRoot(), tools.toPath(reference));
String actualMetadata = org.apache.archiva.common.utils.FileUtils.readFileToString(metadataFile, Charset.defaultCharset());
DetailedDiff detailedDiff = new DetailedDiff(new Diff(expectedMetadata, actualMetadata));
if (!detailedDiff.similar()) {
// If it isn't similar, dump the difference.
assertEquals(expectedMetadata, actualMetadata);
}
}
use of org.custommonkey.xmlunit.DetailedDiff in project cerberus-source by cerberustesting.
the class XmlUnitService method getDifferencesFromXml.
@Override
public String getDifferencesFromXml(String left, String right) {
try {
// Gets the detailed diff between left and right argument
Document leftDocument = inputTranslator.translate(left);
Document rightDocument = inputTranslator.translate(right);
DetailedDiff diffs = new DetailedDiff(XMLUnit.compareXML(leftDocument, rightDocument));
// Creates the result structure which will contain difference list
Differences resultDiff = new Differences();
// Add each difference to our result structure
for (Object diff : diffs.getAllDifferences()) {
if (!(diff instanceof Difference)) {
LOG.warn("Unable to handle no XMLUnit Difference " + diff);
continue;
}
Difference wellTypedDiff = (Difference) diff;
String xPathLocation = wellTypedDiff.getControlNodeDetail().getXpathLocation();
// Then we retrieve XPath from the right structure.
if (xPathLocation == null) {
xPathLocation = wellTypedDiff.getTestNodeDetail().getXpathLocation();
}
// This case should never happen
if (xPathLocation == null) {
LOG.warn("Null left and right differences found");
xPathLocation = NULL_XPATH;
}
resultDiff.addDifference(new org.cerberus.service.xmlunit.Difference(xPathLocation));
}
// Finally returns the String representation of our result structure
return resultDiff.mkString();
} catch (InputTranslatorException e) {
LOG.warn("Unable to get differences from XML", e);
}
return null;
}
Aggregations