# This patch file was generated by NetBeans IDE
# It uses platform neutral UTF-8 encoding and \n newlines.
--- TestImageIOUtils.java (1557344)
+++ Current File
@@ -28,6 +28,9 @@
import org.apache.commons.logging.LogFactory;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
/**
* Test suite for ImageIOUtil.
@@ -35,6 +38,7 @@
*/
public class TestImageIOUtils extends TestCase
{
+ private final ExecutorService threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
/**
* Logger instance.
@@ -43,45 +47,98 @@
private boolean testFailed = false;
- /**
- * Validate page rendering for all supported image formats (JDK5).
- *
- * @param file The file to validate
- * @param inDir Name of the input directory
- * @param outDir Name of the output directory
- * @throws Exception when there is an exception
- */
- private void doTestFile(File file, String inDir, String outDir) throws Exception
+ private class WriteImageThread extends Thread
{
+ File file;
+ String imageFormat;
+ String outputPrefix;
+ int imageType, resolution;
+ boolean failed = false;
+
+ public WriteImageThread(File file, String imageFormat, String outputPrefix, int imageType, int resolution)
+ {
+ this.file = file;
+ this.imageFormat = imageFormat;
+ this.outputPrefix = outputPrefix;
+ this.imageType = imageType;
+ this.resolution = resolution;
+ }
+
+ @Override
+ public void run()
+ {
PDDocument document = null;
- String imageType = "png";
- LOG.info("Preparing to convert " + file.getName());
try
{
- int resolution = 120;
document = PDDocument.load(file);
- // testing PNG
- writeImage(document, imageType, outDir + file.getName() + "-", BufferedImage.TYPE_INT_RGB, resolution);
- // testing JPG/JPEG
- imageType = "jpg";
- writeImage(document, imageType, outDir + file.getName() + "-", BufferedImage.TYPE_INT_RGB, resolution);
- // testing BMP
- imageType = "bmp";
- writeImage(document, imageType, outDir + file.getName() + "-", BufferedImage.TYPE_INT_RGB, resolution);
- // testing WBMP
- imageType = "wbmp";
- writeImage(document, imageType, outDir + file.getName() + "-", BufferedImage.TYPE_BYTE_BINARY, resolution);
+ writeImage(document, imageFormat, outputPrefix, imageType, resolution);
+ System.out.println ("Done: " + outputPrefix + "1." + imageFormat);
}
- catch (Exception e)
+ catch (IOException ex)
{
+ System.err.println ("Fail: " + outputPrefix + "1." + imageFormat);
+ LOG.error("IOException", ex);
testFailed = true;
- LOG.error("Error converting file " + file.getName() + " using image type " + imageType, e);
}
finally
{
+ try
+ {
document.close();
}
+ catch (IOException ex)
+ {
+ }
+ }
+ }
+ }
+
+ /**
+ * Validate page rendering for all supported image formats (JDK5).
+ *
+ * @param file The file to validate
+ * @param inDir Name of the input directory
+ * @param outDir Name of the output directory
+ * @throws Exception when there is an exception
+ */
+ private void doTestFile(File file, String inDir, String outDir) throws Exception
+ {
+// PDDocument document = null;
+// String imageType = "png";
+ LOG.info("Preparing to convert " + file.getName());
+// try
+// {
+ int resolution = 120;
+ //document = PDDocument.load(file);
+
+ threadPool.execute(new WriteImageThread(file, "png", outDir + file.getName() + "-", BufferedImage.TYPE_INT_RGB, resolution));
+ threadPool.execute(new WriteImageThread(file, "jpg", outDir + file.getName() + "-", BufferedImage.TYPE_INT_RGB, resolution));
+ threadPool.execute(new WriteImageThread(file, "bmp", outDir + file.getName() + "-", BufferedImage.TYPE_INT_RGB, resolution));
+ threadPool.execute(new WriteImageThread(file, "wbmp", outDir + file.getName() + "-", BufferedImage.TYPE_INT_RGB, resolution));
+
+// // testing PNG
+// writeImage(document, imageType, outDir + file.getName() + "-", BufferedImage.TYPE_INT_RGB, resolution);
+// // testing JPG/JPEG
+// imageType = "jpg";
+// writeImage(document, imageType, outDir + file.getName() + "-", BufferedImage.TYPE_INT_RGB, resolution);
+// // testing BMP
+// imageType = "bmp";
+// writeImage(document, imageType, outDir + file.getName() + "-", BufferedImage.TYPE_INT_RGB, resolution);
+// // testing WBMP
+// imageType = "wbmp";
+// writeImage(document, imageType, outDir + file.getName() + "-", BufferedImage.TYPE_BYTE_BINARY, resolution);
+// }
+// catch (Exception e)
+// {
+// testFailed = true;
+// LOG.error("Error converting file " + file.getName() + " using image type " + imageType, e);
+// }
+// finally
+// {
+// document.close();
+// }
+
}
private void writeImage(PDDocument document, String imageFormat, String outputPrefix, int imageType, int resolution)
@@ -89,7 +146,7 @@
{
List pages = document.getDocumentCatalog().getAllPages();
BufferedImage image = RenderUtil.convertToImage(pages.get(0), imageType, resolution);
- String fileName = outputPrefix + 1;
+ String fileName = outputPrefix + "1";
System.out.println("Writing: " + fileName + "." + imageFormat);
ImageIOUtil.writeImage(image, imageFormat, fileName, imageType, resolution);
}
@@ -103,6 +160,9 @@
{
String inDir = "src/test/resources/input/rendering";
String outDir = "target/test-output/";
+
+ System.out.println ("Threads in thread pool: " + Runtime.getRuntime().availableProcessors());
+
new File(outDir).mkdir();
File[] testFiles = new File(inDir).listFiles(new FilenameFilter()
{
@@ -117,6 +177,11 @@
doTestFile(testFiles[n], inDir, outDir);
}
+ threadPool.shutdown();
+ while (!threadPool.awaitTermination(10, TimeUnit.SECONDS))
+ System.out.println ("Thread pool not yet terminated, keep waiting");
+ System.out.println ("Thread pool terminated");
+
if (testFailed)
{
fail("One or more failures, see test log for details");