1 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 40 public class FailureTrace implements IMenuListener { 41 private static final int MAX_LABEL_LENGTH = 256; 42 43 static final String FRAME_PREFIX= "at "; private Table fTable; 45 private TestRunnerViewPart fTestRunner; 46 private String 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 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 if (fFailure != null && fFailure.isComparisonFailure()) 103 manager.add(fCompareAction); 104 } 105 106 public String getTrace() { 107 return fInputTrace; 108 } 109 110 private String getSelectedText() { 111 return fTable.getSelection()[0].getText(); 112 } 113 114 private Action createOpenEditorAction(String traceLine) { 115 try { 116 String 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 lineNumber= traceLine; 125 lineNumber= lineNumber.substring(lineNumber.indexOf(':') + 1, lineNumber.lastIndexOf(')')); 126 int line= Integer.valueOf(lineNumber).intValue(); 127 String cuName= traceLine.substring(traceLine.lastIndexOf('(') + 1, traceLine.lastIndexOf(':')); 129 return new OpenEditorAtLineAction(fTestRunner, cuName, testName, line); 130 } catch(NumberFormatException e) { 131 } 132 catch(IndexOutOfBoundsException e) { 133 } 134 return null; 135 } 136 137 141 Composite getComposite(){ 142 return fTable; 143 } 144 145 148 public void refresh() { 149 updateTable(fInputTrace); 150 } 151 152 156 public void showFailure(TestElement test) { 157 fFailure= test; 158 String trace= ""; 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 trace) { 177 if(trace == null || trace.trim().equals("")) { 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 [] getFilterPatterns() { 190 if (JUnitPreferencePage.getFilterStack()) 191 return JUnitPreferencePage.getFilterPatterns(); 192 return new String [0]; 193 } 194 195 199 public void setInformation(String text) { 200 clear(); 201 TableItem tableItem= fFailureTableDisplay.newTableItem(); 202 tableItem.setText(text); 203 } 204 205 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 |