forked from junit-team/junit4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestRunner.java
481 lines (412 loc) · 12.7 KB
/
TestRunner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package junit.awtui;
import junit.framework.*;
import junit.runner.*;
import java.util.Vector;
import java.lang.reflect.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
/**
* An AWT based user interface to run tests.
* Enter the name of a class which either provides a static
* suite method or is a subclass of TestCase.
* <pre>
* Synopsis: java junit.awtui.TestRunner [-noloading] [TestCase]
* </pre>
* TestRunner takes as an optional argument the name of the testcase class to be run.
*/
public class TestRunner extends BaseTestRunner {
protected Frame fFrame;
protected Vector fExceptions;
protected Vector fFailedTests;
protected Thread fRunner;
protected TestResult fTestResult;
protected TextArea fTraceArea;
protected TextField fSuiteField;
protected Button fRun;
protected ProgressBar fProgressIndicator;
protected List fFailureList;
protected Logo fLogo;
protected Label fNumberOfErrors;
protected Label fNumberOfFailures;
protected Label fNumberOfRuns;
protected Button fQuitButton;
protected Button fRerunButton;
protected TextField fStatusLine;
protected Checkbox fUseLoadingRunner;
protected static Font PLAIN_FONT= new Font("dialog", Font.PLAIN, 12);
private static final int GAP= 4;
private static final String SUITE_METHODNAME= "suite";
public TestRunner() {
}
private void about() {
AboutDialog about= new AboutDialog(fFrame);
about.setModal(true);
about.setLocation(300, 300);
about.setVisible(true);
}
public void addError(Test test, Throwable t) {
fNumberOfErrors.setText(Integer.toString(fTestResult.errorCount()));
appendFailure("Error", test, t);
}
public void addFailure(Test test, AssertionFailedError t) {
fNumberOfFailures.setText(Integer.toString(fTestResult.failureCount()));
appendFailure("Failure", test, t);
}
protected void addGrid(Panel p, Component co, int x, int y, int w, int fill, double wx, int anchor) {
GridBagConstraints c= new GridBagConstraints();
c.gridx= x; c.gridy= y;
c.gridwidth= w;
c.anchor= anchor;
c.weightx= wx;
c.fill= fill;
if (fill == GridBagConstraints.BOTH || fill == GridBagConstraints.VERTICAL)
c.weighty= 1.0;
c.insets= new Insets(y == 0 ? GAP : 0, x == 0 ? GAP : 0, GAP, GAP);
p.add(co, c);
}
private void appendFailure(String kind, Test test, Throwable t) {
kind+= ": " + test;
String msg= t.getMessage();
if (msg != null) {
kind+= ":" + truncate(msg);
}
fFailureList.add(kind);
fExceptions.addElement(t);
fFailedTests.addElement(test);
if (fFailureList.getItemCount() == 1) {
fFailureList.select(0);
failureSelected();
}
}
/**
* Creates the JUnit menu. Clients override this
* method to add additional menu items.
*/
protected Menu createJUnitMenu() {
Menu menu= new Menu("JUnit");
MenuItem mi= new MenuItem("About...");
mi.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
about();
}
}
);
menu.add(mi);
menu.addSeparator();
mi= new MenuItem("Exit");
mi.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
}
);
menu.add(mi);
return menu;
}
protected void createMenus(MenuBar mb) {
mb.add(createJUnitMenu());
}
protected TestResult createTestResult() {
return new TestResult();
}
protected Frame createUI(String suiteName) {
Frame frame= new Frame("JUnit");
Image icon= loadFrameIcon();
if (icon != null)
frame.setIconImage(icon);
frame.setLayout(new BorderLayout(0, 0));
frame.setBackground(SystemColor.control);
final Frame finalFrame= frame;
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
finalFrame.dispose();
System.exit(0);
}
}
);
MenuBar mb = new MenuBar();
createMenus(mb);
frame.setMenuBar(mb);
//---- first section
Label suiteLabel= new Label("Test class name:");
fSuiteField= new TextField(suiteName != null ? suiteName : "");
fSuiteField.selectAll();
fSuiteField.requestFocus();
fSuiteField.setFont(PLAIN_FONT);
fSuiteField.setColumns(40);
fSuiteField.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
runSuite();
}
}
);
fSuiteField.addTextListener(
new TextListener() {
public void textValueChanged(TextEvent e) {
fRun.setEnabled(fSuiteField.getText().length() > 0);
fStatusLine.setText("");
}
}
);
fRun= new Button("Run");
fRun.setEnabled(false);
fRun.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
runSuite();
}
}
);
boolean useLoader= useReloadingTestSuiteLoader();
fUseLoadingRunner= new Checkbox("Reload classes every run", useLoader);
if (inVAJava())
fUseLoadingRunner.setVisible(false);
//---- second section
fProgressIndicator= new ProgressBar();
//---- third section
fNumberOfErrors= new Label("0000", Label.RIGHT);
fNumberOfErrors.setText("0");
fNumberOfErrors.setFont(PLAIN_FONT);
fNumberOfFailures= new Label("0000", Label.RIGHT);
fNumberOfFailures.setText("0");
fNumberOfFailures.setFont(PLAIN_FONT);
fNumberOfRuns= new Label("0000", Label.RIGHT);
fNumberOfRuns.setText("0");
fNumberOfRuns.setFont(PLAIN_FONT);
Panel numbersPanel= new Panel(new FlowLayout());
numbersPanel.add(new Label("Runs:")); numbersPanel.add(fNumberOfRuns);
numbersPanel.add(new Label(" Errors:")); numbersPanel.add(fNumberOfErrors);
numbersPanel.add(new Label(" Failures:")); numbersPanel.add(fNumberOfFailures);
//---- fourth section
Label failureLabel= new Label("Errors and Failures:");
fFailureList= new List(5);
fFailureList.addItemListener(
new ItemListener() {
public void itemStateChanged(ItemEvent e) {
failureSelected();
}
}
);
fRerunButton= new Button("Run");
fRerunButton.setEnabled(false);
fRerunButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
rerun();
}
}
);
Panel failedPanel= new Panel(new GridLayout(0, 1, 0, 2));
failedPanel.add(fRerunButton);
fTraceArea= new TextArea();
fTraceArea.setRows(5);
fTraceArea.setColumns(60);
//---- fifth section
fStatusLine= new TextField();
fStatusLine.setFont(PLAIN_FONT);
fStatusLine.setEditable(false);
fStatusLine.setForeground(Color.red);
fQuitButton= new Button("Exit");
fQuitButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
);
// ---------
fLogo= new Logo();
//---- overall layout
Panel panel= new Panel(new GridBagLayout());
addGrid(panel, suiteLabel, 0, 0, 2, GridBagConstraints.HORIZONTAL, 1.0, GridBagConstraints.WEST);
addGrid(panel, fSuiteField, 0, 1, 2, GridBagConstraints.HORIZONTAL, 1.0, GridBagConstraints.WEST);
addGrid(panel, fRun, 2, 1, 1, GridBagConstraints.HORIZONTAL, 0.0, GridBagConstraints.CENTER);
addGrid(panel, fUseLoadingRunner, 0, 2, 2, GridBagConstraints.HORIZONTAL, 1.0, GridBagConstraints.WEST);
addGrid(panel, fProgressIndicator, 0, 3, 2, GridBagConstraints.HORIZONTAL, 1.0, GridBagConstraints.WEST);
addGrid(panel, fLogo, 2, 3, 1, GridBagConstraints.NONE, 0.0, GridBagConstraints.NORTH);
addGrid(panel, numbersPanel, 0, 4, 2, GridBagConstraints.NONE, 0.0, GridBagConstraints.CENTER);
addGrid(panel, failureLabel, 0, 5, 2, GridBagConstraints.HORIZONTAL, 1.0, GridBagConstraints.WEST);
addGrid(panel, fFailureList, 0, 6, 2, GridBagConstraints.BOTH, 1.0, GridBagConstraints.WEST);
addGrid(panel, failedPanel, 2, 6, 1, GridBagConstraints.HORIZONTAL, 0.0, GridBagConstraints.CENTER);
addGrid(panel, fTraceArea, 0, 7, 2, GridBagConstraints.BOTH, 1.0, GridBagConstraints.WEST);
addGrid(panel, fStatusLine, 0, 8, 2, GridBagConstraints.HORIZONTAL, 1.0, GridBagConstraints.CENTER);
addGrid(panel, fQuitButton, 2, 8, 1, GridBagConstraints.HORIZONTAL, 0.0, GridBagConstraints.CENTER);
frame.add(panel, BorderLayout.CENTER);
frame.pack();
return frame;
}
public void failureSelected() {
fRerunButton.setEnabled(isErrorSelected());
showErrorTrace();
}
public void endTest(Test test) {
setLabelValue(fNumberOfRuns, fTestResult.runCount());
synchronized(this) {
fProgressIndicator.step(fTestResult.wasSuccessful());
}
}
private boolean isErrorSelected() {
return fFailureList.getSelectedIndex() != -1;
}
private Image loadFrameIcon() {
Toolkit toolkit= Toolkit.getDefaultToolkit();
try {
java.net.URL url= BaseTestRunner.class.getResource("smalllogo.gif");
return toolkit.createImage((ImageProducer) url.getContent());
} catch (Exception ex) {
}
return null;
}
public Thread getRunner() {
return fRunner;
}
public static void main(String[] args) {
new TestRunner().start(args);
}
public static void run(Class test) {
String args[]= { test.getName() };
main(args);
}
public void rerun() {
int index= fFailureList.getSelectedIndex();
if (index == -1)
return;
Test test= (Test)fFailedTests.elementAt(index);
if (!(test instanceof TestCase)) {
showInfo("Could not reload "+ test.toString());
return;
}
Test reloadedTest= null;
try {
Class reloadedTestClass= getLoader().reload(test.getClass());
Class[] classArgs= { String.class };
Constructor constructor= reloadedTestClass.getConstructor(classArgs);
Object[] args= new Object[]{((TestCase)test).getName()};
reloadedTest=(Test)constructor.newInstance(args);
} catch(Exception e) {
showInfo("Could not reload "+ test.toString());
return;
}
TestResult result= new TestResult();
reloadedTest.run(result);
String message= reloadedTest.toString();
if(result.wasSuccessful())
showInfo(message+" was successful");
else if (result.errorCount() == 1)
showStatus(message+" had an error");
else
showStatus(message+" had a failure");
}
protected void reset() {
setLabelValue(fNumberOfErrors, 0);
setLabelValue(fNumberOfFailures, 0);
setLabelValue(fNumberOfRuns, 0);
fProgressIndicator.reset();
fRerunButton.setEnabled(false);
fFailureList.removeAll();
fExceptions= new Vector(10);
fFailedTests= new Vector(10);
fTraceArea.setText("");
}
/**
* runs a suite.
* @deprecated use runSuite() instead
*/
public void run() {
runSuite();
}
protected void runFailed(String message) {
showStatus(message);
fRun.setLabel("Run");
fRunner= null;
}
synchronized public void runSuite() {
if (fRunner != null) {
fTestResult.stop();
} else {
setLoading(shouldReload());
fRun.setLabel("Stop");
showInfo("Initializing...");
reset();
showInfo("Load Test Case...");
final Test testSuite= getTest(fSuiteField.getText());
if (testSuite != null) {
fRunner= new Thread() {
public void run() {
fTestResult= createTestResult();
fTestResult.addListener(TestRunner.this);
fProgressIndicator.start(testSuite.countTestCases());
showInfo("Running...");
long startTime= System.currentTimeMillis();
testSuite.run(fTestResult);
if (fTestResult.shouldStop()) {
showStatus("Stopped");
} else {
long endTime= System.currentTimeMillis();
long runTime= endTime-startTime;
showInfo("Finished: " + elapsedTimeAsString(runTime) + " seconds");
}
fTestResult= null;
fRun.setLabel("Run");
fRunner= null;
System.gc();
}
};
fRunner.start();
}
}
}
private boolean shouldReload() {
return !inVAJava() && fUseLoadingRunner.getState();
}
private void setLabelValue(Label label, int value) {
label.setText(Integer.toString(value));
label.invalidate();
label.getParent().validate();
}
public void setSuiteName(String suite) {
fSuiteField.setText(suite);
}
private void showErrorTrace() {
int index= fFailureList.getSelectedIndex();
if (index == -1)
return;
Throwable t= (Throwable) fExceptions.elementAt(index);
fTraceArea.setText(getFilteredTrace(t));
}
private void showInfo(String message) {
fStatusLine.setFont(PLAIN_FONT);
fStatusLine.setForeground(Color.black);
fStatusLine.setText(message);
}
protected void clearStatus() {
showStatus("");
}
private void showStatus(String status) {
fStatusLine.setFont(PLAIN_FONT);
fStatusLine.setForeground(Color.red);
fStatusLine.setText(status);
}
/**
* Starts the TestRunner
*/
public void start(String[] args) {
String suiteName= processArguments(args);
fFrame= createUI(suiteName);
fFrame.setLocation(200, 200);
fFrame.setVisible(true);
if (suiteName != null) {
setSuiteName(suiteName);
runSuite();
}
}
public void startTest(Test test) {
showInfo("Running: "+test);
}
}