1. package junit.swingui;
  2. import java.awt.*;
  3. import java.util.Vector;
  4. import javax.swing.*;
  5. import javax.swing.tree.*;
  6. import junit.framework.*;
  7. /**
  8. * A Panel showing a test suite as a tree.
  9. */
  10. class TestSuitePanel extends JPanel implements TestListener {
  11. private JTree fTree;
  12. private JScrollPane fScrollTree;
  13. private TestTreeModel fModel;
  14. static class TestTreeCellRenderer extends DefaultTreeCellRenderer {
  15. private Icon fErrorIcon;
  16. private Icon fOkIcon;
  17. private Icon fFailureIcon;
  18. TestTreeCellRenderer() {
  19. super();
  20. loadIcons();
  21. }
  22. void loadIcons() {
  23. fErrorIcon= TestRunner.getIconResource(getClass(), "icons/error.gif");
  24. fOkIcon= TestRunner.getIconResource(getClass(), "icons/ok.gif");
  25. fFailureIcon= TestRunner.getIconResource(getClass(), "icons/failure.gif");
  26. }
  27. String stripParenthesis(Object o) {
  28. String text= o.toString ();
  29. int pos= text.indexOf('(');
  30. if (pos < 1)
  31. return text;
  32. return text.substring (0, pos);
  33. }
  34. public Component getTreeCellRendererComponent(JTree tree, Object value,
  35. boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
  36. Component c= super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
  37. TreeModel model= tree.getModel();
  38. if (model instanceof TestTreeModel) {
  39. TestTreeModel testModel= (TestTreeModel)model;
  40. Test t= (Test)value;
  41. String s= "";
  42. if (testModel.isFailure(t)) {
  43. if (fFailureIcon != null)
  44. setIcon(fFailureIcon);
  45. s= " - Failed";
  46. }
  47. else if (testModel.isError(t)) {
  48. if (fErrorIcon != null)
  49. setIcon(fErrorIcon);
  50. s= " - Error";
  51. }
  52. else if (testModel.wasRun(t)) {
  53. if (fOkIcon != null)
  54. setIcon(fOkIcon);
  55. s= " - Passed";
  56. }
  57. if (c instanceof JComponent)
  58. ((JComponent)c).setToolTipText(getText()+s);
  59. }
  60. setText(stripParenthesis(value));
  61. return c;
  62. }
  63. }
  64. public TestSuitePanel() {
  65. super(new BorderLayout());
  66. setPreferredSize(new Dimension(300, 100));
  67. fTree= new JTree();
  68. fTree.setModel(null);
  69. fTree.setRowHeight(20);
  70. ToolTipManager.sharedInstance().registerComponent(fTree);
  71. fTree.putClientProperty("JTree.lineStyle", "Angled");
  72. fScrollTree= new JScrollPane(fTree);
  73. add(fScrollTree, BorderLayout.CENTER);
  74. }
  75. public void addError(final Test test, final Throwable t) {
  76. fModel.addError(test);
  77. fireTestChanged(test, true);
  78. }
  79. public void addFailure(final Test test, final AssertionFailedError t) {
  80. fModel.addFailure(test);
  81. fireTestChanged(test, true);
  82. }
  83. /**
  84. * A test ended.
  85. */
  86. public void endTest(Test test) {
  87. fModel.addRunTest(test);
  88. fireTestChanged(test, false);
  89. }
  90. /**
  91. * A test started.
  92. */
  93. public void startTest(Test test) {
  94. }
  95. /**
  96. * Returns the selected test or null if multiple or none is selected
  97. */
  98. public Test getSelectedTest() {
  99. TreePath[] paths= fTree.getSelectionPaths();
  100. if (paths != null && paths.length == 1)
  101. return (Test)paths[0].getLastPathComponent();
  102. return null;
  103. }
  104. /**
  105. * Returns the Tree
  106. */
  107. public JTree getTree() {
  108. return fTree;
  109. }
  110. /**
  111. * Shows the test hierarchy starting at the given test
  112. */
  113. public void showTestTree(Test root) {
  114. fModel= new TestTreeModel(root);
  115. fTree.setModel(fModel);
  116. fTree.setCellRenderer(new TestTreeCellRenderer());
  117. }
  118. private void fireTestChanged(final Test test, final boolean expand) {
  119. SwingUtilities.invokeLater(
  120. new Runnable() {
  121. public void run() {
  122. Vector vpath= new Vector();
  123. int index= fModel.findTest(test, (Test)fModel.getRoot(), vpath);
  124. if (index >= 0) {
  125. Object[] path= new Object[vpath.size()];
  126. vpath.copyInto(path);
  127. TreePath treePath= new TreePath(path);
  128. fModel.fireNodeChanged(treePath, index);
  129. if (expand) {
  130. Object[] fullPath= new Object[vpath.size()+1];
  131. vpath.copyInto(fullPath);
  132. fullPath[vpath.size()]= fModel.getChild(treePath.getLastPathComponent(), index);;
  133. TreePath fullTreePath= new TreePath(fullPath);
  134. fTree.scrollPathToVisible(fullTreePath);
  135. }
  136. }
  137. }
  138. }
  139. );
  140. }
  141. }