1
1
package fi .helsinki .cs .tmc .runners ;
2
2
3
3
import static fi .helsinki .cs .tmc .langs .domain .RunResult .Status .COMPILE_FAILED ;
4
+ import static java .util .logging .Level .INFO ;
4
5
5
- import com .google .common .base .Throwables ;
6
- import com .google .common .collect .ImmutableList ;
7
- import com .google .common .util .concurrent .FutureCallback ;
8
- import com .google .common .util .concurrent .Futures ;
9
- import com .google .common .util .concurrent .ListenableFuture ;
10
6
import fi .helsinki .cs .tmc .core .domain .Exercise ;
11
7
import fi .helsinki .cs .tmc .data .ResultCollector ;
12
8
import fi .helsinki .cs .tmc .data .TestCaseResult ;
15
11
import fi .helsinki .cs .tmc .exerciseSubmitter .ExerciseSubmitter ;
16
12
import fi .helsinki .cs .tmc .langs .domain .RunResult ;
17
13
import fi .helsinki .cs .tmc .langs .domain .TestResult ;
18
-
19
14
import fi .helsinki .cs .tmc .model .CourseDb ;
20
- import fi .helsinki .cs .tmc .model .NbTmcSettings ;
21
15
import fi .helsinki .cs .tmc .model .ProjectMediator ;
22
16
import fi .helsinki .cs .tmc .model .TmcCoreSingleton ;
23
17
import fi .helsinki .cs .tmc .model .TmcProjectInfo ;
24
18
import fi .helsinki .cs .tmc .ui .ConvenientDialogDisplayer ;
25
19
import fi .helsinki .cs .tmc .ui .TestResultDisplayer ;
26
- import fi .helsinki .cs .tmc .core .exceptions .TmcCoreException ;
27
- import java .nio .file .Paths ;
20
+ import fi .helsinki .cs .tmc .utilities .BgTask ;
21
+ import fi .helsinki .cs .tmc .utilities .BgTaskListener ;
22
+ import fi .helsinki .cs .tmc .utilities .CancellableCallable ;
23
+
24
+ import com .google .common .base .Throwables ;
25
+ import com .google .common .collect .ImmutableList ;
26
+ import com .google .common .util .concurrent .ListenableFuture ;
27
+
28
+ import org .netbeans .api .project .Project ;
29
+
28
30
import java .util .ArrayList ;
29
31
import java .util .List ;
30
- import static java .util .logging .Level .INFO ;
31
32
import java .util .logging .Logger ;
32
- import javax .swing .SwingUtilities ;
33
- import org .netbeans .api .progress .ProgressHandle ;
34
- import org .netbeans .api .progress .ProgressHandleFactory ;
35
- import org .netbeans .api .project .Project ;
36
- import org .openide .util .Exceptions ;
37
33
38
34
public class TestRunHandler {
39
35
@@ -69,62 +65,51 @@ public void performAction(final ResultCollector resultCollector, Project... proj
69
65
for (final Project project : projects ) {
70
66
final TmcProjectInfo projectInfo = projectMediator .wrapProject (project );
71
67
eventBus .post (new InvokedEvent (projectInfo ));
72
- final ProgressHandle runningTestsLocally = ProgressHandleFactory .createSystemHandle (
73
- "Running tests." );
74
- runningTestsLocally .start ();
75
- try {
76
- ListenableFuture <RunResult > result = TmcCoreSingleton .getInstance ().test (projectInfo .getProjectDirAsPath ());
77
- Futures .addCallback (result , new FutureCallback <RunResult >() {
78
- @ Override
79
- public void onSuccess (final RunResult result ) {
80
- explainResults (result , projectInfo , resultCollector );
81
- runningTestsLocally .finish ();
82
- }
68
+ BgTaskListener bgTaskListener = new BgTaskListener <RunResult >() {
69
+ @ Override
83
70
84
- @ Override
85
- public void onFailure ( final Throwable ex ) {
86
- explainFailure ( ex );
87
- runningTestsLocally . finish () ;
71
+ public void bgTaskReady ( RunResult result ) {
72
+ if ( result . status == COMPILE_FAILED ) {
73
+ dialogDisplayer . displayError ( "The code did not compile." );
74
+ return ;
88
75
}
76
+ Exercise ex = projectMediator .tryGetExerciseForProject (projectInfo , courseDb );
77
+ boolean canSubmit = ex .isReturnable ();
78
+ resultDisplayer .showLocalRunResult (testResultsToTestCaseResults (result .testResults ), canSubmit , new Runnable () {
79
+ @ Override
80
+ public void run () {
81
+ exerciseSubmitter .performAction (projectInfo .getProject ());
82
+ }
83
+ }, resultCollector );
84
+ }
89
85
90
- });
91
- } catch (TmcCoreException ex ) {
92
- runningTestsLocally .finish ();
93
- Exceptions .printStackTrace (ex );
94
- }
95
- }
96
- }
86
+ @ Override
87
+ public void bgTaskFailed (Throwable ex ) {
88
+ log .log (INFO , "performAction of TestRunHandler failed with message: {0}, \n trace: {1}" ,
89
+ new Object []{ex .getMessage (), Throwables .getStackTraceAsString (ex )});
90
+ dialogDisplayer .displayError ("Failed to run the tests: " + ex .getMessage ());
91
+ }
97
92
98
- private void explainFailure (final Throwable ex ) {
99
- SwingUtilities .invokeLater (new Runnable () {
100
- @ Override
101
- public void run () {
102
- log .log (INFO , "performAction of TestRunHandler failed with message: {0}, \n trace: {1}" ,
103
- new Object []{ex .getMessage (), Throwables .getStackTraceAsString (ex )});
104
- dialogDisplayer .displayError ("Failed to run the tests: " + ex .getMessage ());
105
- }
106
- });
107
- }
93
+ @ Override
94
+ public void bgTaskCancelled () {
95
+ }
96
+ };
97
+ BgTask .start ("Running tests" , new CancellableCallable <RunResult >() {
98
+
99
+ ListenableFuture <RunResult > result ;
108
100
109
- private void explainResults (final RunResult result , final TmcProjectInfo projectInfo , final ResultCollector resultCollector ) {
110
- SwingUtilities .invokeLater (new Runnable () {
111
- @ Override
112
- public void run () {
113
- if (result .status == COMPILE_FAILED ) {
114
- dialogDisplayer .displayError ("The code did not compile." );
115
- return ;
101
+ @ Override
102
+ public RunResult call () throws Exception {
103
+ result = TmcCoreSingleton .getInstance ().test (projectInfo .getProjectDirAsPath ());
104
+ return result .get ();
116
105
}
117
- Exercise ex = projectMediator .tryGetExerciseForProject (projectInfo , courseDb );
118
- boolean canSubmit = ex .isReturnable ();
119
- List <TestCaseResult > list = testResultsToTestCaseResults (result .testResults );
120
- resultDisplayer .showLocalRunResult (list , canSubmit , new Runnable () {
121
- @ Override
122
- public void run () {
123
- exerciseSubmitter .performAction (projectInfo .getProject ());
124
- }
125
- }, resultCollector );
126
- }
127
- });
106
+
107
+ @ Override
108
+ public boolean cancel () {
109
+ return result .cancel (true );
110
+ }
111
+ }, bgTaskListener );
112
+ }
128
113
}
129
114
130
115
private List <TestCaseResult > testResultsToTestCaseResults (ImmutableList <TestResult > testresults ) {
@@ -135,4 +120,4 @@ private List<TestCaseResult> testResultsToTestCaseResults(ImmutableList<TestResu
135
120
}
136
121
return testCaseResults ;
137
122
}
138
- }
123
+ }
0 commit comments