parameters = Arrays
.stream(testClass.getDeclaredMethods())
.filter(m -> m.isAnnotationPresent(Parameters.class))
.peek(m -> checkParametersAnnotationForMethod(m))
.reduce((o, n) -> {
throw new InternalError(
"@Parameters method can't only be once");
}).orElse(null);
parameterFields = Arrays
.stream(testClass.getDeclaredFields())
.filter(f -> f.isAnnotationPresent(Parameter.class))
.peek(f -> {
if (parameters == null) {
throw new InternalError(
"@Parameter can't be used without @Parameters method");
}
})
.peek(f -> checkParameterAnnotationForField(f))
.collect(
Collectors
.<Field, Integer, Field> toMap(
(Field f) -> f.getAnnotation(
Parameter.class).value(),
(Field f) -> f,
(f1, f2) -> {
throw new InternalError(
"@Parameter can't be used twice with the same value number");
}));
if (parameters != null) {
// assuming field numbering 0 to
int size = parameterFields.size();
if (size == 0) {
throw new InternalError("No @Parameter field found");
}
int expected = size * (size - 1) / 2;
int sum = parameterFields.keySet().stream().mapToInt(i -> i).sum();
if (sum != expected) {
throw new InternalError(
"@Parameter field number aren't continuus");
}
parameterFields
.values()
.stream()
.forEach(
f -> {
Parameter p = f.getAnnotation(Parameter.class);
if (p.filter()) {
if (filterParameterField != null) {
throw new InternalError(
"@Parameter filter attribute can only be used once per test class.");
}
if (!BiFunction.class.isAssignableFrom(f
.getType())) {
throw new InternalError(
"@Parameter filter attribute can only be use on BiFunction.");
}
filterParameterField = f;
}