View | Details | Raw Unified | Return to bug 372588 | Differences between
and this patch

Collapse All | Expand All

(-)a/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/TestRunnerViewPart.java (-2 / +315 lines)
Lines 71-76 import org.eclipse.core.commands.IHandler; Link Here
71
71
72
import org.eclipse.core.runtime.Assert;
72
import org.eclipse.core.runtime.Assert;
73
import org.eclipse.core.runtime.CoreException;
73
import org.eclipse.core.runtime.CoreException;
74
import org.eclipse.core.runtime.IPath;
74
import org.eclipse.core.runtime.IProgressMonitor;
75
import org.eclipse.core.runtime.IProgressMonitor;
75
import org.eclipse.core.runtime.IStatus;
76
import org.eclipse.core.runtime.IStatus;
76
import org.eclipse.core.runtime.Platform;
77
import org.eclipse.core.runtime.Platform;
Lines 79-84 import org.eclipse.core.runtime.jobs.ILock; Link Here
79
import org.eclipse.core.runtime.jobs.Job;
80
import org.eclipse.core.runtime.jobs.Job;
80
import org.eclipse.core.runtime.preferences.InstanceScope;
81
import org.eclipse.core.runtime.preferences.InstanceScope;
81
82
83
import org.eclipse.core.resources.IFile;
84
82
import org.eclipse.jface.action.Action;
85
import org.eclipse.jface.action.Action;
83
import org.eclipse.jface.action.IAction;
86
import org.eclipse.jface.action.IAction;
84
import org.eclipse.jface.action.IMenuListener;
87
import org.eclipse.jface.action.IMenuListener;
Lines 96-105 import org.eclipse.jface.dialogs.InputDialog; Link Here
96
import org.eclipse.jface.dialogs.MessageDialog;
99
import org.eclipse.jface.dialogs.MessageDialog;
97
import org.eclipse.jface.operation.IRunnableWithProgress;
100
import org.eclipse.jface.operation.IRunnableWithProgress;
98
import org.eclipse.jface.resource.ImageDescriptor;
101
import org.eclipse.jface.resource.ImageDescriptor;
102
import org.eclipse.jface.viewers.IPostSelectionProvider;
103
import org.eclipse.jface.viewers.ISelection;
104
import org.eclipse.jface.viewers.ISelectionChangedListener;
105
import org.eclipse.jface.viewers.ISelectionProvider;
106
import org.eclipse.jface.viewers.SelectionChangedEvent;
107
108
import org.eclipse.jface.text.ITextSelection;
99
109
100
import org.eclipse.ui.IActionBars;
110
import org.eclipse.ui.IActionBars;
101
import org.eclipse.ui.IEditorActionBarContributor;
111
import org.eclipse.ui.IEditorActionBarContributor;
102
import org.eclipse.ui.IEditorPart;
112
import org.eclipse.ui.IEditorPart;
113
import org.eclipse.ui.IEditorReference;
103
import org.eclipse.ui.IMemento;
114
import org.eclipse.ui.IMemento;
104
import org.eclipse.ui.IPartListener2;
115
import org.eclipse.ui.IPartListener2;
105
import org.eclipse.ui.IViewPart;
116
import org.eclipse.ui.IViewPart;
Lines 115-120 import org.eclipse.ui.actions.ActionFactory; Link Here
115
import org.eclipse.ui.handlers.IHandlerActivation;
126
import org.eclipse.ui.handlers.IHandlerActivation;
116
import org.eclipse.ui.handlers.IHandlerService;
127
import org.eclipse.ui.handlers.IHandlerService;
117
import org.eclipse.ui.part.EditorActionBarContributor;
128
import org.eclipse.ui.part.EditorActionBarContributor;
129
import org.eclipse.ui.part.FileEditorInput;
118
import org.eclipse.ui.part.PageSwitcher;
130
import org.eclipse.ui.part.PageSwitcher;
119
import org.eclipse.ui.part.ViewPart;
131
import org.eclipse.ui.part.ViewPart;
120
import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
132
import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
Lines 128-138 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; Link Here
128
import org.eclipse.debug.ui.DebugUITools;
140
import org.eclipse.debug.ui.DebugUITools;
129
141
130
import org.eclipse.jdt.core.ElementChangedEvent;
142
import org.eclipse.jdt.core.ElementChangedEvent;
143
import org.eclipse.jdt.core.IClasspathEntry;
144
import org.eclipse.jdt.core.ICompilationUnit;
131
import org.eclipse.jdt.core.IElementChangedListener;
145
import org.eclipse.jdt.core.IElementChangedListener;
132
import org.eclipse.jdt.core.IJavaElement;
146
import org.eclipse.jdt.core.IJavaElement;
133
import org.eclipse.jdt.core.IJavaElementDelta;
147
import org.eclipse.jdt.core.IJavaElementDelta;
134
import org.eclipse.jdt.core.IJavaProject;
148
import org.eclipse.jdt.core.IJavaProject;
149
import org.eclipse.jdt.core.IMethod;
150
import org.eclipse.jdt.core.IType;
135
import org.eclipse.jdt.core.JavaCore;
151
import org.eclipse.jdt.core.JavaCore;
152
import org.eclipse.jdt.core.JavaModelException;
136
153
137
import org.eclipse.jdt.internal.junit.BasicElementLabels;
154
import org.eclipse.jdt.internal.junit.BasicElementLabels;
138
import org.eclipse.jdt.internal.junit.JUnitCorePlugin;
155
import org.eclipse.jdt.internal.junit.JUnitCorePlugin;
Lines 149-154 import org.eclipse.jdt.internal.junit.model.TestRunSession; Link Here
149
166
150
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
167
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
151
168
169
import org.eclipse.jdt.ui.JavaUI;
170
171
import org.eclipse.jdt.internal.ui.actions.AbstractToggleLinkingAction;
172
import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
152
import org.eclipse.jdt.internal.ui.viewsupport.ViewHistory;
173
import org.eclipse.jdt.internal.ui.viewsupport.ViewHistory;
153
174
154
/**
175
/**
Lines 210-215 public class TestRunnerViewPart extends ViewPart { Link Here
210
	private Action fPreviousAction;
231
	private Action fPreviousAction;
211
232
212
	private StopAction fStopAction;
233
	private StopAction fStopAction;
234
235
	private LinkWithEditorAction fLinkWithEditorAction;
236
213
	private JUnitCopyAction fCopyAction;
237
	private JUnitCopyAction fCopyAction;
214
	private Action fPasteAction;
238
	private Action fPasteAction;
215
239
Lines 342-348 public class TestRunnerViewPart extends ViewPart { Link Here
342
366
343
	protected boolean fPartIsVisible= false;
367
	protected boolean fPartIsVisible= false;
344
368
345
346
	private class RunnerViewHistory extends ViewHistory<TestRunSession> {
369
	private class RunnerViewHistory extends ViewHistory<TestRunSession> {
347
370
348
		@Override
371
		@Override
Lines 705-710 public class TestRunnerViewPart extends ViewPart { Link Here
705
			startUpdateJobs();
728
			startUpdateJobs();
706
729
707
			fStopAction.setEnabled(true);
730
			fStopAction.setEnabled(true);
731
			fLinkWithEditorAction.setEnabled(true);
732
708
			fRerunLastTestAction.setEnabled(true);
733
			fRerunLastTestAction.setEnabled(true);
709
		}
734
		}
710
735
Lines 722-727 public class TestRunnerViewPart extends ViewPart { Link Here
722
					if (isDisposed())
747
					if (isDisposed())
723
						return;
748
						return;
724
					fStopAction.setEnabled(lastLaunchIsKeptAlive());
749
					fStopAction.setEnabled(lastLaunchIsKeptAlive());
750
					fLinkWithEditorAction.setEnabled(fTestRunSession != null);
725
					updateRerunFailedFirstAction();
751
					updateRerunFailedFirstAction();
726
					processChangesInUI();
752
					processChangesInUI();
727
					if (hasErrorsOrFailures()) {
753
					if (hasErrorsOrFailures()) {
Lines 899-904 public class TestRunnerViewPart extends ViewPart { Link Here
899
		}
925
		}
900
	}
926
	}
901
927
928
	private class LinkWithEditorAction extends AbstractToggleLinkingAction {
929
		
930
		@Override
931
		public void run() {
932
			setLinkingEnabled(isChecked());
933
		}
934
	}
935
902
	private class RerunLastAction extends Action {
936
	private class RerunLastAction extends Action {
903
		public RerunLastAction() {
937
		public RerunLastAction() {
904
			setText(JUnitMessages.TestRunnerViewPart_rerunaction_label);
938
			setText(JUnitMessages.TestRunnerViewPart_rerunaction_label);
Lines 1200-1205 public class TestRunnerViewPart extends ViewPart { Link Here
1200
		}
1234
		}
1201
	}
1235
	}
1202
1236
1237
	/**
1238
	 * Links the selected test method with the Java Editor
1239
	 * @param enabled boolean to indicate if the link with editor is enabled (<code>true</code>) or not (<code>false</code>) 
1240
	 */
