1. package junit.swingui;
  2. import java.util.*;
  3. import javax.swing.event.*;
  4. import javax.swing.tree.*;
  5. import junit.extensions.TestDecorator;
  6. import junit.framework.*;
  7. /**
  8. * A tree model for a Test.
  9. */
  10. class TestTreeModel implements TreeModel {
  11. private Test fRoot;
  12. private Vector fModelListeners= new Vector();
  13. private Hashtable fFailures= new Hashtable();
  14. private Hashtable fErrors= new Hashtable();
  15. private Hashtable fRunTests= new Hashtable();
  16. /**
  17. * Constructs a tree model with the given test as its root.
  18. */
  19. public TestTreeModel(Test root) {
  20. super();
  21. fRoot= root;
  22. }
  23. /**
  24. * adds a TreeModelListener
  25. */
  26. public void addTreeModelListener(TreeModelListener l) {
  27. if (!fModelListeners.contains(l))
  28. fModelListeners.addElement(l);
  29. }
  30. /**
  31. * Removes a TestModelListener
  32. */
  33. public void removeTreeModelListener(TreeModelListener l) {
  34. fModelListeners.removeElement(l);
  35. }
  36. /**
  37. * Finds the path to a test. Returns the index of the test in its
  38. * parent test suite.
  39. */
  40. public int findTest(Test target, Test node, Vector path) {
  41. if (target.equals(node))
  42. return 0;
  43. TestSuite suite= isTestSuite(node);
  44. for (int i= 0; i < getChildCount(node); i++) {
  45. Test t= suite.testAt(i);
  46. int index= findTest(target, t, path);
  47. if (index >= 0) {
  48. path.insertElementAt(node, 0);
  49. if (path.size() == 1)
  50. return i;
  51. return index;
  52. }
  53. }
  54. return -1;
  55. }
  56. /**
  57. * Fires a node changed event
  58. */
  59. public void fireNodeChanged(TreePath path, int index) {
  60. int[] indices= {index};
  61. Object[] changedChildren= {getChild(path.getLastPathComponent(), index)};
  62. TreeModelEvent event= new TreeModelEvent(this, path, indices, changedChildren);
  63. Enumeration e= fModelListeners.elements();
  64. while (e.hasMoreElements()) {
  65. TreeModelListener l= (TreeModelListener) e.nextElement();
  66. l.treeNodesChanged(event);
  67. }
  68. }
  69. /**
  70. * Gets the test at the given index
  71. */
  72. public Object getChild(Object parent, int index) {
  73. TestSuite suite= isTestSuite(parent);
  74. if (suite != null)
  75. return suite.testAt(index);
  76. return null;
  77. }
  78. /**
  79. * Gets the number of tests.
  80. */
  81. public int getChildCount(Object parent) {
  82. TestSuite suite= isTestSuite(parent);
  83. if (suite != null)
  84. return suite.testCount();
  85. return 0;
  86. }
  87. /**
  88. * Gets the index of a test in a test suite
  89. */
  90. public int getIndexOfChild(Object parent, Object child) {
  91. TestSuite suite= isTestSuite(parent);
  92. if (suite != null) {
  93. int i= 0;
  94. for (Enumeration e= suite.tests(); e.hasMoreElements(); i++) {
  95. if (child.equals((Test)e.nextElement()))
  96. return i;
  97. }
  98. }
  99. return -1;
  100. }
  101. /**
  102. * Returns the root of the tree
  103. */
  104. public Object getRoot() {
  105. return fRoot;
  106. }
  107. /**
  108. * Tests if the test is a leaf.
  109. */
  110. public boolean isLeaf(Object node) {
  111. return isTestSuite(node) == null;
  112. }
  113. /**
  114. * Tests if the node is a TestSuite.
  115. */
  116. TestSuite isTestSuite(Object node) {
  117. if (node instanceof TestSuite)
  118. return (TestSuite)node;
  119. if (node instanceof TestDecorator) {
  120. Test baseTest= ((TestDecorator)node).getTest();
  121. return isTestSuite(baseTest);
  122. }
  123. return null;
  124. }
  125. /**
  126. * Called when the value of the model object was changed in the view
  127. */
  128. public void valueForPathChanged(TreePath path, Object newValue) {
  129. // we don't support direct editing of the model
  130. System.out.println("TreeModel.valueForPathChanged: not implemented");
  131. }
  132. /**
  133. * Remembers a test failure
  134. */
  135. void addFailure(Test t) {
  136. fFailures.put(t, t);
  137. }
  138. /**
  139. * Remembers a test error
  140. */
  141. void addError(Test t) {
  142. fErrors.put(t, t);
  143. }
  144. /**
  145. * Remembers that a test was run
  146. */
  147. void addRunTest(Test t) {
  148. fRunTests.put(t, t);
  149. }
  150. /**
  151. * Returns whether a test was run
  152. */
  153. boolean wasRun(Test t) {
  154. return fRunTests.get(t) != null;
  155. }
  156. /**
  157. * Tests whether a test was an error
  158. */
  159. boolean isError(Test t) {
  160. return (fErrors != null) && fErrors.get(t) != null;
  161. }
  162. /**
  163. * Tests whether a test was a failure
  164. */
  165. boolean isFailure(Test t) {
  166. return (fFailures != null) && fFailures.get(t) != null;
  167. }
  168. /**
  169. * Resets the test results
  170. */
  171. void resetResults() {
  172. fFailures= new Hashtable();
  173. fRunTests= new Hashtable();
  174. fErrors= new Hashtable();
  175. }
  176. }