GhostscriptHelper { public void runGhostScriptImageGeneration(String pdf, String outDir, String image) throws IOException, InterruptedException { runGhostScriptImageGeneration(pdf, outDir, image, null); } GhostscriptHelper();  GhostscriptHelper(String newGsExec); String getCliExecutionCommand(); void runGhostScriptImageGeneration(String pdf, String outDir, String image); void runGhostScriptImageGeneration(String pdf, String outDir, String image, String pageList); static final String GHOSTSCRIPT_ENVIRONMENT_VARIABLE; }
@Test public void runGhostScriptIncorrectOutputDirectory() throws IOException, InterruptedException { String inputPdf = sourceFolder + "imageHandlerUtilTest.pdf"; String exceptionMessage = "Cannot open output directory for " + inputPdf; junitExpectedException.expect(IllegalArgumentException.class); junitExpectedException.expectMessage(exceptionMessage); GhostscriptHelper ghostscriptHelper = new GhostscriptHelper(); ghostscriptHelper.runGhostScriptImageGeneration(inputPdf, "-", "outputPageImage.png", "1"); }
@Test public void runGhostScriptIncorrectParams() throws IOException, InterruptedException { String inputPdf = sourceFolder + "imageHandlerUtilTest.pdf"; String exceptionMessage = "GhostScript failed for " + inputPdf; junitExpectedException.expect(GhostscriptHelper.GhostscriptExecutionException.class); junitExpectedException.expectMessage(exceptionMessage); GhostscriptHelper ghostscriptHelper = new GhostscriptHelper(); ghostscriptHelper.runGhostScriptImageGeneration(inputPdf, destinationFolder, "outputPageImage.png", "q@W"); }
@Test public void runGhostScriptTestForSpecificPage() throws IOException, InterruptedException { String inputPdf = sourceFolder + "imageHandlerUtilTest.pdf"; GhostscriptHelper ghostscriptHelper = new GhostscriptHelper(); ghostscriptHelper.runGhostScriptImageGeneration(inputPdf, destinationFolder, "specificPage.png", "1"); Assert.assertEquals(1, FileUtil.listFilesInDirectory(destinationFolder, true).length); Assert.assertTrue(FileUtil.fileExists(destinationFolder + "specificPage.png")); }
@Test public void runGhostScriptTestForSeveralSpecificPages() throws IOException, InterruptedException { String inputPdf = sourceFolder + "imageHandlerUtilTest.pdf"; GhostscriptHelper ghostscriptHelper = new GhostscriptHelper(); String imageFileName = new File(inputPdf).getName() + "_severalSpecificPages-%03d.png"; ghostscriptHelper.runGhostScriptImageGeneration(inputPdf, destinationFolder, imageFileName, "1,3"); Assert.assertEquals(2, FileUtil.listFilesInDirectory(destinationFolder, true).length); Assert.assertTrue(FileUtil.fileExists(destinationFolder + "imageHandlerUtilTest.pdf_severalSpecificPages-001.png")); Assert.assertTrue(FileUtil.fileExists(destinationFolder + "imageHandlerUtilTest.pdf_severalSpecificPages-002.png")); }
@Test public void runGhostScriptTestForAllPages() throws IOException, InterruptedException { String inputPdf = sourceFolder + "imageHandlerUtilTest.pdf"; GhostscriptHelper ghostscriptHelper = new GhostscriptHelper(); String imageFileName = new File(inputPdf).getName() + "_allPages-%03d.png"; ghostscriptHelper.runGhostScriptImageGeneration(inputPdf, destinationFolder, imageFileName); Assert.assertEquals(3, FileUtil.listFilesInDirectory(destinationFolder, true).length); Assert.assertTrue(FileUtil.fileExists(destinationFolder + "imageHandlerUtilTest.pdf_allPages-001.png")); Assert.assertTrue(FileUtil.fileExists(destinationFolder + "imageHandlerUtilTest.pdf_allPages-002.png")); Assert.assertTrue(FileUtil.fileExists(destinationFolder + "imageHandlerUtilTest.pdf_allPages-003.png")); }
@Test public void dSaferParamInGhostScriptHelperTest() throws IOException, InterruptedException { String cmpPdf = sourceFolder + "maliciousPsInvokingCalcExe.ps"; String maliciousPsInvokingCalcExe = destinationFolder + "maliciousPsInvokingCalcExe.png"; int majorVersion = 0; int minorVersion = 0; boolean isWindows = identifyOsType().toLowerCase().contains("win"); if (isWindows) { String gsExec = SystemUtil.getPropertyOrEnvironmentVariable(GhostscriptHelper.GHOSTSCRIPT_ENVIRONMENT_VARIABLE); if (gsExec == null) { gsExec = SystemUtil.getPropertyOrEnvironmentVariable(GhostscriptHelper.GHOSTSCRIPT_ENVIRONMENT_VARIABLE_LEGACY); } String[] pathParts = gsExec.split("\\d\\.\\d\\d"); for (int i = 0; i < pathParts.length; i++) { gsExec = gsExec.replace(pathParts[i], ""); } String[] version = gsExec.split("\\."); majorVersion = Integer.parseInt(version[0]); minorVersion = Integer.parseInt(version[1]); } try { GhostscriptHelper ghostscriptHelper = new GhostscriptHelper(); ghostscriptHelper.runGhostScriptImageGeneration(cmpPdf, destinationFolder, "maliciousPsInvokingCalcExe.png"); if (isWindows) { Assert.assertTrue((majorVersion > 9 || (majorVersion == 9 && minorVersion >= 50))); } } catch (GhostscriptHelper.GhostscriptExecutionException e) { if (isWindows) { Assert.assertTrue((majorVersion < 9 || (majorVersion == 9 && minorVersion < 50))); } } Assert.assertFalse(FileUtil.fileExists(maliciousPsInvokingCalcExe)); }
@Test public void ghostScriptImageGenerationTest() throws IOException, InterruptedException { String filename = "resultantImage.png"; String psFile = sourceFolder + "simple.ps"; String resultantImage = destinationFolder + filename; String cmpResultantImage = sourceFolder + "cmp_" + filename; String diff = destinationFolder + "diff_" + filename; GhostscriptHelper ghostscriptHelper = new GhostscriptHelper(); ghostscriptHelper.runGhostScriptImageGeneration(psFile, destinationFolder, filename); Assert.assertTrue(FileUtil.fileExists(resultantImage)); ImageMagickHelper imageMagickHelper = new ImageMagickHelper(); Assert.assertTrue(imageMagickHelper.runImageMagickImageCompare(resultantImage, cmpResultantImage, diff)); }