Skip to content

Commit 9a47f5f

Browse files
Improve GhostScript and ImageMagick util classes formatting and their tests
QA-12194
1 parent ea6918d commit 9a47f5f

File tree

4 files changed

+99
-55
lines changed

4 files changed

+99
-55
lines changed

io/src/main/java/com/itextpdf/io/util/GhostscriptHelper.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ This file is part of the iText (R) project.
4747

4848
import java.io.IOException;
4949

50+
/**
51+
* A utility class that is used as an interface to run 3rd-party tool Ghostscript.
52+
* Ghostscript is an interpreter for the PostScript language and PDF files, it allows to render them
53+
* as images.
54+
*
55+
* <p>
56+
* The Ghostscript needs to be installed independently on the system. This class provides a convenient
57+
* way to run it by passing a terminal command. The command can either be specified explicitly or by a mean
58+
* of environment variable {@link #GHOSTSCRIPT_ENVIRONMENT_VARIABLE}.
59+
*/
5060
public class GhostscriptHelper {
5161
/**
5262
* The name of the environment variable with the command to execute Ghostscript operations.
@@ -62,10 +72,19 @@ public class GhostscriptHelper {
6272

6373
private String gsExec;
6474

75+
/**
76+
* Creates new instance that will rely on Ghostscript execution command defined by {@link
77+
* #GHOSTSCRIPT_ENVIRONMENT_VARIABLE} environment variable.
78+
*/
6579
public GhostscriptHelper() {
6680
this(null);
6781
}
6882

83+
/**
84+
* Creates new instance that will rely on Ghostscript execution command defined as passed argument.
85+
*
86+
* @param newGsExec the Ghostscript execution command; if null - environment variables will be used instead
87+
*/
6988
public GhostscriptHelper(String newGsExec) {
7089
gsExec = newGsExec;
7190
if (gsExec == null) {
@@ -98,10 +117,12 @@ public String getCliExecutionCommand() {
98117
* @param pdf Path to the pdf file.
99118
* @param outDir Path to the output directory
100119
* @param image Path to the generated image
120+
*
101121
* @throws IOException if there are file's reading/writing issues
102122
* @throws InterruptedException if there is thread interruption while executing GhostScript.
103123
*/
104-
public void runGhostScriptImageGeneration(String pdf, String outDir, String image) throws IOException, InterruptedException {
124+
public void runGhostScriptImageGeneration(String pdf, String outDir, String image)
125+
throws IOException, InterruptedException {
105126
runGhostScriptImageGeneration(pdf, outDir, image, null);
106127
}
107128

@@ -114,13 +135,15 @@ public void runGhostScriptImageGeneration(String pdf, String outDir, String imag
114135
* @param pageList String with numbers of the required pages to extract as image. Should be formatted as string with
115136
* numbers, separated by commas, without whitespaces. Can be null, if it is required to extract
116137
* all pages as images.
138+
*
117139
* @throws IOException if there are file's reading/writing issues
118140
* @throws InterruptedException if there is thread interruption while executing GhostScript.
119141
*/
120142
public void runGhostScriptImageGeneration(String pdf, String outDir, String image, String pageList)
121143
throws IOException, InterruptedException {
122144
if (!FileUtil.directoryExists(outDir)) {
123-
throw new IllegalArgumentException(IoExceptionMessage.CANNOT_OPEN_OUTPUT_DIRECTORY.replace("<filename>", pdf));
145+
throw new IllegalArgumentException(
146+
IoExceptionMessage.CANNOT_OPEN_OUTPUT_DIRECTORY.replace("<filename>", pdf));
124147
}
125148

126149
pageList = (pageList == null) ? "" : "-sPageList=<pagelist>".replace("<pagelist>", pageList);

io/src/main/java/com/itextpdf/io/util/ImageMagickHelper.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ This file is part of the iText (R) project.
4747

4848
import java.io.IOException;
4949

50+
/**
51+
* A utility class that is used as an interface to run 3rd-party tool ImageMagick.
52+
* ImageMagick among other things allows to compare images and this class provides means to utilize this feature.
53+
*
54+
* <p>
55+
* The ImageMagick needs to be installed independently on the system. This class provides a convenient
56+
* way to run it by passing a terminal command. The command can either be specified explicitly or by a mean
57+
* of environment variable {@link #MAGICK_COMPARE_ENVIRONMENT_VARIABLE}.
58+
*/
5059
public class ImageMagickHelper {
5160
/**
5261
* The name of the environment variable with the command to execute ImageMagic comparison operations.
@@ -60,10 +69,19 @@ public class ImageMagickHelper {
6069

6170
private String compareExec;
6271

72+
/**
73+
* Creates new instance that will rely on ImageMagick execution command defined by {@link
74+
* #MAGICK_COMPARE_ENVIRONMENT_VARIABLE} environment variable.
75+
*/
6376
public ImageMagickHelper() {
6477
this(null);
6578
}
6679

80+
/**
81+
* Creates new instance that will rely on ImageMagick execution command defined as passed argument.
82+
*
83+
* @param newCompareExec the ImageMagick execution command; if null - environment variables will be used instead
84+
*/
6785
public ImageMagickHelper(String newCompareExec) {
6886
compareExec = newCompareExec;
6987
if (compareExec == null) {
@@ -95,7 +113,9 @@ public String getCliExecutionCommand() {
95113
* @param outImageFilePath Path to the output image file
96114
* @param cmpImageFilePath Path to the cmp image file
97115
* @param diffImageName Path to the difference output image file
116+
*
98117
* @return boolean result of comparing: true - images are visually equal
118+
*
99119
* @throws IOException if there are file's reading/writing issues
100120
* @throws InterruptedException if there is thread interruption while executing ImageMagick.
101121
*/
@@ -112,13 +132,14 @@ public boolean runImageMagickImageCompare(String outImageFilePath, String cmpIma
112132
* @param diffImageName Path to the difference output image file
113133
* @param fuzzValue String fuzziness value to compare images. Should be formatted as string with integer
114134
* or decimal number. Can be null, if it is not required to use fuzziness
135+
*
115136
* @return boolean result of comparing: true - images are visually equal
137+
*
116138
* @throws IOException if there are file's reading/writing issues
117139
* @throws InterruptedException if there is thread interruption while executing ImageMagick.
118140
*/
119141
public boolean runImageMagickImageCompare(String outImageFilePath, String cmpImageFilePath,
120-
String diffImageName, String fuzzValue)
121-
throws IOException, InterruptedException {
142+
String diffImageName, String fuzzValue) throws IOException, InterruptedException {
122143
fuzzValue = (fuzzValue == null) ? "" : " -metric AE -fuzz <fuzzValue>%".replace("<fuzzValue>", fuzzValue);
123144

124145
StringBuilder currCompareParams = new StringBuilder();

io/src/test/java/com/itextpdf/io/util/GhostscriptHelperTest.java

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ This file is part of the iText (R) project.
5656

5757
@Category(IntegrationTest.class)
5858
public class GhostscriptHelperTest extends ExtendedITextTest {
59-
private final static String sourceFolder = "./src/test/resources/com/itextpdf/io/util/GhostscriptHelperTest/";
60-
private static final String destinationFolder = "./target/test/com/itextpdf/io/GhostscriptHelperTest/";
59+
private final static String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/io/util/GhostscriptHelperTest/";
60+
private static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/io/GhostscriptHelperTest/";
6161

6262
@Before
6363
public void setUp() {
64-
createOrClearDestinationFolder(destinationFolder);
64+
createOrClearDestinationFolder(DESTINATION_FOLDER);
6565
}
6666

6767
@Test
@@ -99,7 +99,7 @@ public void ghostScriptEnvVarIsIncorrect() {
9999

100100
@Test
101101
public void runGhostScriptIncorrectOutputDirectory() throws IOException, InterruptedException {
102-
String inputPdf = sourceFolder + "imageHandlerUtilTest.pdf";
102+
String inputPdf = SOURCE_FOLDER + "imageHandlerUtilTest.pdf";
103103
String exceptionMessage = "Cannot open output directory for " + inputPdf;
104104

105105
GhostscriptHelper ghostscriptHelper = new GhostscriptHelper();
@@ -113,67 +113,67 @@ public void runGhostScriptIncorrectOutputDirectory() throws IOException, Interru
113113

114114
@Test
115115
public void runGhostScriptIncorrectParams() throws IOException, InterruptedException {
116-
String inputPdf = sourceFolder + "imageHandlerUtilTest.pdf";
116+
String inputPdf = SOURCE_FOLDER + "imageHandlerUtilTest.pdf";
117117
String exceptionMessage = "GhostScript failed for " + inputPdf;
118118

119119
GhostscriptHelper ghostscriptHelper = new GhostscriptHelper();
120120

121121
Exception e = Assert.assertThrows(GhostscriptHelper.GhostscriptExecutionException.class,
122-
() -> ghostscriptHelper.runGhostScriptImageGeneration(inputPdf, destinationFolder,
122+
() -> ghostscriptHelper.runGhostScriptImageGeneration(inputPdf, DESTINATION_FOLDER,
123123
"outputPageImage.png", "q@W")
124124
);
125125
Assert.assertEquals(exceptionMessage, e.getMessage());
126126
}
127127

128128
@Test
129129
public void runGhostScriptTestForSpecificPage() throws IOException, InterruptedException {
130-
String inputPdf = sourceFolder + "imageHandlerUtilTest.pdf";
130+
String inputPdf = SOURCE_FOLDER + "imageHandlerUtilTest.pdf";
131131

132132
GhostscriptHelper ghostscriptHelper = new GhostscriptHelper();
133-
ghostscriptHelper.runGhostScriptImageGeneration(inputPdf, destinationFolder,
133+
ghostscriptHelper.runGhostScriptImageGeneration(inputPdf, DESTINATION_FOLDER,
134134
"specificPage.png", "1");
135135

136-
Assert.assertEquals(1, FileUtil.listFilesInDirectory(destinationFolder, true).length);
137-
Assert.assertTrue(FileUtil.fileExists(destinationFolder + "specificPage.png"));
136+
Assert.assertEquals(1, FileUtil.listFilesInDirectory(DESTINATION_FOLDER, true).length);
137+
Assert.assertTrue(FileUtil.fileExists(DESTINATION_FOLDER + "specificPage.png"));
138138
}
139139

140140
@Test
141141
public void runGhostScriptTestForSeveralSpecificPages() throws IOException, InterruptedException {
142-
String inputPdf = sourceFolder + "imageHandlerUtilTest.pdf";
142+
String inputPdf = SOURCE_FOLDER + "imageHandlerUtilTest.pdf";
143143

144144
GhostscriptHelper ghostscriptHelper = new GhostscriptHelper();
145145
String imageFileName = new File(inputPdf).getName() + "_severalSpecificPages-%03d.png";
146-
ghostscriptHelper.runGhostScriptImageGeneration(inputPdf, destinationFolder,
146+
ghostscriptHelper.runGhostScriptImageGeneration(inputPdf, DESTINATION_FOLDER,
147147
imageFileName, "1,3");
148148

149-
Assert.assertEquals(2, FileUtil.listFilesInDirectory(destinationFolder, true).length);
150-
Assert.assertTrue(FileUtil.fileExists(destinationFolder + "imageHandlerUtilTest.pdf_severalSpecificPages-001.png"));
151-
Assert.assertTrue(FileUtil.fileExists(destinationFolder + "imageHandlerUtilTest.pdf_severalSpecificPages-002.png"));
149+
Assert.assertEquals(2, FileUtil.listFilesInDirectory(DESTINATION_FOLDER, true).length);
150+
Assert.assertTrue(FileUtil.fileExists(DESTINATION_FOLDER + "imageHandlerUtilTest.pdf_severalSpecificPages-001.png"));
151+
Assert.assertTrue(FileUtil.fileExists(DESTINATION_FOLDER + "imageHandlerUtilTest.pdf_severalSpecificPages-002.png"));
152152
}
153153

154154
@Test
155155
public void runGhostScriptTestForAllPages() throws IOException, InterruptedException {
156-
String inputPdf = sourceFolder + "imageHandlerUtilTest.pdf";
156+
String inputPdf = SOURCE_FOLDER + "imageHandlerUtilTest.pdf";
157157

158158
GhostscriptHelper ghostscriptHelper = new GhostscriptHelper();
159159
String imageFileName = new File(inputPdf).getName() + "_allPages-%03d.png";
160-
ghostscriptHelper.runGhostScriptImageGeneration(inputPdf, destinationFolder, imageFileName);
160+
ghostscriptHelper.runGhostScriptImageGeneration(inputPdf, DESTINATION_FOLDER, imageFileName);
161161

162-
Assert.assertEquals(3, FileUtil.listFilesInDirectory(destinationFolder, true).length);
163-
Assert.assertTrue(FileUtil.fileExists(destinationFolder + "imageHandlerUtilTest.pdf_allPages-001.png"));
164-
Assert.assertTrue(FileUtil.fileExists(destinationFolder + "imageHandlerUtilTest.pdf_allPages-002.png"));
165-
Assert.assertTrue(FileUtil.fileExists(destinationFolder + "imageHandlerUtilTest.pdf_allPages-003.png"));
162+
Assert.assertEquals(3, FileUtil.listFilesInDirectory(DESTINATION_FOLDER, true).length);
163+
Assert.assertTrue(FileUtil.fileExists(DESTINATION_FOLDER + "imageHandlerUtilTest.pdf_allPages-001.png"));
164+
Assert.assertTrue(FileUtil.fileExists(DESTINATION_FOLDER + "imageHandlerUtilTest.pdf_allPages-002.png"));
165+
Assert.assertTrue(FileUtil.fileExists(DESTINATION_FOLDER + "imageHandlerUtilTest.pdf_allPages-003.png"));
166166
}
167167

168168
@Test
169169
public void dSaferParamInGhostScriptHelperTest() throws IOException, InterruptedException {
170-
String input = sourceFolder + "unsafePostScript.ps";
170+
String input = SOURCE_FOLDER + "unsafePostScript.ps";
171171
String outputName = "unsafePostScript.png";
172-
String maliciousResult1 = destinationFolder + "output1.txt";
173-
String maliciousResult2 = destinationFolder + "output2.txt";
172+
String maliciousResult1 = DESTINATION_FOLDER + "output1.txt";
173+
String maliciousResult2 = DESTINATION_FOLDER + "output2.txt";
174174
try {
175175
GhostscriptHelper ghostscriptHelper = new GhostscriptHelper();
176-
ghostscriptHelper.runGhostScriptImageGeneration(input, destinationFolder, outputName);
176+
ghostscriptHelper.runGhostScriptImageGeneration(input, DESTINATION_FOLDER, outputName);
177177
} catch (GhostscriptHelper.GhostscriptExecutionException e) {
178178
System.out.println("Error code was returned on processing of malicious script with -dSAFER option enabled. "
179179
+ "This is expected for some environments and ghostscript versions. "
@@ -186,13 +186,13 @@ public void dSaferParamInGhostScriptHelperTest() throws IOException, Interrupted
186186
@Test
187187
public void ghostScriptImageGenerationTest() throws IOException, InterruptedException {
188188
String filename = "resultantImage.png";
189-
String psFile = sourceFolder + "simple.ps";
190-
String resultantImage = destinationFolder + filename;
191-
String cmpResultantImage = sourceFolder + "cmp_" + filename;
192-
String diff = destinationFolder + "diff_" + filename;
189+
String psFile = SOURCE_FOLDER + "simple.ps";
190+
String resultantImage = DESTINATION_FOLDER + filename;
191+
String cmpResultantImage = SOURCE_FOLDER + "cmp_" + filename;
192+
String diff = DESTINATION_FOLDER + "diff_" + filename;
193193

194194
GhostscriptHelper ghostscriptHelper = new GhostscriptHelper();
195-
ghostscriptHelper.runGhostScriptImageGeneration(psFile, destinationFolder, filename);
195+
ghostscriptHelper.runGhostScriptImageGeneration(psFile, DESTINATION_FOLDER, filename);
196196
Assert.assertTrue(FileUtil.fileExists(resultantImage));
197197

198198
ImageMagickHelper imageMagickHelper = new ImageMagickHelper();

io/src/test/java/com/itextpdf/io/util/ImageMagickHelperTest.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ This file is part of the iText (R) project.
5454

5555
@Category(IntegrationTest.class)
5656
public class ImageMagickHelperTest extends ExtendedITextTest {
57-
private final static String sourceFolder = "./src/test/resources/com/itextpdf/io/util/ImageMagickHelperTest/";
58-
private static final String destinationFolder = "./target/test/com/itextpdf/io/ImageMagickHelperTest/";
57+
private final static String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/io/util/ImageMagickHelperTest/";
58+
private static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/io/ImageMagickHelperTest/";
5959

6060
@Before
6161
public void setUp() {
62-
createOrClearDestinationFolder(destinationFolder);
62+
createOrClearDestinationFolder(DESTINATION_FOLDER);
6363
}
6464

6565
@Test
@@ -82,9 +82,9 @@ public void imageMagickEnvVarIsExplicitlySpecified() {
8282

8383
@Test
8484
public void imageMagickEnvVarIsNull() throws IOException, InterruptedException {
85-
String inputImage = sourceFolder + "image.png";
86-
String cmpImage = sourceFolder + "cmp_image.png";
87-
String diff = destinationFolder + "diff.png";
85+
String inputImage = SOURCE_FOLDER + "image.png";
86+
String cmpImage = SOURCE_FOLDER + "cmp_image.png";
87+
String diff = DESTINATION_FOLDER + "diff.png";
8888

8989
ImageMagickHelper imageMagickHelper = new ImageMagickHelper(null);
9090
boolean result = imageMagickHelper.runImageMagickImageCompare(inputImage, cmpImage, diff);
@@ -103,9 +103,9 @@ public void imageMagickEnvVarIsIncorrect() {
103103

104104
@Test
105105
public void runImageMagickForEqualImages() throws IOException, InterruptedException {
106-
String inputImage = sourceFolder + "image.png";
107-
String cmpImage = sourceFolder + "cmp_image.png";
108-
String diff = destinationFolder + "diff_equalImages.png";
106+
String inputImage = SOURCE_FOLDER + "image.png";
107+
String cmpImage = SOURCE_FOLDER + "cmp_image.png";
108+
String diff = DESTINATION_FOLDER + "diff_equalImages.png";
109109

110110
ImageMagickHelper imageMagickHelper = new ImageMagickHelper();
111111
boolean result = imageMagickHelper.runImageMagickImageCompare(inputImage, cmpImage, diff);
@@ -116,9 +116,9 @@ public void runImageMagickForEqualImages() throws IOException, InterruptedExcept
116116

117117
@Test
118118
public void runImageMagickForEqualImagesWithFuzzParam() throws IOException, InterruptedException {
119-
String inputImage = sourceFolder + "image.png";
120-
String cmpImage = sourceFolder + "cmp_image.png";
121-
String diff = destinationFolder + "diff_equalImagesFuzzParam.png";
119+
String inputImage = SOURCE_FOLDER + "image.png";
120+
String cmpImage = SOURCE_FOLDER + "cmp_image.png";
121+
String diff = DESTINATION_FOLDER + "diff_equalImagesFuzzParam.png";
122122

123123
ImageMagickHelper imageMagickHelper = new ImageMagickHelper();
124124
boolean result = imageMagickHelper.runImageMagickImageCompare(inputImage, cmpImage, diff, "0.5");
@@ -129,9 +129,9 @@ public void runImageMagickForEqualImagesWithFuzzParam() throws IOException, Inte
129129

130130
@Test
131131
public void runImageMagickForDifferentImages() throws IOException, InterruptedException {
132-
String inputImage = sourceFolder + "Im1_1.jpg";
133-
String cmpImage = sourceFolder + "cmp_Im1_1.jpg";
134-
String diff = destinationFolder + "diff_differentImages.png";
132+
String inputImage = SOURCE_FOLDER + "Im1_1.jpg";
133+
String cmpImage = SOURCE_FOLDER + "cmp_Im1_1.jpg";
134+
String diff = DESTINATION_FOLDER + "diff_differentImages.png";
135135

136136
ImageMagickHelper imageMagickHelper = new ImageMagickHelper();
137137
boolean result = imageMagickHelper.runImageMagickImageCompare(inputImage, cmpImage, diff);
@@ -142,9 +142,9 @@ public void runImageMagickForDifferentImages() throws IOException, InterruptedEx
142142

143143
@Test
144144
public void runImageMagickForDifferentImagesWithFuzzParamNotEqual() throws IOException, InterruptedException {
145-
String inputImage = sourceFolder + "Im1_1.jpg";
146-
String cmpImage = sourceFolder + "cmp_Im1_1.jpg";
147-
String diff = destinationFolder + "diff_differentImagesFuzzNotEnough.png";
145+
String inputImage = SOURCE_FOLDER + "Im1_1.jpg";
146+
String cmpImage = SOURCE_FOLDER + "cmp_Im1_1.jpg";
147+
String diff = DESTINATION_FOLDER + "diff_differentImagesFuzzNotEnough.png";
148148

149149
ImageMagickHelper imageMagickHelper = new ImageMagickHelper();
150150
boolean result = imageMagickHelper.runImageMagickImageCompare(inputImage, cmpImage, diff, "0.1");
@@ -155,9 +155,9 @@ public void runImageMagickForDifferentImagesWithFuzzParamNotEqual() throws IOExc
155155

156156
@Test
157157
public void runImageMagickForDifferentImagesWithFuzzParamEqual() throws IOException, InterruptedException {
158-
String inputImage = sourceFolder + "Im1_1.jpg";
159-
String cmpImage = sourceFolder + "cmp_Im1_1.jpg";
160-
String diff = destinationFolder + "diff_differentImagesFuzzEnough.png";
158+
String inputImage = SOURCE_FOLDER + "Im1_1.jpg";
159+
String cmpImage = SOURCE_FOLDER + "cmp_Im1_1.jpg";
160+
String diff = DESTINATION_FOLDER + "diff_differentImagesFuzzEnough.png";
161161

162162
ImageMagickHelper imageMagickHelper = new ImageMagickHelper();
163163
boolean result = imageMagickHelper.runImageMagickImageCompare(inputImage, cmpImage, diff, "2.1");

0 commit comments

Comments
 (0)