|
|
@@ -37,17 +37,24 @@ import org.junit.Ignore;
|
|
|
*/
|
|
|
public final class TestContractRunner {
|
|
|
|
|
|
+ private static final boolean DEBUG = "true".equals(System.getProperty("assira.junit.debug"));
|
|
|
+
|
|
|
private final SortedSet<MethodRunner> methods = new TreeSet<>();
|
|
|
|
|
|
private final List<TesterWrapper> testers = new ArrayList<>();
|
|
|
|
|
|
private final List<Pattern> ignored = new ArrayList<>();
|
|
|
-
|
|
|
+
|
|
|
private final PrintStream output;
|
|
|
-
|
|
|
- private static final boolean DEBUG = "true".equals(System.getProperty("assira.junit.debug"));
|
|
|
|
|
|
+ private final Generator generator = new Generator();
|
|
|
+
|
|
|
private boolean debug = DEBUG;
|
|
|
+
|
|
|
+ private Function<Object, Object> function;
|
|
|
+
|
|
|
+ private Supplier<Object> supplier;
|
|
|
+
|
|
|
|
|
|
public TestContractRunner(PrintStream output) {
|
|
|
this.output = output;
|
|
|
@@ -133,25 +140,33 @@ public final class TestContractRunner {
|
|
|
});
|
|
|
return this;
|
|
|
}
|
|
|
-
|
|
|
- public boolean run(Function<int[], ?> generator) {
|
|
|
- return irun(new TGeneratorF(generator));
|
|
|
- }
|
|
|
-
|
|
|
- public boolean run(Supplier<?> generator) {
|
|
|
- return irun(new TGeneratorS(generator));
|
|
|
- }
|
|
|
-
|
|
|
- private boolean irun(TGenerator generator) { //NOPMD
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public TestContractRunner supplier(Supplier<?> supplier) {
|
|
|
+ this.supplier = (Supplier)supplier;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public <P> TestContractRunner function(P ident, Function<P, ?> function) {
|
|
|
+ this.function = (Function)function;
|
|
|
+ this.supplier = () -> this.function.apply(ident);
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean run() {
|
|
|
for(TesterWrapper tester : testers) {
|
|
|
tester.bind(this);
|
|
|
}
|
|
|
- Class<?> type = generator.get().getClass();
|
|
|
+ if(null == supplier) {
|
|
|
+ throw new AssertionError("Supplier is not set.");
|
|
|
+ }
|
|
|
+ Class<?> type = supplier.get().getClass();
|
|
|
Counter counter = new Counter();
|
|
|
List<String> failed = new ArrayList<>();
|
|
|
methods.stream().filter((m)->m.accept(type)).forEach((m)->{
|
|
|
try {
|
|
|
- m.run(counter, generator);
|
|
|
+ m.run(counter);
|
|
|
} catch(Throwable cause) {
|
|
|
failed.add(m.label);
|
|
|
cause.printStackTrace(output);
|
|
|
@@ -173,6 +188,8 @@ public final class TestContractRunner {
|
|
|
private void ireset() {
|
|
|
debug = DEBUG;
|
|
|
ignored.clear();
|
|
|
+ function = null;
|
|
|
+ supplier = null;
|
|
|
for(TesterWrapper tester : testers) {
|
|
|
tester.resetParams();
|
|
|
}
|
|
|
@@ -195,8 +212,8 @@ public final class TestContractRunner {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- protected void invoke(TGenerator argument) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
|
|
- method.invoke(that, argument.get());
|
|
|
+ protected void invoke() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
|
|
+ method.invoke(that, supplier.get());
|
|
|
}
|
|
|
|
|
|
}
|
|
|
@@ -208,8 +225,8 @@ public final class TestContractRunner {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- protected void invoke(TGenerator argument) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
|
|
- method.invoke(that, argument);
|
|
|
+ protected void invoke() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
|
|
+ method.invoke(that, generator);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
@@ -245,9 +262,9 @@ public final class TestContractRunner {
|
|
|
return type.isAssignableFrom(param);
|
|
|
}
|
|
|
|
|
|
- protected abstract void invoke(TGenerator argument) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException;
|
|
|
+ protected abstract void invoke() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException;
|
|
|
|
|
|
- public void run(Counter counter, TGenerator param) throws RuntimeException {
|
|
|
+ public void run(Counter counter) throws RuntimeException {
|
|
|
for(Pattern p : ignored) {
|
|
|
if(p.matcher(nid).matches()) {
|
|
|
if(debug) {
|
|
|
@@ -258,7 +275,7 @@ public final class TestContractRunner {
|
|
|
}
|
|
|
int index = counter.next();
|
|
|
try {
|
|
|
- invoke(param);
|
|
|
+ invoke();
|
|
|
if(debug) {
|
|
|
printf(type, index, method, "");
|
|
|
}
|
|
|
@@ -313,9 +330,9 @@ public final class TestContractRunner {
|
|
|
|
|
|
private static Class<?> typeofFunction(Method method) throws AssertionError {
|
|
|
ParameterizedType gparam = (ParameterizedType)method.getGenericParameterTypes()[0];
|
|
|
- if(!gparam.getActualTypeArguments()[0].equals(int[].class)) {
|
|
|
- throw new AssertionError("Function must accept int[] in method: " + method, causeForMethod(method));
|
|
|
- }
|
|
|
+// if(!gparam.getActualTypeArguments()[0].equals(int[].class)) {
|
|
|
+// throw new AssertionError("Function must accept int[] in method: " + method, causeForMethod(method));
|
|
|
+// }
|
|
|
Type tparam = gparam.getActualTypeArguments()[1];
|
|
|
if(tparam instanceof Class<?>) {
|
|
|
return (Class<?>)tparam;
|
|
|
@@ -392,48 +409,26 @@ public final class TestContractRunner {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
-
|
|
|
- private interface TGenerator extends Function<int[], Object>, Supplier<Object> { }
|
|
|
-
|
|
|
- private static class TGeneratorF implements TGenerator {
|
|
|
-
|
|
|
- private final Function<int[], ?> function;
|
|
|
-
|
|
|
- public TGeneratorF(Function<int[], ?> function) {
|
|
|
- this.function = function;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Object get() {
|
|
|
- return function.apply(new int[0]);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Object apply(int[] t) {
|
|
|
- return function.apply(t);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- private static class TGeneratorS implements TGenerator {
|
|
|
-
|
|
|
- private final Supplier<?> function;
|
|
|
-
|
|
|
- public TGeneratorS(Supplier<?> function) {
|
|
|
- this.function = function;
|
|
|
- }
|
|
|
+
|
|
|
+ private final class Generator implements Function<Object, Object>, Supplier<Object> {
|
|
|
|
|
|
- @Override
|
|
|
- public Object get() {
|
|
|
- return function.get();
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ public Object apply(Object params) {
|
|
|
+ if( null == function) {
|
|
|
+ throw new AssertionError("Function is not set.");
|
|
|
+ }
|
|
|
+ return function.apply(params);
|
|
|
+ }
|
|
|
|
|
|
- @Override
|
|
|
- public Object apply(int[] t) {
|
|
|
- throw new UnsupportedOperationException("Generator function not supplied.");
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ public Object get() {
|
|
|
+ if(null == supplier) {
|
|
|
+ throw new AssertionError("Supplier is not set.");
|
|
|
+ }
|
|
|
+ return supplier.get();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
private static final class IParam {
|
|
|
private final Object object;
|