1 package junit.swingui; 2 3 import java.util.Enumeration ; 4 import java.util.Hashtable ; 5 import java.util.Vector ; 6 7 import javax.swing.event.TreeModelEvent ; 8 import javax.swing.event.TreeModelListener ; 9 import javax.swing.tree.TreeModel ; 10 import javax.swing.tree.TreePath ; 11 12 import junit.extensions.TestDecorator; 13 import junit.framework.Test; 14 import junit.framework.TestSuite; 15 16 19 class TestTreeModel implements TreeModel { 20 private Test fRoot; 21 private Vector fModelListeners= new Vector (); 22 private Hashtable fFailures= new Hashtable (); 23 private Hashtable fErrors= new Hashtable (); 24 private Hashtable fRunTests= new Hashtable (); 25 26 29 public TestTreeModel(Test root) { 30 super(); 31 fRoot= root; 32 } 33 34 37 public void addTreeModelListener(TreeModelListener l) { 38 if (!fModelListeners.contains(l)) 39 fModelListeners.addElement(l); 40 } 41 44 public void removeTreeModelListener(TreeModelListener l) { 45 fModelListeners.removeElement(l); 46 } 47 51 public int findTest(Test target, Test node, Vector path) { 52 if (target.equals(node)) 53 return 0; 54 55 TestSuite suite= isTestSuite(node); 56 for (int i= 0; i < getChildCount(node); i++) { 57 Test t= suite.testAt(i); 58 int index= findTest(target, t, path); 59 if (index >= 0) { 60 path.insertElementAt(node, 0); 61 if (path.size() == 1) 62 return i; 63 return index; 64 } 65 } 66 return -1; 67 } 68 71 public void fireNodeChanged(TreePath path, int index) { 72 int[] indices= {index}; 73 Object [] changedChildren= {getChild(path.getLastPathComponent(), index)}; 74 TreeModelEvent event= new TreeModelEvent (this, path, indices, changedChildren); 75 76 Enumeration e= fModelListeners.elements(); 77 while (e.hasMoreElements()) { 78 TreeModelListener l= (TreeModelListener ) e.nextElement(); 79 l.treeNodesChanged(event); 80 } 81 } 82 85 public Object getChild(Object parent, int index) { 86 TestSuite suite= isTestSuite(parent); 87 if (suite != null) 88 return suite.testAt(index); 89 return null; 90 } 91 94 public int getChildCount(Object parent) { 95 TestSuite suite= isTestSuite(parent); 96 if (suite != null) 97 return suite.testCount(); 98 return 0; 99 } 100 103 public int getIndexOfChild(Object parent, Object child) { 104 TestSuite suite= isTestSuite(parent); 105 if (suite != null) { 106 int i= 0; 107 for (Enumeration e= suite.tests(); e.hasMoreElements(); i++) { 108 if (child.equals(e.nextElement())) 109 return i; 110 } 111 } 112 return -1; 113 } 114 117 public Object getRoot() { 118 return fRoot; 119 } 120 123 public boolean isLeaf(Object node) { 124 return isTestSuite(node) == null; 125 } 126 129 TestSuite isTestSuite(Object node) { 130 if (node instanceof TestSuite) 131 return (TestSuite)node; 132 if (node instanceof TestDecorator) { 133 Test baseTest= ((TestDecorator)node).getTest(); 134 return isTestSuite(baseTest); 135 } 136 return null; 137 } 138 139 142 public void valueForPathChanged(TreePath path, Object newValue) { 143 System.out.println("TreeModel.valueForPathChanged: not implemented"); 145 } 146 149 void addFailure(Test t) { 150 fFailures.put(t, t); 151 } 152 155 void addError(Test t) { 156 fErrors.put(t, t); 157 } 158 161 void addRunTest(Test t) { 162 fRunTests.put(t, t); 163 } 164 167 boolean wasRun(Test t) { 168 return fRunTests.get(t) != null; 169 } 170 173 boolean isError(Test t) { 174 return (fErrors != null) && fErrors.get(t) != null; 175 } 176 179 boolean isFailure(Test t) { 180 return (fFailures != null) && fFailures.get(t) != null; 181 } 182 185 void resetResults() { 186 fFailures= new Hashtable (); 187 fRunTests= new Hashtable (); 188 fErrors= new Hashtable (); 189 } 190 } | Popular Tags |