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 @@ + + org.eclipse.pde.api.tools.apiAnalysisBuilder + + + org.eclipse.jdt.core.javanature org.eclipse.pde.PluginNature + org.eclipse.pde.api.tools.apiAnalysisNature diff --git a/bundles/org.eclipse.test.performance/META-INF/MANIFEST.MF b/bundles/org.eclipse.test.performance/META-INF/MANIFEST.MF index e8f03a38..b015473e 100644 --- a/bundles/org.eclipse.test.performance/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.test.performance/META-INF/MANIFEST.MF @@ -2,11 +2,12 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %Plugin.name Bundle-SymbolicName: org.eclipse.test.performance -Bundle-Version: 3.7.0.qualifier +Bundle-Version: 3.8.0.qualifier Bundle-Activator: org.eclipse.test.internal.performance.PerformanceTestPlugin Bundle-Vendor: %Plugin.providerName Bundle-Localization: plugin Export-Package: org.eclipse.perfmsr.core, + org.eclipse.test, org.eclipse.test.internal.performance, org.eclipse.test.internal.performance.data, org.eclipse.test.internal.performance.db, @@ -18,6 +19,6 @@ Require-Bundle: org.eclipse.core.runtime, Cloudscape;resolution:=optional, org.apache.derby;resolution:=optional, org.eclipse.test.performance.derby;bundle-version="10.4.2";resolution:=optional -Eclipse-LazyStart: true +Bundle-ActivationPolicy: lazy Bundle-ClassPath: . Bundle-RequiredExecutionEnvironment: J2SE-1.4 diff --git a/bundles/org.eclipse.test.performance/src/org/eclipse/test/OrderedTestSuite.java b/bundles/org.eclipse.test.performance/src/org/eclipse/test/OrderedTestSuite.java new file mode 100644 index 00000000..93c0e82b --- /dev/null +++ b/bundles/org.eclipse.test.performance/src/org/eclipse/test/OrderedTestSuite.java @@ -0,0 +1,82 @@ +/******************************************************************************* + * Copyright (c) 2007, 2012 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.test; + +import java.lang.reflect.Method; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + + +/** + * Test suite with user-specified test order. Fails if not all test methods are + * listed. + * + *

+ * 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