blob: 86db273e7f059bc0066eeb5c2157cf3b7b476c9b [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004, 2007 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.wst.jsdt.core.tests.junit.extension;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Vector;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class PerformanceTestSuite extends TestSuite {
/**
* Constructs a TestSuite from the given class. Adds all the methods
* starting with "testPerf" as test cases to the suite.
*/
public PerformanceTestSuite(final Class theClass) {
setName(theClass.getName());
try {
getTestConstructor(theClass); // Avoid generating multiple error messages
} catch (NoSuchMethodException e) {
addTest(addWarningTest("Class "+theClass.getName()+" has no public constructor TestCase(String name) or TestCase()"));
return;
}
if (!Modifier.isPublic(theClass.getModifiers())) {
addTest(addWarningTest("Class "+theClass.getName()+" is not public"));
return;
}
Class superClass= theClass;
Vector names= new Vector();
while (Test.class.isAssignableFrom(superClass)) {
Method[] methods= superClass.getDeclaredMethods();
for (int i= 0; i < methods.length; i++) {
addTestMethod(methods[i], names, theClass);
}
superClass= superClass.getSuperclass();
}
if (countTestCases() == 0)
addTest(addWarningTest("No tests found in "+theClass.getName()));
}
public PerformanceTestSuite(String name) {
setName(name);
}
private void addTestMethod(Method m, Vector names, Class theClass) {
String name= m.getName();
if (names.contains(name))
return;
if (! isPublicTestMethod(m)) {
if (isTestMethod(m))
addTest(addWarningTest("Test method isn't public: "+m.getName()));
return;
}
names.addElement(name);
addTest(createTest(theClass, name));
}
public void addTestSuite(Class theClass) {
addTest(new PerformanceTestSuite(theClass));
}
private boolean isPublicTestMethod(Method m) {
return isTestMethod(m) && Modifier.isPublic(m.getModifiers());
}
private boolean isTestMethod(Method m) {
String name= m.getName();
Class[] parameters= m.getParameterTypes();
Class returnType= m.getReturnType();
return parameters.length == 0 && name.startsWith("testPerf") && returnType.equals(Void.TYPE);
}
/**
* Returns a test which will fail and log a warning message.
*/
private static Test addWarningTest(final String message) {
return new TestCase("warning") {
protected void runTest() {
fail(message);
}
};
}
}