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

Collapse All | Expand All

(-)a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/junit/tests/TestSorting.java (+104 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2014 Sandra Lions 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
 *     Sandra Lions <sandra.lions-piron@oracle.com> - [JUnit] allow to sort by name and by execution time - https://bugs.eclipse.org/bugs/show_bug.cgi?id=219466
10
 *******************************************************************************/
11
package org.eclipse.jdt.junit.tests;
12
13
import java.util.ArrayList;
14
import java.util.Arrays;
15
import java.util.List;
16
17
import org.eclipse.jdt.junit.JUnitCore;
18
import org.eclipse.jdt.junit.TestRunListener;
19
20
import org.eclipse.swt.widgets.Table;
21
22
import org.eclipse.jface.viewers.TableViewer;
23
24
import org.eclipse.ui.IWorkbenchPage;
25
26
import org.eclipse.jdt.core.IType;
27
28
import org.eclipse.jdt.internal.junit.ui.JUnitPlugin;
29
import org.eclipse.jdt.internal.junit.ui.TestRunnerViewPart;
30
import org.eclipse.jdt.internal.junit.ui.TestRunnerViewPart.SortingCriterion;
31
32
public class TestSorting extends AbstractTestRunListenerTest {
33
34
	private String[] runSequenceTest(IType typeToLaunch) throws Exception {
35
		TestRunLog log= new TestRunLog();
36
		final TestRunListener testRunListener= new TestRunListeners.SequenceTest(log);
37
		JUnitCore.addTestRunListener(testRunListener);
38
		try {
39
			return launchJUnit(typeToLaunch, new TestRunLog());
40
		} finally {
41
			JUnitCore.removeTestRunListener(testRunListener);
42
		}
43
	}
44
45
	public void testSorting() throws Exception {
46
47
		IWorkbenchPage activePage= JUnitPlugin.getActivePage();
48
		TestRunnerViewPart testRunnerViewPart= (TestRunnerViewPart)activePage.showView(TestRunnerViewPart.NAME);
49
		testRunnerViewPart.setLayoutMode(TestRunnerViewPart.LAYOUT_FLAT); // TableViewer
50
51
		String source= "package pack;\n"
52
				+ "import junit.framework.TestCase;\n"
53
				+ "public class ATestCase extends TestCase {\n"
54
				+ "	private String fString;\n"
55
				+ "	public void testB_FirstTest() throws Exception {\n"
56
				+ "	    fString= \"first\";\n"
57
				+ "	    Thread.sleep(30);\n"
58
				+ "	}\n"
59
				+ "	public void testa_SecondTest() throws Exception {\n"
60
				+ "	    fString= \"second\";\n"
61
				+ "	    Thread.sleep(50);\n"
62
				+ "	}\n"
63
				+ "	public void testC_ThirdTest() throws Exception {\n"
64
				+ "	    fString= \"second\";\n"
65
				+ "	    Thread.sleep(50);\n"
66
				+ "	}\n"
67
				+ "	public void testA_FourthTest() throws Exception {\n"
68
				+ "	    fString= \"third\";\n"
69
				+ "	    Thread.sleep(40);\n"
70
				+ "	}\n"
71
				+ "}";
72
73
		IType aTestCase= createType(source, "pack", "ATestCase.java");
74
		runSequenceTest(aTestCase);
75
76
		Table table= ((TableViewer)testRunnerViewPart.getTestViewer().getActiveViewer()).getTable();
77
		assertEquals(4, table.getItemCount());
78
79
		List<String> testResults;
80
81
		testRunnerViewPart.setSortingCriterion(SortingCriterion.SORT_BY_NAME);
82
		testResults= new ArrayList<String>();
83
		for (int i= 0; i < table.getItems().length; i++) {
84
			String text= table.getItems()[i].getText();
85
			testResults.add(i, text.substring(0, text.indexOf("_")));
86
		}
87
		assertTrue(Arrays.deepEquals(new String[] { "testA", "testa", "testB", "testC" }, testResults.toArray()));
88
89
		testRunnerViewPart.setSortingCriterion(SortingCriterion.SORT_BY_EXECUTION_TIME);
90
		testResults= new ArrayList<String>();
91
		for (int i= 0; i < table.getItems().length; i++) {
92
			String text= table.getItems()[i].getText();
93
			testResults.add(0, text.substring(text.indexOf("(") + 1, text.length()));
94
		}
95
		String previousResult= null;
96
		for (int i= 0; i < testResults.size(); i++) {
97
			String testResult= testResults.get(i);
98
			if (previousResult != null) {
99
				assertTrue(previousResult.compareTo(testResult) <= 0);
100
			}
101
			previousResult= testResult;
102
		}
103
	}
104
}
(-)a/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/JUnitMessages.java (+6 lines)
Lines 9-14 Link Here
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     David Saff (saff@mit.edu) - bug 102632: [JUnit] Support for JUnit 4.
10
 *     David Saff (saff@mit.edu) - bug 102632: [JUnit] Support for JUnit 4.
11
 *     Robert Konigsberg <konigsberg@google.com> - [JUnit] Improve discoverability of the ability to run a single method under JUnit Tests - https://bugs.eclipse.org/bugs/show_bug.cgi?id=285637
11
 *     Robert Konigsberg <konigsberg@google.com> - [JUnit] Improve discoverability of the ability to run a single method under JUnit Tests - https://bugs.eclipse.org/bugs/show_bug.cgi?id=285637
12
 *     Sandra Lions <sandra.lions-piron@oracle.com> - [JUnit] allow to sort by name and by execution time - https://bugs.eclipse.org/bugs/show_bug.cgi?id=219466
12
 *******************************************************************************/
13
 *******************************************************************************/
13
package org.eclipse.jdt.internal.junit.ui;
14
package org.eclipse.jdt.internal.junit.ui;
14
15
Lines 297-302 public final class JUnitMessages extends NLS { Link Here
297
	public static String TestRunnerViewPart_show_failures_only;
298
	public static String TestRunnerViewPart_show_failures_only;
298
	public static String TestRunnerViewPart_hierarchical_layout;
299
	public static String TestRunnerViewPart_hierarchical_layout;
299
300
301
	public static String TestRunnerViewPart_sort_by_menu;
302
	public static String TestRunnerViewPart_toggle_name_label;
303
	public static String TestRunnerViewPart_toggle_execution_order_label;
304
	public static String TestRunnerViewPart_toggle_execution_time_label;
305
300
	public static String TestSessionLabelProvider_testName_elapsedTimeInSeconds;
306
	public static String TestSessionLabelProvider_testName_elapsedTimeInSeconds;
301
307
302
	public static String TestSessionLabelProvider_testName_JUnitVersion;
308
	public static String TestSessionLabelProvider_testName_JUnitVersion;
(-)a/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/JUnitMessages.properties (+5 lines)
Lines 8-13 Link Here
8
# Contributors:
8
# Contributors:
9
#     IBM Corporation - initial API and implementation
9
#     IBM Corporation - initial API and implementation
10
#     Robert Konigsberg <konigsberg@google.com> - [JUnit] Improve discoverability of the ability to run a single method under JUnit Tests - https://bugs.eclipse.org/bugs/show_bug.cgi?id=285637
10
#     Robert Konigsberg <konigsberg@google.com> - [JUnit] Improve discoverability of the ability to run a single method under JUnit Tests - https://bugs.eclipse.org/bugs/show_bug.cgi?id=285637
11
#     Sandra Lions <sandra.lions-piron@oracle.com> - [JUnit] allow to sort by name and by execution time - https://bugs.eclipse.org/bugs/show_bug.cgi?id=219466
11
###############################################################################
12
###############################################################################
12
CopyTrace_action_label=Copy Trace
13
CopyTrace_action_label=Copy Trace
13
CopyTraceAction_problem=Problem Copying to Clipboard
14
CopyTraceAction_problem=Problem Copying to Clipboard
Lines 75-80 TestRunnerViewPart_select_test_run=&Select a test run: Link Here
75
TestRunnerViewPart_stopaction_tooltip=Stop JUnit Test Run
76
TestRunnerViewPart_stopaction_tooltip=Stop JUnit Test Run
76
TestRunnerViewPart_show_execution_time=Show Execution &Time
77
TestRunnerViewPart_show_execution_time=Show Execution &Time
77
TestRunnerViewPart_show_failures_only=Show &Failures Only
78
TestRunnerViewPart_show_failures_only=Show &Failures Only
79
TestRunnerViewPart_sort_by_menu=&Sort By
80
TestRunnerViewPart_toggle_name_label=Name
81
TestRunnerViewPart_toggle_execution_order_label=Execution Order
82
TestRunnerViewPart_toggle_execution_time_label=Execution Time
78
TestRunnerViewPart_rerunaction_label=Rerun Test
83
TestRunnerViewPart_rerunaction_label=Rerun Test
79
TestRunnerViewPart_rerunaction_tooltip=Rerun Test
84
TestRunnerViewPart_rerunaction_tooltip=Rerun Test
80
TestRunnerViewPart_hierarchical_layout=Show Tests in &Hierarchy 
85
TestRunnerViewPart_hierarchical_layout=Show Tests in &Hierarchy 
(-)a/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/TestRunnerViewPart.java (-4 / +108 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2013 IBM Corporation and others.
2
 * Copyright (c) 2000, 2014 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 16-21 Link Here
16
 *     Andrew Eisenberg <andrew@eisenberg.as> - [JUnit] Rerun failed first does not work with JUnit4 - https://bugs.eclipse.org/bugs/show_bug.cgi?id=140392
16
 *     Andrew Eisenberg <andrew@eisenberg.as> - [JUnit] Rerun failed first does not work with JUnit4 - https://bugs.eclipse.org/bugs/show_bug.cgi?id=140392
17
 *     Thirumala Reddy Mutchukota <thirumala@google.com> - [JUnit] Avoid rerun test launch on UI thread - https://bugs.eclipse.org/bugs/show_bug.cgi?id=411841
17
 *     Thirumala Reddy Mutchukota <thirumala@google.com> - [JUnit] Avoid rerun test launch on UI thread - https://bugs.eclipse.org/bugs/show_bug.cgi?id=411841
18
 *     Andrew Eisenberg <andrew@eisenberg.as> - [JUnit] Add a monospace font option for the junit results view - https://bugs.eclipse.org/bugs/show_bug.cgi?id=411794
18
 *     Andrew Eisenberg <andrew@eisenberg.as> - [JUnit] Add a monospace font option for the junit results view - https://bugs.eclipse.org/bugs/show_bug.cgi?id=411794
19
 *     Sandra Lions <sandra.lions-piron@oracle.com> - [JUnit] allow to sort by name and by execution time - https://bugs.eclipse.org/bugs/show_bug.cgi?id=219466
19
 *******************************************************************************/
20
 *******************************************************************************/
20
package org.eclipse.jdt.internal.junit.ui;
21
package org.eclipse.jdt.internal.junit.ui;
21
22
Lines 163-170 public class TestRunnerViewPart extends ViewPart { Link Here
163
164
164
	static final int REFRESH_INTERVAL= 200;
165
	static final int REFRESH_INTERVAL= 200;
165
166
166
	static final int LAYOUT_FLAT= 0;
167
	public static final int LAYOUT_FLAT= 0;
167
	static final int LAYOUT_HIERARCHICAL= 1;
168
	public static final int LAYOUT_HIERARCHICAL= 1;
168
169
169
	/**
170
	/**
170
	 * Whether the output scrolls and reveals tests as they are executed.
171
	 * Whether the output scrolls and reveals tests as they are executed.
Lines 203-208 public class TestRunnerViewPart extends ViewPart { Link Here
203
	 */
204
	 */
204
	private boolean fIsDisposed= false;
205
	private boolean fIsDisposed= false;
205
206
207
	public enum SortingCriterion {
208
		SORT_BY_NAME, SORT_BY_EXECUTION_ORDER, SORT_BY_EXECUTION_TIME
209
	}
210
	/**
211
	 * The current sorting criterion.
212
	 */
213
	private SortingCriterion fSortingCriterion= SortingCriterion.SORT_BY_EXECUTION_ORDER;
214
206
	/**
215
	/**
207
	 * Actions
216
	 * Actions
208
	 */
217
	 */
Lines 225-230 public class TestRunnerViewPart extends ViewPart { Link Here
225
	private ShowTimeAction fShowTimeAction;
234
	private ShowTimeAction fShowTimeAction;
226
	private ActivateOnErrorAction fActivateOnErrorAction;
235
	private ActivateOnErrorAction fActivateOnErrorAction;
227
	private IMenuListener fViewMenuListener;
236
	private IMenuListener fViewMenuListener;
237
	
238
	private MenuManager fSortByMenu;
239
	private ToggleSortingAction[] fToggleSortingActions;
228
240
229
	private TestRunSession fTestRunSession;
241
	private TestRunSession fTestRunSession;
230
	private TestSessionListener fTestSessionListener;
242
	private TestSessionListener fTestSessionListener;
Lines 279-284 public class TestRunnerViewPart extends ViewPart { Link Here
279
	 */
291
	 */
280
	static final String TAG_SHOW_TIME= "time"; //$NON-NLS-1$
292
	static final String TAG_SHOW_TIME= "time"; //$NON-NLS-1$
281
293
294
	static final String TAG_SORTING_CRITERION= "SortingCriterion"; //$NON-NLS-1$
295
282
	/**
296
	/**
283
	 * @since 3.5
297
	 * @since 3.5
284
	 */
298
	 */
Lines 706-711 public class TestRunnerViewPart extends ViewPart { Link Here
706
720
707
			fStopAction.setEnabled(true);
721
			fStopAction.setEnabled(true);
708
			fRerunLastTestAction.setEnabled(true);
722
			fRerunLastTestAction.setEnabled(true);
723
			
724
			getDisplay().asyncExec(new Runnable() {
725
				public void run() {
726
					setSortingCriterion(SortingCriterion.SORT_BY_EXECUTION_ORDER);
727
				}
728
			});
709
		}
729
		}
710
730
711
		public void sessionEnded(long elapsedTime){
731
		public void sessionEnded(long elapsedTime){
Lines 961-966 public class TestRunnerViewPart extends ViewPart { Link Here
961
		}
981
		}
962
	}
982
	}
963
983
984
	private class ToggleSortingAction extends Action {
985
		private final SortingCriterion fActionSortingCriterion;
986
987
		public ToggleSortingAction(SortingCriterion sortingCriterion) {
988
			super("", AS_RADIO_BUTTON); //$NON-NLS-1$
989
			switch (sortingCriterion) {
990
				case SORT_BY_NAME:
991
					setText(JUnitMessages.TestRunnerViewPart_toggle_name_label);
992
					break;
993
				case SORT_BY_EXECUTION_ORDER:
994
					setText(JUnitMessages.TestRunnerViewPart_toggle_execution_order_label);
995
					break;
996
				case SORT_BY_EXECUTION_TIME:
997
					setText(JUnitMessages.TestRunnerViewPart_toggle_execution_time_label);
998
					break;
999
				default:
1000
					break;
1001
			}
1002
			fActionSortingCriterion= sortingCriterion;
1003
		}
1004
1005
		@Override
1006
		public void run() {
1007
			if (isChecked()) {
1008
				setSortingCriterion(fActionSortingCriterion);
1009
			}
1010
		}
1011
1012
		public SortingCriterion getActionSortingCriterion() {
1013
			return fActionSortingCriterion;
1014
		}
1015
	}
1016
1017
	public SortingCriterion getSortingCriterion() {
1018
		return fSortingCriterion;
1019
	}
1020
1021
	public void setSortingCriterion(SortingCriterion sortingCriterion) {
1022
		fSortingCriterion= sortingCriterion;
1023
		fTestViewer.setSortingCriterion(sortingCriterion);
1024
		for (int i= 0; i < fToggleSortingActions.length; ++i) {
1025
			fToggleSortingActions[i].setChecked(sortingCriterion == fToggleSortingActions[i].getActionSortingCriterion());
1026
		}
1027
	}
1028
1029
	private void updateSortByMenu() {
1030
		if (fTestRunSession != null) {
1031
			if (fTestRunSession.isStarting() || fTestRunSession.isRunning() || fTestRunSession.isKeptAlive()) {
1032
				setSortingCriterion(SortingCriterion.SORT_BY_EXECUTION_ORDER);
1033
				fSortByMenu.getMenu().setEnabled(false);
1034
			} else {
1035
				fSortByMenu.getMenu().setEnabled(true);
1036
			}
1037
		} else {
1038
			setSortingCriterion(SortingCriterion.SORT_BY_EXECUTION_ORDER);
1039
			fSortByMenu.getMenu().setEnabled(false);
1040
		}
1041
	}
1042
964
	/**
1043
	/**
965
	 * Listen for for modifications to Java elements
1044
	 * Listen for for modifications to Java elements
966
	 */
1045
	 */
Lines 1145-1150 public class TestRunnerViewPart extends ViewPart { Link Here
1145
		memento.putString(TAG_FAILURES_ONLY, fFailuresOnlyFilterAction.isChecked() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
1224
		memento.putString(TAG_FAILURES_ONLY, fFailuresOnlyFilterAction.isChecked() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
1146
		memento.putInteger(TAG_LAYOUT, fLayout);
1225
		memento.putInteger(TAG_LAYOUT, fLayout);
1147
		memento.putString(TAG_SHOW_TIME, fShowTimeAction.isChecked() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
1226
		memento.putString(TAG_SHOW_TIME, fShowTimeAction.isChecked() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
1227
		memento.putString(TAG_SORTING_CRITERION, fSortingCriterion.name());
1148
	}
1228
	}
1149
1229
1150
	private void restoreLayoutState(IMemento memento) {
1230
	private void restoreLayoutState(IMemento memento) {
Lines 1183-1188 public class TestRunnerViewPart extends ViewPart { Link Here
1183
		boolean showTime= true;
1263
		boolean showTime= true;
1184
		if (time != null)
1264
		if (time != null)
1185
			showTime= time.equals("true"); //$NON-NLS-1$
1265
			showTime= time.equals("true"); //$NON-NLS-1$
1266
		
1267
		SortingCriterion sortingCriterion= SortingCriterion.SORT_BY_EXECUTION_ORDER;
1268
		String tagSortingCriterion= memento.getString(TAG_SORTING_CRITERION);
1269
		if (tagSortingCriterion != null) {
1270
			sortingCriterion= Enum.valueOf(SortingCriterion.class, tagSortingCriterion);
1271
		}
1272
		setSortingCriterion(sortingCriterion);
1186
1273
1187
		setFilterAndLayout(showFailuresOnly, layoutValue);
1274
		setFilterAndLayout(showFailuresOnly, layoutValue);
1188
		setShowExecutionTime(showTime);
1275
		setShowExecutionTime(showTime);
Lines 1514-1519 action enablement Link Here
1514
				fStopAction.setEnabled(fTestRunSession.isKeptAlive());
1601
				fStopAction.setEnabled(fTestRunSession.isKeptAlive());
1515
				fTestViewer.expandFirstLevel();
1602
				fTestViewer.expandFirstLevel();
1516
			}
1603
			}
1604
			
1605
			updateSortByMenu();
1517
		}
1606
		}
1518
		return deactivatedSession;
1607
		return deactivatedSession;
1519
	}
1608
	}
Lines 1942-1947 action enablement Link Here
1942
		viewMenu.add(fShowTestHierarchyAction);
2031
		viewMenu.add(fShowTestHierarchyAction);
1943
		viewMenu.add(fShowTimeAction);
2032
		viewMenu.add(fShowTimeAction);
1944
		viewMenu.add(new Separator());
2033
		viewMenu.add(new Separator());
2034
		fToggleSortingActions=
2035
				new ToggleSortingAction[] {
2036
						new ToggleSortingAction(SortingCriterion.SORT_BY_EXECUTION_ORDER),
2037
						new ToggleSortingAction(SortingCriterion.SORT_BY_EXECUTION_TIME),
2038
						new ToggleSortingAction(SortingCriterion.SORT_BY_NAME)};
2039
		fSortByMenu= new MenuManager(JUnitMessages.TestRunnerViewPart_sort_by_menu);
2040
		for (int i= 0; i < fToggleSortingActions.length; ++i) {
2041
			fSortByMenu.add(fToggleSortingActions[i]);
2042
		}
2043
		viewMenu.add(fSortByMenu);
2044
		viewMenu.add(new Separator());
1945
2045
1946
		MenuManager layoutSubMenu= new MenuManager(JUnitMessages.TestRunnerViewPart_layout_menu);
2046
		MenuManager layoutSubMenu= new MenuManager(JUnitMessages.TestRunnerViewPart_layout_menu);
1947
		for (int i = 0; i < fToggleOrientationActions.length; ++i) {
2047
		for (int i = 0; i < fToggleOrientationActions.length; ++i) {
Lines 1958-1963 action enablement Link Here
1958
		fViewMenuListener= new IMenuListener() {
2058
		fViewMenuListener= new IMenuListener() {
1959
			public void menuAboutToShow(IMenuManager manager) {
2059
			public void menuAboutToShow(IMenuManager manager) {
1960
				fActivateOnErrorAction.update();
2060
				fActivateOnErrorAction.update();
2061
				updateSortByMenu();
1961
			}
2062
			}
1962
		};
2063
		};
1963
2064
Lines 2174-2185 action enablement Link Here
2174
		return fFailureTrace;
2275
		return fFailureTrace;
2175
	}
2276
	}
2176
2277
2278
	public TestViewer getTestViewer() {
2279
		return fTestViewer;
2280
	}
2177
2281
2178
	void setShowFailuresOnly(boolean failuresOnly) {
2282
	void setShowFailuresOnly(boolean failuresOnly) {
2179
		setFilterAndLayout(failuresOnly, fLayout);
2283
		setFilterAndLayout(failuresOnly, fLayout);
2180
	}
2284
	}
2181
2285
2182
	private void setLayoutMode(int mode) {
2286
	public void setLayoutMode(int mode) {
2183
		setFilterAndLayout(fFailuresOnlyFilterAction.isChecked(), mode);
2287
		setFilterAndLayout(fFailuresOnlyFilterAction.isChecked(), mode);
2184
	}
2288
	}
2185
2289
(-)a/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/TestViewer.java (-4 / +91 lines)
Lines 10-22 Link Here
10
 *     Brock Janiczak (brockj@tpg.com.au)
10
 *     Brock Janiczak (brockj@tpg.com.au)
11
 *         - https://bugs.eclipse.org/bugs/show_bug.cgi?id=102236: [JUnit] display execution time next to each test
11
 *         - https://bugs.eclipse.org/bugs/show_bug.cgi?id=102236: [JUnit] display execution time next to each test
12
 *     Xavier Coulon <xcoulon@redhat.com> - https://bugs.eclipse.org/bugs/show_bug.cgi?id=102512 - [JUnit] test method name cut off before (
12
 *     Xavier Coulon <xcoulon@redhat.com> - https://bugs.eclipse.org/bugs/show_bug.cgi?id=102512 - [JUnit] test method name cut off before (
13
13
 *     Sandra Lions <sandra.lions-piron@oracle.com> - [JUnit] allow to sort by name and by execution time - https://bugs.eclipse.org/bugs/show_bug.cgi?id=219466
14
 *******************************************************************************/
14
 *******************************************************************************/
15
15
16
package org.eclipse.jdt.internal.junit.ui;
16
package org.eclipse.jdt.internal.junit.ui;
17
17
18
import java.util.AbstractList;
18
import java.util.AbstractList;
19
import java.util.Arrays;
19
import java.util.Arrays;
20
import java.util.Comparator;
20
import java.util.HashSet;
21
import java.util.HashSet;
21
import java.util.LinkedHashSet;
22
import java.util.LinkedHashSet;
22
import java.util.LinkedList;
23
import java.util.LinkedList;
Lines 48-53 import org.eclipse.jface.viewers.StructuredViewer; Link Here
48
import org.eclipse.jface.viewers.TableViewer;
49
import org.eclipse.jface.viewers.TableViewer;
49
import org.eclipse.jface.viewers.TreeViewer;
50
import org.eclipse.jface.viewers.TreeViewer;
50
import org.eclipse.jface.viewers.Viewer;
51
import org.eclipse.jface.viewers.Viewer;
52
import org.eclipse.jface.viewers.ViewerComparator;
51
import org.eclipse.jface.viewers.ViewerFilter;
53
import org.eclipse.jface.viewers.ViewerFilter;
52
54
53
import org.eclipse.ui.IWorkbenchActionConstants;
55
import org.eclipse.ui.IWorkbenchActionConstants;
Lines 65-70 import org.eclipse.jdt.internal.junit.model.TestElement.Status; Link Here
65
import org.eclipse.jdt.internal.junit.model.TestRoot;
67
import org.eclipse.jdt.internal.junit.model.TestRoot;
66
import org.eclipse.jdt.internal.junit.model.TestRunSession;
68
import org.eclipse.jdt.internal.junit.model.TestRunSession;
67
import org.eclipse.jdt.internal.junit.model.TestSuiteElement;
69
import org.eclipse.jdt.internal.junit.model.TestSuiteElement;
70
import org.eclipse.jdt.internal.junit.ui.TestRunnerViewPart.SortingCriterion;
68
71
69
import org.eclipse.jdt.internal.ui.viewsupport.ColoringLabelProvider;
72
import org.eclipse.jdt.internal.ui.viewsupport.ColoringLabelProvider;
70
import org.eclipse.jdt.internal.ui.viewsupport.SelectionProviderMediator;
73
import org.eclipse.jdt.internal.ui.viewsupport.SelectionProviderMediator;
Lines 324-329 public class TestViewer { Link Here
324
		}
327
		}
325
	}
328
	}
326
329
330
	public synchronized void setSortingCriterion(SortingCriterion sortingCriterion) {
331
		ViewerComparator viewComparator;
332
		switch (sortingCriterion) {
333
			case SORT_BY_EXECUTION_ORDER:
334
				viewComparator= null;
335
				break;
336
			case SORT_BY_EXECUTION_TIME:
337
				viewComparator= new TestExecutionTimeComparator();
338
				break;
339
			case SORT_BY_NAME:
340
				viewComparator= new TestNameComparator();
341
				break;
342
			default:
343
				viewComparator= null;
344
				break;
345
		}
346
		fTableViewer.setComparator(viewComparator);
347
		fTreeViewer.setComparator(viewComparator);
348
	}
349
350
	private final class TestNameComparator extends ViewerComparator {
351
		@Override
352
		public int compare(Viewer viewer, Object o1, Object o2) {
353
			return compareName(o1, o2);
354
		}
355
	}
356
357
	private final class TestExecutionTimeComparator extends ViewerComparator {
358
		@Override
359
		public int compare(Viewer viewer, Object o1, Object o2) {
360
			return compareElapsedTime(o1, o2);
361
		}
362
	}
363
364
	private Comparator<ITestElement> getComparator() {
365
		SortingCriterion sortingCriterion= fTestRunnerPart.getSortingCriterion();
366
		Comparator<ITestElement> comparator;
367
		switch (sortingCriterion) {
368
			case SORT_BY_EXECUTION_ORDER:
369
				comparator= null;
370
				break;
371
			case SORT_BY_EXECUTION_TIME:
372
				comparator= new Comparator<ITestElement>() {
373
					public int compare(ITestElement o1, ITestElement o2) {
374
						return compareElapsedTime(o1, o2);
375
					}
376
				};
377
				break;
378
			case SORT_BY_NAME:
379
				comparator= new Comparator<ITestElement>() {
380
					public int compare(ITestElement o1, ITestElement o2) {
381
						return compareName(o1, o2);
382
					}
383
				};
384
				break;
385
			default:
386
				comparator= null;
387
				break;
388
		}
389
		return comparator;
390
	}
391
392
	private int compareElapsedTime(Object o1, Object o2) {
393
		double elapsedTime1= ((TestElement)o1).getElapsedTimeInSeconds();
394
		double elapsedTime2= ((TestElement)o2).getElapsedTimeInSeconds();
395
		return Double.compare(elapsedTime2, elapsedTime1);
396
	}
397
398
	private int compareName(Object o1, Object o2) {
399
		String testName1= ((TestElement)o1).getTestName();
400
		String testName2= ((TestElement)o2).getTestName();
401
		return testName1.toLowerCase().compareTo(testName2.toLowerCase());
402
	}
403
327
	public synchronized void setShowFailuresOnly(boolean failuresOnly, int layoutMode) {
404
	public synchronized void setShowFailuresOnly(boolean failuresOnly, int layoutMode) {
328
		/*
405
		/*
329
		 * Management of fTreeViewer and fTableViewer
406
		 * Management of fTreeViewer and fTableViewer
Lines 399-405 public class TestViewer { Link Here
399
			fTableHasFilter= filter;
476
			fTableHasFilter= filter;
400
	}
477
	}
401
478
402
	private StructuredViewer getActiveViewer() {
479
	public StructuredViewer getActiveViewer() {
403
		if (fLayoutMode == TestRunnerViewPart.LAYOUT_HIERARCHICAL)
480
		if (fLayoutMode == TestRunnerViewPart.LAYOUT_HIERARCHICAL)
404
			return fTreeViewer;
481
			return fTreeViewer;
405
		else
482
		else
Lines 613-619 public class TestViewer { Link Here
613
		if (parent == null)
690
		if (parent == null)
614
			return null;
691
			return null;
615
692
616
		List<ITestElement> siblings= Arrays.asList(parent.getChildren());
693
		ITestElement[] elements= parent.getChildren();
694
		Comparator<ITestElement> comparator= getComparator();
695
		if (comparator != null) {
696
			Arrays.sort(elements, comparator);
697
		}
698
		List<ITestElement> siblings= Arrays.asList(elements);
617
		if (! showNext)
699
		if (! showNext)
618
			siblings= new ReverseList<ITestElement>(siblings);
700
			siblings= new ReverseList<ITestElement>(siblings);
619
701
Lines 632-638 public class TestViewer { Link Here
632
	}
714
	}
633
715
634
	private TestCaseElement getNextChildFailure(TestSuiteElement root, boolean showNext) {
716
	private TestCaseElement getNextChildFailure(TestSuiteElement root, boolean showNext) {
635
		List<ITestElement> children= Arrays.asList(root.getChildren());
717
		ITestElement[] elements= root.getChildren();
718
		Comparator<ITestElement> comparator= getComparator();
719
		if (comparator != null) {
720
			Arrays.sort(elements, comparator);
721
		}
722
		List<ITestElement> children= Arrays.asList(elements);
636
		if (! showNext)
723
		if (! showNext)
637
			children= new ReverseList<ITestElement>(children);
724
			children= new ReverseList<ITestElement>(children);
638
		for (int i= 0; i < children.size(); i++) {
725
		for (int i= 0; i < children.size(); i++) {
(-)a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/junit/tests/JUnitJUnitTests.java (-1 / +5 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2010 IBM Corporation and others.
2
 * Copyright (c) 2005, 2014 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Sandra Lions <sandra.lions-piron@oracle.com> - [JUnit] allow to sort by name and by execution time - https://bugs.eclipse.org/bugs/show_bug.cgi?id=219466
10
 *******************************************************************************/
11
 *******************************************************************************/
11
12
12
package org.eclipse.jdt.junit.tests;
13
package org.eclipse.jdt.junit.tests;
Lines 37-42 public class JUnitJUnitTests { Link Here
37
38
38
		suite.addTestSuite(JUnit3TestFinderTest.class);
39
		suite.addTestSuite(JUnit3TestFinderTest.class);
39
		suite.addTestSuite(JUnit4TestFinderTest.class);
40
		suite.addTestSuite(JUnit4TestFinderTest.class);
41
		
42
		suite.addTestSuite(TestSorting.class);
43
40
		//$JUnit-END$
44
		//$JUnit-END$
41
		return suite;
45
		return suite;
42
	}
46
	}
(-)a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/junit/tests/TestSorting.java (-1 / +1 lines)
Lines 36-42 public class TestSorting extends AbstractTestRunListenerTest { Link Here
36
		final TestRunListener testRunListener= new TestRunListeners.SequenceTest(log);
36
		final TestRunListener testRunListener= new TestRunListeners.SequenceTest(log);
37
		JUnitCore.addTestRunListener(testRunListener);
37
		JUnitCore.addTestRunListener(testRunListener);
38
		try {
38
		try {
39
			return launchJUnit(typeToLaunch, new TestRunLog());
39
			return launchJUnit(typeToLaunch, log);
40
		} finally {
40
		} finally {
41
			JUnitCore.removeTestRunListener(testRunListener);
41
			JUnitCore.removeTestRunListener(testRunListener);
42
		}
42
		}

Return to bug 219466