Skip to content

Commit b275781

Browse files
committed
Run tests locally show the cancellable tasks all the way
1 parent f5bf0c5 commit b275781

File tree

3 files changed

+82
-130
lines changed

3 files changed

+82
-130
lines changed

tmc-plugin/src/fi/helsinki/cs/tmc/actions/RunTestsLocallyAction.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ protected boolean enabledFor(Exercise exercise) {
6767

6868
@Override
6969
public void run() {
70-
// TODO(jamo): use bg task
7170
Exercise exercise = exerciseForProject(project);
7271
if (exercise != null) {
7372
ResultCollector resultCollector = new ResultCollector(exercise);
Lines changed: 31 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package fi.helsinki.cs.tmc.runners;
22

3-
import com.google.common.util.concurrent.FutureCallback;
4-
import com.google.common.util.concurrent.Futures;
5-
import com.google.common.util.concurrent.ListenableFuture;
63
import fi.helsinki.cs.tmc.data.ResultCollector;
74
import fi.helsinki.cs.tmc.model.ProjectMediator;
85
import fi.helsinki.cs.tmc.model.TmcProjectInfo;
@@ -11,88 +8,59 @@
118
import fi.helsinki.cs.tmc.stylerunner.validation.ValidationResult;
129
import fi.helsinki.cs.tmc.ui.ConvenientDialogDisplayer;
1310
import fi.helsinki.cs.tmc.core.exceptions.TmcCoreException;
14-
import fi.helsinki.cs.tmc.stylerunner.validation.Strategy;
15-
import fi.helsinki.cs.tmc.stylerunner.validation.ValidationError;
16-
import java.io.File;
17-
import java.nio.file.Paths;
18-
import java.util.HashMap;
19-
import java.util.List;
20-
import java.util.Map;
11+
import fi.helsinki.cs.tmc.utilities.BgTask;
12+
import fi.helsinki.cs.tmc.utilities.BgTaskListener;
2113

22-
import javax.swing.SwingUtilities;
14+
import com.google.common.util.concurrent.ListenableFuture;
2315

2416
import org.netbeans.api.project.Project;
2517

2618
import org.openide.util.Exceptions;
19+
import java.util.concurrent.ExecutionException;
2720

28-
public final class CheckstyleRunHandler {
21+
public final class CheckstyleRunHandler implements Runnable {
2922

3023
private Project project;
3124
private final ConvenientDialogDisplayer dialogDisplayer = ConvenientDialogDisplayer.getDefault();
3225
private ValidationResult validationResult = new CheckstyleResult();
3326

3427
public void performAction(final ResultCollector resultCollector, final Project project) {
3528
this.project = project;
36-
final TmcProjectInfo projectInfo = ProjectMediator.getInstance().wrapProject(project);
37-
final String projectType = projectInfo.getProjectType().name();
38-
ProjectMediator.getInstance().saveAllFiles();
39-
40-
try {
41-
ListenableFuture<ValidationResult> result = TmcCoreSingleton.getInstance().runCheckstyle(projectInfo.getProjectDirAsPath());
42-
Futures.addCallback(result, new ExplainValidationResult(resultCollector, dialogDisplayer));
4329

44-
} catch (TmcCoreException ex) {
45-
ConvenientDialogDisplayer.getDefault().displayError("Checkstyle audit failed.");
46-
Exceptions.printStackTrace(ex);
47-
}
30+
BgTask.start("Running validations", this, new BgTaskListener<Object>() {
4831

49-
}
50-
51-
}
52-
53-
class ExplainValidationResult implements FutureCallback<ValidationResult> {
32+
@Override
33+
public void bgTaskFailed(final Throwable exception) {
34+
dialogDisplayer.displayError("Failed to validate the code.");
35+
}
5436

55-
ResultCollector resultCollector;
56-
ConvenientDialogDisplayer dialogDisplayer;
57-
58-
public ExplainValidationResult(ResultCollector resultCollector, ConvenientDialogDisplayer dialogDisplayer) {
59-
this.resultCollector = resultCollector;
60-
this.dialogDisplayer = dialogDisplayer;
61-
}
37+
@Override
38+
public void bgTaskCancelled() {
39+
}
6240

63-
@Override
64-
public void onSuccess(final ValidationResult v) {
65-
SwingUtilities.invokeLater(new Runnable() {
6641
@Override
67-
public void run() {
68-
System.out.println("style done yay");
69-
resultCollector.setValidationResult(v);
42+
public void bgTaskReady(final Object nothing) {
43+
resultCollector.setValidationResult(validationResult);
7044
}
7145
});
7246
}
7347

7448
@Override
75-
public void onFailure(final Throwable ex) {
76-
SwingUtilities.invokeLater(new Runnable() {
77-
@Override
78-
public void run() {
79-
if (ex instanceof UnsupportedOperationException) {
80-
resultCollector.setValidationResult(new ValidationResult() {
81-
82-
@Override
83-
public Strategy getStrategy() {
84-
return null;
85-
}
49+
public void run() {
50+
try {
51+
final TmcProjectInfo projectInfo = ProjectMediator.getInstance().wrapProject(project);
52+
final String projectType = projectInfo.getProjectType().name();
53+
ProjectMediator.getInstance().saveAllFiles();
54+
ListenableFuture<ValidationResult> result;
55+
result = TmcCoreSingleton.getInstance().runCheckstyle(projectInfo.getProjectDirAsPath());
56+
validationResult = result.get();
57+
} catch (InterruptedException ex) {
58+
Exceptions.printStackTrace(ex);
59+
} catch (ExecutionException ex) {
60+
Exceptions.printStackTrace(ex);
61+
} catch (TmcCoreException ex) {
62+
Exceptions.printStackTrace(ex);
63+
}
8664

87-
@Override
88-
public Map<File, List<ValidationError>> getValidationErrors() {
89-
return new HashMap<File, List<ValidationError>>();
90-
}
91-
});
92-
} else {
93-
dialogDisplayer.displayError("Failed to validate the code.");
94-
}
95-
}
96-
});
9765
}
98-
}
66+
}
Lines changed: 51 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package fi.helsinki.cs.tmc.runners;
22

33
import 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;
106
import fi.helsinki.cs.tmc.core.domain.Exercise;
117
import fi.helsinki.cs.tmc.data.ResultCollector;
128
import fi.helsinki.cs.tmc.data.TestCaseResult;
@@ -15,25 +11,25 @@
1511
import fi.helsinki.cs.tmc.exerciseSubmitter.ExerciseSubmitter;
1612
import fi.helsinki.cs.tmc.langs.domain.RunResult;
1713
import fi.helsinki.cs.tmc.langs.domain.TestResult;
18-
1914
import fi.helsinki.cs.tmc.model.CourseDb;
20-
import fi.helsinki.cs.tmc.model.NbTmcSettings;
2115
import fi.helsinki.cs.tmc.model.ProjectMediator;
2216
import fi.helsinki.cs.tmc.model.TmcCoreSingleton;
2317
import fi.helsinki.cs.tmc.model.TmcProjectInfo;
2418
import fi.helsinki.cs.tmc.ui.ConvenientDialogDisplayer;
2519
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+
2830
import java.util.ArrayList;
2931
import java.util.List;
30-
import static java.util.logging.Level.INFO;
3132
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;
3733

3834
public 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}, \ntrace: {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}, \ntrace: {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

Comments
 (0)