-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathTestRunHandler.java
More file actions
142 lines (123 loc) · 5.71 KB
/
TestRunHandler.java
File metadata and controls
142 lines (123 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package fi.helsinki.cs.tmc.runners;
import static fi.helsinki.cs.tmc.langs.domain.RunResult.Status.COMPILE_FAILED;
import static java.util.logging.Level.INFO;
import fi.helsinki.cs.tmc.actions.ServerErrorHelper;
import fi.helsinki.cs.tmc.core.domain.Exercise;
import fi.helsinki.cs.tmc.data.ResultCollector;
import fi.helsinki.cs.tmc.data.TestCaseResult;
import fi.helsinki.cs.tmc.events.TmcEvent;
import fi.helsinki.cs.tmc.events.TmcEventBus;
import fi.helsinki.cs.tmc.exerciseSubmitter.ExerciseSubmitter;
import fi.helsinki.cs.tmc.langs.domain.RunResult;
import fi.helsinki.cs.tmc.langs.domain.TestResult;
import fi.helsinki.cs.tmc.model.CourseDb;
import fi.helsinki.cs.tmc.model.ProjectMediator;
import fi.helsinki.cs.tmc.model.TmcCoreSingleton;
import fi.helsinki.cs.tmc.model.TmcProjectInfo;
import fi.helsinki.cs.tmc.ui.ConvenientDialogDisplayer;
import fi.helsinki.cs.tmc.ui.TestResultDisplayer;
import fi.helsinki.cs.tmc.utilities.BgTask;
import fi.helsinki.cs.tmc.utilities.BgTaskListener;
import fi.helsinki.cs.tmc.utilities.CancellableCallable;
import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.ListenableFuture;
import org.netbeans.api.project.Project;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
public class TestRunHandler {
private static final Logger log = Logger.getLogger(TestRunHandler.class.getName());
public static class InvokedEvent implements TmcEvent {
public final TmcProjectInfo projectInfo;
public InvokedEvent(TmcProjectInfo projectInfo) {
this.projectInfo = projectInfo;
}
}
private final ProjectMediator projectMediator;
private final ConvenientDialogDisplayer dialogDisplayer;
private final TmcEventBus eventBus;
private final TestResultDisplayer resultDisplayer;
private final ExerciseSubmitter exerciseSubmitter;
private final CourseDb courseDb;
public TestRunHandler() {
this.projectMediator = ProjectMediator.getInstance();
this.dialogDisplayer = ConvenientDialogDisplayer.getDefault();
this.eventBus = TmcEventBus.getDefault();
this.resultDisplayer = TestResultDisplayer.getInstance();
this.exerciseSubmitter = new ExerciseSubmitter();
this.courseDb = CourseDb.getInstance();
}
public void performAction(final ResultCollector resultCollector, Project... projects) {
projectMediator.saveAllFiles();
for (final Project project : projects) {
final TmcProjectInfo projectInfo = projectMediator.wrapProject(project);
eventBus.post(new InvokedEvent(projectInfo));
BgTaskListener bgTaskListener = new BgTaskListener<RunResult>() {
@Override
public void bgTaskReady(RunResult result) {
if (result.status == COMPILE_FAILED) {
dialogDisplayer.displayError("The code did not compile.");
return;
}
Exercise ex = projectMediator.tryGetExerciseForProject(projectInfo, courseDb);
boolean canSubmit = ex.isReturnable();
resultDisplayer.showLocalRunResult(
testResultsToTestCaseResults(result.testResults),
canSubmit,
new Runnable() {
@Override
public void run() {
exerciseSubmitter.performAction(
projectInfo.getProject());
}
},
resultCollector);
}
@Override
public void bgTaskFailed(Throwable ex) {
log.log(
INFO,
"performAction of TestRunHandler failed with message: {0}, \ntrace: {1}",
new Object[]{
ex.getMessage(), Throwables.getStackTraceAsString(ex)
});
String msg = ServerErrorHelper.getServerExceptionMsg(ex);
if (!Strings.isNullOrEmpty(msg)) {
dialogDisplayer.displayError(
"Failed to run the tests: " + ex.getMessage());
}
}
@Override
public void bgTaskCancelled() {
}
};
BgTask.start(
"Running tests",
new CancellableCallable<RunResult>() {
ListenableFuture<RunResult> result;
@Override
public RunResult call() throws Exception {
result = TmcCoreSingleton.getInstance()
.test(projectInfo.getProjectDirAsPath());
return result.get();
}
@Override
public boolean cancel() {
return result.cancel(true);
}
},
bgTaskListener);
}
}
private List<TestCaseResult> testResultsToTestCaseResults(
ImmutableList<TestResult> testresults) {
List<TestCaseResult> testCaseResults = new ArrayList<TestCaseResult>();
for (TestResult result : testresults) {
TestCaseResult testCase = new TestCaseResult(result.name, result.passed, result.errorMessage);
testCaseResults.add(testCase);
}
return testCaseResults;
}
}