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

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

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

import java.util.*;
import java.util.stream.*;

import net.jqwik.api.*;
import net.jqwik.api.domains.*;
import net.jqwik.api.lifecycle.*;
import net.jqwik.engine.descriptor.*;
import net.jqwik.engine.support.*;

class DomainContextFactory {

	private final PropertyLifecycleContext propertyLifecycleContext;
	private final PropertyMethodDescriptor methodDescriptor;

	DomainContextFactory(
		PropertyLifecycleContext propertyLifecycleContext,
		PropertyMethodDescriptor methodDescriptor
	) {
		this.propertyLifecycleContext = propertyLifecycleContext;
		this.methodDescriptor = methodDescriptor;
	}

	DomainContext createCombinedDomainContext() {
		Set domainAnnotations = methodDescriptor.getDomains();

		if (domainAnnotations.isEmpty()) {
			return DomainContext.global();
		}
		Set domainContexts =
			domainAnnotations
				.stream()
				.flatMap(this::expandDomain)
				.map(this::annotationToTuple)
				.distinct()
				.map(this::createDomainContext)
				.peek(domainContext -> {
					domainContext.initialize(propertyLifecycleContext);
				})
				.collect(Collectors.toSet());
		return new CombinedDomainContext(domainContexts);
	}

	// Use this transformation to make distinct() call on stream meaningful
	private Tuple.Tuple2, Integer> annotationToTuple(Domain domain) {
		return Tuple.of(domain.value(), domain.priority());
	}

	private Stream expandDomain(Domain domain) {
		Stream inheritedDomains =
			JqwikAnnotationSupport.findContainerAnnotations(domain.value(), Domain.class).stream();
		return Stream.concat(
			Stream.of(domain),
			inheritedDomains
		);
	}

	private DomainContext createDomainContext(Tuple.Tuple2, Integer> domainSpec) {
		Class domainContextClass = domainSpec.get1();
		int domainPriority = domainSpec.get2();
		try {
			DomainContext domainContext =
				JqwikReflectionSupport.newInstanceInTestContext(domainContextClass, propertyLifecycleContext.testInstance());

			if (domainPriority != Domain.PRIORITY_NOT_SET) {
				domainContext.setDefaultPriority(domainPriority);
			}
			return domainContext;
		} catch (Throwable throwable) {
			String message = String.format(
				"Cannot instantiate domain context @Domain(\"%s\") on [%s].",
				domainContextClass, methodDescriptor.getTargetMethod()
			);
			throw new JqwikException(message);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy