/*
 * Copyright 2015-2018 the original author or authors.
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v2.0 which
 * accompanies this distribution and is available at
 *
 * http://www.eclipse.org/legal/epl-v20.html
 */

package org.junit.jupiter.engine;

import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;
import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request;

import java.util.Set;

import org.junit.platform.engine.DiscoverySelector;
import org.junit.platform.engine.ExecutionRequest;
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.testkit.engine.ExecutionRecorder;
import org.junit.platform.testkit.engine.ExecutionResults;

/**
 * Abstract base class for tests involving the {@link JupiterTestEngine}.
 *
 * @since 5.0
 */
public abstract class AbstractJupiterTestEngineTests {

	private final JupiterTestEngine engine = new JupiterTestEngine();

	protected ExecutionResults executeTestsForClass(Class<?> testClass) {
		return executeTests(selectClass(testClass));
	}

	protected ExecutionResults executeTests(DiscoverySelector... selectors) {
		return executeTests(request().selectors(selectors).build());
	}

	protected ExecutionResults executeTests(LauncherDiscoveryRequest request) {
		TestDescriptor testDescriptor = discoverTests(request);
		ExecutionRecorder executionRecorder = new ExecutionRecorder();
		engine.execute(new ExecutionRequest(testDescriptor, executionRecorder, request.getConfigurationParameters()));
		return executionRecorder.getExecutionResults();
	}

	protected TestDescriptor discoverTests(DiscoverySelector... selectors) {
		return discoverTests(request().selectors(selectors).build());
	}

	protected TestDescriptor discoverTests(LauncherDiscoveryRequest request) {
		return engine.discover(request, UniqueId.forEngine(engine.getId()));
	}

	protected UniqueId discoverUniqueId(Class<?> clazz, String methodName) {
		TestDescriptor engineDescriptor = discoverTests(selectMethod(clazz, methodName));
		Set<? extends TestDescriptor> descendants = engineDescriptor.getDescendants();
		// @formatter:off
		TestDescriptor testDescriptor = descendants.stream()
				.skip(descendants.size() - 1)
				.findFirst()
				.orElseGet(() -> fail("no descendants"));
		// @formatter:on
		return testDescriptor.getUniqueId();
	}

}

@@ -728,59 +728,8 @@
 or;%0A
-import org.junit.platform.engine.ExecutionRequest;%0A
 impo
@@ -1567,289 +1567,61 @@
 %7B%0A%09%09
-TestDescriptor testDescriptor = discoverTests(request);%0A%09%09ExecutionRecorder executionRecorder = new ExecutionRecorder();%0A%09%09engine.execute(new ExecutionRequest(testDescriptor, executionRecorder, request.getConfigurationParameters()));%0A%09%09return executionRecorder.getExecutionResults(
+return ExecutionRecorder.execute(this.engine, request
 );%0A%09

Simplify internal use of the Test Kit