1 package junit.swingui; 2 3 import java.awt.*; 4 5 import javax.swing.*; 6 import javax.swing.event.*; 7 import junit.framework.*; 8 import junit.runner.BaseTestRunner; 9 10 11 /*** 12 * A view presenting the test failures as a list. 13 */ 14 public class FailureRunView implements TestRunView { 15 JList fFailureList; 16 TestRunContext fRunContext; 17 18 /*** 19 * Renders TestFailures in a JList 20 */ 21 static class FailureListCellRenderer extends DefaultListCellRenderer { 22 private Icon fFailureIcon; 23 private Icon fErrorIcon; 24 25 FailureListCellRenderer() { 26 super(); 27 loadIcons(); 28 } 29 30 void loadIcons() { 31 fFailureIcon= TestRunner.getIconResource(getClass(), "icons/failure.gif"); 32 fErrorIcon= TestRunner.getIconResource(getClass(), "icons/error.gif"); 33 } 34 35 public Component getListCellRendererComponent( 36 JList list, Object value, int modelIndex, 37 boolean isSelected, boolean cellHasFocus) { 38 39 Component c= super.getListCellRendererComponent(list, value, modelIndex, isSelected, cellHasFocus); 40 TestFailure failure= (TestFailure)value; 41 String text= failure.failedTest().toString(); 42 String msg= failure.exceptionMessage(); 43 if (msg != null) 44 text+= ":" + BaseTestRunner.truncate(msg); 45 46 if (failure.isFailure()) { 47 if (fFailureIcon != null) 48 setIcon(fFailureIcon); 49 } else { 50 if (fErrorIcon != null) 51 setIcon(fErrorIcon); 52 } 53 setText(text); 54 setToolTipText(text); 55 return c; 56 } 57 } 58 59 public FailureRunView(TestRunContext context) { 60 fRunContext= context; 61 fFailureList= new JList(fRunContext.getFailures()); 62 fFailureList.setFont(new Font("Dialog", Font.PLAIN, 12)); 63 64 fFailureList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 65 fFailureList.setCellRenderer(new FailureListCellRenderer()); 66 fFailureList.setVisibleRowCount(5); 67 68 fFailureList.addListSelectionListener( 69 new ListSelectionListener() { 70 public void valueChanged(ListSelectionEvent e) { 71 testSelected(); 72 } 73 } 74 ); 75 } 76 77 public Test getSelectedTest() { 78 int index= fFailureList.getSelectedIndex(); 79 if (index == -1) 80 return null; 81 82 ListModel model= fFailureList.getModel(); 83 TestFailure failure= (TestFailure)model.getElementAt(index); 84 return failure.failedTest(); 85 } 86 87 public void activate() { 88 testSelected(); 89 } 90 91 public void addTab(JTabbedPane pane) { 92 JScrollPane scrollPane= new JScrollPane(fFailureList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 93 Icon errorIcon= TestRunner.getIconResource(getClass(), "icons/error.gif"); 94 pane.addTab("Failures", errorIcon, scrollPane, "The list of failed tests"); 95 } 96 97 public void revealFailure(Test failure) { 98 fFailureList.setSelectedIndex(0); 99 } 100 101 public void aboutToStart(Test suite, TestResult result) { 102 } 103 104 public void runFinished(Test suite, TestResult result) { 105 } 106 107 protected void testSelected() { 108 fRunContext.handleTestSelected(getSelectedTest()); 109 } 110 }