KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junit > swingui > TestRunner


1 package junit.swingui;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Component JavaDoc;
5 import java.awt.GridBagConstraints JavaDoc;
6 import java.awt.GridBagLayout JavaDoc;
7 import java.awt.GridLayout JavaDoc;
8 import java.awt.Image JavaDoc;
9 import java.awt.Insets JavaDoc;
10 import java.awt.event.ActionEvent JavaDoc;
11 import java.awt.event.ActionListener JavaDoc;
12 import java.awt.event.ItemEvent JavaDoc;
13 import java.awt.event.ItemListener JavaDoc;
14 import java.awt.event.KeyAdapter JavaDoc;
15 import java.awt.event.KeyEvent JavaDoc;
16 import java.awt.event.WindowAdapter JavaDoc;
17 import java.awt.event.WindowEvent JavaDoc;
18 import java.io.BufferedReader JavaDoc;
19 import java.io.BufferedWriter JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileReader JavaDoc;
22 import java.io.FileWriter JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import javax.swing.DefaultListModel JavaDoc;
30 import javax.swing.Icon JavaDoc;
31 import javax.swing.ImageIcon JavaDoc;
32 import javax.swing.JButton JavaDoc;
33 import javax.swing.JCheckBox JavaDoc;
34 import javax.swing.JComboBox JavaDoc;
35 import javax.swing.JFrame JavaDoc;
36 import javax.swing.JLabel JavaDoc;
37 import javax.swing.JMenu JavaDoc;
38 import javax.swing.JMenuBar JavaDoc;
39 import javax.swing.JMenuItem JavaDoc;
40 import javax.swing.JOptionPane JavaDoc;
41 import javax.swing.JPanel JavaDoc;
42 import javax.swing.JScrollPane JavaDoc;
43 import javax.swing.JSeparator JavaDoc;
44 import javax.swing.JSplitPane JavaDoc;
45 import javax.swing.JTabbedPane JavaDoc;
46 import javax.swing.ListModel JavaDoc;
47 import javax.swing.ScrollPaneConstants JavaDoc;
48 import javax.swing.SwingConstants JavaDoc;
49 import javax.swing.SwingUtilities JavaDoc;
50 import javax.swing.event.ChangeEvent JavaDoc;
51 import javax.swing.event.ChangeListener JavaDoc;
52 import javax.swing.event.DocumentEvent JavaDoc;
53
54 import junit.framework.Test;
55 import junit.framework.TestCase;
56 import junit.framework.TestFailure;
57 import junit.framework.TestResult;
58 import junit.framework.TestSuite;
59 import junit.runner.BaseTestRunner;
60 import junit.runner.FailureDetailView;
61 import junit.runner.SimpleTestCollector;
62 import junit.runner.TestCollector;
63 import junit.runner.TestRunListener;
64 import junit.runner.Version;
65
66 /**
67  * A Swing based user interface to run tests.
68  * Enter the name of a class which either provides a static
69  * suite method or is a subclass of TestCase.
70  * <pre>
71  * Synopsis: java junit.swingui.TestRunner [-noloading] [TestCase]
72  * </pre>
73  * TestRunner takes as an optional argument the name of the testcase class to be run.
74  */

