All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.jqwik.engine.execution.DefaultPropertyLifecycleContext Maven / Gradle / Ivy

The newest version!
package net.jqwik.engine.execution;

import java.lang.reflect.*;
import java.util.*;

import net.jqwik.api.*;
import net.jqwik.api.lifecycle.*;
import net.jqwik.api.lifecycle.ResolveParameterHook.*;
import net.jqwik.engine.descriptor.*;
import net.jqwik.engine.execution.lifecycle.*;
import net.jqwik.engine.support.*;

public class DefaultPropertyLifecycleContext extends AbstractLifecycleContext implements PropertyLifecycleContext {

	private final PropertyMethodDescriptor methodDescriptor;
	private final ContainerInstances testInstances;
	private final ParameterSupplierResolver parameterSupplierResolver;

	public DefaultPropertyLifecycleContext(
		PropertyMethodDescriptor methodDescriptor,
		Object testInstance,
		Reporter reporter,
		ResolveParameterHook resolveParameterHook
	) {
		this(methodDescriptor, new ContainerInstances(testInstance), reporter, resolveParameterHook);
	}

	public DefaultPropertyLifecycleContext(
		PropertyMethodDescriptor methodDescriptor,
		ContainerInstances testInstances,
		Reporter reporter,
		ResolveParameterHook resolveParameterHook
	) {
		super(reporter, methodDescriptor);
		this.parameterSupplierResolver = new ParameterSupplierResolver(resolveParameterHook, this);
		this.methodDescriptor = methodDescriptor;
		this.testInstances = testInstances;
	}

	@Override
	public Method targetMethod() {
		return methodDescriptor.getTargetMethod();
	}

	@Override
	public Class containerClass() {
		return methodDescriptor.getContainerClass();
	}

	@Override
	public Optional optionalElement() {
		return Optional.of(targetMethod());
	}

	@Override
	public Optional> optionalContainerClass() {
		return Optional.of(containerClass());
	}

	@Override
	public Object testInstance() {
		return testInstances.target();
	}

	@Override
	public List testInstances() {
		return testInstances.all();
	}

	@Override
	public String extendedLabel() {
		return methodDescriptor.extendedLabel();
	}

	@Override
	public  T newInstance(Class clazz) {
		return JqwikReflectionSupport.newInstanceInTestContext(clazz, testInstance());
	}

	@Override
	public Optional resolveParameter(Executable executable, int index) {
		return parameterSupplierResolver.resolveParameter(executable, index, containerClass());
	}

	@Override
	public PropertyAttributes attributes() {
		return methodDescriptor.getConfiguration().getPropertyAttributes();
	}

	@Override
	public String toString() {
		return toString(PropertyLifecycleContext.class);
	}

}