KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  *******************************************************************************/

11 package org.eclipse.jdt.internal.junit.ui;
12
13 import java.io.BufferedReader JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.StringReader JavaDoc;
16
17 import org.eclipse.debug.core.ILaunchManager;
18 import org.eclipse.jdt.junit.ITestRunListener;
19
20 import org.eclipse.jdt.internal.junit.Messages;
21
22 import org.eclipse.jface.action.IMenuListener;
23 import org.eclipse.jface.action.IMenuManager;
24 import org.eclipse.jface.action.MenuManager;
25 import org.eclipse.jface.action.Separator;
26 import org.eclipse.jface.util.ListenerList;
27 import org.eclipse.jface.util.OpenStrategy;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.jface.viewers.ISelectionChangedListener;
30 import org.eclipse.jface.viewers.ISelectionProvider;
31 import org.eclipse.jface.viewers.SelectionChangedEvent;
32 import org.eclipse.jface.viewers.StructuredSelection;
33 import org.eclipse.ui.IWorkbenchActionConstants;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.custom.CTabFolder;
36 import org.eclipse.swt.custom.CTabItem;
37 import org.eclipse.swt.dnd.Clipboard;
38 import org.eclipse.swt.events.DisposeEvent;
39 import org.eclipse.swt.events.DisposeListener;
40 import org.eclipse.swt.events.MouseAdapter;
41 import org.eclipse.swt.events.MouseEvent;
42 import org.eclipse.swt.events.SelectionAdapter;
43 import org.eclipse.swt.events.SelectionEvent;
44 import org.eclipse.swt.events.SelectionListener;
45 import org.eclipse.swt.graphics.Image;
46 import org.eclipse.swt.layout.GridData;
47 import org.eclipse.swt.layout.GridLayout;
48 import org.eclipse.swt.widgets.Composite;
49 import org.eclipse.swt.widgets.Menu;
50 import org.eclipse.swt.widgets.Table;
51 import org.eclipse.swt.widgets.TableItem;
52
53
54 /**
55  * A tab presenting the failed tests in a table.
56  */

