5
5
import fi .helsinki .cs .tmc .model .SourceFileLookup ;
6
6
import fi .helsinki .cs .tmc .utilities .ExceptionUtils ;
7
7
8
+ import com .google .common .collect .ImmutableList ;
8
9
import java .awt .Color ;
9
10
import java .awt .GridBagConstraints ;
10
11
import java .awt .GridBagLayout ;
13
14
import java .util .HashMap ;
14
15
import java .util .logging .Level ;
15
16
import java .util .logging .Logger ;
17
+ import java .util .regex .Matcher ;
18
+ import java .util .regex .Pattern ;
16
19
17
20
import javax .swing .AbstractAction ;
18
21
import javax .swing .Action ;
@@ -50,6 +53,9 @@ public final class TestCaseResultCell {
50
53
private final GridBagConstraints gbc = new GridBagConstraints ();
51
54
private final JPanel detailView ;
52
55
private final ResultCell resultCell ;
56
+
57
+ private static final Pattern JAVA_STACKTRACE_ELEMENT_PATTERN = Pattern .compile ("([\\ w.]+)(\\ .\\ w+|\\ $([\\ w\\ .]+))\\ ((\\ w+.java):(\\ d+)\\ )" );
58
+
53
59
54
60
public TestCaseResultCell (final Exercise exercise , final TestResult result , final SourceFileLookup sourceFileLookup ) {
55
61
@@ -65,10 +71,10 @@ public TestCaseResultCell(final Exercise exercise, final TestResult result, fina
65
71
final String title = (result .isSuccessful () ? "PASS: " : "FAIL: " ) + result .getName ();
66
72
67
73
this .resultCell = new ResultCell (getResultColor (),
68
- getResultTextColor (),
69
- title ,
70
- result .getMessage (),
71
- detailView );
74
+ getResultTextColor (),
75
+ title ,
76
+ result .getMessage (),
77
+ detailView );
72
78
}
73
79
74
80
public ResultCell getCell () {
@@ -218,57 +224,54 @@ public void actionPerformed(final ActionEvent event) {
218
224
private Action detailedMessageAction = new AbstractAction ("Show detailed message" ) {
219
225
220
226
@ Override
221
- public void actionPerformed (final ActionEvent event ) {
227
+ public void actionPerformed (ActionEvent event ) {
222
228
223
229
detailView .remove (detailedMessageButton );
224
230
225
- final ExceptionDisplay display = new ExceptionDisplay ();
226
- // TODO
227
- // addException(display, result.backtrace, false);
231
+ ExceptionDisplay display = new ExceptionDisplay ();
232
+ addException (display , result .getException (), false );
228
233
display .finish ();
229
234
230
235
detailView .add (display , gbc );
231
-
232
236
233
237
resultCell .revalidate ();
234
238
resultCell .repaint ();
235
239
}
236
240
237
- // private void addException(ExceptionDisplay display, ImmutableList<String> ex, boolean isCause) {
238
- // String mainLine;
239
- // if (ex.message != null) {
240
- // mainLine = ex.className + ": " + ex.message;
241
- // } else {
242
- // mainLine = ex.className;
243
- // }
244
- // if (isCause) {
245
- // mainLine = "Caused by: " + mainLine;
246
- // }
247
- // display.addBoldTextLine(mainLine);
248
- //
249
- // addStackTraceLines(display, ex.stackTrace);
250
- //
251
- // if (ex.cause != null) {
252
- // addException(display, ex.cause, true);
253
- // }
254
- // }
255
-
256
- private void addStackTraceLines (ExceptionDisplay display , StackTraceElement [] stackTrace ) {
257
- for (final StackTraceElement ste : stackTrace ) {
258
- final FileObject sourceFile = sourceFileLookup .findSourceFileFor (exercise , ste .getClassName ());
259
-
260
- if (sourceFile != null && ste .getLineNumber () > 0 ) {
261
- display .addLink (ste .toString (), new ActionListener () {
262
- @ Override
263
- public void actionPerformed (ActionEvent e ) {
264
- openAtLine (sourceFile , ste .getLineNumber ());
265
- }
266
- });
267
- } else {
268
- display .addTextLine (ste .toString ());
241
+ private void addException (ExceptionDisplay display , ImmutableList <String > ex , boolean isCause ) {
242
+ if (ex .size () > 0 ) {
243
+ display .addBoldTextLine (ex .get (0 ));
244
+ }
245
+
246
+ addStackTraceLines (display , ex );
247
+ }
248
+
249
+ private void addStackTraceLines (ExceptionDisplay display , ImmutableList <String > stackTrace ) {
250
+ for (final String ste : stackTrace ) {
251
+ Matcher matcher = JAVA_STACKTRACE_ELEMENT_PATTERN .matcher (ste );
252
+ boolean added = false ;
253
+ if (matcher .matches ()) {
254
+ String packageAndClass = matcher .group (1 );
255
+ final int row = Integer .parseInt (matcher .group (5 ));
256
+ final FileObject sourceFile = sourceFileLookup .findSourceFileFor (exercise , packageAndClass );
257
+
258
+ if (sourceFile != null && row > 0 ) {
259
+ display .addLink (ste , new ActionListener () {
260
+ @ Override
261
+ public void actionPerformed (ActionEvent e ) {
262
+ openAtLine (sourceFile , row );
263
+ }
264
+ });
265
+ added = true ;
266
+
267
+ }
268
+ if (!added ) {
269
+ display .addTextLine (ste );
270
+ }
269
271
}
270
272
}
271
273
}
274
+
272
275
273
276
private void openAtLine (FileObject sourceFile , final int lineNum ) {
274
277
try {
0 commit comments