Skip to content

Commit ad6a9c6

Browse files
authored
Merge pull request #72 from testmycode/java-fixes
Make ant projects to use ProcessRunner
2 parents c0495eb + 1f83df0 commit ad6a9c6

File tree

4 files changed

+17
-34
lines changed

4 files changed

+17
-34
lines changed

tmc-langs-framework/src/main/java/fi/helsinki/cs/tmc/langs/utils/ProcessRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public ProcessRunner(String[] command, Path workDir) {
2828
}
2929

3030
@Override
31-
public ProcessResult call() throws Exception {
31+
public ProcessResult call() throws IOException, InterruptedException {
3232
Process process = null;
3333
try {
3434
ProcessBuilder processBuilder = new ProcessBuilder(command);

tmc-langs-java/src/main/java/fi/helsinki/cs/tmc/langs/java/ant/AntPlugin.java

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import fi.helsinki.cs.tmc.langs.java.TestRunFileAndLogs;
1212
import fi.helsinki.cs.tmc.langs.java.exception.TestRunnerException;
1313
import fi.helsinki.cs.tmc.langs.java.exception.TestScannerException;
14+
import fi.helsinki.cs.tmc.langs.utils.ProcessResult;
15+
import fi.helsinki.cs.tmc.langs.utils.ProcessRunner;
1416

1517
import com.google.common.base.Optional;
1618
import com.google.common.base.Preconditions;
@@ -25,20 +27,16 @@
2527
import org.slf4j.Logger;
2628
import org.slf4j.LoggerFactory;
2729

28-
import java.io.BufferedReader;
2930
import java.io.File;
3031
import java.io.IOException;
3132
import java.io.InputStream;
32-
import java.io.InputStreamReader;
3333
import java.io.PrintStream;
3434
import java.nio.charset.Charset;
3535
import java.nio.file.Files;
3636
import java.nio.file.LinkOption;
3737
import java.nio.file.Path;
3838
import java.nio.file.Paths;
3939
import java.nio.file.StandardCopyOption;
40-
import java.util.List;
41-
import java.util.Map;
4240

4341
/**
4442
* A {@link fi.helsinki.cs.tmc.langs.LanguagePlugin} that defines the behaviour
@@ -211,37 +209,21 @@ protected TestRunFileAndLogs createRunResultFile(Path projectBasePath)
211209
resultFile,
212210
classPath,
213211
exercise.get());
214-
List<String> testRunnerArguments = argumentBuilder.getArguments();
215212

216-
StringBuilder stdout = new StringBuilder();
217-
StringBuilder stderr = new StringBuilder();
213+
ProcessRunner runner = new ProcessRunner(argumentBuilder.getCommand(), projectBasePath);
218214

219215
try {
220-
Process process = new ProcessBuilder(testRunnerArguments).start();
221-
process.waitFor();
222-
final BufferedReader output =
223-
new BufferedReader(new InputStreamReader(process.getInputStream()));
224-
final BufferedReader error =
225-
new BufferedReader(new InputStreamReader(process.getErrorStream()));
226-
227-
String line;
228-
while ((line = output.readLine()) != null) {
229-
stdout.append(line);
230-
}
231-
while ((line = error.readLine()) != null) {
232-
stderr.append(line);
233-
}
216+
ProcessResult result = runner.call();
217+
log.info("Successfully ran tests for project at {}", projectBasePath);
218+
return new TestRunFileAndLogs(
219+
resultFile.toFile(),
220+
result.output.getBytes(Charset.forName("UTF-8")),
221+
result.errorOutput.getBytes(Charset.forName("UTF-8"))
222+
);
234223
} catch (InterruptedException | IOException e) {
235224
log.error("Failed to run tests", e);
236225
throw new TestRunnerException(e);
237226
}
238-
239-
log.info("Successfully ran tests for project at {}", projectBasePath);
240-
241-
return new TestRunFileAndLogs(
242-
resultFile.toFile(),
243-
stdout.toString().getBytes(Charset.forName("UTF-8")),
244-
stderr.toString().getBytes(Charset.forName("UTF-8")));
245227
}
246228

247229
private String getJvmOptions(Path projectBasePath) {

tmc-langs-java/src/main/java/fi/helsinki/cs/tmc/langs/java/ant/TestRunnerArgumentBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ private List<String> createTestCaseArgumentList(ExerciseDesc exercise) {
8686
}
8787

8888
/**
89-
* Get the arguments as a list of Strings, usable with ProcessBuilder.
89+
* Get the command with the arguments as a string array. This can be used to start the process.
9090
*/
91-
public List<String> getArguments() {
92-
return arguments;
91+
public String[] getCommand() {
92+
return arguments.toArray(new String[0]);
9393
}
9494
}

tmc-langs-java/src/main/java/fi/helsinki/cs/tmc/langs/java/maven/MavenInvokatorMavenTaskRunner.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,12 @@ public void consumeLine(String line) {
9797

9898
private Path useBundledMaven() {
9999
Path mavenHome = getConfigDirectory();
100-
if (Files.exists(mavenHome)) {
100+
Path extractedMavenLocation = mavenHome.resolve("apache-maven-3.3.9");
101+
if (Files.exists(extractedMavenLocation)) {
101102
log.info("Maven already extracted");
102103

103104
// Add the name of the extracted folder to the path
104-
return mavenHome.resolve("apache-maven-3.3.9");
105+
return extractedMavenLocation;
105106
}
106107
log.info("Maven bundle not previously extracted, extracting...");
107108
try {

0 commit comments

Comments
 (0)