57 public class FailureTab extends TestRunTab implements IMenuListener, ISelectionProvider {
58     private Table fTable;
59     private TestRunnerViewPart fRunnerViewPart;
60     private Clipboard fClipboard;
61     private boolean fMoveSelection= false;
62     private ListenerList fSelectionListeners= new ListenerList();
63     
64     private final Image fErrorIcon= TestRunnerViewPart.createImage("obj16/testerr.gif"); //$NON-NLS-1$
65
private final Image fFailureIcon= TestRunnerViewPart.createImage("obj16/testfail.gif"); //$NON-NLS-1$
66
private final Image fFailureTabIcon= TestRunnerViewPart.createImage("obj16/failures.gif"); //$NON-NLS-1$
67

68     public FailureTab() {
69     }
70
71     public void createTabControl(CTabFolder tabFolder, Clipboard clipboard, TestRunnerViewPart runner) {
72         fRunnerViewPart= runner;
73         fClipboard= clipboard;
74         
75         CTabItem failureTab= new CTabItem(tabFolder, SWT.NONE);
76         failureTab.setText(getName());
77         failureTab.setImage(fFailureTabIcon);
78
79         Composite composite= new Composite(tabFolder, SWT.NONE);
80         GridLayout gridLayout= new GridLayout();
81         gridLayout.marginHeight= 0;
82         gridLayout.marginWidth= 0;
83         composite.setLayout(gridLayout);
84         
85         GridData gridData= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL);
86         composite.setLayoutData(gridData);
87             
88         fTable= new Table(composite, SWT.NONE);
89         gridLayout= new GridLayout();
90         gridLayout.marginHeight= 0;
91         gridLayout.marginWidth= 0;
92         fTable.setLayout(gridLayout);
93         OpenStrategy handler = new OpenStrategy(fTable);
94         handler.addPostSelectionListener(new SelectionAdapter() {
95             public void widgetSelected(SelectionEvent e) {
96                 fireSelectionChanged();
97             }
98         });
99         
100         gridData= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL);
101         fTable.setLayoutData(gridData);
102         
103         failureTab.setControl(composite);
104         failureTab.setToolTipText(JUnitMessages.FailureRunView_tab_tooltip);
105         
106         initMenu();
107         addListeners();
108     }
109
110     private void disposeIcons() {
111         fErrorIcon.dispose();
112         fFailureIcon.dispose();
113         fFailureTabIcon.dispose();
114     }
115
116     private void initMenu() {
117         MenuManager menuMgr= new MenuManager("#PopupMenu"); //$NON-NLS-1$
118
menuMgr.setRemoveAllWhenShown(true);
119         menuMgr.addMenuListener(this);
120         fRunnerViewPart.getSite().registerContextMenu(menuMgr, this);
121         Menu menu= menuMgr.createContextMenu(fTable);
122         fTable.setMenu(menu);
123     }
124     
125     public String JavaDoc getName() {
126         return JUnitMessages.FailureRunView_tab_title;
127     }
128     
129     public String JavaDoc getSelectedTestId() {
130         TestRunInfo testInfo = getSelectedTestInfo();
131         if (testInfo == null)
132             return null;
133         return testInfo.getTestId();
134     }
135
136     private TestRunInfo getSelectedTestInfo() {
137         return getTestInfo(fTable.getSelectionIndex());
138     }
139
140     private TestRunInfo getTestInfo(int index) {
141         if (index == -1)
142             return null;
143         return getTestInfo(fTable.getItem(index));
144     }
145     
146     public String JavaDoc getAllFailedTestNames() {
147         StringBuffer JavaDoc trace= new StringBuffer JavaDoc();
148         String JavaDoc lineDelim= System.getProperty("line.separator", "\n"); //$NON-NLS-1$//$NON-NLS-2$
149
for (int i= 0; i < fTable.getItemCount(); i++) {
150             TestRunInfo testInfo= getTestInfo(i);
151             trace.append(testInfo.getTestName()).append(lineDelim);
152             String JavaDoc failureTrace= testInfo.getTrace();
153             if (failureTrace != null) {
154                 StringReader JavaDoc stringReader= new StringReader JavaDoc(failureTrace);
155                 BufferedReader JavaDoc bufferedReader= new BufferedReader JavaDoc(stringReader);
156                 String JavaDoc line;
157                 try {
158                     while ((line= bufferedReader.readLine()) != null)
159                         trace.append(line+lineDelim);
160                 } catch (IOException JavaDoc e) {
161                     trace.append(lineDelim);
162                 }
163             }
164         }
165         return trace.toString();
166     }
167     
168     private String JavaDoc getClassName() {
169         TableItem item= getSelectedItem();
170         TestRunInfo info= getTestInfo(item);
171         return info.getClassName();
172     }
173     
174     private String JavaDoc getMethodName() {
175         TableItem item= getSelectedItem();
176         TestRunInfo info= getTestInfo(item);
177         return info.getTestMethodName();
178     }
179     
180     public void menuAboutToShow(IMenuManager manager){
181         if (fTable.getSelectionCount() > 0) {
182             String JavaDoc className= getClassName();
183             String JavaDoc methodName= getMethodName();
184             if (className != null) {
185                 manager.add(new OpenTestAction(fRunnerViewPart, className, methodName));
186                 manager.add(new Separator());
187                 manager.add(new RerunAction(fRunnerViewPart, getSelectedTestId(), className, methodName, ILaunchManager.RUN_MODE));
188                 if (!fRunnerViewPart.lastLaunchIsKeptAlive())
189                     manager.add(new RerunAction(fRunnerViewPart, getSelectedTestId(), className, methodName, ILaunchManager.DEBUG_MODE));
190                 manager.add(new Separator());
191                 manager.add(new CopyFailureListAction(fRunnerViewPart, FailureTab.this, fClipboard));
192             }
193         }
194         manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
195         manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS + "-end")); //$NON-NLS-1$
196
}
197     
198     private TableItem getSelectedItem() {
199         int index= fTable.getSelectionIndex();
200         if (index == -1)
201             return null;
202         return fTable.getItem(index);
203     }
204         
205     public void setSelectedTest(String JavaDoc testId){
206         TableItem[] items= fTable.getItems();
207         for (int i= 0; i < items.length; i++) {
208             TableItem tableItem= items[i];
209             TestRunInfo info= getTestInfo(tableItem);
210             if (info.getTestId().equals(testId)){
211                 if (fRunnerViewPart.isAutoScroll()) {
212                     fTable.setSelection(new TableItem[] { tableItem });
213                     fTable.showItem(tableItem);
214                 }
215                 return;
216             }
217         }
218     }
219
220     private TestRunInfo getTestInfo(TableItem item) {
221         return (TestRunInfo)item.getData();
222     }
223     
224     public void setFocus() {
225         fTable.setFocus();
226     }
227     
228     public void endTest(String JavaDoc testId){
229         TestRunInfo testInfo= fRunnerViewPart.getTestInfo(testId);
230         if(testInfo == null || testInfo.getStatus() == ITestRunListener.STATUS_OK)
231             return;
232
233         TableItem tableItem= new TableItem(fTable, SWT.NONE);
234         updateTableItem(testInfo, tableItem);
235         if (fRunnerViewPart.isAutoScroll())
236             fTable.showItem(tableItem);
237     }
238
239     private void updateTableItem(TestRunInfo testInfo, TableItem tableItem) {
240         String JavaDoc label= Messages.format(JUnitMessages.FailureRunView_labelfmt, new String JavaDoc[] { testInfo.getTestMethodName(), testInfo.getClassName() });
241         tableItem.setText(label);
242         if (testInfo.getStatus() == ITestRunListener.STATUS_FAILURE)
243             tableItem.setImage(fFailureIcon);
244         else
245             tableItem.setImage(fErrorIcon);
246         tableItem.setData(testInfo);
247     }
248
249     private TableItem findItem(String JavaDoc testId) {
250         TableItem[] items= fTable.getItems();
251         for (int i= 0; i < items.length; i++) {
252             TestRunInfo info= getTestInfo(items[i]);
253             if (info.getTestId().equals(testId))
254                 return items[i];
255         }
256         return null;
257     }
258
259     public void activate() {
260         fMoveSelection= false;
261         testSelected();
262     }
263
264     public void aboutToStart() {
265         fMoveSelection= false;
266         fTable.removeAll();
267     }
268
269     private void testSelected() {
270         fRunnerViewPart.handleTestSelected(getSelectedTestInfo());
271     }
272     
273     private void addListeners() {
274         fTable.addSelectionListener(new SelectionListener() {
275             public void widgetSelected(SelectionEvent e) {
276                 activate();
277             }
278             public void widgetDefaultSelected(SelectionEvent e) {
279                 handleDefaultSelected(null);
280             }
281         });
282         
283         fTable.addDisposeListener(new DisposeListener() {
284             public void widgetDisposed(DisposeEvent e) {
285                 disposeIcons();
286             }
287         });
288
289         fTable.addMouseListener(new MouseAdapter() {
290             public void mouseDown(MouseEvent e) {
291                 activate();
292             }
293             public void mouseUp(MouseEvent e) {
294                 activate();
295             }
296         });
297     }
298     
299     void handleDefaultSelected(MouseEvent e) {
300         if (fTable.getSelectionCount() > 0)
301             new OpenTestAction(fRunnerViewPart, getClassName(), getMethodName()).run();
302     }
303     
304     /*
305      * @see ITestRunView#testStatusChanged(TestRunInfo)
306      */

