Search in sources :

Example 1 with ChangeDelta

use of difflib.ChangeDelta in project pdfbox by apache.

the class TestTextStripper method compareResult.

private void compareResult(File expectedFile, File outFile, File inFile, boolean bSort, File diffFile) throws IOException {
    boolean localFail = false;
    try (LineNumberReader expectedReader = new LineNumberReader(new InputStreamReader(new FileInputStream(expectedFile), ENCODING));
        LineNumberReader actualReader = new LineNumberReader(new InputStreamReader(new FileInputStream(outFile), ENCODING))) {
        while (true) {
            String expectedLine = expectedReader.readLine();
            while (expectedLine != null && expectedLine.trim().length() == 0) {
                expectedLine = expectedReader.readLine();
            }
            String actualLine = actualReader.readLine();
            while (actualLine != null && actualLine.trim().length() == 0) {
                actualLine = actualReader.readLine();
            }
            if (!stringsEqual(expectedLine, actualLine)) {
                this.bFail = true;
                localFail = true;
                log.error("FAILURE: Line mismatch for file " + inFile.getName() + " (sort = " + bSort + ")" + " at expected line: " + expectedReader.getLineNumber() + " at actual line: " + actualReader.getLineNumber() + "\nexpected line was: \"" + expectedLine + "\"" + "\nactual line was:   \"" + actualLine + "\"" + "\n");
            // lets report all lines, even though this might produce some verbose logging
            // break;
            }
            if (expectedLine == null || actualLine == null) {
                break;
            }
        }
    }
    if (!localFail) {
        outFile.delete();
    } else {
        // https://code.google.com/p/java-diff-utils/wiki/SampleUsage
        List<String> original = fileToLines(expectedFile);
        List<String> revised = fileToLines(outFile);
        // Compute diff. Get the Patch object. Patch is the container for computed deltas.
        Patch<String> patch = DiffUtils.diff(original, revised);
        try (PrintStream diffPS = new PrintStream(diffFile, ENCODING)) {
            patch.getDeltas().forEach(delta -> {
                if (delta instanceof ChangeDelta) {
                    ChangeDelta<String> cdelta = (ChangeDelta<String>) delta;
                    diffPS.println("Org: " + cdelta.getOriginal());
                    diffPS.println("New: " + cdelta.getRevised());
                    diffPS.println();
                } else if (delta instanceof DeleteDelta) {
                    DeleteDelta<String> ddelta = (DeleteDelta<String>) delta;
                    diffPS.println("Org: " + ddelta.getOriginal());
                    diffPS.println("New: " + ddelta.getRevised());
                    diffPS.println();
                } else if (delta instanceof InsertDelta) {
                    InsertDelta<String> idelta = (InsertDelta<String>) delta;
                    diffPS.println("Org: " + idelta.getOriginal());
                    diffPS.println("New: " + idelta.getRevised());
                    diffPS.println();
                } else {
                    diffPS.println(delta);
                }
            });
        }
    }
}
Also used : PrintStream(java.io.PrintStream) ChangeDelta(difflib.ChangeDelta) InputStreamReader(java.io.InputStreamReader) DeleteDelta(difflib.DeleteDelta) FileInputStream(java.io.FileInputStream) LineNumberReader(java.io.LineNumberReader) InsertDelta(difflib.InsertDelta)

Aggregations

ChangeDelta (difflib.ChangeDelta)1 DeleteDelta (difflib.DeleteDelta)1 InsertDelta (difflib.InsertDelta)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 LineNumberReader (java.io.LineNumberReader)1 PrintStream (java.io.PrintStream)1