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 2015/05/15 12:56:29 UTC
svn commit: r1679539 -
/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/rendering/TestPDFToImage.java
Author: tilman
Date: Fri May 15 10:56:28 2015
New Revision: 1679539
URL: http://svn.apache.org/r1679539
Log:
PDFBOX-1695: remove unneeded stuff, allow better outside access, reformat
Modified:
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/rendering/TestPDFToImage.java
Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/rendering/TestPDFToImage.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/rendering/TestPDFToImage.java?rev=1679539&r1=1679538&r2=1679539&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/rendering/TestPDFToImage.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/rendering/TestPDFToImage.java Fri May 15 10:56:28 2015
@@ -40,22 +40,20 @@ import javax.imageio.ImageIO;
*
* FILE SET VALIDATION
*
- * This test suite is designed to test PDFToImage using a set of PDF files and
- * known good output for each. The default mode of testAll() is to process each
- * *.pdf file in "src/test/resources/input/rendering". An output file is created
- * in "target/test-output/rendering" with the same name as the PDF file, plus an
- * additional page number and ".png" suffix.
+ * This test suite is designed to test PDFToImage using a set of PDF files and known good output for
+ * each. The default mode of testAll() is to process each *.pdf file in
+ * "src/test/resources/input/rendering". An output file is created in "target/test-output/rendering"
+ * with the same name as the PDF file, plus an additional page number and ".png" suffix.
*
- * The output file is then tested against a known good result file from the
- * input directory (again, with the same name as the tested PDF file, but with
- * the additional page number and ".png" suffix).
+ * The output file is then tested against a known good result file from the input directory (again,
+ * with the same name as the tested PDF file, but with the additional page number and ".png"
+ * suffix).
*
- * If the two aren't identical, a graphical .diff.png file is created. If they
- * are identical, the output .png file is deleted. If a "good result" file
- * doesn't exist, the output .png file is left there for human inspection.
+ * If the two aren't identical, a graphical .diff.png file is created. If they are identical, the
+ * output .png file is deleted. If a "good result" file doesn't exist, the output .png file is left
+ * there for human inspection.
*
- * Errors are flagged by creating empty files with appropriate names in the
- * target directory.
+ * Errors are flagged by creating empty files with appropriate names in the target directory.
*
* @author Daniel Wilson
* @author Ben Litchfield
@@ -69,8 +67,10 @@ public class TestPDFToImage extends Test
*/
private static final Log LOG = LogFactory.getLog(TestPDFToImage.class);
- private boolean bFail = false;
- private File mcurFile = null;
+ String inDir = "src/test/resources/input/rendering";
+ String outDir = "target/test-output/rendering/";
+ String inDirExt = "target/test-input-ext/rendering";
+ String outDirExt = "target/test-output-ext/rendering";
/**
* Test class constructor.
@@ -90,15 +90,21 @@ public class TestPDFToImage extends Test
@Override
public void setUp()
{
- // If you want to test a single file using DEBUG logging, from an IDE,
- // you can do something like this:
- //
- // System.setProperty("org.apache.pdfbox.util.TextStripper.file", "FVS318Ref.pdf");
+ }
+
+ public void setInDir(String inDir)
+ {
+ this.inDir = inDir;
+ }
+
+ public void setOutDir(String outDir)
+ {
+ this.outDir = outDir;
}
/**
- * Create an image; the part between the smaller and the larger image is
- * painted black, the rest in white
+ * Create an image; the part between the smaller and the larger image is painted black, the rest
+ * in white
*
* @param minWidth width of the smaller image
* @param minHeight width of the smaller image
@@ -123,15 +129,14 @@ public class TestPDFToImage extends Test
}
/**
- * Get the difference between two images, identical colors are set to white,
- * differences are xored, the highest bit of each color is reset to avoid
- * colors that are too light
+ * Get the difference between two images, identical colors are set to white, differences are
+ * xored, the highest bit of each color is reset to avoid colors that are too light
*
* @param bim1
* @param bim2
- * @return If the images are different, the function returns a diff image If
- * the images are identical, the function returns null If the size is
- * different, a black border on the botton and the right is created
+ * @return If the images are different, the function returns a diff image If the images are
+ * identical, the function returns null If the size is different, a black border on the botton
+ * and the right is created
*
* @throws IOException
*/
@@ -180,34 +185,35 @@ public class TestPDFToImage extends Test
}
/**
- * Validate text extraction on a single file.
+ * Validate the renderings of a single file.
*
* @param file The file to validate
- * @param bLogResult Whether to log the extracted text
* @param inDir Name of the input directory
* @param outDir Name of the output directory
- * @throws Exception when there is an exception
+ * @return false if the test failed (not identical or other problem), true if the test succeeded
+ * (all identical)
+ * @throws IOException when there is an exception
*/
- public void doTestFile(File file, boolean bLogResult, String inDir, String outDir)
- throws Exception
+ public boolean doTestFile(final File file, String inDir, String outDir) throws IOException
{
PDDocument document = null;
+ boolean failed = false;
LOG.info("Opening: " + file.getName());
try
{
- new FileOutputStream(new File(outDir + file.getName() + ".parseerror")).close();
+ new FileOutputStream(new File(outDir, file.getName() + ".parseerror")).close();
document = PDDocument.load(file, null);
- String outputPrefix = outDir + file.getName() + "-";
+ String outputPrefix = outDir + '/' + file.getName() + "-";
int numPages = document.getNumberOfPages();
if (numPages < 1)
{
- this.bFail = true;
+ failed = true;
LOG.error("file " + file.getName() + " has < 1 page");
}
else
{
- new File(outDir + file.getName() + ".parseerror").delete();
+ new File(outDir, file.getName() + ".parseerror").delete();
}
LOG.info("Rendering: " + file.getName());
@@ -225,19 +231,19 @@ public class TestPDFToImage extends Test
}
// test to see whether file is destroyed in pdfbox
- new FileOutputStream(new File(outDir + file.getName() + ".saveerror")).close();
+ new FileOutputStream(new File(outDir, file.getName() + ".saveerror")).close();
File tmpFile = File.createTempFile("pdfbox", ".pdf");
document.setAllSecurityToBeRemoved(true);
document.save(tmpFile);
- new File(outDir + file.getName() + ".saveerror").delete();
- new FileOutputStream(new File(outDir + file.getName() + ".reloaderror")).close();
+ new File(outDir, file.getName() + ".saveerror").delete();
+ new FileOutputStream(new File(outDir, file.getName() + ".reloaderror")).close();
PDDocument.load(tmpFile, null).close();
- new File(outDir + file.getName() + ".reloaderror").delete();
+ new File(outDir, file.getName() + ".reloaderror").delete();
tmpFile.delete();
}
catch (Exception e)
{
- this.bFail = true;
+ failed = true;
LOG.error("Error converting file " + file.getName(), e);
}
finally
@@ -255,25 +261,28 @@ public class TestPDFToImage extends Test
{
new File(outDir + file.getName() + ".cmperror").delete();
- mcurFile = file;
-
File[] outFiles = new File(outDir).listFiles(new FilenameFilter()
{
@Override
public boolean accept(File dir, String name)
{
return (name.endsWith(".png")
- && name.startsWith(mcurFile.getName(), 0))
+ && name.startsWith(file.getName(), 0))
&& !name.endsWith(".png-diff.png");
}
});
+ if (outFiles.length == 0)
+ {
+ failed = true;
+ LOG.warn("*** TEST FAILURE *** Output missing for file: " + file.getName());
+ }
for (File outFile : outFiles)
{
new File(outFile.getAbsolutePath() + "-diff.png").delete(); // delete diff file from a previous run
File inFile = new File(inDir + '/' + outFile.getName());
if (!inFile.exists())
{
- this.bFail = true;
+ failed = true;
LOG.warn("*** TEST FAILURE *** Input missing for file: " + inFile.getName());
}
else if (!filesAreIdentical(outFile, inFile))
@@ -283,7 +292,7 @@ public class TestPDFToImage extends Test
BufferedImage bim3 = diffImages(ImageIO.read(inFile), ImageIO.read(outFile));
if (bim3 != null)
{
- this.bFail = true;
+ failed = true;
LOG.warn("*** TEST FAILURE *** Input and output not identical for file: " + inFile.getName());
ImageIO.write(bim3, "png", new File(outFile.getAbsolutePath() + "-diff.png"));
}
@@ -304,11 +313,12 @@ public class TestPDFToImage extends Test
}
catch (Exception e)
{
- new FileOutputStream(new File(outDir + file.getName() + ".cmperror")).close();
- this.bFail = true;
+ new FileOutputStream(new File(outDir, file.getName() + ".cmperror")).close();
+ failed = true;
LOG.error("Error comparing file output for " + file.getName(), e);
}
+ return !failed;
}
/**
@@ -316,53 +326,66 @@ public class TestPDFToImage extends Test
*
* @throws Exception when there is an exception
*/
- public void testRenderImage()
+ public void testRenderImages()
throws Exception
{
- String filename = System.getProperty("org.apache.pdfbox.util.TextStripper.file");
- String inDir = "src/test/resources/input/rendering";
- String outDir = "target/test-output/rendering/";
- String inDirExt = "target/test-input-ext/rendering";
- String outDirExt = "target/test-output-ext/rendering";
+ boolean failed = false;
new File(outDir).mkdirs();
- if ((filename == null) || (filename.length() == 0))
+ File[] testFiles = new File(inDir).listFiles(new FilenameFilter()
{
- File[] testFiles = new File(inDir).listFiles(new FilenameFilter()
+ @Override
+ public boolean accept(File dir, String name)
{
- @Override
- public boolean accept(File dir, String name)
- {
- return (name.endsWith(".pdf") || name.endsWith(".ai"));
- }
- });
- for (File testFile : testFiles)
+ return (name.endsWith(".pdf") || name.endsWith(".ai"));
+ }
+ });
+ for (File testFile : testFiles)
+ {
+ if (!doTestFile(testFile, inDir, outDir))
{
- doTestFile(testFile, false, inDir, outDir);
+ failed = true;
}
- testFiles = new File(inDirExt).listFiles(new FilenameFilter()
+ }
+ testFiles = new File(inDirExt).listFiles(new FilenameFilter()
+ {
+ @Override
+ public boolean accept(File dir, String name)
{
- @Override
- public boolean accept(File dir, String name)
- {
- return (name.endsWith(".pdf") || name.endsWith(".ai"));
- }
- });
- if (testFiles != null)
+ return (name.endsWith(".pdf") || name.endsWith(".ai"));
+ }
+ });
+ if (testFiles != null)
+ {
+ for (File testFile : testFiles)
{
- for (File testFile : testFiles)
+ if (!doTestFile(testFile, inDirExt, outDirExt))
{
- doTestFile(testFile, false, inDirExt, outDirExt);
+ failed = true;
}
}
}
- else
+
+ if (failed)
{
- doTestFile(new File(inDir, filename), true, inDir, outDir);
+ fail("One or more failures, see test log for details");
}
+ }
+
+ /**
+ * Test to validate image rendering of file.
+ *
+ * @param filename the file name to validate.
+ *
+ * @throws Exception when there is an exception
+ */
+ public void testRenderImage(String filename)
+ throws Exception
+ {
+ new File(outDir).mkdirs();
- if (this.bFail)
+ if (!doTestFile(new File(inDir, filename), inDir, outDir))
{
fail("One or more failures, see test log for details");
}