KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > ui > FailureTrace


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Sebastian Davids: sdavids@gmx.de bug 37333, 26653
11  * Johan Walles: walles@mailblocks.com bug 68737
12  *******************************************************************************/

13 package org.eclipse.jdt.internal.junit.ui;
14
15 import org.eclipse.core.runtime.Assert;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.dnd.Clipboard;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Menu;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.swt.widgets.Table;
24 import org.eclipse.swt.widgets.TableItem;
25 import org.eclipse.swt.widgets.ToolBar;
26
27 import org.eclipse.jface.action.Action;
28 import org.eclipse.jface.action.IMenuListener;
29 import org.eclipse.jface.action.IMenuManager;
30 import org.eclipse.jface.action.MenuManager;
31 import org.eclipse.jface.action.ToolBarManager;
32 import org.eclipse.jface.util.IOpenEventListener;
33 import org.eclipse.jface.util.OpenStrategy;
34
35 import org.eclipse.jdt.internal.junit.model.TestElement;
36
37 /**
38  * A pane that shows a stack trace of a failed test.
39  */

40 public class FailureTrace implements IMenuListener {
41     private static final int MAX_LABEL_LENGTH = 256;
42     
43     static final String JavaDoc FRAME_PREFIX= "at "; //$NON-NLS-1$
44
private Table fTable;
45     private TestRunnerViewPart fTestRunner;
46     private String JavaDoc fInputTrace;
47     private final Clipboard fClipboard;
48     private TestElement fFailure;
49     private CompareResultsAction fCompareAction;
50     private final FailureTableDisplay fFailureTableDisplay;
51
52     public FailureTrace(Composite parent, Clipboard clipboard, TestRunnerViewPart testRunner, ToolBar toolBar) {
53         Assert.isNotNull(clipboard);
54         
55         // fill the failure trace viewer toolbar
56
ToolBarManager failureToolBarmanager= new ToolBarManager(toolBar);
57         failureToolBarmanager.add(new EnableStackFilterAction(this));
58         fCompareAction = new CompareResultsAction(this);
59         fCompareAction.setEnabled(false);
60         failureToolBarmanager.add(fCompareAction);
61         failureToolBarmanager.update(true);
62         
63         fTable= new Table(parent, SWT.SINGLE | SWT.V_SCROLL | SWT.H_SCROLL);
64         fTestRunner= testRunner;
65         fClipboard= clipboard;
66         
67         OpenStrategy handler = new OpenStrategy(fTable);
68         handler.addOpenListener(new IOpenEventListener() {
69             public void handleOpen(SelectionEvent e) {
70                 if (fTable.getSelectionIndex() == 0 && fFailure.isComparisonFailure()) {
71                     fCompareAction.run();
72                 }
73                 if (fTable.getSelection().length != 0) {
74                     Action a = createOpenEditorAction(getSelectedText());
75                     if (a != null)
76                         a.run();
77                 }
78             }
79         });
80         
81         initMenu();
82         
83         fFailureTableDisplay = new FailureTableDisplay(fTable);
84     }
85     
86     private void initMenu() {
87         MenuManager menuMgr= new MenuManager();
88         menuMgr.setRemoveAllWhenShown(true);
89         menuMgr.addMenuListener(this);
90         Menu menu= menuMgr.createContextMenu(fTable);
91         fTable.setMenu(menu);
92     }
93     
94     public void menuAboutToShow(IMenuManager manager) {
95         if (fTable.getSelectionCount() > 0) {
96             Action a= createOpenEditorAction(getSelectedText());
97             if (a != null)
98                 manager.add(a);
99             manager.add(new JUnitCopyAction(FailureTrace.this, fClipboard));
100         }
101         // fix for bug 68058
102
if (fFailure != null && fFailure.isComparisonFailure())
103             manager.add(fCompareAction);
104     }
105
106     public String JavaDoc getTrace() {
107         return fInputTrace;
108     }
109     
110     private String JavaDoc getSelectedText() {
111         return fTable.getSelection()[0].getText();
112     }
113
114     private Action createOpenEditorAction(String JavaDoc traceLine) {
115         try {
116             String JavaDoc testName= traceLine;
117             testName= testName.substring(testName.indexOf(FRAME_PREFIX));
118             testName= testName.substring(FRAME_PREFIX.length(), testName.lastIndexOf('(')).trim();
119             testName= testName.substring(0, testName.lastIndexOf('.'));
120             int innerSeparatorIndex= testName.indexOf('$');
121             if (innerSeparatorIndex != -1)
122                 testName= testName.substring(0, innerSeparatorIndex);
123             
124             String JavaDoc lineNumber= traceLine;
125             lineNumber= lineNumber.substring(lineNumber.indexOf(':') + 1, lineNumber.lastIndexOf(')'));
126             int line= Integer.valueOf(lineNumber).intValue();
127             //fix for bug 37333
128
String JavaDoc cuName= traceLine.substring(traceLine.lastIndexOf('(') + 1, traceLine.lastIndexOf(':'));
129             return new OpenEditorAtLineAction(fTestRunner, cuName, testName, line);
130         } catch(NumberFormatException JavaDoc e) {
131         }
132         catch(IndexOutOfBoundsException JavaDoc e) {
133         }
134         return null;
135     }
136     
137     /**
138      * Returns the composite used to present the trace
139      * @return The composite
140      */

141     Composite getComposite(){
142         return fTable;
143     }
144     
145     /**
146      * Refresh the table from the trace.
147      */

148     public void refresh() {
149         updateTable(fInputTrace);
150     }
151     
152     /**
153      * Shows a TestFailure
154      * @param test the failed test
155      */

156     public void showFailure(TestElement test) {
157         fFailure= test;
158         String JavaDoc trace= ""; //$NON-NLS-1$
159
updateEnablement(test);
160         if (test != null)
161             trace= test.getTrace();
162         if (fInputTrace == trace)
163             return;
164         fInputTrace= trace;
165         updateTable(trace);
166     }
167
168     public void updateEnablement(TestElement test) {
169         boolean enableCompare= test != null && test.isComparisonFailure();
170         fCompareAction.setEnabled(enableCompare);
171         if (enableCompare) {
172             fCompareAction.updateOpenDialog(test);
173         }
174     }
175
176     private void updateTable(String JavaDoc trace) {
177         if(trace == null || trace.trim().equals("")) { //$NON-NLS-1$
178
clear();
179             return;
180         }
181         trace= trace.trim();
182         fTable.setRedraw(false);
183         fTable.removeAll();
184         new TextualTrace(trace, getFilterPatterns()).display(
185                 fFailureTableDisplay, MAX_LABEL_LENGTH);
186         fTable.setRedraw(true);
187     }
188
189     private String JavaDoc[] getFilterPatterns() {
190         if (JUnitPreferencePage.getFilterStack())
191             return JUnitPreferencePage.getFilterPatterns();
192         return new String JavaDoc[0];
193     }
194
195     /**
196      * Shows other information than a stack trace.
197      * @param text the informational message to be shown
198      */

199     public void setInformation(String JavaDoc text) {
200         clear();
201         TableItem tableItem= fFailureTableDisplay.newTableItem();
202         tableItem.setText(text);
203     }
204
205     /**
206      * Clears the non-stack trace info
207      */

208     public void clear() {
209         fTable.removeAll();
210         fInputTrace= null;
211     }
212
213     public TestElement getFailedTest() {
214         return fFailure;
215     }
216
217     public Shell getShell() {
218         return fTable.getShell();
219     }
220
221     public FailureTableDisplay getFailureTableDisplay() {
222         return fFailureTableDisplay;
223     }
224 }
225
Popular Tags