/*
 * Copyright 2015-2016 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 v1.0 which
 * accompanies this distribution and is available at
 *
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.junit.gen5.engine.junit4.execution;

import static org.junit.gen5.engine.TestExecutionResult.failed;

import org.junit.gen5.engine.EngineExecutionListener;
import org.junit.gen5.engine.TestExecutionResult;
import org.junit.gen5.engine.junit4.descriptor.RunnerTestDescriptor;
import org.junit.runner.JUnitCore;

public class RunnerExecutor {

	private final EngineExecutionListener engineExecutionListener;

	public RunnerExecutor(EngineExecutionListener engineExecutionListener) {
		this.engineExecutionListener = engineExecutionListener;
	}

	public void execute(RunnerTestDescriptor runnerTestDescriptor) {
		TestRun testRun = new TestRun(runnerTestDescriptor);
		JUnitCore core = new JUnitCore();
		core.addListener(new RunListenerAdapter(testRun, engineExecutionListener));
		try {
			core.run(runnerTestDescriptor.getRunner());
		}
		catch (Throwable t) {
			reportUnexpectedFailure(testRun, runnerTestDescriptor, failed(t));
		}
	}

	private void reportUnexpectedFailure(TestRun testRun, RunnerTestDescriptor runnerTestDescriptor,
			TestExecutionResult result) {
		if (testRun.isNotStarted(runnerTestDescriptor)) {
			engineExecutionListener.executionStarted(runnerTestDescriptor);
		}
		engineExecutionListener.executionFinished(runnerTestDescriptor, result);
	}

}

@@ -628,16 +628,81 @@
 nitCore;
+%0Aimport org.junit.runner.Request;%0Aimport org.junit.runner.Runner;
 %0A%0Apublic
@@ -1182,16 +1182,91 @@
 ore.run(
+new Request() %7B%0A%0A%09%09%09%09@Override%0A%09%09%09%09public Runner getRunner() %7B%0A%09%09%09%09%09return 
 runnerTe
@@ -1289,16 +1289,29 @@
 Runner()
+;%0A%09%09%09%09%7D%0A%0A%09%09%09%7D
 );%0A%09%09%7D%0A%09

Use official JUnitCore API (was: test-only)