|
2 | 2 |
|
3 | 3 | import com.google.common.base.Optional;
|
4 | 4 | import com.google.common.base.Throwables;
|
| 5 | +import com.google.common.collect.ImmutableList; |
| 6 | +import com.google.common.util.concurrent.FutureCallback; |
| 7 | +import com.google.common.util.concurrent.Futures; |
| 8 | +import com.google.common.util.concurrent.ListenableFuture; |
5 | 9 | import hy.tmc.core.domain.Exercise;
|
6 | 10 | import fi.helsinki.cs.tmc.data.ResultCollector;
|
| 11 | +import fi.helsinki.cs.tmc.data.TestCaseResult; |
7 | 12 | import fi.helsinki.cs.tmc.data.TestRunResult;
|
8 | 13 | import fi.helsinki.cs.tmc.events.TmcEvent;
|
9 | 14 | import fi.helsinki.cs.tmc.events.TmcEventBus;
|
10 | 15 | import fi.helsinki.cs.tmc.exerciseSubmitter.ExerciseSubmitter;
|
| 16 | +import fi.helsinki.cs.tmc.langs.RunResult; |
| 17 | +import static fi.helsinki.cs.tmc.langs.RunResult.Status.COMPILE_FAILED; |
| 18 | +import fi.helsinki.cs.tmc.langs.TestResult; |
11 | 19 | import fi.helsinki.cs.tmc.model.CourseDb;
|
| 20 | +import fi.helsinki.cs.tmc.model.NBTmcSettings; |
12 | 21 | import fi.helsinki.cs.tmc.model.ProjectMediator;
|
| 22 | +import fi.helsinki.cs.tmc.model.TmcCoreSingleton; |
13 | 23 | import fi.helsinki.cs.tmc.model.TmcProjectInfo;
|
14 | 24 | import static fi.helsinki.cs.tmc.model.TmcProjectType.JAVA_MAVEN;
|
15 | 25 | import static fi.helsinki.cs.tmc.model.TmcProjectType.JAVA_SIMPLE;
|
|
18 | 28 | import fi.helsinki.cs.tmc.ui.TestResultDisplayer;
|
19 | 29 | import fi.helsinki.cs.tmc.utilities.BgTask;
|
20 | 30 | import fi.helsinki.cs.tmc.utilities.BgTaskListener;
|
| 31 | +import hy.tmc.core.exceptions.TmcCoreException; |
| 32 | +import java.util.ArrayList; |
21 | 33 | import java.util.Arrays;
|
| 34 | +import java.util.List; |
22 | 35 | import static java.util.logging.Level.INFO;
|
23 | 36 | import java.util.logging.Logger;
|
| 37 | +import javax.swing.SwingUtilities; |
24 | 38 | import org.netbeans.api.project.Project;
|
| 39 | +import org.openide.util.Exceptions; |
25 | 40 |
|
26 | 41 | public class TestRunHandler {
|
27 | 42 |
|
28 | 43 | private static final Logger log = Logger.getLogger(TestRunHandler.class.getName());
|
29 | 44 |
|
30 | 45 | public static class InvokedEvent implements TmcEvent {
|
| 46 | + |
31 | 47 | public final TmcProjectInfo projectInfo;
|
32 | 48 |
|
33 | 49 | public InvokedEvent(TmcProjectInfo projectInfo) {
|
@@ -56,48 +72,65 @@ public void performAction(final ResultCollector resultCollector, Project... proj
|
56 | 72 | for (final Project project : projects) {
|
57 | 73 | final TmcProjectInfo projectInfo = projectMediator.wrapProject(project);
|
58 | 74 | eventBus.post(new InvokedEvent(projectInfo));
|
59 |
| - final ExerciseRunner runner = getRunner(projectInfo); |
60 |
| - BgTask.start("Running tests", runner.getTestRunningTask(projectInfo), new BgTaskListener<TestRunResult>() { |
61 |
| - @Override |
62 |
| - public void bgTaskReady(TestRunResult result) { |
63 |
| - if (!result.getCompilationSuccess()) { |
64 |
| - dialogDisplayer.displayError("The code did not compile."); |
65 |
| - return; |
| 75 | + try { |
| 76 | + ListenableFuture<RunResult> result = TmcCoreSingleton.getInstance().test(projectInfo.getProjectDirAbsPath(), NBTmcSettings.getDefault()); |
| 77 | + Futures.addCallback(result, new FutureCallback<RunResult>() { |
| 78 | + @Override |
| 79 | + public void onSuccess(final RunResult result) { |
| 80 | + explainResults(result, projectInfo, resultCollector); |
66 | 81 | }
|
67 |
| - Exercise ex = projectMediator.tryGetExerciseForProject(projectInfo, courseDb); |
68 |
| - boolean canSubmit = ex.isReturnable(); |
69 |
| - resultDisplayer.showLocalRunResult(result.getTestCaseResults(), canSubmit, new Runnable() { |
70 |
| - @Override |
71 |
| - public void run() { |
72 |
| - exerciseSubmitter.performAction(projectInfo.getProject()); |
73 |
| - } |
74 |
| - }, resultCollector); |
75 |
| - } |
76 | 82 |
|
77 |
| - @Override |
78 |
| - public void bgTaskFailed(Throwable ex) { |
79 |
| - log.log(INFO, "performAction of TestRunHandler failed with message: {0}, \ntrace: {1}", |
80 |
| - new Object[]{ex.getMessage(), Throwables.getStackTraceAsString(ex)}); |
81 |
| - dialogDisplayer.displayError("Failed to run the tests: " + ex.getMessage()); |
82 |
| - } |
| 83 | + @Override |
| 84 | + public void onFailure(final Throwable ex) { |
| 85 | + explainFailure(ex); |
| 86 | + } |
83 | 87 |
|
84 |
| - @Override |
85 |
| - public void bgTaskCancelled() { |
86 |
| - } |
87 |
| - }); |
| 88 | + }); |
| 89 | + } catch (TmcCoreException ex) { |
| 90 | + Exceptions.printStackTrace(ex); |
| 91 | + } |
88 | 92 | }
|
89 | 93 | }
|
90 | 94 |
|
91 |
| - private AbstractExerciseRunner getRunner(TmcProjectInfo projectInfo) { |
92 |
| - switch (projectInfo.getProjectType()) { |
93 |
| - case JAVA_MAVEN: |
94 |
| - return new MavenExerciseRunner(); |
95 |
| - case JAVA_SIMPLE: |
96 |
| - return new AntExerciseRunner(); |
97 |
| - case MAKEFILE: |
98 |
| - return new MakefileExerciseRunner(); |
99 |
| - default: |
100 |
| - throw new IllegalArgumentException("Unknown project type: " + projectInfo.getProjectType()); |
| 95 | + private void explainFailure(final Throwable ex) { |
| 96 | + SwingUtilities.invokeLater(new Runnable() { |
| 97 | + @Override |
| 98 | + public void run() { |
| 99 | + log.log(INFO, "performAction of TestRunHandler failed with message: {0}, \ntrace: {1}", |
| 100 | + new Object[]{ex.getMessage(), Throwables.getStackTraceAsString(ex)}); |
| 101 | + dialogDisplayer.displayError("Failed to run the tests: " + ex.getMessage()); |
| 102 | + } |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + private void explainResults(final RunResult result, final TmcProjectInfo projectInfo, final ResultCollector resultCollector) { |
| 107 | + SwingUtilities.invokeLater(new Runnable() { |
| 108 | + |
| 109 | + @Override |
| 110 | + public void run() { |
| 111 | + if (result.status == COMPILE_FAILED) { |
| 112 | + dialogDisplayer.displayError("The code did not compile."); |
| 113 | + return; |
| 114 | + } |
| 115 | + Exercise ex = projectMediator.tryGetExerciseForProject(projectInfo, courseDb); |
| 116 | + boolean canSubmit = ex.isReturnable(); |
| 117 | + List<TestCaseResult> list = testResultsToTestCaseResults(result.testResults); |
| 118 | + resultDisplayer.showLocalRunResult(list, canSubmit, new Runnable() { |
| 119 | + @Override |
| 120 | + public void run() { |
| 121 | + exerciseSubmitter.performAction(projectInfo.getProject()); |
| 122 | + } |
| 123 | + }, resultCollector); |
| 124 | + } |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + private List<TestCaseResult> testResultsToTestCaseResults(ImmutableList<TestResult> testresults) { |
| 129 | + List<TestCaseResult> testCaseResults = new ArrayList<TestCaseResult>(); |
| 130 | + for (TestResult result : testresults) { |
| 131 | + TestCaseResult testCase = new TestCaseResult(result.name, result.passed, result.errorMessage); |
| 132 | + testCaseResults.add(testCase); |
101 | 133 | }
|
| 134 | + return testCaseResults; |
102 | 135 | }
|
103 | 136 | }
|
0 commit comments