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 ) {
@@ -51,53 +67,128 @@ public TestRunHandler() {
51
67
this .courseDb = CourseDb .getInstance ();
52
68
}
53
69
70
+ /*public void performAction(final ResultCollector resultCollector, Project... projects) {
71
+ projectMediator.saveAllFiles();
72
+ for (final Project project : projects) {
73
+ final TmcProjectInfo projectInfo = projectMediator.wrapProject(project);
74
+ eventBus.post(new InvokedEvent(projectInfo));
75
+ final ExerciseRunner runner = getRunner(projectInfo);
76
+ BgTask.start("Running tests", runner.getTestRunningTask(projectInfo), new BgTaskListener<TestRunResult>() {
77
+ @Override
78
+ public void bgTaskReady(TestRunResult result) {
79
+ if (!result.getCompilationSuccess()) {
80
+ dialogDisplayer.displayError("The code did not compile.");
81
+ return;
82
+ }
83
+ Exercise ex = projectMediator.tryGetExerciseForProject(projectInfo, courseDb);
84
+ boolean canSubmit = ex.isReturnable();
85
+ resultDisplayer.showLocalRunResult(result.getTestCaseResults(), canSubmit, new Runnable() {
86
+ @Override
87
+ public void run() {
88
+ exerciseSubmitter.performAction(projectInfo.getProject());
89
+ }
90
+ }, resultCollector);
91
+ }
92
+
93
+ @Override
94
+ public void bgTaskFailed(Throwable ex) {
95
+ log.log(INFO, "performAction of TestRunHandler failed with message: {0}, \ntrace: {1}",
96
+ new Object[]{ex.getMessage(), Throwables.getStackTraceAsString(ex)});
97
+ dialogDisplayer.displayError("Failed to run the tests: " + ex.getMessage());
98
+ }
99
+
100
+ @Override
101
+ public void bgTaskCancelled() {
102
+ }
103
+ });
104
+ }
105
+ }*/
54
106
public void performAction (final ResultCollector resultCollector , Project ... projects ) {
55
107
projectMediator .saveAllFiles ();
56
108
for (final Project project : projects ) {
109
+ System .out .println ("Uusi Projekti" );
57
110
final TmcProjectInfo projectInfo = projectMediator .wrapProject (project );
58
111
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 ;
112
+ try {
113
+ ListenableFuture < RunResult > result = TmcCoreSingleton . getInstance (). test ( projectInfo . getProjectDirAbsPath ( ), NBTmcSettings . getDefault ());
114
+ Futures . addCallback ( result , new FutureCallback < RunResult >() {
115
+ @ Override
116
+ public void onSuccess ( final RunResult result ) {
117
+ System . out . println ( "RunResult onnistui " );
118
+ explainResults ( result , projectInfo , resultCollector ) ;
66
119
}
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
120
77
- @ Override
78
- public void bgTaskFailed (Throwable ex ) {
79
- log .log (INFO , "performAction of TestRunHandler failed with message: {0}, \n trace: {1}" ,
80
- new Object []{ex .getMessage (), Throwables .getStackTraceAsString (ex )});
81
- dialogDisplayer .displayError ("Failed to run the tests: " + ex .getMessage ());
82
- }
121
+ @ Override
122
+ public void onFailure (final Throwable ex ) {
123
+ explainFailure (ex );
124
+ }
125
+
126
+ });
127
+ } catch (TmcCoreException ex ) {
128
+ Exceptions .printStackTrace (ex );
129
+ }
83
130
84
- @ Override
85
- public void bgTaskCancelled () {
86
- }
87
- });
88
131
}
132
+
89
133
}
90
134
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 ());
135
+ private void explainFailure (final Throwable ex ) {
136
+ System .out .println ("Explain failuressa" );
137
+ SwingUtilities .invokeLater (new Runnable () {
138
+ @ Override
139
+ public void run () {
140
+ log .log (INFO , "performAction of TestRunHandler failed with message: {0}, \n trace: {1}" ,
141
+ new Object []{ex .getMessage (), Throwables .getStackTraceAsString (ex )});
142
+ dialogDisplayer .displayError ("Failed to run the tests: " + ex .getMessage ());
143
+ }
144
+
145
+ });
146
+ }
147
+
148
+ private void explainResults (final RunResult result , final TmcProjectInfo projectInfo , final ResultCollector resultCollector ) {
149
+ System .out .println ("Explain resultissa" );
150
+ /*SwingUtilities.invokeLater(new Runnable() {
151
+
152
+ @Override
153
+ public void run() {*/
154
+ if (result .status == COMPILE_FAILED ) {
155
+ dialogDisplayer .displayError ("The code did not compile." );
156
+ return ;
157
+ }
158
+ Exercise ex = projectMediator .tryGetExerciseForProject (projectInfo , courseDb );
159
+ boolean canSubmit = ex .isReturnable ();
160
+ List <TestCaseResult > list = testResultsToTestCaseResults (result .testResults );
161
+ resultDisplayer .showLocalRunResult (list , canSubmit , new Runnable () {
162
+ @ Override
163
+ public void run () {
164
+ exerciseSubmitter .performAction (projectInfo .getProject ());
165
+ }
166
+ }, resultCollector );
167
+ /*
168
+ }
169
+
170
+ });*/
171
+ }
172
+
173
+ private List <TestCaseResult > testResultsToTestCaseResults (ImmutableList <TestResult > testresults ) {
174
+ List <TestCaseResult > testCaseResults = new ArrayList <TestCaseResult >();
175
+ for (TestResult result : testresults ) {
176
+ TestCaseResult testCase = new TestCaseResult (result .name , result .passed , result .errorMessage );
177
+ testCaseResults .add (testCase );
101
178
}
179
+ return testCaseResults ;
102
180
}
181
+
182
+ /*private AbstractExerciseRunner getRunner(TmcProjectInfo projectInfo) {
183
+ switch (projectInfo.getProjectType()) {
184
+ case JAVA_MAVEN:
185
+ return new MavenExerciseRunner();
186
+ case JAVA_SIMPLE:
187
+ return new AntExerciseRunner();
188
+ case MAKEFILE:
189
+ return new MakefileExerciseRunner();
190
+ default:
191
+ throw new IllegalArgumentException("Unknown project type: " + projectInfo.getProjectType());
192
+ }
193
+ }*/
103
194
}
0 commit comments