KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junit > swingui > FailureRunView


1 package junit.swingui;
2
3 import java.awt.Component JavaDoc;
4 import java.awt.Font JavaDoc;
5
6 import javax.swing.DefaultListCellRenderer JavaDoc;
7 import javax.swing.Icon JavaDoc;
8 import javax.swing.JList JavaDoc;
9 import javax.swing.JScrollPane JavaDoc;
10 import javax.swing.JTabbedPane JavaDoc;
11 import javax.swing.ListModel JavaDoc;
12 import javax.swing.ListSelectionModel JavaDoc;
13 import javax.swing.ScrollPaneConstants JavaDoc;
14 import javax.swing.event.ListSelectionEvent JavaDoc;
15 import javax.swing.event.ListSelectionListener JavaDoc;
16
17 import junit.framework.Test;
18 import junit.framework.TestFailure;
19 import junit.framework.TestResult;
20 import junit.runner.BaseTestRunner;
21
22
23 /**
24  * A view presenting the test failures as a list.
25  */

26 public class FailureRunView implements TestRunView {
27     JList JavaDoc fFailureList;
28     TestRunContext fRunContext;
29     
30     /**
31      * Renders TestFailures in a JList
32      */

33     static class FailureListCellRenderer extends DefaultListCellRenderer JavaDoc {
34         private Icon JavaDoc fFailureIcon;
35         private Icon JavaDoc fErrorIcon;
36         
37         FailureListCellRenderer() {
38                 super();
39                 loadIcons();
40         }
41     
42         void loadIcons() {
43             fFailureIcon= TestRunner.getIconResource(getClass(), "icons/failure.gif");
44             fErrorIcon= TestRunner.getIconResource(getClass(), "icons/error.gif");
45         }
46                         
47         public Component JavaDoc getListCellRendererComponent(
48             JList JavaDoc list, Object JavaDoc value, int modelIndex,
49             boolean isSelected, boolean cellHasFocus) {
50     
51             Component JavaDoc c= super.getListCellRendererComponent(list, value, modelIndex, isSelected, cellHasFocus);
52             TestFailure failure= (TestFailure)value;
53             String JavaDoc text= failure.failedTest().toString();
54             String JavaDoc msg= failure.exceptionMessage();
55             if (msg != null)
56                 text+= ":" + BaseTestRunner.truncate(msg);
57      
58             if (failure.isFailure()) {
59                 if (fFailureIcon != null)
60                     setIcon(fFailureIcon);
61             } else {
62                 if (fErrorIcon != null)
63                     setIcon(fErrorIcon);
64             }
65             setText(text);
66             setToolTipText(text);
67             return c;
68         }
69     }
70     
71     public FailureRunView(TestRunContext context) {
72         fRunContext= context;
73         fFailureList= new JList JavaDoc(fRunContext.getFailures());
74         fFailureList.setFont(new Font JavaDoc("Dialog", Font.PLAIN, 12));
75  
76         fFailureList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
77         fFailureList.setCellRenderer(new FailureListCellRenderer());
78         fFailureList.setVisibleRowCount(5);
79
80         fFailureList.addListSelectionListener(
81             new ListSelectionListener JavaDoc() {
82                 public void valueChanged(ListSelectionEvent JavaDoc e) {
83                     testSelected();
84                 }
85             }
86         );
87     }
88     
89     public Test getSelectedTest() {
90         int index= fFailureList.getSelectedIndex();
91         if (index == -1)
92             return null;
93             
94         ListModel JavaDoc model= fFailureList.getModel();
95         TestFailure failure= (TestFailure)model.getElementAt(index);
96         return failure.failedTest();
97     }
98     
99     public void activate() {
100         testSelected();
101     }
102     
103     public void addTab(JTabbedPane JavaDoc pane) {
104         JScrollPane JavaDoc scrollPane= new JScrollPane JavaDoc(fFailureList, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
105         Icon JavaDoc errorIcon= TestRunner.getIconResource(getClass(), "icons/error.gif");
106         pane.addTab("Failures", errorIcon, scrollPane, "The list of failed tests");
107     }
108         
109     public void revealFailure(Test failure) {
110         fFailureList.setSelectedIndex(0);
111     }
112     
113     public void aboutToStart(Test suite, TestResult result) {
114     }
115
116     public void runFinished(Test suite, TestResult result) {
117     }
118
119     protected void testSelected() {
120         fRunContext.handleTestSelected(getSelectedTest());
121     }
122 }
123
Popular Tags