Skip to content

Commit f0ffe30

Browse files
yulian-gaponenkoiText-CI
authored andcommitted
Improve GhostScript and ImageMagick util classes formatting and their tests
QA-12194 Autoported commit. Original commit hash: [9a47f5f69]
1 parent e4d28f4 commit f0ffe30

File tree

5 files changed

+89
-52
lines changed

5 files changed

+89
-52
lines changed

itext.tests/itext.io.tests/itext/io/util/GhostscriptHelperTest.cs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ source product.
4747

4848
namespace iText.IO.Util {
4949
public class GhostscriptHelperTest : ExtendedITextTest {
50-
private static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
50+
private static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
5151
.CurrentContext.TestDirectory) + "/resources/itext/io/util/GhostscriptHelperTest/";
5252

53-
private static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
53+
private static readonly String DESTINATION_FOLDER = NUnit.Framework.TestContext.CurrentContext.TestDirectory
5454
+ "/test/itext/io/GhostscriptHelperTest/";
5555

5656
[NUnit.Framework.SetUp]
5757
public virtual void SetUp() {
58-
CreateOrClearDestinationFolder(destinationFolder);
58+
CreateOrClearDestinationFolder(DESTINATION_FOLDER);
5959
}
6060

6161
[NUnit.Framework.Test]
@@ -88,7 +88,7 @@ public virtual void GhostScriptEnvVarIsIncorrect() {
8888

8989
[NUnit.Framework.Test]
9090
public virtual void RunGhostScriptIncorrectOutputDirectory() {
91-
String inputPdf = sourceFolder + "imageHandlerUtilTest.pdf";
91+
String inputPdf = SOURCE_FOLDER + "imageHandlerUtilTest.pdf";
9292
String exceptionMessage = "Cannot open output directory for " + inputPdf;
9393
GhostscriptHelper ghostscriptHelper = new GhostscriptHelper();
9494
Exception e = NUnit.Framework.Assert.Catch(typeof(ArgumentException), () => ghostscriptHelper.RunGhostScriptImageGeneration
@@ -98,61 +98,61 @@ public virtual void RunGhostScriptIncorrectOutputDirectory() {
9898

9999
[NUnit.Framework.Test]
100100
public virtual void RunGhostScriptIncorrectParams() {
101-
String inputPdf = sourceFolder + "imageHandlerUtilTest.pdf";
101+
String inputPdf = SOURCE_FOLDER + "imageHandlerUtilTest.pdf";
102102
String exceptionMessage = "GhostScript failed for " + inputPdf;
103103
GhostscriptHelper ghostscriptHelper = new GhostscriptHelper();
104104
Exception e = NUnit.Framework.Assert.Catch(typeof(GhostscriptHelper.GhostscriptExecutionException), () =>
105-
ghostscriptHelper.RunGhostScriptImageGeneration(inputPdf, destinationFolder, "outputPageImage.png", "q@W"
105+
ghostscriptHelper.RunGhostScriptImageGeneration(inputPdf, DESTINATION_FOLDER, "outputPageImage.png", "q@W"
106106
));
107107
NUnit.Framework.Assert.AreEqual(exceptionMessage, e.Message);
108108
}
109109

110110
[NUnit.Framework.Test]
111111
public virtual void RunGhostScriptTestForSpecificPage() {
112-
String inputPdf = sourceFolder + "imageHandlerUtilTest.pdf";
112+
String inputPdf = SOURCE_FOLDER + "imageHandlerUtilTest.pdf";
113113
GhostscriptHelper ghostscriptHelper = new GhostscriptHelper();
114-
ghostscriptHelper.RunGhostScriptImageGeneration(inputPdf, destinationFolder, "specificPage.png", "1");
115-
NUnit.Framework.Assert.AreEqual(1, FileUtil.ListFilesInDirectory(destinationFolder, true).Length);
116-
NUnit.Framework.Assert.IsTrue(FileUtil.FileExists(destinationFolder + "specificPage.png"));
114+
ghostscriptHelper.RunGhostScriptImageGeneration(inputPdf, DESTINATION_FOLDER, "specificPage.png", "1");
115+
NUnit.Framework.Assert.AreEqual(1, FileUtil.ListFilesInDirectory(DESTINATION_FOLDER, true).Length);
116+
NUnit.Framework.Assert.IsTrue(FileUtil.FileExists(DESTINATION_FOLDER + "specificPage.png"));
117117
}
118118

119119
[NUnit.Framework.Test]
120120
public virtual void RunGhostScriptTestForSeveralSpecificPages() {
121-
String inputPdf = sourceFolder + "imageHandlerUtilTest.pdf";
121+
String inputPdf = SOURCE_FOLDER + "imageHandlerUtilTest.pdf";
122122
GhostscriptHelper ghostscriptHelper = new GhostscriptHelper();
123123
String imageFileName = new FileInfo(inputPdf).Name + "_severalSpecificPages-%03d.png";
124-
ghostscriptHelper.RunGhostScriptImageGeneration(inputPdf, destinationFolder, imageFileName, "1,3");
125-
NUnit.Framework.Assert.AreEqual(2, FileUtil.ListFilesInDirectory(destinationFolder, true).Length);
126-
NUnit.Framework.Assert.IsTrue(FileUtil.FileExists(destinationFolder + "imageHandlerUtilTest.pdf_severalSpecificPages-001.png"
124+
ghostscriptHelper.RunGhostScriptImageGeneration(inputPdf, DESTINATION_FOLDER, imageFileName, "1,3");
125+
NUnit.Framework.Assert.AreEqual(2, FileUtil.ListFilesInDirectory(DESTINATION_FOLDER, true).Length);
126+
NUnit.Framework.Assert.IsTrue(FileUtil.FileExists(DESTINATION_FOLDER + "imageHandlerUtilTest.pdf_severalSpecificPages-001.png"
127127
));
128-
NUnit.Framework.Assert.IsTrue(FileUtil.FileExists(destinationFolder + "imageHandlerUtilTest.pdf_severalSpecificPages-002.png"
128+
NUnit.Framework.Assert.IsTrue(FileUtil.FileExists(DESTINATION_FOLDER + "imageHandlerUtilTest.pdf_severalSpecificPages-002.png"
129129
));
130130
}
131131

132132
[NUnit.Framework.Test]
133133
public virtual void RunGhostScriptTestForAllPages() {
134-
String inputPdf = sourceFolder + "imageHandlerUtilTest.pdf";
134+
String inputPdf = SOURCE_FOLDER + "imageHandlerUtilTest.pdf";
135135
GhostscriptHelper ghostscriptHelper = new GhostscriptHelper();
136136
String imageFileName = new FileInfo(inputPdf).Name + "_allPages-%03d.png";
137-
ghostscriptHelper.RunGhostScriptImageGeneration(inputPdf, destinationFolder, imageFileName);
138-
NUnit.Framework.Assert.AreEqual(3, FileUtil.ListFilesInDirectory(destinationFolder, true).Length);
139-
NUnit.Framework.Assert.IsTrue(FileUtil.FileExists(destinationFolder + "imageHandlerUtilTest.pdf_allPages-001.png"
137+
ghostscriptHelper.RunGhostScriptImageGeneration(inputPdf, DESTINATION_FOLDER, imageFileName);
138+
NUnit.Framework.Assert.AreEqual(3, FileUtil.ListFilesInDirectory(DESTINATION_FOLDER, true).Length);
139+
NUnit.Framework.Assert.IsTrue(FileUtil.FileExists(DESTINATION_FOLDER + "imageHandlerUtilTest.pdf_allPages-001.png"
140140
));
141-
NUnit.Framework.Assert.IsTrue(FileUtil.FileExists(destinationFolder + "imageHandlerUtilTest.pdf_allPages-002.png"
141+
NUnit.Framework.Assert.IsTrue(FileUtil.FileExists(DESTINATION_FOLDER + "imageHandlerUtilTest.pdf_allPages-002.png"
142142
));
143-
NUnit.Framework.Assert.IsTrue(FileUtil.FileExists(destinationFolder + "imageHandlerUtilTest.pdf_allPages-003.png"
143+
NUnit.Framework.Assert.IsTrue(FileUtil.FileExists(DESTINATION_FOLDER + "imageHandlerUtilTest.pdf_allPages-003.png"
144144
));
145145
}
146146

147147
[NUnit.Framework.Test]
148148
public virtual void DSaferParamInGhostScriptHelperTest() {
149-
String input = sourceFolder + "unsafePostScript.ps";
149+
String input = SOURCE_FOLDER + "unsafePostScript.ps";
150150
String outputName = "unsafePostScript.png";
151-
String maliciousResult1 = destinationFolder + "output1.txt";
152-
String maliciousResult2 = destinationFolder + "output2.txt";
151+
String maliciousResult1 = DESTINATION_FOLDER + "output1.txt";
152+
String maliciousResult2 = DESTINATION_FOLDER + "output2.txt";
153153
try {
154154
GhostscriptHelper ghostscriptHelper = new GhostscriptHelper();
155-
ghostscriptHelper.RunGhostScriptImageGeneration(input, destinationFolder, outputName);
155+
ghostscriptHelper.RunGhostScriptImageGeneration(input, DESTINATION_FOLDER, outputName);
156156
}
157157
catch (GhostscriptHelper.GhostscriptExecutionException) {
158158
System.Console.Out.WriteLine("Error code was returned on processing of malicious script with -dSAFER option enabled. "
@@ -166,12 +166,12 @@ public virtual void DSaferParamInGhostScriptHelperTest() {
166166
[NUnit.Framework.Test]
167167
public virtual void GhostScriptImageGenerationTest() {
168168
String filename = "resultantImage.png";
169-
String psFile = sourceFolder + "simple.ps";
170-
String resultantImage = destinationFolder + filename;
171-
String cmpResultantImage = sourceFolder + "cmp_" + filename;
172-
String diff = destinationFolder + "diff_" + filename;
169+
String psFile = SOURCE_FOLDER + "simple.ps";
170+
String resultantImage = DESTINATION_FOLDER + filename;
171+
String cmpResultantImage = SOURCE_FOLDER + "cmp_" + filename;
172+
String diff = DESTINATION_FOLDER + "diff_" + filename;
173173
GhostscriptHelper ghostscriptHelper = new GhostscriptHelper();
174-
ghostscriptHelper.RunGhostScriptImageGeneration(psFile, destinationFolder, filename);
174+
ghostscriptHelper.RunGhostScriptImageGeneration(psFile, DESTINATION_FOLDER, filename);
175175
NUnit.Framework.Assert.IsTrue(FileUtil.FileExists(resultantImage));
176176
ImageMagickHelper imageMagickHelper = new ImageMagickHelper();
177177
NUnit.Framework.Assert.IsTrue(imageMagickHelper.RunImageMagickImageCompare(resultantImage, cmpResultantImage

itext.tests/itext.io.tests/itext/io/util/ImageMagickHelperTest.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ source product.
4646

4747
namespace iText.IO.Util {
4848
public class ImageMagickHelperTest : ExtendedITextTest {
49-
private static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
49+
private static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
5050
.CurrentContext.TestDirectory) + "/resources/itext/io/util/ImageMagickHelperTest/";
5151

52-
private static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
52+
private static readonly String DESTINATION_FOLDER = NUnit.Framework.TestContext.CurrentContext.TestDirectory
5353
+ "/test/itext/io/ImageMagickHelperTest/";
5454

5555
[NUnit.Framework.SetUp]
5656
public virtual void SetUp() {
57-
CreateOrClearDestinationFolder(destinationFolder);
57+
CreateOrClearDestinationFolder(DESTINATION_FOLDER);
5858
}
5959

6060
[NUnit.Framework.Test]
@@ -77,9 +77,9 @@ public virtual void ImageMagickEnvVarIsExplicitlySpecified() {
7777

7878
[NUnit.Framework.Test]
7979
public virtual void ImageMagickEnvVarIsNull() {
80-
String inputImage = sourceFolder + "image.png";
81-
String cmpImage = sourceFolder + "cmp_image.png";
82-
String diff = destinationFolder + "diff.png";
80+
String inputImage = SOURCE_FOLDER + "image.png";
81+
String cmpImage = SOURCE_FOLDER + "cmp_image.png";
82+
String diff = DESTINATION_FOLDER + "diff.png";
8383
ImageMagickHelper imageMagickHelper = new ImageMagickHelper(null);
8484
bool result = imageMagickHelper.RunImageMagickImageCompare(inputImage, cmpImage, diff);
8585
NUnit.Framework.Assert.IsTrue(result);
@@ -94,9 +94,9 @@ public virtual void ImageMagickEnvVarIsIncorrect() {
9494

9595
[NUnit.Framework.Test]
9696
public virtual void RunImageMagickForEqualImages() {
97-
String inputImage = sourceFolder + "image.png";
98-
String cmpImage = sourceFolder + "cmp_image.png";
99-
String diff = destinationFolder + "diff_equalImages.png";
97+
String inputImage = SOURCE_FOLDER + "image.png";
98+
String cmpImage = SOURCE_FOLDER + "cmp_image.png";
99+
String diff = DESTINATION_FOLDER + "diff_equalImages.png";
100100
ImageMagickHelper imageMagickHelper = new ImageMagickHelper();
101101
bool result = imageMagickHelper.RunImageMagickImageCompare(inputImage, cmpImage, diff);
102102
NUnit.Framework.Assert.IsTrue(result);
@@ -105,9 +105,9 @@ public virtual void RunImageMagickForEqualImages() {
105105

106106
[NUnit.Framework.Test]
107107
public virtual void RunImageMagickForEqualImagesWithFuzzParam() {
108-
String inputImage = sourceFolder + "image.png";
109-
String cmpImage = sourceFolder + "cmp_image.png";
110-
String diff = destinationFolder + "diff_equalImagesFuzzParam.png";
108+
String inputImage = SOURCE_FOLDER + "image.png";
109+
String cmpImage = SOURCE_FOLDER + "cmp_image.png";
110+
String diff = DESTINATION_FOLDER + "diff_equalImagesFuzzParam.png";
111111
ImageMagickHelper imageMagickHelper = new ImageMagickHelper();
112112
bool result = imageMagickHelper.RunImageMagickImageCompare(inputImage, cmpImage, diff, "0.5");
113113
NUnit.Framework.Assert.IsTrue(result);
@@ -116,9 +116,9 @@ public virtual void RunImageMagickForEqualImagesWithFuzzParam() {
116116

117117
[NUnit.Framework.Test]
118118
public virtual void RunImageMagickForDifferentImages() {
119-
String inputImage = sourceFolder + "Im1_1.jpg";
120-
String cmpImage = sourceFolder + "cmp_Im1_1.jpg";
121-
String diff = destinationFolder + "diff_differentImages.png";
119+
String inputImage = SOURCE_FOLDER + "Im1_1.jpg";
120+
String cmpImage = SOURCE_FOLDER + "cmp_Im1_1.jpg";
121+
String diff = DESTINATION_FOLDER + "diff_differentImages.png";
122122
ImageMagickHelper imageMagickHelper = new ImageMagickHelper();
123123
bool result = imageMagickHelper.RunImageMagickImageCompare(inputImage, cmpImage, diff);
124124
NUnit.Framework.Assert.IsFalse(result);
@@ -127,9 +127,9 @@ public virtual void RunImageMagickForDifferentImages() {
127127

128128
[NUnit.Framework.Test]
129129
public virtual void RunImageMagickForDifferentImagesWithFuzzParamNotEqual() {
130-
String inputImage = sourceFolder + "Im1_1.jpg";
131-
String cmpImage = sourceFolder + "cmp_Im1_1.jpg";
132-
String diff = destinationFolder + "diff_differentImagesFuzzNotEnough.png";
130+
String inputImage = SOURCE_FOLDER + "Im1_1.jpg";
131+
String cmpImage = SOURCE_FOLDER + "cmp_Im1_1.jpg";
132+
String diff = DESTINATION_FOLDER + "diff_differentImagesFuzzNotEnough.png";
133133
ImageMagickHelper imageMagickHelper = new ImageMagickHelper();
134134
bool result = imageMagickHelper.RunImageMagickImageCompare(inputImage, cmpImage, diff, "0.1");
135135
NUnit.Framework.Assert.IsFalse(result);
@@ -138,9 +138,9 @@ public virtual void RunImageMagickForDifferentImagesWithFuzzParamNotEqual() {
138138

139139
[NUnit.Framework.Test]
140140
public virtual void RunImageMagickForDifferentImagesWithFuzzParamEqual() {
141-
String inputImage = sourceFolder + "Im1_1.jpg";
142-
String cmpImage = sourceFolder + "cmp_Im1_1.jpg";
143-
String diff = destinationFolder + "diff_differentImagesFuzzEnough.png";
141+
String inputImage = SOURCE_FOLDER + "Im1_1.jpg";
142+
String cmpImage = SOURCE_FOLDER + "cmp_Im1_1.jpg";
143+
String diff = DESTINATION_FOLDER + "diff_differentImagesFuzzEnough.png";
144144
ImageMagickHelper imageMagickHelper = new ImageMagickHelper();
145145
bool result = imageMagickHelper.RunImageMagickImageCompare(inputImage, cmpImage, diff, "2.1");
146146
NUnit.Framework.Assert.IsTrue(result);

itext/itext.io/itext/io/util/GhostscriptHelper.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ source product.
4545
using iText.IO;
4646

4747
namespace iText.IO.Util {
48+
/// <summary>A utility class that is used as an interface to run 3rd-party tool Ghostscript.</summary>
49+
/// <remarks>
50+
/// A utility class that is used as an interface to run 3rd-party tool Ghostscript.
51+
/// Ghostscript is an interpreter for the PostScript language and PDF files, it allows to render them
52+
/// as images.
53+
/// <para />
54+
/// The Ghostscript needs to be installed independently on the system. This class provides a convenient
55+
/// way to run it by passing a terminal command. The command can either be specified explicitly or by a mean
56+
/// of environment variable
57+
/// <see cref="GHOSTSCRIPT_ENVIRONMENT_VARIABLE"/>.
58+
/// </remarks>
4859
public class GhostscriptHelper {
4960
/// <summary>The name of the environment variable with the command to execute Ghostscript operations.</summary>
5061
public const String GHOSTSCRIPT_ENVIRONMENT_VARIABLE = "ITEXT_GS_EXEC";
@@ -58,10 +69,18 @@ public class GhostscriptHelper {
5869

5970
private String gsExec;
6071

72+
/// <summary>
73+
/// Creates new instance that will rely on Ghostscript execution command defined by
74+
/// <see cref="GHOSTSCRIPT_ENVIRONMENT_VARIABLE"/>
75+
/// environment variable.
76+
/// </summary>
6177
public GhostscriptHelper()
6278
: this(null) {
6379
}
6480

81+
/// <summary>Creates new instance that will rely on Ghostscript execution command defined as passed argument.</summary>
82+
/// <param name="newGsExec">the Ghostscript execution command; if null - environment variables will be used instead
83+
/// </param>
6584
public GhostscriptHelper(String newGsExec) {
6685
gsExec = newGsExec;
6786
if (gsExec == null) {

itext/itext.io/itext/io/util/ImageMagickHelper.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ source product.
4646
using iText.IO;
4747

4848
namespace iText.IO.Util {
49+
/// <summary>A utility class that is used as an interface to run 3rd-party tool ImageMagick.</summary>
50+
/// <remarks>
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+
/// <para />
54+
/// The ImageMagick needs to be installed independently on the system. This class provides a convenient
55+
/// way to run it by passing a terminal command. The command can either be specified explicitly or by a mean
56+
/// of environment variable
57+
/// <see cref="MAGICK_COMPARE_ENVIRONMENT_VARIABLE"/>.
58+
/// </remarks>
4959
public class ImageMagickHelper {
5060
/// <summary>The name of the environment variable with the command to execute ImageMagic comparison operations.
5161
/// </summary>
@@ -58,10 +68,18 @@ public class ImageMagickHelper {
5868

5969
private String compareExec;
6070

71+
/// <summary>
72+
/// Creates new instance that will rely on ImageMagick execution command defined by
73+
/// <see cref="MAGICK_COMPARE_ENVIRONMENT_VARIABLE"/>
74+
/// environment variable.
75+
/// </summary>
6176
public ImageMagickHelper()
6277
: this(null) {
6378
}
6479

80+
/// <summary>Creates new instance that will rely on ImageMagick execution command defined as passed argument.</summary>
81+
/// <param name="newCompareExec">the ImageMagick execution command; if null - environment variables will be used instead
82+
/// </param>
6583
public ImageMagickHelper(String newCompareExec) {
6684
compareExec = newCompareExec;
6785
if (compareExec == null) {

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ea6918d622843d25251d3e4a4f097d1de1babb1e
1+
9a47f5f698e7cfccda49810bff84abcd45368154

0 commit comments

Comments
 (0)