1241
	public void setLinkingEnabled(boolean enabled) {
1242
		final IWorkbenchPage page = getSite().getPage();
1243
		if(page == null) {
1244
			return;
1245
		}
1246
		if (enabled) {
1247
			// add an IPartListener for future editor activations/opening/closing/etc.
1248
			page.addPartListener(fLinkWithEditorPartListener);
1249
		} else {
1250
			// removes the IPartListener
1251
			page.removePartListener(fLinkWithEditorPartListener);
1252
		}
1253
		for(IEditorReference editorReference : page.getEditorReferences()) {
1254
			final IEditorPart editor = editorReference.getEditor(false);
1255
			// set the current editor as active as well
1256
			if (editor != null && enabled) {
1257
				editorActivated(editor);
1258
			} 
1259
			// unset the current editor as active as well
1260
			else if(editor != null && !enabled){
1261
				editorDeactivated(editor);
1262
			}
1263
		}
1264
	}
1265
	
1266
	
1267
	private final IPartListener2 fLinkWithEditorPartListener= new IPartListener2() {
1268
		public void partVisible(IWorkbenchPartReference partRef) {}
1269
		public void partBroughtToTop(IWorkbenchPartReference partRef) {}
1270
		public void partHidden(IWorkbenchPartReference partRef) {}
1271
		public void partOpened(IWorkbenchPartReference partRef) {}
1272
		public void partInputChanged(IWorkbenchPartReference partRef) {}
1273
		public void partClosed(IWorkbenchPartReference partRef) {}
1274
1275
		public void partActivated(IWorkbenchPartReference partRef) {
1276
			if (partRef instanceof IEditorReference) {
1277
				editorActivated(((IEditorReference) partRef).getEditor(true));
1278
			}
1279
		}
1280
		
1281
		public void partDeactivated(IWorkbenchPartReference partRef) {
1282
			if (partRef instanceof IEditorReference) {
1283
				editorDeactivated(((IEditorReference) partRef).getEditor(true));
1284
			}
1285
		}
1286
1287
	};
