KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junit > swingui > TestHierarchyRunView


1 package junit.swingui;
2
3 import java.util.Vector JavaDoc;
4
5 import javax.swing.Icon JavaDoc;
6 import javax.swing.JTabbedPane JavaDoc;
7 import javax.swing.JTree JavaDoc;
8 import javax.swing.event.TreeSelectionEvent JavaDoc;
9 import javax.swing.event.TreeSelectionListener JavaDoc;
10 import javax.swing.tree.TreePath JavaDoc;
11
12 import junit.framework.Test;
13 import junit.framework.TestResult;
14
15 /**
16  * A hierarchical view of a test run.
17  * The contents of a test suite is shown
18  * as a tree.
19  */

20 public class TestHierarchyRunView implements TestRunView {
21     TestSuitePanel fTreeBrowser;
22     TestRunContext fTestContext;
23     
24     public TestHierarchyRunView(TestRunContext context) {
25         fTestContext= context;
26         fTreeBrowser= new TestSuitePanel();
27         fTreeBrowser.getTree().addTreeSelectionListener(
28             new TreeSelectionListener JavaDoc() {
29                 public void valueChanged(TreeSelectionEvent JavaDoc e) {
30                     testSelected();
31                 }
32             }
33         );
34     }
35     
36     public void addTab(JTabbedPane JavaDoc pane) {
37         Icon JavaDoc treeIcon= TestRunner.getIconResource(getClass(), "icons/hierarchy.gif");
38         pane.addTab("Test Hierarchy", treeIcon, fTreeBrowser, "The test hierarchy");
39     }
40     
41     public Test getSelectedTest() {
42         return fTreeBrowser.getSelectedTest();
43     }
44     
45     public void activate() {
46         testSelected();
47     }
48     
49     public void revealFailure(Test failure) {
50         JTree JavaDoc tree= fTreeBrowser.getTree();
51         TestTreeModel model= (TestTreeModel)tree.getModel();
52         Vector JavaDoc vpath= new Vector JavaDoc();
53         int index= model.findTest(failure, (Test)model.getRoot(), vpath);
54         if (index >= 0) {
55             Object JavaDoc[] path= new Object JavaDoc[vpath.size()+1];
56             vpath.copyInto(path);
57             Object JavaDoc last= path[vpath.size()-1];
58             path[vpath.size()]= model.getChild(last, index);
59             TreePath JavaDoc selectionPath= new TreePath JavaDoc(path);
60             tree.setSelectionPath(selectionPath);
61             tree.makeVisible(selectionPath);
62         }
63     }
64     
65     public void aboutToStart(Test suite, TestResult result) {
66         fTreeBrowser.showTestTree(suite);
67         result.addListener(fTreeBrowser);
68     }
69
70     public void runFinished(Test suite, TestResult result) {
71         result.removeListener(fTreeBrowser);
72     }
73
74     protected void testSelected() {
75         fTestContext.handleTestSelected(getSelectedTest());
76     }
77 }
78
Popular Tags