FailureRunView.java revision 58a8b0aba2dec5695628a2bf25a3fae42c2c3533
1package junit.swingui;
2
3import java.awt.Component;
4import java.awt.Font;
5
6import javax.swing.DefaultListCellRenderer;
7import javax.swing.Icon;
8import javax.swing.JList;
9import javax.swing.JScrollPane;
10import javax.swing.JTabbedPane;
11import javax.swing.ListModel;
12import javax.swing.ListSelectionModel;
13import javax.swing.ScrollPaneConstants;
14import javax.swing.event.ListSelectionEvent;
15import javax.swing.event.ListSelectionListener;
16
17import junit.framework.Test;
18import junit.framework.TestFailure;
19import junit.framework.TestResult;
20import junit.runner.BaseTestRunner;
21
22
23/**
24 * A view presenting the test failures as a list.
25 */
26public class FailureRunView implements TestRunView {
27	JList fFailureList;
28	TestRunContext fRunContext;
29
30	/**
31	 * Renders TestFailures in a JList
32	 */
33	static class FailureListCellRenderer extends DefaultListCellRenderer {
34		private Icon fFailureIcon;
35		private Icon fErrorIcon;
36
37		FailureListCellRenderer() {
38	    		super();
39	    		loadIcons();
40		}
41
42		void loadIcons() {
43			fFailureIcon= TestRunner.getIconResource(getClass(), "icons/failure.gif");
44			fErrorIcon= TestRunner.getIconResource(getClass(), "icons/error.gif");
45		}
46
47		public Component getListCellRendererComponent(
48			JList list, Object value, int modelIndex,
49			boolean isSelected, boolean cellHasFocus) {
50
51		    Component c= super.getListCellRendererComponent(list, value, modelIndex, isSelected, cellHasFocus);
52			TestFailure failure= (TestFailure)value;
53			String text= failure.failedTest().toString();
54			String msg= failure.exceptionMessage();
55			if (msg != null)
56				text+= ":" + BaseTestRunner.truncate(msg);
57
58			if (failure.isFailure()) {
59				if (fFailureIcon != null)
60		    		setIcon(fFailureIcon);
61			} else {
62		    	if (fErrorIcon != null)
63		    		setIcon(fErrorIcon);
64		    }
65			setText(text);
66			setToolTipText(text);
67			return c;
68		}
69	}
70
71	public FailureRunView(TestRunContext context) {
72		fRunContext= context;
73		fFailureList= new JList(fRunContext.getFailures());
74		fFailureList.setFont(new Font("Dialog", Font.PLAIN, 12));
75
76		fFailureList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
77		fFailureList.setCellRenderer(new FailureListCellRenderer());
78		fFailureList.setVisibleRowCount(5);
79
80		fFailureList.addListSelectionListener(
81			new ListSelectionListener() {
82				public void valueChanged(ListSelectionEvent e) {
83					testSelected();
84				}
85			}
86		);
87	}
88
89	public Test getSelectedTest() {
90		int index= fFailureList.getSelectedIndex();
91		if (index == -1)
92			return null;
93
94		ListModel model= fFailureList.getModel();
95		TestFailure failure= (TestFailure)model.getElementAt(index);
96		return failure.failedTest();
97	}
98
99	public void activate() {
100		testSelected();
101	}
102
103	public void addTab(JTabbedPane pane) {
104		JScrollPane scrollPane= new JScrollPane(fFailureList, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
105		Icon errorIcon= TestRunner.getIconResource(getClass(), "icons/error.gif");
106		pane.addTab("Failures", errorIcon, scrollPane, "The list of failed tests");
107	}
108
109	public void revealFailure(Test failure) {
110		fFailureList.setSelectedIndex(0);
111	}
112
113	public void aboutToStart(Test suite, TestResult result) {
114	}
115
116	public void runFinished(Test suite, TestResult result) {
117	}
118
119	protected void testSelected() {
120		fRunContext.handleTestSelected(getSelectedTest());
121	}
122}
123