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