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