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