You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2020/07/15 04:12:37 UTC
svn commit: r1879868 -
/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/text/TestTextStripper.java
Author: tilman
Date: Wed Jul 15 04:12:37 2020
New Revision: 1879868
URL: http://svn.apache.org/viewvc?rev=1879868&view=rev
Log:
PDFBOX-4909: refactor for further tests; remove dead code; remove printStackTrace()
Modified:
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/text/TestTextStripper.java
Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/text/TestTextStripper.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/text/TestTextStripper.java?rev=1879868&r1=1879867&r2=1879868&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/text/TestTextStripper.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/text/TestTextStripper.java Wed Jul 15 04:12:37 2020
@@ -250,7 +250,6 @@ public class TestTextStripper extends Te
}
}
- //System.out.println(" " + inFile + (bSort ? " (sorted)" : ""));
try (PDDocument document = Loader.loadPDF(inFile))
{
File outFile;
@@ -302,113 +301,114 @@ public class TestTextStripper extends Te
return;
}
- boolean localFail = false;
+ compareResult(expectedFile, outFile, inFile, bSort, diffFile);
+ }
+ }
- try (LineNumberReader expectedReader =
- new LineNumberReader(new InputStreamReader(new FileInputStream(expectedFile), ENCODING));
- LineNumberReader actualReader =
- new LineNumberReader(new InputStreamReader(new FileInputStream(outFile), ENCODING)))
- {
- while (true)
+ 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 )
{
- 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 )
+ 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)
{
- actualLine = actualReader.readLine();
+ ChangeDelta cdelta = (ChangeDelta) delta;
+ diffPS.println("Org: " + cdelta.getOriginal());
+ diffPS.println("New: " + cdelta.getRevised());
+ diffPS.println();
}
- if (!stringsEqual(expectedLine, actualLine))
+ else if (delta instanceof DeleteDelta)
{
- 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;
+ DeleteDelta ddelta = (DeleteDelta) delta;
+ diffPS.println("Org: " + ddelta.getOriginal());
+ diffPS.println("New: " + ddelta.getRevised());
+ diffPS.println();
}
-
- if( expectedLine == null || actualLine==null)
+ else if (delta instanceof InsertDelta)
{
- break;
+ InsertDelta idelta = (InsertDelta) delta;
+ diffPS.println("Org: " + idelta.getOriginal());
+ diffPS.println("New: " + idelta.getRevised());
+ diffPS.println();
}
- }
- }
- 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))
- {
- for (Object delta : patch.getDeltas())
+ else
{
- if (delta instanceof ChangeDelta)
- {
- ChangeDelta cdelta = (ChangeDelta) delta;
- diffPS.println("Org: " + cdelta.getOriginal());
- diffPS.println("New: " + cdelta.getRevised());
- diffPS.println();
- }
- else if (delta instanceof DeleteDelta)
- {
- DeleteDelta ddelta = (DeleteDelta) delta;
- diffPS.println("Org: " + ddelta.getOriginal());
- diffPS.println("New: " + ddelta.getRevised());
- diffPS.println();
- }
- else if (delta instanceof InsertDelta)
- {
- InsertDelta idelta = (InsertDelta) delta;
- diffPS.println("Org: " + idelta.getOriginal());
- diffPS.println("New: " + idelta.getRevised());
- diffPS.println();
- }
- else
- {
- diffPS.println(delta);
- }
+ diffPS.println(delta);
}
- }
+ });
}
}
}
// Helper method for get the file content
- private static List<String> fileToLines(File file)
+ private static List<String> fileToLines(File file) throws IOException
{
List<String> lines = new LinkedList<>();
String line;
- try
+
+ try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), ENCODING)))
{
- try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), ENCODING)))
+ while ((line = in.readLine()) != null)
{
- while ((line = in.readLine()) != null)
- {
- lines.add(line);
- }
+ lines.add(line);
}
}
- catch (IOException e)
- {
- e.printStackTrace();
- }
+
return lines;
}
@@ -547,14 +547,7 @@ public class TestTextStripper extends Te
*/
private void doTestDir(File inDir, File outDir) throws Exception
{
- File[] testFiles = inDir.listFiles(new FilenameFilter()
- {
- @Override
- public boolean accept(File dir, String name)
- {
- return (name.endsWith(".pdf"));
- }
- });
+ File[] testFiles = inDir.listFiles((File dir, String name) -> name.endsWith(".pdf"));
for (File testFile : testFiles)
{
//Test without sorting