Skip to content
Snippets Groups Projects
Commit 30b2f000 authored by dgelessus's avatar dgelessus
Browse files

Replace PolySuite with standard JUnit Parameterized runner

parent 39d9d0dd
No related branches found
No related tags found
No related merge requests found
package de.tla2b.examples;
import java.io.File;
import java.util.List;
import de.tla2b.util.AbstractParseModuleTest;
import de.tla2b.util.PolySuite;
import de.tla2b.util.PolySuite.Config;
import de.tla2b.util.PolySuite.Configuration;
import de.tla2b.util.TestUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(PolySuite.class)
@RunWith(Parameterized.class)
public class RegressionTests extends AbstractParseModuleTest {
private final File moduleFile;
public RegressionTests(File machine, Object result) {
public RegressionTests(File machine) {
this.moduleFile = machine;
}
......@@ -24,8 +23,8 @@ public class RegressionTests extends AbstractParseModuleTest {
TestUtil.loadTlaFile(moduleFile.getPath());
}
@Config
public static Configuration getConfig() {
return getConfiguration2("./src/test/resources/regression");
@Parameterized.Parameters(name = "{0}")
public static List<File> getConfig() {
return getModulesRecursively("./src/test/resources/regression");
}
}
......@@ -3,9 +3,6 @@ package de.tla2b.util;
import java.io.File;
import java.util.ArrayList;
import de.tla2b.util.PolySuite.Configuration;
public abstract class AbstractParseModuleTest {
private static final String TLA_SUFFIX = ".tla";
......@@ -26,28 +23,4 @@ public abstract class AbstractParseModuleTest {
}
return files;
}
protected static Configuration getConfiguration2(String path) {
final ArrayList<File> allModules = getModulesRecursively(path);
return new Configuration() {
public int size() {
return allModules.size();
}
public File getTestValue(int index) {
return allModules.get(index);
}
public String getTestName(int index) {
return allModules.get(index).getName();
}
public Object getExpectedValue(int index) {
return 1;
}
};
}
}
package de.tla2b.util;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
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 org.junit.runners.model.TestClass;
public class PolySuite extends Suite {
// //////////////////////////////
// Public helper interfaces
/**
* Annotation for a method which returns a {@link Configuration}
* to be injected into the test class constructor
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public static @interface Config {
}
public static interface Configuration {
int size();
Object getTestValue(int index);
Object getExpectedValue(int index);
String getTestName(int index);
}
// //////////////////////////////
// Fields
private final List<Runner> runners;
// //////////////////////////////
// Constructor
/**
* Only called reflectively. Do not use programmatically.
* @param c the test class
* @throws Throwable if something bad happens
*/
public PolySuite(Class<?> c) throws Throwable {
super(c, Collections.<Runner>emptyList());
TestClass testClass = getTestClass();
Class<?> jTestClass = testClass.getJavaClass();
Configuration configuration = getConfiguration(testClass);
List<Runner> runners = new ArrayList<Runner>();
for (int i = 0, size = configuration.size(); i < size; i++) {
SingleRunner runner = new SingleRunner(jTestClass, configuration.getTestValue(i), configuration.getTestName(i), configuration.getExpectedValue(i));
runners.add(runner);
}
this.runners = runners;
}
// //////////////////////////////
// Overrides
@Override
protected List<Runner> getChildren() {
return runners;
}
// //////////////////////////////
// Private
private Configuration getConfiguration(TestClass testClass) throws Throwable {
return (Configuration) getConfigMethod(testClass).invokeExplosively(null);
}
private FrameworkMethod getConfigMethod(TestClass testClass) {
List<FrameworkMethod> methods = testClass.getAnnotatedMethods(Config.class);
if (methods.isEmpty()) {
throw new IllegalStateException("@" + Config.class.getSimpleName() + " method not found");
}
if (methods.size() > 1) {
throw new IllegalStateException("Too many @" + Config.class.getSimpleName() + " methods");
}
FrameworkMethod method = methods.get(0);
int modifiers = method.getMethod().getModifiers();
if (!(Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))) {
throw new IllegalStateException("@" + Config.class.getSimpleName() + " method \"" + method.getName() + "\" must be public static");
}
return method;
}
// //////////////////////////////
// Helper classes
private static class SingleRunner extends BlockJUnit4ClassRunner {
private final Object testVal;
private final Object expectedVal;
private final String testName;
SingleRunner(Class<?> testClass, Object testVal, String testName, Object expectedVal) throws InitializationError {
super(testClass);
this.testVal = testVal;
this.expectedVal = expectedVal;
this.testName = testName;
}
@Override
protected Object createTest() throws Exception {
return getTestClass().getOnlyConstructor().newInstance(testVal, expectedVal);
}
@Override
protected String getName() {
return testName;
}
@Override
protected String testName(FrameworkMethod method) {
return testName + ": " + method.getName();
}
@Override
protected void validateConstructor(List<Throwable> errors) {
validateOnlyOneConstructor(errors);
}
@Override
protected Statement classBlock(RunNotifier notifier) {
return childrenInvoker(notifier);
}
}
}
\ No newline at end of file
package testing;
import java.io.File;
import java.util.List;
import de.be4.classicalb.core.parser.BParser;
import de.be4.classicalb.core.parser.node.Start;
import de.be4.classicalb.core.parser.util.PrettyPrinter;
import de.tla2b.util.AbstractParseModuleTest;
import de.tla2b.util.FileUtils;
import de.tla2b.util.PolySuite;
import de.tla2b.util.PolySuite.Config;
import de.tla2b.util.PolySuite.Configuration;
import de.tla2b.util.TestUtil;
import de.tla2bAst.Translator;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import static org.junit.Assert.assertEquals;
@RunWith(PolySuite.class)
@RunWith(Parameterized.class)
public class ExampleFilesTest extends AbstractParseModuleTest {
private final File moduleFile;
public ExampleFilesTest(File machine, Object result) {
public ExampleFilesTest(File machine) {
this.moduleFile = machine;
}
......@@ -62,8 +61,8 @@ public class ExampleFilesTest extends AbstractParseModuleTest {
assertEquals(expectedTree, resultTree);
}
@Config
public static Configuration getConfig() {
return getConfiguration2("./src/test/resources/prettyprint/OperationsTest/");
@Parameterized.Parameters(name = "{0}")
public static List<File> getConfig() {
return getModulesRecursively("./src/test/resources/prettyprint/OperationsTest/");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment