Skip to content

Commit 3a792a1

Browse files
author
puny
committed
Fixes for PR#106
1 parent 9b38f33 commit 3a792a1

File tree

6 files changed

+89
-80
lines changed

6 files changed

+89
-80
lines changed

tmc-plugin/src/fi/helsinki/cs/tmc/data/TestRunResult.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
package fi.helsinki.cs.tmc.data;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45

56
public class TestRunResult {
7+
public static enum Status {
8+
PASSED,
9+
COMPILE_FAILED
10+
}
11+
612
private final List<TestCaseResult> testCaseResults;
13+
public final Status status;
714

815
public TestRunResult(List<TestCaseResult> testCaseResults) {
16+
this.status = Status.PASSED;
17+
this.testCaseResults = testCaseResults;
18+
}
19+
20+
public TestRunResult(Status status) {
21+
this.status = status;
22+
this.testCaseResults = new ArrayList<TestCaseResult>();
23+
}
24+
25+
public TestRunResult(List<TestCaseResult> testCaseResults, Status status) {
926
this.testCaseResults = testCaseResults;
27+
this.status = status;
1028
}
1129

1230
public List<TestCaseResult> getTestCaseResults() {

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

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package fi.helsinki.cs.tmc.runners;
22

3-
import com.google.common.base.Optional;
43
import fi.helsinki.cs.tmc.data.Exercise;
54
import fi.helsinki.cs.tmc.data.TestRunResult;
5+
import fi.helsinki.cs.tmc.data.TestRunResult.Status;
66
import fi.helsinki.cs.tmc.model.TmcProjectInfo;
77
import fi.helsinki.cs.tmc.model.UserVisibleException;
88
import fi.helsinki.cs.tmc.testscanner.TestMethod;
@@ -19,58 +19,57 @@
1919
import java.util.concurrent.ExecutionException;
2020
import java.util.concurrent.Future;
2121
import java.util.logging.Level;
22-
import static java.util.logging.Level.INFO;
2322
import java.util.logging.Logger;
2423
import org.apache.tools.ant.module.api.support.ActionUtils;
2524
import org.netbeans.api.java.classpath.ClassPath;
2625
import org.netbeans.api.project.Project;
2726
import org.openide.execution.ExecutorTask;
2827
import org.openide.filesystems.FileObject;
2928
import org.openide.filesystems.FileUtil;
30-
import org.openide.util.Exceptions;
3129
import org.openide.windows.InputOutput;
3230

31+
3332
public class AntExerciseRunner extends AbstractJavaExerciseRunner {
3433

3534
private static final Logger log = Logger.getLogger(AntExerciseRunner.class.getName());
35+
private static final Integer SUCCESS = 0;
3636

3737
@Override
38-
public Callable<Optional<TestRunResult>> getTestRunningTask(final TmcProjectInfo projectInfo) {
39-
return new Callable<Optional<TestRunResult>>() {
38+
public Callable<TestRunResult> getTestRunningTask(final TmcProjectInfo projectInfo) {
39+
return new Callable<TestRunResult>() {
4040
@Override
41-
public Optional<TestRunResult> call() throws Exception {
42-
43-
log.log(INFO, "Starting compile");
44-
45-
Project project = projectInfo.getProject();
46-
FileObject buildScript = project.getProjectDirectory().getFileObject("build.xml");
47-
if (buildScript == null) {
48-
throw new RuntimeException("Project has no build.xml");
49-
}
50-
ExecutorTask task;
51-
52-
try {
53-
task = ActionUtils.runTarget(buildScript, new String[]{"compile-test"}, null);
54-
} catch (IOException ex) {
55-
Exceptions.printStackTrace(ex);
56-
throw ExceptionUtils.toRuntimeException(ex);
57-
} catch (IllegalArgumentException ex) {
58-
Exceptions.printStackTrace(ex);
59-
throw ExceptionUtils.toRuntimeException(ex);
60-
}
61-
62-
int compileResult = task.result();
63-
if (compileResult == 0) {
64-
log.log(INFO, "Compile success for project {0}", projectInfo.toString());
65-
return Optional.of(runTestTask(projectInfo));
41+
public TestRunResult call() throws Exception {
42+
if (compileProject(projectInfo) == SUCCESS) {
43+
log.log(Level.INFO, "Compile success for project {0}", projectInfo.toString());
44+
return runTests(projectInfo);
6645
} else {
67-
return Optional.absent();
46+
return new TestRunResult(Status.COMPILE_FAILED);
6847
}
6948
}
7049
};
7150
}
7251

73-
protected TestRunResult runTestTask(final TmcProjectInfo projectInfo) throws UserVisibleException, IOException, InterruptedException, ExecutionException {
52+
protected int compileProject(TmcProjectInfo projectInfo) {
53+
log.info("Starting compile");
54+
Project project = projectInfo.getProject();
55+
FileObject buildScript = project.getProjectDirectory().getFileObject("build.xml");
56+
57+
if (buildScript == null) {
58+
throw new RuntimeException("Project has no build.xml");
59+
}
60+
61+
ExecutorTask task;
62+
63+
try {
64+
task = ActionUtils.runTarget(buildScript, new String[]{"compile-test"}, null);
65+
} catch (IOException ex) {
66+
throw ExceptionUtils.toRuntimeException(ex);
67+
}
68+
69+
return task.result();
70+
}
71+
72+
protected TestRunResult runTests(final TmcProjectInfo projectInfo) throws UserVisibleException, IOException, InterruptedException, ExecutionException {
7473

7574
FileObject testDir = findTestDir(projectInfo);
7675
if (testDir == null) {
@@ -137,7 +136,11 @@ private TestRunResult runTests(final TmcProjectInfo projectInfo, FileObject test
137136
throw ex;
138137
} catch (UserVisibleException ex) {
139138
throw ex;
140-
} catch (Throwable t) {
139+
} catch (InterruptedException t) {
140+
throw new UserVisibleException("Failed to run tests", t);
141+
} catch (ExecutionException t) {
142+
throw new UserVisibleException("Failed to run tests", t);
143+
} catch (IOException t) {
141144
throw new UserVisibleException("Failed to run tests", t);
142145
}
143146
}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
package fi.helsinki.cs.tmc.runners;
22

3-
import com.google.common.base.Optional;
43
import fi.helsinki.cs.tmc.data.TestRunResult;
54
import fi.helsinki.cs.tmc.model.TmcProjectInfo;
65
import java.util.concurrent.Callable;
76

87
/**
98
* ExerciseRunner for implementing runner tasks
10-
* Using Optional as a container for the result itself, if TestRunResult is not present
11-
* compilation failure should be assumed.
12-
*
139
*/
1410
public interface ExerciseRunner {
15-
public abstract Callable<Optional<TestRunResult>> getTestRunningTask(TmcProjectInfo projectInfo);
11+
public abstract Callable<TestRunResult> getTestRunningTask(TmcProjectInfo projectInfo);
1612
}

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

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
package fi.helsinki.cs.tmc.runners;
22

3-
import com.google.common.base.Optional;
43
import fi.helsinki.cs.tmc.data.Exercise;
54
import fi.helsinki.cs.tmc.data.TestRunResult;
5+
import fi.helsinki.cs.tmc.data.TestRunResult.Status;
66
import fi.helsinki.cs.tmc.model.TmcProjectInfo;
77
import fi.helsinki.cs.tmc.model.UserVisibleException;
8-
import fi.helsinki.cs.tmc.utilities.BgTask;
9-
import fi.helsinki.cs.tmc.utilities.BgTaskListener;
108
import fi.helsinki.cs.tmc.utilities.process.ProcessResult;
119
import fi.helsinki.cs.tmc.utilities.process.ProcessRunner;
1210
import java.io.File;
1311
import java.io.IOException;
1412
import java.util.Arrays;
1513
import java.util.concurrent.Callable;
1614
import java.util.logging.Level;
17-
import static java.util.logging.Level.INFO;
18-
import static java.util.logging.Level.WARNING;
1915
import java.util.logging.Logger;
2016
import org.netbeans.api.project.Project;
2117
import org.openide.filesystems.FileObject;
@@ -27,19 +23,19 @@ public class MakefileExerciseRunner extends AbstractExerciseRunner {
2723
private static final Logger log = Logger.getLogger(MakefileExerciseRunner.class.getName());
2824

2925
@Override
30-
public Callable<Optional<TestRunResult>> getTestRunningTask(final TmcProjectInfo projectInfo) {
26+
public Callable<TestRunResult> getTestRunningTask(final TmcProjectInfo projectInfo) {
3127
final InputOutput io = IOProvider.getDefault().getIO(projectInfo.getProjectName(), false);
3228

33-
return new Callable<Optional<TestRunResult>>() {
29+
return new Callable<TestRunResult>() {
3430
@Override
35-
public Optional<TestRunResult> call() throws Exception {
31+
public TestRunResult call() throws Exception {
3632
log.log(Level.INFO, "Compiling project {0}", projectInfo.getProjectName());
3733
Project project = projectInfo.getProject();
3834
FileObject makeFile = project.getProjectDirectory().getFileObject("Makefile");
3935

4036
if (makeFile == null) {
41-
log.log(INFO, "Project has no Makefile");
42-
return Optional.absent();
37+
log.info("Project has no Makefile");
38+
throw new RuntimeException("Project has no Makefile");
4339
}
4440

4541
File workDir = projectInfo.getProjectDirAsFile();
@@ -52,12 +48,12 @@ public Optional<TestRunResult> call() throws Exception {
5248
int ret = result.statusCode;
5349
if (ret != 0) {
5450
io.select();
55-
log.log(INFO, "Compile resulted in non-zero exit code {0}", result.statusCode);
56-
return Optional.absent();
51+
log.log(Level.INFO, "Compile resulted in non-zero exit code {0}", result.statusCode);
52+
return new TestRunResult(Status.COMPILE_FAILED);
5753
}
5854

59-
log.log(INFO, "Running tests");
60-
return Optional.of(runTests(projectInfo, true));
55+
log.info("Running tests");
56+
return runTests(projectInfo, true);
6157
} catch (Exception ex) {
6258
io.select();
6359
throw ex;
@@ -68,7 +64,7 @@ public Optional<TestRunResult> call() throws Exception {
6864

6965
// TODO: use make
7066
private TestRunResult runTests(final TmcProjectInfo projectInfo, final boolean withValgrind) throws Exception {
71-
log.log(INFO, "Running tests {0}", projectInfo.getProjectName());
67+
log.log(Level.INFO, "Running tests {0}", projectInfo.getProjectName());
7268
final File testDir = projectInfo.getProjectDirAsFile();
7369
String[] command;
7470

@@ -80,7 +76,7 @@ private TestRunResult runTests(final TmcProjectInfo projectInfo, final boolean w
8076
command = new String[]{testDir.getAbsolutePath()
8177
+ File.separatorChar + "test" + File.separatorChar + "test"};
8278
}
83-
log.log(INFO, "Running tests for project {0} with command {1}",
79+
log.log(Level.INFO, "Running tests for project {0} with command {1}",
8480
new Object[]{projectInfo.getProjectName(), Arrays.deepToString(command)});
8581

8682
ProcessRunner runner = new ProcessRunner(command, testDir, IOProvider.getDefault()
@@ -91,11 +87,11 @@ private TestRunResult runTests(final TmcProjectInfo projectInfo, final boolean w
9187
runner.call();
9288
log.info("Running tests completed");
9389
} catch (IOException e) {
94-
log.log(INFO, "IOException while running tests, kinda wanted. {0}", e.getMessage());
90+
log.log(Level.INFO, "IOException while running tests, kinda wanted. {0}", e.getMessage());
9591
if (withValgrind) {
9692
return runTests(projectInfo, false);
9793
} else {
98-
log.log(WARNING, "Failed to run tests for project: \"{0}\" with command: \"{1}\".\n\"{2}\"",
94+
log.log(Level.WARNING, "Failed to run tests for project: \"{0}\" with command: \"{1}\".\n\"{2}\"",
9995
new Object[]{projectInfo.getProjectName(), Arrays.deepToString(command), e.getMessage()});
10096
throw new UserVisibleException("Failed to run tests:\n" + e.getMessage());
10197
}
@@ -108,10 +104,10 @@ private TestRunResult runTests(final TmcProjectInfo projectInfo, final boolean w
108104
Exercise exercise = projectMediator.tryGetExerciseForProject(projectInfo, courseDb);
109105

110106
if (exercise != null) {
111-
log.log(INFO, "Parsing exercises with valgrind strategy {0}", exercise.getValgrindStrategy());
107+
log.log(Level.INFO, "Parsing exercises with valgrind strategy {0}", exercise.getValgrindStrategy());
112108
return resultParser.parseCTestResults(resultsFile, valgrindLog, exercise.getValgrindStrategy());
113109
} else {
114-
log.log(INFO, "Parsing exercises with out valgrind strategy");
110+
log.log(Level.INFO, "Parsing exercises with out valgrind strategy");
115111
return resultParser.parseCTestResults(resultsFile, valgrindLog, null);
116112
}
117113
}

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package fi.helsinki.cs.tmc.runners;
22

3-
import com.google.common.base.Optional;
43
import fi.helsinki.cs.tmc.data.Exercise;
54
import fi.helsinki.cs.tmc.data.TestRunResult;
5+
import fi.helsinki.cs.tmc.data.TestRunResult.Status;
66
import fi.helsinki.cs.tmc.model.TmcProjectInfo;
77
import fi.helsinki.cs.tmc.utilities.maven.MavenRunBuilder;
88
import fi.helsinki.cs.tmc.utilities.process.ProcessResult;
@@ -14,7 +14,7 @@
1414
import java.util.List;
1515
import java.util.Map;
1616
import java.util.concurrent.Callable;
17-
import static java.util.logging.Level.INFO;
17+
import java.util.logging.Level;
1818
import java.util.logging.Logger;
1919
import org.apache.commons.lang3.StringUtils;
2020
import org.openide.windows.IOProvider;
@@ -26,14 +26,14 @@ public class MavenExerciseRunner extends AbstractJavaExerciseRunner {
2626
private static final Logger log = Logger.getLogger(MavenExerciseRunner.class.getName());
2727

2828
@Override
29-
public Callable<Optional<TestRunResult>> getTestRunningTask(final TmcProjectInfo projectInfo) {
29+
public Callable<TestRunResult> getTestRunningTask(final TmcProjectInfo projectInfo) {
3030
final InputOutput inOut = IOProvider.getDefault().getIO(projectInfo.getProjectName(), false);
3131

32-
return new Callable<Optional<TestRunResult>>() {
32+
return new Callable<TestRunResult>() {
3333
@Override
34-
public Optional<TestRunResult> call() throws Exception {
34+
public TestRunResult call() throws Exception {
3535
File projectDir = projectInfo.getProjectDirAsFile();
36-
log.log(INFO, "Starting compile");
36+
log.info("Starting compile");
3737
String goal = "test-compile";
3838

3939
final ProcessRunner runner = new MavenRunBuilder()
@@ -46,11 +46,11 @@ public Optional<TestRunResult> call() throws Exception {
4646
int ret = result.statusCode;
4747
if (ret != 0) {
4848
inOut.select();
49-
log.log(INFO, "Compile resulted in non-zero exit code {0}", result.statusCode);
50-
return Optional.absent();
49+
log.log(Level.INFO, "Compile resulted in non-zero exit code {0}", result.statusCode);
50+
return new TestRunResult(Status.COMPILE_FAILED);
5151
} else {
52-
log.log(INFO, "Running tests");
53-
return Optional.of(runTests(projectInfo));
52+
log.log(Level.INFO, "Running tests");
53+
return runTests(projectInfo, inOut);
5454
}
5555
} catch (Exception ex) {
5656
inOut.select();
@@ -61,12 +61,10 @@ public Optional<TestRunResult> call() throws Exception {
6161
};
6262
}
6363

64-
public TestRunResult runTests(final TmcProjectInfo projectInfo) throws Exception {
64+
public TestRunResult runTests(final TmcProjectInfo projectInfo, InputOutput inOut) throws Exception {
6565
final File projectDir = projectInfo.getProjectDirAsFile();
6666
String goal = MAVEN_TEST_RUN_GOAL;
6767
Map<String, String> props = new HashMap<String, String>();
68-
InputOutput inOut = getIoTab();
69-
7068
List<String> jvmOpts = new ArrayList<String>();
7169

7270
Exercise exercise = tryGetExercise(projectInfo.getProject());

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fi.helsinki.cs.tmc.runners;
22

33
import com.google.common.base.Optional;
4+
import com.google.common.base.Throwables;
45
import fi.helsinki.cs.tmc.data.Exercise;
56
import fi.helsinki.cs.tmc.data.ResultCollector;
67
import fi.helsinki.cs.tmc.data.TestRunResult;
@@ -56,16 +57,16 @@ public void performAction(final ResultCollector resultCollector, Project... proj
5657
final TmcProjectInfo projectInfo = projectMediator.wrapProject(project);
5758
eventBus.post(new InvokedEvent(projectInfo));
5859
final ExerciseRunner runner = getRunner(projectInfo);
59-
BgTask.start("Running tests", runner.getTestRunningTask(projectInfo), new BgTaskListener<Optional<TestRunResult>>() {
60+
BgTask.start("Running tests", runner.getTestRunningTask(projectInfo), new BgTaskListener<TestRunResult>() {
6061
@Override
61-
public void bgTaskReady(Optional<TestRunResult> result) {
62-
if (!result.isPresent()) {
62+
public void bgTaskReady(TestRunResult result) {
63+
if (result.status == TestRunResult.Status.COMPILE_FAILED) {
6364
dialogDisplayer.displayError("The code did not compile.");
6465
return;
6566
}
6667
Exercise ex = projectMediator.tryGetExerciseForProject(projectInfo, courseDb);
6768
boolean canSubmit = ex.isReturnable();
68-
resultDisplayer.showLocalRunResult(result.get().getTestCaseResults(), canSubmit, new Runnable() {
69+
resultDisplayer.showLocalRunResult(result.getTestCaseResults(), canSubmit, new Runnable() {
6970
@Override
7071
public void run() {
7172
exerciseSubmitter.performAction(projectInfo.getProject());
@@ -75,8 +76,8 @@ public void run() {
7576

7677
@Override
7778
public void bgTaskFailed(Throwable ex) {
78-
log.log(INFO, "performAction of TestRunHandler failed with message: {0}, trace: {1}",
79-
new Object[]{ex.getMessage(), Arrays.deepToString(ex.getStackTrace())});
79+
log.log(INFO, "performAction of TestRunHandler failed with message: {0}, \ntrace: {1}",
80+
new Object[]{ex.getMessage(), Throwables.getStackTraceAsString(ex)});
8081
dialogDisplayer.displayError("Failed to run the tests: " + ex.getMessage());
8182
}
8283

@@ -90,13 +91,10 @@ public void bgTaskCancelled() {
9091
private AbstractExerciseRunner getRunner(TmcProjectInfo projectInfo) {
9192
switch (projectInfo.getProjectType()) {
9293
case JAVA_MAVEN:
93-
log.log(INFO, "Maven task selected");
9494
return new MavenExerciseRunner();
9595
case JAVA_SIMPLE:
96-
log.log(INFO, "Ant task selected");
9796
return new AntExerciseRunner();
9897
case MAKEFILE:
99-
log.log(INFO, "Makefile task selected");
10098
return new MakefileExerciseRunner();
10199
default:
102100
throw new IllegalArgumentException("Unknown project type: " + projectInfo.getProjectType());

0 commit comments

Comments
 (0)