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

net.jqwik.spring.JupiterExtensionContextAdapter Maven / Gradle / Ivy

The newest version!
package net.jqwik.spring;

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

import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.*;
import org.junit.jupiter.api.parallel.*;

import net.jqwik.api.lifecycle.*;

class JupiterExtensionContextAdapter implements ExtensionContext {

	private final LifecycleContext context;

	JupiterExtensionContextAdapter(LifecycleContext context) {
		this.context = context;
	}

	@Override
	public Optional getParent() {
		return Optional.empty();
	}

	@Override
	public ExtensionContext getRoot() {
		// Used in expression evaluation to get store
		return this;
	}

	@Override
	public String getUniqueId() {
		throw new UnsupportedOperationException();
	}

	@Override
	public String getDisplayName() {
		return context.label();
	}

	@Override
	public Set getTags() {
		throw new UnsupportedOperationException();
	}

	@Override
	public Optional getElement() {
		if (context instanceof PropertyLifecycleContext) {
			return Optional.of(((PropertyLifecycleContext) context).targetMethod());
		}
		if (context instanceof TryLifecycleContext) {
			return Optional.of(((TryLifecycleContext) context).targetMethod());
		}
		return context.optionalElement();
	}

	@Override
	public Optional> getTestClass() {
		return context.optionalContainerClass();
	}

	@Override
	public Optional getTestInstanceLifecycle() {
		return Optional.empty();
	}

	@Override
	public Optional getTestInstance() {
		if (context instanceof PropertyLifecycleContext) {
			return Optional.of(((PropertyLifecycleContext) context).testInstance());
		}
		if (context instanceof TryLifecycleContext) {
			return Optional.of(((TryLifecycleContext) context).testInstance());
		}
		return Optional.empty();
	}

	@Override
	public Optional getTestInstances() {
		throw new UnsupportedOperationException();
	}

	@Override
	public Optional getTestMethod() {
		if (context instanceof PropertyLifecycleContext) {
			return Optional.of(((PropertyLifecycleContext) context).targetMethod());
		}
		if (context instanceof TryLifecycleContext) {
			return Optional.of(((TryLifecycleContext) context).targetMethod());
		}
		return Optional.empty();
	}

	@Override
	public Optional getExecutionException() {
		return Optional.empty();
	}

	@Override
	public Optional getConfigurationParameter(String key) {
		return Optional.empty();
	}

	@Override
	public  Optional getConfigurationParameter(String key, Function transformer) {
		return Optional.empty();
	}

	@Override
	public void publishReportEntry(Map map) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Store getStore(Namespace namespace) {
		return new JupiterStoreAdapter(context);
	}

	@Override
	public ExecutionMode getExecutionMode() {
		return ExecutionMode.SAME_THREAD;
	}

	@Override
	public ExecutableInvoker getExecutableInvoker() {
		// TODO: Is it being used for Spring extension?
		return null;
	}
}