11package fi .helsinki .cs .tmc .runners ;
22
33import static fi .helsinki .cs .tmc .langs .domain .RunResult .Status .COMPILE_FAILED ;
4+ import static java .util .logging .Level .INFO ;
45
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 ;
106import fi .helsinki .cs .tmc .core .domain .Exercise ;
117import fi .helsinki .cs .tmc .data .ResultCollector ;
128import fi .helsinki .cs .tmc .data .TestCaseResult ;
1511import fi .helsinki .cs .tmc .exerciseSubmitter .ExerciseSubmitter ;
1612import fi .helsinki .cs .tmc .langs .domain .RunResult ;
1713import fi .helsinki .cs .tmc .langs .domain .TestResult ;
18-
1914import fi .helsinki .cs .tmc .model .CourseDb ;
20- import fi .helsinki .cs .tmc .model .NbTmcSettings ;
2115import fi .helsinki .cs .tmc .model .ProjectMediator ;
2216import fi .helsinki .cs .tmc .model .TmcCoreSingleton ;
2317import fi .helsinki .cs .tmc .model .TmcProjectInfo ;
2418import fi .helsinki .cs .tmc .ui .ConvenientDialogDisplayer ;
2519import 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+
2830import java .util .ArrayList ;
2931import java .util .List ;
30- import static java .util .logging .Level .INFO ;
3132import 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 ;
3733
3834public class TestRunHandler {
3935
@@ -69,62 +65,51 @@ public void performAction(final ResultCollector resultCollector, Project... proj
6965 for (final Project project : projects ) {
7066 final TmcProjectInfo projectInfo = projectMediator .wrapProject (project );
7167 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
8370
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 ;
8875 }
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+ }
8985
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+ }
9792
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 ;
108100
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 ();
116105 }
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+ }
128113 }
129114
130115 private List <TestCaseResult > testResultsToTestCaseResults (ImmutableList <TestResult > testresults ) {
@@ -135,4 +120,4 @@ private List<TestCaseResult> testResultsToTestCaseResults(ImmutableList<TestResu
135120 }
136121 return testCaseResults ;
137122 }
138- }
123+ }
0 commit comments