net.jqwik.engine.properties.PropertyMethodDataResolver Maven / Gradle / Ivy
package net.jqwik.engine.properties;
import java.lang.reflect.*;
import java.util.*;
import java.util.function.*;
import org.junit.platform.commons.support.*;
import net.jqwik.api.*;
import net.jqwik.api.providers.*;
import net.jqwik.engine.support.*;
import static net.jqwik.engine.support.JqwikReflectionSupport.*;
public class PropertyMethodDataResolver implements DataResolver {
private final Class> containerClass;
private final Object testInstance;
public PropertyMethodDataResolver(Class> containerClass, Object testInstance) {
this.containerClass = containerClass;
this.testInstance = testInstance;
}
@SuppressWarnings("unchecked")
@Override
public Optional> forMethod(Method method) {
Optional optionalDataFrom = AnnotationSupport.findAnnotation(method, FromData.class);
return optionalDataFrom
.map(FromData::value)
.map(generatorName -> {
Supplier exceptionSupplier =
() -> new JqwikException("No data provider method (annotated with @Data) for generator [" + generatorName + "] found");
return findGenerator(generatorName).orElseThrow(exceptionSupplier);
})
.map(generatorMethod -> JqwikReflectionSupport.invokeMethodPotentiallyOuter(generatorMethod, testInstance))
.map(invocationResult -> (Iterable) invocationResult);
}
private Optional findGenerator(String generatorName) {
Function generatorNameSupplier = method -> {
Data generateAnnotation = method.getDeclaredAnnotation(Data.class);
return generateAnnotation.value();
};
TypeUsage targetType = TypeUsage.of(Iterable.class, TypeUsage.wildcard(TypeUsage.of(Tuple.class)));
return findGeneratorMethod(generatorName, this.containerClass, Data.class, generatorNameSupplier, targetType);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy