Skip to content

Commit edf666a

Browse files
introfogiText-CI
authored andcommitted
Add runProcessAndWait method overload and coresponding unit test
DEVSIX-4144
1 parent b040aed commit edf666a

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,26 +107,40 @@ public static String getPropertyOrEnvironmentVariable(String name) {
107107
}
108108

109109
public static boolean runProcessAndWait(String exec, String params) throws IOException, InterruptedException {
110-
return runProcessAndGetExitCode(exec, params) == 0;
110+
return runProcessAndWait(exec, params, null);
111+
}
112+
113+
public static boolean runProcessAndWait(String exec, String params, String workingDirPath) throws IOException, InterruptedException {
114+
return runProcessAndGetExitCode(exec, params, workingDirPath) == 0;
111115
}
112116

113117
public static int runProcessAndGetExitCode(String exec, String params) throws IOException, InterruptedException {
114-
Process p = runProcess(exec, params);
118+
return runProcessAndGetExitCode(exec, params, null);
119+
}
120+
121+
public static int runProcessAndGetExitCode(String exec, String params, String workingDirPath) throws IOException, InterruptedException {
122+
Process p = runProcess(exec, params, workingDirPath);
115123
System.out.println(getProcessOutput(p));
116124
return p.waitFor();
117125
}
118126

119127
public static String runProcessAndGetOutput(String command, String params) throws IOException {
120-
return getProcessOutput(runProcess(command, params));
128+
return getProcessOutput(runProcess(command, params, null));
121129
}
122130

123131
public static StringBuilder runProcessAndCollectErrors(String execPath, String params) throws IOException {
124-
return printProcessErrorsOutput(runProcess(execPath, params));
132+
return printProcessErrorsOutput(runProcess(execPath, params, null));
125133
}
126134

127-
static Process runProcess(String execPath, String params) throws IOException {
135+
static Process runProcess(String execPath, String params, String workingDirPath) throws IOException {
128136
List<String> cmdList = prepareProcessArguments(execPath, params);
129-
return Runtime.getRuntime().exec(cmdList.toArray(new String[cmdList.size()]));
137+
String[] cmdArray = cmdList.toArray(new String[cmdList.size()]);
138+
if (workingDirPath != null) {
139+
File workingDir = new File(workingDirPath);
140+
return Runtime.getRuntime().exec(cmdArray, null, workingDir);
141+
} else {
142+
return Runtime.getRuntime().exec(cmdArray);
143+
}
130144
}
131145

132146
static List<String> prepareProcessArguments(String exec, String params) {

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,30 @@ This file is part of the iText (R) project.
3030
import java.io.IOException;
3131
import java.io.InputStream;
3232
import java.io.OutputStream;
33+
3334
import java.util.Arrays;
3435
import java.util.Collections;
3536
import java.util.List;
37+
3638
import org.junit.Assert;
39+
import org.junit.Before;
3740
import org.junit.Test;
3841
import org.junit.experimental.categories.Category;
3942

4043
@Category(UnitTest.class)
4144
public class SystemUtilTest extends ExtendedITextTest {
42-
4345
private final static String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/io/util/SystemUtilTest/";
4446

47+
private static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/io/util/SystemUtilTest/";
48+
4549
// This is empty file that used to check the logic for existed execution file
4650
private final static String STUB_EXEC_FILE = SOURCE_FOLDER + "folder with space/stubFile";
4751

52+
@Before
53+
public void setUp() {
54+
createOrClearDestinationFolder(DESTINATION_FOLDER);
55+
}
56+
4857
@Test
4958
public void prepareProcessArgumentsStubExecFileTest() {
5059
List<String> processArguments = SystemUtil.prepareProcessArguments(STUB_EXEC_FILE, "param1 param2");
@@ -133,6 +142,28 @@ public void getProcessOutputEmptyTest() throws IOException {
133142
Assert.assertEquals("This is error info", result);
134143
}
135144

145+
@Test
146+
public void runProcessAndWaitWithWorkingDirectoryTest() throws IOException, InterruptedException {
147+
String imageMagickPath = SystemUtil.getPropertyOrEnvironmentVariable(ImageMagickHelper.MAGICK_COMPARE_ENVIRONMENT_VARIABLE);
148+
if (imageMagickPath == null) {
149+
imageMagickPath = SystemUtil.getPropertyOrEnvironmentVariable(ImageMagickHelper.MAGICK_COMPARE_ENVIRONMENT_VARIABLE_LEGACY);
150+
}
151+
String inputImage = "image.jpg";
152+
String cmpImage = "cmp_image.jpg";
153+
String diff = System.getProperty("user.dir") + DESTINATION_FOLDER.substring(1) + "diff.png";
154+
155+
StringBuilder currCompareParams = new StringBuilder();
156+
currCompareParams
157+
.append("'")
158+
.append(inputImage).append("' '")
159+
.append(cmpImage).append("' '")
160+
.append(diff).append("'");
161+
boolean result = SystemUtil.runProcessAndWait(imageMagickPath, currCompareParams.toString(), SOURCE_FOLDER);
162+
163+
Assert.assertFalse(result);
164+
Assert.assertTrue(FileUtil.fileExists(diff));
165+
}
166+
136167

137168
static class TestProcess extends Process {
138169

10.9 KB
Loading
12.7 KB
Loading

0 commit comments

Comments
 (0)