Skip to content

Commit 97f4f07

Browse files
Ville Heikkinenpuny
authored andcommitted
Refactor MavenExerciseRunner
1 parent 41aef22 commit 97f4f07

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed

tmc-plugin/src/fi/helsinki/cs/tmc/runners/AntExerciseRunner.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ public Callable<Optional<TestRunResult>> getTestRunningTask(final TmcProjectInfo
4040
@Override
4141
public Optional<TestRunResult> call() throws Exception {
4242

43-
log.log(INFO,
44-
"Starting compile");
43+
log.log(INFO, "Starting compile");
4544

4645
Project project = projectInfo.getProject();
4746
FileObject buildScript = project.getProjectDirectory().getFileObject("build.xml");
@@ -63,15 +62,15 @@ public Optional<TestRunResult> call() throws Exception {
6362
int compileResult = task.result();
6463
if (compileResult == 0) {
6564
log.log(INFO, "Compile success for project {0}", projectInfo.toString());
66-
return Optional.of(runTests(projectInfo));
65+
return Optional.of(runTestTask(projectInfo));
6766
} else {
6867
return Optional.absent();
6968
}
7069
}
7170
};
7271
}
7372

74-
protected TestRunResult runTests(final TmcProjectInfo projectInfo) throws UserVisibleException, IOException, InterruptedException, ExecutionException {
73+
protected TestRunResult runTestTask(final TmcProjectInfo projectInfo) throws UserVisibleException, IOException, InterruptedException, ExecutionException {
7574

7675
FileObject testDir = findTestDir(projectInfo);
7776
if (testDir == null) {
Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/**
2-
* package fi.helsinki.cs.tmc.runners;
1+
package fi.helsinki.cs.tmc.runners;
32

3+
import com.google.common.base.Optional;
44
import fi.helsinki.cs.tmc.data.Exercise;
55
import fi.helsinki.cs.tmc.data.TestRunResult;
66
import fi.helsinki.cs.tmc.model.TmcProjectInfo;
@@ -14,47 +14,53 @@
1414
import java.util.List;
1515
import java.util.Map;
1616
import java.util.concurrent.Callable;
17-
import java.util.logging.Level;
17+
import static java.util.logging.Level.INFO;
18+
import java.util.logging.Logger;
1819
import org.apache.commons.lang3.StringUtils;
1920
import org.openide.windows.IOProvider;
2021
import org.openide.windows.InputOutput;
2122

2223
public class MavenExerciseRunner extends AbstractJavaExerciseRunner {
24+
2325
private static final String MAVEN_TEST_RUN_GOAL = "fi.helsinki.cs.tmc:tmc-maven-plugin:1.6:test";
26+
private static final Logger log = Logger.getLogger(MavenExerciseRunner.class.getName());
2427

2528
@Override
26-
public Callable<Integer> getCompilingTask(TmcProjectInfo projectInfo) {
27-
File projectDir = projectInfo.getProjectDirAsFile();
28-
29-
String goal = "test-compile";
30-
final InputOutput inOut = IOProvider.getDefault().getIO(projectInfo.getProjectName(), false);
31-
32-
final ProcessRunner runner = new MavenRunBuilder()
33-
.setProjectDir(projectDir)
34-
.addGoal(goal)
35-
.setIO(inOut)
36-
.createProcessRunner();
37-
38-
return new Callable<Integer>() {
29+
public Callable<Optional<TestRunResult>> getTestRunningTask(final TmcProjectInfo projectInfo) {
30+
return new Callable<Optional<TestRunResult>>() {
3931
@Override
40-
public Integer call() throws Exception {
32+
public Optional<TestRunResult> call() throws Exception {
33+
File projectDir = projectInfo.getProjectDirAsFile();
34+
log.log(INFO, "Starting compile");
35+
String goal = "test-compile";
36+
final InputOutput inOut = IOProvider.getDefault().getIO(projectInfo.getProjectName(), false);
37+
38+
final ProcessRunner runner = new MavenRunBuilder()
39+
.setProjectDir(projectDir)
40+
.addGoal(goal)
41+
.setIO(inOut)
42+
.createProcessRunner();
4143
try {
4244
ProcessResult result = runner.call();
4345
int ret = result.statusCode;
4446
if (ret != 0) {
4547
inOut.select();
48+
log.log(INFO, "Compile resulted in non-zero exit code {0}", result.statusCode);
49+
return Optional.absent();
50+
} else {
51+
log.log(INFO, "Running tests");
52+
return Optional.of(runTests(projectInfo));
4653
}
47-
return ret;
4854
} catch (Exception ex) {
4955
inOut.select();
5056
throw ex;
5157
}
5258
}
59+
5360
};
5461
}
5562

56-
@Override
57-
public Callable<TestRunResult> getTestRunningTask(final TmcProjectInfo projectInfo) {
63+
public TestRunResult runTests(final TmcProjectInfo projectInfo) throws Exception {
5864
final File projectDir = projectInfo.getProjectDirAsFile();
5965
String goal = MAVEN_TEST_RUN_GOAL;
6066
Map<String, String> props = new HashMap<String, String>();
@@ -83,17 +89,11 @@ public Callable<TestRunResult> getTestRunningTask(final TmcProjectInfo projectIn
8389
.setIO(inOut)
8490
.createProcessRunner();
8591

86-
return new Callable<TestRunResult>() {
87-
@Override
88-
public TestRunResult call() throws Exception {
89-
runner.call();
90-
File resultsFile = new File(
91-
projectDir.getPath() + File.separator
92-
+ "target" + File.separator
93-
+ "test_output.txt");
94-
return resultParser.parseTestResults(resultsFile);
95-
}
96-
};
92+
runner.call();
93+
File resultsFile = new File(
94+
projectDir.getPath() + File.separator
95+
+ "target" + File.separator
96+
+ "test_output.txt");
97+
return resultParser.parseTestResults(resultsFile);
9798
}
9899
}
99-
*/

tmc-plugin/src/fi/helsinki/cs/tmc/runners/TestRunHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ public void bgTaskCancelled() {
9090
private AbstractExerciseRunner getRunner(TmcProjectInfo projectInfo) {
9191
switch (projectInfo.getProjectType()) {
9292
case JAVA_MAVEN:
93-
return null;
93+
log.log(INFO, "Maven task selected");
94+
return new MavenExerciseRunner();
9495
case JAVA_SIMPLE:
9596
log.log(INFO, "Ant task selected");
9697
return new AntExerciseRunner();

0 commit comments

Comments
 (0)