307     public void testStatusChanged(TestRunInfo info) {
308         TableItem item= findItem(info.getTestId());
309         if (item != null) {
310             if (info.getStatus() == ITestRunListener.STATUS_OK) {
311                 item.dispose();
312                 return;
313             }
314             updateTableItem(info, item);
315         }
316         if (item == null && info.getStatus() != ITestRunListener.STATUS_OK) {
317             item= new TableItem(fTable, SWT.NONE);
318             updateTableItem(info, item);
319         }
320         if (item != null)
321             fTable.showItem(item);
322     }
323
324     /* (non-Javadoc)
325      * @see org.eclipse.jdt.internal.junit.ui.ITestRunView#selectNext()
326      */

327     public void selectNext() {
328         if (fTable.getItemCount() == 0)
329             return;
330             
331         int index= fTable.getSelectionIndex();
332         if (index == -1)
333             index= 0;
334         
335         if (fMoveSelection)
336             index= Math.min(fTable.getItemCount()-1, index+1);
337         else
338             fMoveSelection= true;
339             
340         selectTest(index);
341     }
342
343     /* (non-Javadoc)
344      * @see org.eclipse.jdt.internal.junit.ui.ITestRunView#selectPrevious()
345      */

346     public void selectPrevious() {
347         if (fTable.getItemCount() == 0)
348             return;
349             
350         int index= fTable.getSelectionIndex();
351         if (index == -1)
352             index= fTable.getItemCount()-1;
353             
354         if (fMoveSelection)
355             index= Math.max(0, index-1);
356         else
357             fMoveSelection= true;
358             
359         selectTest(index);
360     }
361
362     private void selectTest(int index) {
363         TableItem item= fTable.getItem(index);
364         TestRunInfo info= getTestInfo(item);
365         fRunnerViewPart.showTest(info);
366     }
367
368     public void addSelectionChangedListener(ISelectionChangedListener listener) {
369         fSelectionListeners.add(listener);
370     }
371
372     public ISelection getSelection() {
373         int index= fTable.getSelectionIndex();
374         if (index == -1)
375             return StructuredSelection.EMPTY;
376         return new StructuredSelection(getTestInfo(index));
377     }
378
379     public void removeSelectionChangedListener(ISelectionChangedListener listener) {
380         fSelectionListeners.remove(listener);
381     }
382
383     public void setSelection(ISelection selection) {
384     }
385     
386     private void fireSelectionChanged() {
387         SelectionChangedEvent event = new SelectionChangedEvent(this, getSelection());
388         Object JavaDoc[] listeners = fSelectionListeners.getListeners();
389         for (int i = 0; i < listeners.length; i++) {
390             ISelectionChangedListener listener = (ISelectionChangedListener)listeners[i];
391             listener.selectionChanged(event);
392         }
393     }
394
395 }
396
Popular Tags