75 public class TestRunner extends BaseTestRunner implements TestRunContext {
76     private static final int GAP= 4;
77     private static final int HISTORY_LENGTH= 5;
78
79     protected JFrame JavaDoc fFrame;
80     private Thread JavaDoc fRunner;
81     private TestResult fTestResult;
82
83     private JComboBox JavaDoc fSuiteCombo;
84     private ProgressBar fProgressIndicator;
85     private DefaultListModel JavaDoc fFailures;
86     private JLabel JavaDoc fLogo;
87     private CounterPanel fCounterPanel;
88     private JButton JavaDoc fRun;
89     private JButton JavaDoc fQuitButton;
90     private JButton JavaDoc fRerunButton;
91     private StatusLine fStatusLine;
92     private FailureDetailView fFailureView;
93     private JTabbedPane JavaDoc fTestViewTab;
94     private JCheckBox JavaDoc fUseLoadingRunner;
95     private Vector JavaDoc fTestRunViews= new Vector JavaDoc(); // view associated with tab in tabbed pane
96

97     private static final String JavaDoc TESTCOLLECTOR_KEY= "TestCollectorClass";
98     private static final String JavaDoc FAILUREDETAILVIEW_KEY= "FailureViewClass";
99
100     public TestRunner() {
101     }
102
103     public static void main(String JavaDoc[] args) {
104         new TestRunner().start(args);
105     }
106
107     public static void run(Class JavaDoc test) {
108         String JavaDoc args[]= { test.getName() };
109         main(args);
110     }
111
112     public void testFailed(final int status, final Test test, final Throwable JavaDoc t) {
113         SwingUtilities.invokeLater(
114             new Runnable JavaDoc() {
115                 public void run() {
116                     switch (status) {
117                         case TestRunListener.STATUS_ERROR:
118                             fCounterPanel.setErrorValue(fTestResult.errorCount());
119                             appendFailure(test, t);
120                             break;
121                         case TestRunListener.STATUS_FAILURE:
122                             fCounterPanel.setFailureValue(fTestResult.failureCount());
123                             appendFailure(test, t);
124                             break;
125                     }
126                 }
127             }
128         );
129     }
130
131     public void testStarted(String JavaDoc testName) {
132         postInfo("Running: "+testName);
133     }
134
135     public void testEnded(String JavaDoc stringName) {
136         synchUI();
137         SwingUtilities.invokeLater(
138             new Runnable JavaDoc() {
139                 public void run() {
140                     if (fTestResult != null) {
141                         fCounterPanel.setRunValue(fTestResult.runCount());
142                         fProgressIndicator.step(fTestResult.runCount(), fTestResult.wasSuccessful());
143                     }
144                 }
145             }
146         );
147     }
148
149     public void setSuite(String JavaDoc suiteName) {
150         fSuiteCombo.getEditor().setItem(suiteName);
151     }
152
153     private void addToHistory(final String JavaDoc suite) {
154         for (int i= 0; i < fSuiteCombo.getItemCount(); i++) {
155             if (suite.equals(fSuiteCombo.getItemAt(i))) {
156                 fSuiteCombo.removeItemAt(i);
157                 fSuiteCombo.insertItemAt(suite, 0);
158                 fSuiteCombo.setSelectedIndex(0);
159                 return;
160             }
161         }
162         fSuiteCombo.insertItemAt(suite, 0);
163         fSuiteCombo.setSelectedIndex(0);
164         pruneHistory();
165     }
166
167     private void pruneHistory() {
168         int historyLength= getPreference("maxhistory", HISTORY_LENGTH);
169         if (historyLength < 1)
170             historyLength= 1;
171         for (int i= fSuiteCombo.getItemCount()-1; i > historyLength-1; i--)
172             fSuiteCombo.removeItemAt(i);
173     }
174
175     private void appendFailure(Test test, Throwable JavaDoc t) {
176         fFailures.addElement(new TestFailure(test, t));
177         if (fFailures.size() == 1)
178             revealFailure(test);
179     }
180
181     private void revealFailure(Test test) {
182         for (Enumeration JavaDoc e= fTestRunViews.elements(); e.hasMoreElements(); ) {
183             TestRunView v= (TestRunView) e.nextElement();
184             v.revealFailure(test);
185         }
186     }
187
188     protected void aboutToStart(final Test testSuite) {
189         for (Enumeration JavaDoc e= fTestRunViews.elements(); e.hasMoreElements(); ) {
190             TestRunView v= (TestRunView) e.nextElement();
191             v.aboutToStart(testSuite, fTestResult);
192         }
193     }
194
195     protected void runFinished(final Test testSuite) {
196         SwingUtilities.invokeLater(
197             new Runnable JavaDoc() {
198                 public void run() {
199                     for (Enumeration JavaDoc e= fTestRunViews.elements(); e.hasMoreElements(); ) {
200                         TestRunView v= (TestRunView) e.nextElement();
201                         v.runFinished(testSuite, fTestResult);
202                     }
203                 }
204             }
205         );
206     }
207
208     protected CounterPanel createCounterPanel() {
209         return new CounterPanel();
210     }
211
212     protected JPanel JavaDoc createFailedPanel() {
213         JPanel JavaDoc failedPanel= new JPanel JavaDoc(new GridLayout JavaDoc(0, 1, 0, 2));
214         fRerunButton= new JButton JavaDoc("Run");
215         fRerunButton.setEnabled(false);
216         fRerunButton.addActionListener(
217             new ActionListener JavaDoc() {
218                 public void actionPerformed(ActionEvent JavaDoc e) {
219                     rerun();
220                 }
221             }
222         );
223         failedPanel.add(fRerunButton);
224         return failedPanel;
225     }
226
227     protected FailureDetailView createFailureDetailView() {
228         String JavaDoc className= BaseTestRunner.getPreference(FAILUREDETAILVIEW_KEY);
229         if (className != null) {
230             Class JavaDoc viewClass= null;
231             try {
232                 viewClass= Class.forName(className);
233                 return (FailureDetailView)viewClass.newInstance();
234             } catch(Exception JavaDoc e) {
235                 JOptionPane.showMessageDialog(fFrame, "Could not create Failure DetailView - using default view");
236             }
237         }
238         return new DefaultFailureDetailView();
239     }
240
241     /**
242      * Creates the JUnit menu. Clients override this
243      * method to add additional menu items.
244      */

245     protected JMenu JavaDoc createJUnitMenu() {
246         JMenu JavaDoc menu= new JMenu JavaDoc("JUnit");
247         menu.setMnemonic('J');
248         JMenuItem JavaDoc mi1= new JMenuItem JavaDoc("About...");
249         mi1.addActionListener(
250             new ActionListener JavaDoc() {
251                 public void actionPerformed(ActionEvent JavaDoc event) {
252                     about();
253                 }
254             }
255         );
256         mi1.setMnemonic('A');
257         menu.add(mi1);
258
259         menu.addSeparator();
260         JMenuItem JavaDoc mi2= new JMenuItem JavaDoc(" Exit ");
261         mi2.addActionListener(
262             new ActionListener JavaDoc() {
263                 public void actionPerformed(ActionEvent JavaDoc event) {
264                     terminate();
265                 }
266             }
267         );
268         mi2.setMnemonic('x');
269         menu.add(mi2);
270
271         return menu;
272     }
273
274     protected JFrame JavaDoc createFrame() {
275         JFrame JavaDoc frame= new JFrame JavaDoc("JUnit");
276         Image JavaDoc icon= loadFrameIcon();
277         if (icon != null)
278             frame.setIconImage(icon);
279         frame.getContentPane().setLayout(new BorderLayout JavaDoc(0, 0));
280
281         frame.addWindowListener(
282             new WindowAdapter JavaDoc() {
283                 public void windowClosing(WindowEvent JavaDoc e) {
284                     terminate();
285                 }
286             }
287         );
288         return frame;
289     }
290
291     protected JLabel JavaDoc createLogo() {
292         JLabel JavaDoc label;
293         Icon JavaDoc icon= getIconResource(BaseTestRunner.class, "logo.gif");
294         if (icon != null)
295             label= new JLabel JavaDoc(icon);
296         else
297             label= new JLabel JavaDoc("JV");
298         label.setToolTipText("JUnit Version "+Version.id());
299         return label;
300     }
301
302     protected void createMenus(JMenuBar JavaDoc mb) {
303         mb.add(createJUnitMenu());
304     }
305
306     protected JCheckBox JavaDoc createUseLoaderCheckBox() {
307         boolean useLoader= useReloadingTestSuiteLoader();
308         JCheckBox JavaDoc box= new JCheckBox JavaDoc("Reload classes every run", useLoader);
309         box.setToolTipText("Use a custom class loader to reload the classes for every run");
310         if (inVAJava())
311             box.setVisible(false);
312         return box;
313     }
314
315     protected JButton JavaDoc createQuitButton() {
316          // spaces required to avoid layout flicker
317
// Exit is shorter than Stop that shows in the same column
318
JButton JavaDoc quit= new JButton JavaDoc(" Exit ");
319         quit.addActionListener(
320             new ActionListener JavaDoc() {
321                 public void actionPerformed(ActionEvent JavaDoc e) {
322                     terminate();
323                 }
324             }
325         );
326         return quit;
327     }
328
329     protected JButton JavaDoc createRunButton() {
330         JButton JavaDoc run= new JButton JavaDoc("Run");
331         run.setEnabled(true);
332         run.addActionListener(
333             new ActionListener JavaDoc() {
334                 public void actionPerformed(ActionEvent JavaDoc e) {
335                     runSuite();
336                 }
337             }
338         );
339         return run;
340     }
341
342     protected Component JavaDoc createBrowseButton() {
343         JButton JavaDoc browse= new JButton JavaDoc("...");
344         browse.setToolTipText("Select a Test class");
345         browse.addActionListener(
346             new ActionListener JavaDoc() {
347                 public void actionPerformed(ActionEvent JavaDoc e) {
348                     browseTestClasses();
349                 }
350             }
351         );
352         return browse;
353     }
354
355     protected StatusLine createStatusLine() {
356         return new StatusLine(380);
357     }
358
359     protected JComboBox JavaDoc createSuiteCombo() {
360         JComboBox JavaDoc combo= new JComboBox JavaDoc();
361         combo.setEditable(true);
362         combo.setLightWeightPopupEnabled(false);
363
364         combo.getEditor().getEditorComponent().addKeyListener(
365             new KeyAdapter JavaDoc() {
366                 public void keyTyped(KeyEvent JavaDoc e) {
367                     textChanged();
368                     if (e.getKeyChar() == KeyEvent.VK_ENTER)
369                         runSuite();
370                 }
371             }
372         );
373         try {
374             loadHistory(combo);
375         } catch (IOException JavaDoc e) {
376             // fails the first time
377
}
378         combo.addItemListener(
379             new ItemListener JavaDoc() {
380                 public void itemStateChanged(ItemEvent JavaDoc event) {
381                     if (event.getStateChange() == ItemEvent.SELECTED) {
382                         textChanged();
383                     }
384                 }
385             }
386         );
387         return combo;
388     }
389
390     protected JTabbedPane JavaDoc createTestRunViews() {
391         JTabbedPane JavaDoc pane= new JTabbedPane JavaDoc(SwingConstants.BOTTOM);
392
393         FailureRunView lv= new FailureRunView(this);
394         fTestRunViews.addElement(lv);
395         lv.addTab(pane);
396
397         TestHierarchyRunView tv= new TestHierarchyRunView(this);
398         fTestRunViews.addElement(tv);
399         tv.addTab(pane);
400
401         pane.addChangeListener(
402             new ChangeListener JavaDoc() {
403                 public void stateChanged(ChangeEvent JavaDoc e) {
404                     testViewChanged();
405                 }
406             }
407         );
408         return pane;
409     }
410
411     public void testViewChanged() {
412         TestRunView view= (TestRunView)fTestRunViews.elementAt(fTestViewTab.getSelectedIndex());
413         view.activate();
414     }
415
416     protected TestResult createTestResult() {
417         return new TestResult();
418     }
419
420     protected JFrame JavaDoc createUI(String JavaDoc suiteName) {
421         JFrame JavaDoc frame= createFrame();
422         JMenuBar JavaDoc mb= new JMenuBar JavaDoc();
423         createMenus(mb);
424         frame.setJMenuBar(mb);
425
426         JLabel JavaDoc suiteLabel= new JLabel JavaDoc("Test class name:");
427         fSuiteCombo= createSuiteCombo();
428         fRun= createRunButton();
429         frame.getRootPane().setDefaultButton(fRun);
430         Component JavaDoc browseButton= createBrowseButton();
431
432         fUseLoadingRunner= createUseLoaderCheckBox();
433
434         fStatusLine= createStatusLine();
435         if (inMac())
436             fProgressIndicator= new MacProgressBar(fStatusLine);
437         else
438         fProgressIndicator= new ProgressBar();
439         fCounterPanel= createCounterPanel();
440
441         fFailures= new DefaultListModel JavaDoc();
442
443         fTestViewTab= createTestRunViews();
444         JPanel JavaDoc failedPanel= createFailedPanel();
445
446         fFailureView= createFailureDetailView();
447         JScrollPane JavaDoc tracePane= new JScrollPane JavaDoc(fFailureView.getComponent(), ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
448
449         
450                 
451         fQuitButton= createQuitButton();
452         fLogo= createLogo();
453
454         JPanel JavaDoc panel= new JPanel JavaDoc(new GridBagLayout JavaDoc());
455
456         addGrid(panel, suiteLabel, 0, 0, 2, GridBagConstraints.HORIZONTAL, 1.0, GridBagConstraints.WEST);
457         addGrid(panel, fSuiteCombo, 0, 1, 1, GridBagConstraints.HORIZONTAL, 1.0, GridBagConstraints.WEST);
458         addGrid(panel, browseButton, 1, 1, 1, GridBagConstraints.NONE, 0.0, GridBagConstraints.WEST);
459         addGrid(panel, fRun, 2, 1, 1, GridBagConstraints.HORIZONTAL, 0.0, GridBagConstraints.CENTER);
460
461         addGrid(panel, fUseLoadingRunner, 0, 2, 3, GridBagConstraints.NONE, 1.0, GridBagConstraints.WEST);
462         //addGrid(panel, new JSeparator(), 0, 3, 3, GridBagConstraints.HORIZONTAL, 1.0, GridBagConstraints.WEST);
463

464
465         addGrid(panel, fProgressIndicator, 0, 3, 2, GridBagConstraints.HORIZONTAL, 1.0, GridBagConstraints.WEST);
466         addGrid(panel, fLogo, 2, 3, 1, GridBagConstraints.NONE, 0.0, GridBagConstraints.NORTH);
467
468         addGrid(panel, fCounterPanel, 0, 4, 2, GridBagConstraints.NONE, 0.0, GridBagConstraints.WEST);
469         addGrid(panel, new JSeparator JavaDoc(), 0, 5, 2, GridBagConstraints.HORIZONTAL, 1.0, GridBagConstraints.WEST);
470         addGrid(panel, new JLabel JavaDoc("Results:"), 0, 6, 2, GridBagConstraints.HORIZONTAL, 1.0, GridBagConstraints.WEST);
471
472         JSplitPane JavaDoc splitter= new JSplitPane JavaDoc(JSplitPane.VERTICAL_SPLIT, fTestViewTab, tracePane);
473         addGrid(panel, splitter, 0, 7, 2, GridBagConstraints.BOTH, 1.0, GridBagConstraints.WEST);
474
475         addGrid(panel, failedPanel, 2, 7, 1, GridBagConstraints.HORIZONTAL, 0.0, GridBagConstraints.NORTH/*CENTER*/);
476
477         addGrid(panel, fStatusLine, 0, 9, 2, GridBagConstraints.HORIZONTAL, 1.0, GridBagConstraints.CENTER);
478         addGrid(panel, fQuitButton, 2, 9, 1, GridBagConstraints.HORIZONTAL, 0.0, GridBagConstraints.CENTER);
479
480         frame.setContentPane(panel);
481         frame.pack();
482         frame.setLocation(200, 200);
483         return frame;
484     }
485
486     private void addGrid(JPanel JavaDoc p, Component JavaDoc co, int x, int y, int w, int fill, double wx, int anchor) {
487         GridBagConstraints JavaDoc c= new GridBagConstraints JavaDoc();
488         c.gridx= x; c.gridy= y;
489         c.gridwidth= w;
490         c.anchor= anchor;
491         c.weightx= wx;
492         c.fill= fill;
493         if (fill == GridBagConstraints.BOTH || fill == GridBagConstraints.VERTICAL)
494             c.weighty= 1.0;
495         c.insets= new Insets JavaDoc(y == 0 ? 10 : 0, x == 0 ? 10 : GAP, GAP, GAP);
496         p.add(co, c);
497     }
498
499     protected String JavaDoc getSuiteText() {
500         if (fSuiteCombo == null)
501             return "";
502         return (String JavaDoc)fSuiteCombo.getEditor().getItem();
503     }
504
505     public ListModel JavaDoc getFailures() {
506         return fFailures;
507     }
508
509     public void insertUpdate(DocumentEvent JavaDoc event) {
510         textChanged();
511     }
512
513     protected Object JavaDoc instanciateClass(String JavaDoc fullClassName, Object JavaDoc param) {
514         try {
515             Class JavaDoc clazz= Class.forName(fullClassName);
516             if (param == null) {
517                 return clazz.newInstance();
518             } else {
519                 Class JavaDoc[] clazzParam= {param.getClass()};
520                 Constructor JavaDoc clazzConstructor= clazz.getConstructor(clazzParam);
521                 Object JavaDoc[] objectParam= {param};
522                 return clazzConstructor.newInstance(objectParam);
523             }
524         } catch (Exception JavaDoc e) {
525             e.printStackTrace();
526         }
527         return null;
528     }
529
530     public void browseTestClasses() {
531         TestCollector collector= createTestCollector();
532         TestSelector selector= new TestSelector(fFrame, collector);
533         if (selector.isEmpty()) {
534             JOptionPane.showMessageDialog(fFrame, "No Test Cases found.\nCheck that the configured \'TestCollector\' is supported on this platform.");
535             return;
536         }
537         selector.show();
538         String JavaDoc className= selector.getSelectedItem();
539         if (className != null)
540             setSuite(className);
541     }
542
543     TestCollector createTestCollector() {
544         String JavaDoc className= BaseTestRunner.getPreference(TESTCOLLECTOR_KEY);
545         if (className != null) {
546             Class JavaDoc collectorClass= null;
547             try {
548                 collectorClass= Class.forName(className);
549                 return (TestCollector)collectorClass.newInstance();
550             } catch(Exception JavaDoc e) {
551                 JOptionPane.showMessageDialog(fFrame, "Could not create TestCollector - using default collector");
552             }
553         }
554         return new SimpleTestCollector();
555     }
556
557     private Image JavaDoc loadFrameIcon() {
558         ImageIcon JavaDoc icon= (ImageIcon JavaDoc)getIconResource(BaseTestRunner.class, "smalllogo.gif");
559         if (icon != null)
560             return icon.getImage();
561         return null;
562     }
563
564     private void loadHistory(JComboBox JavaDoc combo) throws IOException JavaDoc {
565         BufferedReader JavaDoc br= new BufferedReader JavaDoc(new FileReader JavaDoc(getSettingsFile()));
566         int itemCount= 0;
567         try {
568             String JavaDoc line;
569             while ((line= br.readLine()) != null) {
570                 combo.addItem(line);
571                 itemCount++;
572             }
573             if (itemCount > 0)
574                 combo.setSelectedIndex(0);
575
576         } finally {
577             br.close();
578         }
579     }
580
581     private File JavaDoc getSettingsFile() {
582         String JavaDoc home= System.getProperty("user.home");
583         return new File JavaDoc(home,".junitsession");
584     }
585
586     private void postInfo(final String JavaDoc message) {
587         SwingUtilities.invokeLater(
588             new Runnable JavaDoc() {
589                 public void run() {
590                     showInfo(message);
591                 }
592             }
593         );
594     }
595
596     private void postStatus(final String JavaDoc status) {
597         SwingUtilities.invokeLater(
598             new Runnable JavaDoc() {
599                 public void run() {
600                     showStatus(status);
601                 }
602             }
603         );
604     }
605
606     public void removeUpdate(DocumentEvent JavaDoc event) {
607         textChanged();
608     }
609
610     private void rerun() {
611         TestRunView view= (TestRunView)fTestRunViews.elementAt(fTestViewTab.getSelectedIndex());
612         Test rerunTest= view.getSelectedTest();
613         if (rerunTest != null)
614             rerunTest(rerunTest);
615     }
616
617     private void rerunTest(Test test) {
618         if (!(test instanceof TestCase)) {
619             showInfo("Could not reload "+ test.toString());
620             return;
621         }
622         Test reloadedTest= null;
623         TestCase rerunTest= (TestCase)test;
624
625         try {
626             Class JavaDoc reloadedTestClass= getLoader().reload(test.getClass());
627             reloadedTest= TestSuite.createTest(reloadedTestClass, rerunTest.getName());
628         } catch(Exception JavaDoc e) {
629             showInfo("Could not reload "+ test.toString());
630             return;
631         }
632         TestResult result= new TestResult();
633         reloadedTest.run(result);
634
635         String JavaDoc message= reloadedTest.toString();
636         if(result.wasSuccessful())
637             showInfo(message+" was successful");
638         else if (result.errorCount() == 1)
639             showStatus(message+" had an error");
640         else
641             showStatus(message+" had a failure");
642     }
643
644     protected void reset() {
645         fCounterPanel.reset();
646         fProgressIndicator.reset();
647         fRerunButton.setEnabled(false);
648         fFailureView.clear();
649         fFailures.clear();
650     }
651
652     protected void runFailed(String JavaDoc message) {
653         showStatus(message);
654         fRun.setText("Run");
655         fRunner= null;
656     }
657
658     synchronized public void runSuite() {
659         if (fRunner != null) {
660             fTestResult.stop();
661         } else {
662             setLoading(shouldReload());
663             reset();
664             showInfo("Load Test Case...");
665             final String JavaDoc suiteName= getSuiteText();
666             final Test testSuite= getTest(suiteName);
667             if (testSuite != null) {
668                 addToHistory(suiteName);
669                 doRunTest(testSuite);
670             }
671         }
672     }
673
674     private boolean shouldReload() {
675         return !inVAJava() && fUseLoadingRunner.isSelected();
676     }
677
678
679     synchronized protected void runTest(final Test testSuite) {
680         if (fRunner != null) {
681             fTestResult.stop();
682         } else {
683             reset();
684             if (testSuite != null) {
685                 doRunTest(testSuite);
686             }
687         }
688     }
689
690     private void doRunTest(final Test testSuite) {
691         setButtonLabel(fRun, "Stop");
692         fRunner= new Thread JavaDoc("TestRunner-Thread") {
693             public void run() {
694                 TestRunner.this.start(testSuite);
695                 postInfo("Running...");
696
697                 long startTime= System.currentTimeMillis();
698                 testSuite.run(fTestResult);
699
700                 if (fTestResult.shouldStop()) {
701                     postStatus("Stopped");
702                 } else {
703                     long endTime= System.currentTimeMillis();
704                     long runTime= endTime-startTime;
705                     postInfo("Finished: " + elapsedTimeAsString(runTime) + " seconds");
706                 }
707                 runFinished(testSuite);
708                 setButtonLabel(fRun, "Run");
709                 fRunner= null;
710                 System.gc();
711             }
712         };
713         // make sure that the test result is created before we start the
714
// test runner thread so that listeners can register for it.
715
fTestResult= createTestResult();
716         fTestResult.addListener(TestRunner.this);
717         aboutToStart(testSuite);
718
719         fRunner.start();
720     }
721
722     private void saveHistory() throws IOException JavaDoc {
723         BufferedWriter JavaDoc bw= new BufferedWriter JavaDoc(new FileWriter JavaDoc(getSettingsFile()));
724         try {
725             for (int i= 0; i < fSuiteCombo.getItemCount(); i++) {
726                 String JavaDoc testsuite= fSuiteCombo.getItemAt(i).toString();
727                 bw.write(testsuite, 0, testsuite.length());
728                 bw.newLine();
729             }
730         } finally {
731             bw.close();
732         }
733     }
734
735     private void setButtonLabel(final JButton JavaDoc button, final String JavaDoc label) {
736         SwingUtilities.invokeLater(
737             new Runnable JavaDoc() {
738                 public void run() {
739                     button.setText(label);
740                 }
741             }
742         );
743     }
744
745     public void handleTestSelected(Test test) {
746         fRerunButton.setEnabled(test != null && (test instanceof TestCase));
747         showFailureDetail(test);
748     }
749
750     private void showFailureDetail(Test test) {
751         if (test != null) {
752             ListModel JavaDoc failures= getFailures();
753             for (int i= 0; i < failures.getSize(); i++) {
754                 TestFailure failure= (TestFailure)failures.getElementAt(i);
755                 if (failure.failedTest() == test) {
756                     fFailureView.showFailure(failure);
757                     return;
758                 }
759             }
760         }
761         fFailureView.clear();
762     }
763
764     private void showInfo(String JavaDoc message) {
765         fStatusLine.showInfo(message);
766     }
767
768     private void showStatus(String JavaDoc status) {
769         fStatusLine.showError(status);
770     }
771
772     /**
773      * Starts the TestRunner
774      */

775     public void start(String JavaDoc[] args) {
776         String JavaDoc suiteName= processArguments(args);
777         fFrame= createUI(suiteName);
778         fFrame.pack();
779         fFrame.setVisible(true);
780
781         if (suiteName != null) {
782             setSuite(suiteName);
783             runSuite();
784         }
785     }
786
787     private void start(final Test test) {
788         SwingUtilities.invokeLater(
789             new Runnable JavaDoc() {
790                 public void run() {
791                     int total= test.countTestCases();
792                     fProgressIndicator.start(total);
793                     fCounterPanel.setTotal(total);
794                 }
795             }
796         );
797     }
798
799     /**
800      * Wait until all the events are processed in the event thread
801      */

802     private void synchUI() {
803         try {
804             SwingUtilities.invokeAndWait(
805                 new Runnable JavaDoc() {
806                     public void run() {}
807                 }
808             );
809         }
810         catch (Exception JavaDoc e) {
811         }
812     }
813
814     /**
815      * Terminates the TestRunner
816      */

817     public void terminate() {
818         fFrame.dispose();
819         try {
820             saveHistory();
821         } catch (IOException JavaDoc e) {
822             System.out.println("Couldn't save test run history");
823         }
824         System.exit(0);
825     }
826
827     public void textChanged() {
828         fRun.setEnabled(getSuiteText().length() > 0);
829         clearStatus();
830     }
831
832     protected void clearStatus() {
833         fStatusLine.clear();
834     }
835
836     public static Icon JavaDoc getIconResource(Class JavaDoc clazz, String JavaDoc name) {
837         URL JavaDoc url= clazz.getResource(name);
838         if (url == null) {
839             System.err.println("Warning: could not load \""+name+"\" icon");
840             return null;
841         }
842         return new ImageIcon JavaDoc(url);
843     }
844
845     private void about() {
846         AboutDialog about= new AboutDialog(fFrame);
847         about.show();
848     }
849 }
850
Popular Tags