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

net.jqwik.engine.hooks.lifecycle.TryLifecycleMethodsHook Maven / Gradle / Ivy

There is a newer version: 1.9.1
Show newest version
package net.jqwik.engine.hooks.lifecycle;

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

import org.junit.platform.engine.support.hierarchical.*;

import net.jqwik.api.lifecycle.*;
import net.jqwik.engine.hooks.*;
import net.jqwik.engine.support.*;

public class TryLifecycleMethodsHook implements AroundTryHook {

	private void beforeTry(TryLifecycleContext context) {
		List beforeTryMethods = LifecycleMethods.findBeforeTryMethods(context.containerClass());
		callTryMethods(beforeTryMethods, context);
	}

	private void callTryMethods(List methods, TryLifecycleContext context) {
		Object testInstance = context.testInstance();
		ThrowableCollector throwableCollector = new ThrowableCollector(ignore -> false);
		for (Method method : methods) {
			Object[] parameters = MethodParameterResolver.resolveParameters(method, context);
			throwableCollector.execute(() -> callMethod(method, testInstance, parameters));
		}
		throwableCollector.assertEmpty();
	}

	private void callMethod(Method method, Object target, Object[] parameters) {
		JqwikReflectionSupport.invokeMethodPotentiallyOuter(method, target, parameters);
	}

	private void afterTry(TryLifecycleContext context) {
		List afterTryMethods = LifecycleMethods.findAfterTryMethods(context.containerClass());
		callTryMethods(afterTryMethods, context);
	}

	@Override
	public PropagationMode propagateTo() {
		return PropagationMode.ALL_DESCENDANTS;
	}

	@Override
	public boolean appliesTo(Optional element) {
		return element.map(e -> e instanceof Method).orElse(false);
	}

	@Override
	public int aroundTryProximity() {
		return Hooks.AroundTry.TRY_LIFECYCLE_METHODS_PROXIMITY;
	}

	@Override
	public TryExecutionResult aroundTry(TryLifecycleContext context, TryExecutor aTry, List parameters) {
		beforeTry(context);
		try {
			return aTry.execute(parameters);
		} finally {
			afterTry(context);
		}
	}
}