From 83eac8e066f499629737a7c7e93dc765db3640eb Mon Sep 17 00:00:00 2001
From: Markus Keller
Date: Fri, 27 Jan 2012 15:07:18 +0100
Subject: Fixed bug 369818: Add OrderedTestSuite to
org.eclipse.test.performance
---
bundles/org.eclipse.test.performance/.project | 6 ++
.../META-INF/MANIFEST.MF | 5 +-
.../src/org/eclipse/test/OrderedTestSuite.java | 82 ++++++++++++++++++++++
3 files changed, 91 insertions(+), 2 deletions(-)
create mode 100644 bundles/org.eclipse.test.performance/src/org/eclipse/test/OrderedTestSuite.java
diff --git a/bundles/org.eclipse.test.performance/.project b/bundles/org.eclipse.test.performance/.project
index fa313988..1f09a580 100644
--- a/bundles/org.eclipse.test.performance/.project
+++ b/bundles/org.eclipse.test.performance/.project
@@ -20,9 +20,15 @@
+ * Background: {@link java.lang.Class#getDeclaredMethods()} does not + * specify the order of the methods. Up to JavaSE 6, the methods were usually + * sorted in declaration order, but in JavaSE 7, the order is random. This class + * guarantees reliable test execution order. + *
+ * + * @since 3.8 + */ +public class OrderedTestSuite extends TestSuite { + + /** + * Creates a new ordered test suite that runs tests in the specified execution order. + * + * @param testClass the JUnit-3-style test class + * @param testMethods the names of all test methods in the expected execution order + */ + public OrderedTestSuite(final Class testClass, String[] testMethods) { + super(testClass.getName()); + + Set existingMethods= new HashSet(); + Method[] methods= testClass.getMethods(); // just public member methods + for (int i= 0; i < methods.length; i++) { + Method method= methods[i]; + existingMethods.add(method.getName()); + } + + for (int i= 0; i < testMethods.length; i++) { + final String testMethod= testMethods[i]; + if (existingMethods.remove(testMethod)) { + addTest(createTest(testClass, testMethod)); + } else { + addTest(error(testClass, testMethod, new IllegalArgumentException( + "Class '" + testClass.getName() + " misses test method '" + testMethod + "'."))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + } + } + + for (Iterator iter= existingMethods.iterator(); iter.hasNext();) { + String existingMethod= (String) iter.next(); + if (existingMethod.startsWith("test")) { //$NON-NLS-1$ + addTest(error(testClass, existingMethod, new IllegalArgumentException( + "Test method '" + existingMethod + "' not listed in OrderedTestSuite of class '" + testClass.getName() + "'."))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + } + } + + } + + private static Test error(Class testClass, String testMethod, Exception exception) { + final Throwable e2= exception.fillInStackTrace(); + return new TestCase(testMethod + "(" + testClass.getName() + ")") { //$NON-NLS-1$ //$NON-NLS-2$ + protected void runTest() throws Throwable { + throw e2; + } + }; + } +} -- cgit v1.2.3