
nz.ac.auckland.integration.testing.OrchestratedParameterized Maven / Gradle / Ivy
package nz.ac.auckland.integration.testing;
import nz.ac.auckland.integration.testing.specification.OrchestratedTestSpecification;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.Suite;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* A heavily modified version of JUnit's Parameterized class that
* assumes there's a public static void configure() method available
* to configure a set of test specifications which will then be retrieved
* using the getSpecifications method of OrchestratedTest
*
* Any copyright for similar code will be under the EPL license for JUnit
*
* @author David MacDonald
*/
public class OrchestratedParameterized extends Suite {
private class TestClassRunnerForParameters extends BlockJUnit4ClassRunner {
private final OrchestratedTestSpecification specification;
private final String name;
TestClassRunnerForParameters(Class> type,
OrchestratedTestSpecification specification,
String name) throws InitializationError {
super(type);
this.specification = specification;
this.name = name;
}
@Override
public Object createTest() throws Exception {
Constructor co = getTestClass().getJavaClass().getConstructor();
OrchestratedTest test = (OrchestratedTest) co.newInstance();
test.setSpecification(specification);
return test;
}
@Override
protected String getName() {
return this.name;
}
@Override
protected String testName(FrameworkMethod method) {
return method.getName() + getName();
}
@Override
protected void validateConstructor(List errors) {
validateOnlyOneConstructor(errors);
}
@Override
protected Statement classBlock(RunNotifier notifier) {
return childrenInvoker(notifier);
}
@Override
protected Annotation[] getRunnerAnnotations() {
return new Annotation[0];
}
}
private final ArrayList runners = new ArrayList();
@SuppressWarnings("unchecked")
public OrchestratedParameterized(Class extends OrchestratedTestBuilder> klass) throws Throwable {
super(klass, Collections.emptyList());
Method getSpecifications = OrchestratedTestBuilder.class.getDeclaredMethod("getSpecifications");
getSpecifications.setAccessible(true);
List specifications = (List) getSpecifications.invoke(klass.newInstance());
createRunnersForParameters(specifications);
}
@Override
protected List getChildren() {
return runners;
}
private void createRunnersForParameters(List specifications) throws InitializationError {
int i = 0;
for (OrchestratedTestSpecification specification : specifications) {
String name = String.format("%d: %s", i, specification.getDescription());
TestClassRunnerForParameters runner = new TestClassRunnerForParameters(
getTestClass().getJavaClass(), specification,
name);
runners.add(runner);
i++;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy