01: /*******************************************************************************
02: * Copyright (c) 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.testplugin;
11:
12: import java.lang.reflect.Method;
13: import java.util.HashSet;
14: import java.util.Iterator;
15: import java.util.Set;
16:
17: import junit.framework.Test;
18: import junit.framework.TestCase;
19: import junit.framework.TestSuite;
20:
21: /**
22: * Test suite with user-specified test order.
23: * Fails if not all test methods are listed.
24: */
25: public class OrderedTestSuite extends TestSuite {
26:
27: /**
28: * @param testClass test class
29: * @param testMethods all test methods of the given class, in the requested order
30: */
31: public OrderedTestSuite(final Class testClass, String[] testMethods)
32: throws Exception {
33: super (testClass.getName());
34:
35: Set existingMethods = new HashSet();
36: Method[] methods = testClass.getMethods(); // just public member methods
37: for (int i = 0; i < methods.length; i++) {
38: Method method = methods[i];
39: existingMethods.add(method.getName());
40: }
41:
42: for (int i = 0; i < testMethods.length; i++) {
43: final String testMethod = testMethods[i];
44: if (existingMethods.remove(testMethod)) {
45: addTest(createTest(testClass, testMethod));
46: } else {
47: addTest(error(testMethod, new IllegalArgumentException(
48: "Class '" + testClass.getName()
49: + " misses test method '" + testMethod
50: + "'.")));
51: }
52: }
53:
54: for (Iterator iter = existingMethods.iterator(); iter.hasNext();) {
55: String existingMethod = (String) iter.next();
56: if (existingMethod.startsWith("test")) {
57: addTest(error(
58: existingMethod,
59: new IllegalArgumentException(
60: "Test method '"
61: + existingMethod
62: + "' not listed in OrderedTestSuite of class '"
63: + testClass.getName() + "'.")));
64: }
65: }
66:
67: }
68:
69: public static Test error(String testMethod, Exception exception) {
70: final Throwable e2 = exception.fillInStackTrace();
71: return new TestCase(testMethod) {
72: protected void runTest() throws Throwable {
73: throw e2;
74: }
75: };
76: }
77: }
|