package de.uni_due.nes.jack;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework;
import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework.Test;
/**
* An abstract base class for test cases. How to use it:
*
*
* - Create a subclass with arbitrary name (e.g. TestCase).
* - Create a method and annotate it with {@link TracingFramework.Test}.
* - Within the method call
addPoints(x)
to assing points.
* - Create a main method that executes:
runAll(new TestCase())
*
*
*
* - Note: this class assumes that each test case calls addPoints exactly once. It will
* report all methods when executing them locally that do not conform to this constraint.
* - Deployment: to use the class on the jack server, you can either:
*
* - add its source code to the source files
* - add a jar containing the compiled class to the library files
*
*
*
*
* @author Marcus
*/
public abstract class AbstractTest {
/**
* The points of each test case.
*/
private List points = new ArrayList<>();
/**
* This method will be called by jack after all
* test have been executed to get the resulting
* points.
*
* @return The points resulting from the test.
*/
public int getResult() {
int sum = 0;
for (Integer p: points) {
sum += p;
}
return sum;
}
/**
* Adds points for a test case. This class assumes
* that the method is called exactly once
* per test case.
*
* @param value The points to add.
*/
protected void addPoints(int value) {
points.add(value);
}
/**
* This method is called by the jack framework after the
* class has been instantiated. Subclasses may override
* this to perform expensive initializations.
*/
public void init() {
// does nothing
}
/**
* A utility method to execute any test driver class extending
* abstract test locally without going through Jack.
*
* @param test The test driver to execute.
* @throws Exception Thrown if something fails.
*/
public static void runAll(AbstractTest test) throws Exception {
System.out.println(test.getClass().getName());
System.out.println();
for (Method method : test.getClass().getMethods()) {
if (method.isAnnotationPresent(Test.class)) {
int size = test.points.size();
method.invoke(test);
if (test.points.size() != size + 1) {
System.err.println("Der Test " + method.getAnnotation(Test.class).name() + " lieferte kein oder mehrere Ergebnisse.");
} else {
System.out.println("Punkte: " + test.points.get(test.points.size() - 1) + " (" + method.getAnnotation(Test.class).name() + ")");
}
}
}
System.out.println();
System.out.println("Summe: " + test.getResult());
}
}