1288
	
1289
	/**
1290
	 * Static Inner Java Editor selection listener
1291
	 * When the user moves to another 
1292
	 */
1293
	private class JavaEditorSelectionListener implements ISelectionChangedListener {
1294
		
1295
		private final ICompilationUnit compilationUnit;
1296
1297
		JavaEditorSelectionListener(final ICompilationUnit compilationUnit) {
1298
			this.compilationUnit = compilationUnit;
1299
		}
1300
		
1301
		public void selectionChanged(final SelectionChangedEvent event) {
1302
			selectActiveTestCaseElement(event.getSelection());
1303
		}
1304
		
1305
		private IJavaElement getActiveJavaElement(final ISelection selection) {
1306
			if(selection instanceof ITextSelection) {
1307
				final int offset= ((ITextSelection)selection).getOffset();
1308
				try {
1309
					return compilationUnit.getElementAt(offset);
1310
				} catch (JavaModelException e) {
1311
					JUnitPlugin.log(e);
1312
				}
1313
			}
1314
			return null;
1315
		}
1316
1317
		public void selectActiveTestCaseElement(final ISelection selection) {
1318
			final IJavaElement activeJavaElement = getActiveJavaElement(selection);
1319
			// select the method in the JUnit ViewPart
1320
			if(activeJavaElement != null && activeJavaElement.getElementType() == IJavaElement.METHOD) {
1321
				final IMethod activeJavaMethod = (IMethod)activeJavaElement;
1322
				final IType activeJavaType = (IType)activeJavaMethod.getAncestor(IJavaElement.TYPE);
1323
				fTestViewer.selectTestCaseElement(activeJavaType.getFullyQualifiedName(), activeJavaMethod.getElementName());
1324
			}
1325
			
1326
		}
1327
		
1328
		/**
1329
		 * Overriding java.lang.Object#hashCode() using the
1330
		 * {@link ICompilationUnit#getHandleIdentifier()} value to avoid duplicate listener
1331
		 * registrations in the {@link JavaEditor}'s {@link IPostSelectionProvider}.
1332
		 */
1333
		@Override
1334
		public int hashCode() {
1335
			final int prime= 31;
1336
			int result= 1;
1337
			result= prime * result + ((compilationUnit == null) ? 0 : compilationUnit.getHandleIdentifier().hashCode());
1338
			return result;
1339
		}
1340
1341
		/**
1342
		 * Overriding java.lang.Object#equals() using the
1343
		 * {@link ICompilationUnit#getHandleIdentifier()} value to avoid duplicate listener
1344
		 * registrations in the {@link JavaEditor}'s {@link IPostSelectionProvider}.
1345
		 */
1346
		@Override
1347
		public boolean equals(Object obj) {
1348
			if (this == obj) {
1349
				return true;
1350
			}
1351
			if (obj == null) {
1352
				return false;
1353
			}
1354
			if (getClass() != obj.getClass()) {
1355
				return false;
1356
			}
1357
			JavaEditorSelectionListener other= (JavaEditorSelectionListener)obj;
1358
			if (compilationUnit == null) {
1359
				if (other.compilationUnit != null) {
1360
					return false;
1361
				}
1362
			} else if (!compilationUnit.getHandleIdentifier().equals(other.compilationUnit.getHandleIdentifier())) {
1363
				return false;
1364
			}
1365
			return true;
1366
		}
1367
1368
		
1369
	}
1370
	
1371
	private JavaEditorSelectionListener fJavaEditorSelectionListener;
1372
1373
	/**
1374
	 * An editor has been activated.  Set the selection in this JUnit ViewPart
1375
	 * to match the editor's input, if linking is enabled.
1376
	 * @param editor the activated editor
1377
	 */
1378
	private void editorActivated(IEditorPart editor) {
1379
		if(!(editor instanceof JavaEditor)) {
1380
			return;
1381
		}
1382
		final ISelectionProvider selectionProvider = ((JavaEditor)editor).getSelectionProvider();
1383
		final Object input= JavaUI.getEditorInputJavaElement(editor.getEditorInput());
1384
		if (input instanceof ICompilationUnit && selectionProvider instanceof IPostSelectionProvider) {
1385
			final ICompilationUnit unit = (ICompilationUnit) input;
1386
			final IPostSelectionProvider postSelectionProvider = (IPostSelectionProvider)selectionProvider;
1387
			fJavaEditorSelectionListener = new JavaEditorSelectionListener(unit);
1388
			postSelectionProvider.addPostSelectionChangedListener(fJavaEditorSelectionListener);
1389
			fJavaEditorSelectionListener.selectActiveTestCaseElement(selectionProvider.getSelection());
1390
		}
1391
	}
1392
	
1393
	/**
1394
	 * An editor has been deactivated. 
1395
	 * @param editor the activated editor
1396
	 */
1397
	private void editorDeactivated(IEditorPart editor) {
1398
		if(!(editor instanceof JavaEditor)) {
1399
			return;
1400
		}
1401
		final ISelectionProvider selectionProvider = ((JavaEditor)editor).getSelectionProvider();
1402
		if (selectionProvider instanceof IPostSelectionProvider) {
1403
			final IPostSelectionProvider postSelectionProvider = (IPostSelectionProvider)selectionProvider;
1404
			postSelectionProvider.removePostSelectionChangedListener(fJavaEditorSelectionListener);
1405
		}
1406
	}
1407
	
1408
	
1203
	private void startUpdateJobs() {
1409
	private void startUpdateJobs() {
1204
		postSyncProcessChanges();
1410
		postSyncProcessChanges();
1205
1411
Lines 1507-1517 action enablement Link Here
1507
				startUpdateJobs();
1713
				startUpdateJobs();
1508
1714
1509
				fStopAction.setEnabled(true);
1715
				fStopAction.setEnabled(true);
1716
				fLinkWithEditorAction.setEnabled(true);
1510
1717
1511
			} else /* old or fresh session: don't want jobs at this stage */ {
1718
			} else /* old or fresh session: don't want jobs at this stage */ {
1512
				stopUpdateJobs();
1719
				stopUpdateJobs();
1513
1720
1514
				fStopAction.setEnabled(fTestRunSession.isKeptAlive());
1721
				fStopAction.setEnabled(fTestRunSession.isKeptAlive());
1722
				fLinkWithEditorAction.setEnabled(fTestRunSession != null);
1515
				fTestViewer.expandFirstLevel();
1723
				fTestViewer.expandFirstLevel();
1516
			}
1724
			}
1517
		}
1725
		}
Lines 1887-1892 action enablement Link Here
1887
		fStopAction= new StopAction();
2095
		fStopAction= new StopAction();
1888
		fStopAction.setEnabled(false);
2096
		fStopAction.setEnabled(false);
1889
2097
2098
		fLinkWithEditorAction= new LinkWithEditorAction();
2099
		fLinkWithEditorAction.setEnabled(false);
2100
1890
		fRerunLastTestAction= new RerunLastAction();
2101
		fRerunLastTestAction= new RerunLastAction();
1891
		IHandlerService handlerService= (IHandlerService) getSite().getWorkbenchWindow().getService(IHandlerService.class);
2102
		IHandlerService handlerService= (IHandlerService) getSite().getWorkbenchWindow().getService(IHandlerService.class);
1892
		IHandler handler = new AbstractHandler() {
2103
		IHandler handler = new AbstractHandler() {
Lines 1937-1943 action enablement Link Here
1937
		toolBar.add(fRerunFailedFirstAction);
2148
		toolBar.add(fRerunFailedFirstAction);
1938
		toolBar.add(fStopAction);
2149
		toolBar.add(fStopAction);
1939
		toolBar.add(fViewHistory.createHistoryDropDownAction());
2150
		toolBar.add(fViewHistory.createHistoryDropDownAction());
1940
2151
		toolBar.add(new Separator());
2152
		toolBar.add(fLinkWithEditorAction);
2153
		
1941
2154
1942
		viewMenu.add(fShowTestHierarchyAction);
2155
		viewMenu.add(fShowTestHierarchyAction);
1943
		viewMenu.add(fShowTimeAction);
2156
		viewMenu.add(fShowTimeAction);
Lines 2007-2012 action enablement Link Here
2007
	public void handleTestSelected(TestElement test) {
2220
	public void handleTestSelected(TestElement test) {
2008
		showFailure(test);
2221
		showFailure(test);
2009
		fCopyAction.handleTestSelected(test);
2222
		fCopyAction.handleTestSelected(test);
2223
		// if LinkWithEditor is active, reveal the JavaEditor and select the java method 
2224
		// matching the selected test element, even if the JavaEditor is opened by not active.
2225
		if(fLinkWithEditorAction.isChecked() && test instanceof TestCaseElement) {
2226
			try {
2227
				final TestCaseElement testCaseElement= (TestCaseElement)test;
2228
				for (IEditorReference editorReference : PlatformUI.getWorkbench().getActiveWorkbenchWindow()
2229
						.getActivePage().getEditorReferences()) {
2230
					final IEditorPart editor = editorReference.getEditor(true);
2231
					final IMethod selectedMethod = findSelectedMethodInEditor(editor, testCaseElement);
2232
					if(selectedMethod != null) {
2233
						JavaUI.openInEditor(selectedMethod, false, true);
2234
						break;
2235
					}
2236
				}
2237
			} catch(JavaModelException e) {
2238
				JUnitPlugin.log(e);
2239
			} catch(PartInitException e) {
2240
				JUnitPlugin.log(e);
2241
			}
2242
		}
2243
	}
2244
	
2245
	
2246
	/**
2247
	 * Finds and returns the selected {@link IMethod} matching the given {@link TestCaseElement} in
2248
	 * the given {@link IEditorPart} (if this later is a {@link JavaEditor}), or returns null if the
2249
	 * editor is not a {@link JavaEditor} or is not opened on the expected {@link ICompilationUnit}.
2250
	 * 
2251
	 * @param editor the editor to inspect
2252
	 * @param test the selected test case element 
2253
	 * @return the corresponding Java method element or null
2254
	 * @throws JavaModelException if the inspection of one of the java elements in the given editor failed.
2255
	 */
2256
	private IMethod findSelectedMethodInEditor(IEditorPart editor, TestCaseElement test) throws JavaModelException {
2257
		final String editorClassName = getJavaEditorClassName(editor);
2258
		final String testMethodName= getTestMethodName(test);
2259
		if(test.getClassName().equals(editorClassName)) {
2260
			final IJavaElement editorInputJavaElement= JavaUI.getEditorInputJavaElement(editor.getEditorInput());
2261
			if (editorInputJavaElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
2262
				for (IType type : ((ICompilationUnit)editorInputJavaElement).getTypes()) {
2263
					if (type.getFullyQualifiedName().equals(test.getClassName())) {
2264
						for (IMethod method : type.getMethods()) {
2265
							if (method.getElementName().equals(testMethodName)) {
2266
								return method;
2267
							}
2268
						}
2269
					}
2270
				}
2271
			}
2272
		}
2273
		return null;
2274
	}
2275
2276
	/**
2277
	 * @return the fully qualified name of the Java class opened by the editor if it is a JavaEditor, null otherwise.
2278
	 * @param editor the editor to inspect
2279
	 */
2280
	private String getJavaEditorClassName(final IEditorPart editor) {
2281
		if(editor instanceof JavaEditor && editor.getEditorInput() instanceof FileEditorInput) {
2282
			final JavaEditor javaEditor = (JavaEditor)editor;
2283
			return getEditorClassName(javaEditor);
2284
		}
2285
		return null;
2286
	}
2287
	
2288
	/** 
2289
	 * @return the name of the Java method associated with this test.
2290
	 * @param test the selected unit test
2291
	 */
2292
	private String getTestMethodName(TestCaseElement test) {
2293
		final String testMethodName = test.getTestMethodName();
2294
		// parameterized tests show an index that should be removed from the name
2295
		final int index = testMethodName.indexOf('[');
2296
		if(index != -1) {
2297
			return testMethodName.substring(0, index);
2298
		}
2299
		return testMethodName;
2300
	}
2301
2302
	/**
2303
	 * @return the Java classname associated with the file opened in the given {@link JavaEditor}.
2304
	 * @param javaEditor the javaEditor in which the file is opened.
2305
	 */
2306
	private String getEditorClassName(final JavaEditor javaEditor) {
2307
		try {
2308
			final FileEditorInput editorInput = (FileEditorInput)javaEditor.getEditorInput();
2309
			final IFile inputFile = editorInput.getFile();
2310
			IPath inputFilePath = inputFile.getFullPath();
2311
			IJavaProject javaProject = JavaCore.create(inputFile.getProject());
2312
			for(IClasspathEntry classpathEntry: javaProject.getRawClasspath()) {
2313
				if(classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE && classpathEntry.getPath().isPrefixOf(inputFilePath)) {
2314
					IPath sourcePath = inputFilePath.makeRelativeTo(classpathEntry.getPath());
2315
					String sourceClassName= sourcePath.removeFileExtension().toOSString().replaceAll("/", "."); //$NON-NLS-1$ //$NON-NLS-2$
2316
					return sourceClassName;
2317
				}
2318
			}
2319
		} catch (JavaModelException e) {
2320
			JUnitPlugin.log(e);
2321
		}
2322
		return null;
2010
	}
2323
	}
2011
2324
2012
	private void showFailure(final TestElement test) {
2325
	private void showFailure(final TestElement test) {
(-)a/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/TestViewer.java (-1 / +48 lines)
Lines 21-27 import java.util.LinkedList; Link Here
21
import java.util.List;
21
import java.util.List;
22
import java.util.ListIterator;
22
import java.util.ListIterator;
23
23
24
import org.eclipse.jdt.junit.model.ITestCaseElement;
24
import org.eclipse.jdt.junit.model.ITestElement;
25
import org.eclipse.jdt.junit.model.ITestElement;
26
import org.eclipse.jdt.junit.model.ITestSuiteElement;
25
27
26
import org.eclipse.swt.SWT;
28
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.dnd.Clipboard;
29
import org.eclipse.swt.dnd.Clipboard;
Lines 38-43 import org.eclipse.jface.action.IMenuManager; Link Here
38
import org.eclipse.jface.action.MenuManager;
40
import org.eclipse.jface.action.MenuManager;
39
import org.eclipse.jface.action.Separator;
41
import org.eclipse.jface.action.Separator;
40
import org.eclipse.jface.viewers.AbstractTreeViewer;
42
import org.eclipse.jface.viewers.AbstractTreeViewer;
43
import org.eclipse.jface.viewers.ISelection;
41
import org.eclipse.jface.viewers.ISelectionChangedListener;
44
import org.eclipse.jface.viewers.ISelectionChangedListener;
42
import org.eclipse.jface.viewers.IStructuredSelection;
45
import org.eclipse.jface.viewers.IStructuredSelection;
43
import org.eclipse.jface.viewers.SelectionChangedEvent;
46
import org.eclipse.jface.viewers.SelectionChangedEvent;
Lines 568-573 public class TestViewer { Link Here
568
			fTreeViewer.reveal(current);
571
			fTreeViewer.reveal(current);
569
	}
572
	}
570
573
574
	/**
575
	 * Selects and reveals the given {@link ITestCaseElement} in the viewer's tree.
576
	 * @param testClassName the qualified class name of the test to select
577
	 * @param testMethodName the method name of the test to select
578
	 */
579
	public void selectTestCaseElement(final String testClassName, final String testMethodName) {
580
		// skip if history was cleared and 'Link with Editor' is still enabled.
581
		if(fTestRunSession == null) {
582
			return;
583
		}
584
		final TestRoot testRoot = fTestRunSession.getTestRoot();
585
		final ISelection currentSelection= getActiveViewer().getSelection();
586
		if(currentSelection instanceof IStructuredSelection) {
587
			for(Object selectedItem : ((IStructuredSelection)currentSelection).toList()) {
588
				if(selectedItem instanceof TestCaseElement && ((TestCaseElement)selectedItem).getTestClassName().equals(testClassName)
589
						&& ((TestCaseElement)selectedItem).getTestMethodName().equals(testMethodName)) {
590
					// the current selection in the TestViewer includes the given test class/method
591
					return;
592
				}
593
			}
594
		}
595
		final ITestCaseElement activeTestCaseElement = findTestCaseElement(testRoot, testClassName, testMethodName);
596
		if(activeTestCaseElement != null) {
597
			getActiveViewer().setSelection(new StructuredSelection(activeTestCaseElement), true);
598
		}
599
	}
600
	
601
	private ITestCaseElement findTestCaseElement(final ITestSuiteElement parentElement, final String testClassName, final String testMethodName) {
602
		for(ITestElement childElement : parentElement.getChildren()) {
603
			if(childElement instanceof ITestCaseElement) {
604
				ITestCaseElement testCaseElement = (ITestCaseElement)childElement;
605
				if(testCaseElement.getTestClassName().equals(testClassName) && testCaseElement.getTestMethodName().equals(testMethodName)) {
606
					return testCaseElement;
607
				}
608
			} else if(childElement instanceof ITestSuiteElement) {
609
				final ITestCaseElement localResult= findTestCaseElement((ITestSuiteElement)childElement, testClassName, testMethodName);
610
				if(localResult != null) {
611
					return localResult;
612
				}
613
			}
614
		}
615
		return null;
616
617
	}
618
	
571
	public void selectFirstFailure() {
619
	public void selectFirstFailure() {
572
		TestCaseElement firstFailure= getNextChildFailure(fTestRunSession.getTestRoot(), true);
620
		TestCaseElement firstFailure= getNextChildFailure(fTestRunSession.getTestRoot(), true);
573
		if (firstFailure != null)
621
		if (firstFailure != null)
574
- 

Return to bug 372588