/*
 * Copyright 2015 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;

import static java.util.Collections.emptySet;

import java.util.Optional;
import java.util.Set;

import org.junit.gen5.engine.TestDescriptor;
import org.junit.gen5.engine.TestSource;
import org.junit.gen5.engine.TestTag;
import org.junit.runner.Description;

interface JUnit4TestDescriptor extends TestDescriptor {

	String ENGINE_ID = "junit4";

	@Override
	default String getUniqueId() {
		// TODO Use unique ID if set, too
		return ENGINE_ID + ":" + getDescription().getDisplayName();
	}

	@Override
	default String getDisplayName() {
		return getDescription().getDisplayName();
	}

	@Override
	default boolean isTest() {
		return getDescription().isTest();
	}

	@Override
	default Set<TestTag> getTags() {
		return emptySet();
	}

	@Override
	default Optional<TestSource> getSource() {
		return Optional.empty();
	}

	Description getDescription();

}

@@ -383,24 +383,151 @@
 s.emptySet;%0A
+import static org.junit.gen5.commons.util.ReflectionUtils.findMethods;%0A%0Aimport java.lang.reflect.Method;%0Aimport java.util.List;
 %0Aimport java
@@ -565,16 +565,57 @@
 l.Set;%0A%0A
+import org.junit.gen5.engine.JavaSource;%0A
 import o
@@ -1305,30 +1305,718 @@
 %7B%0A%09%09
-return Optional.empty(
+Optional%3CMethod%3E testMethod = getTestMethod();%0A%09%09if (testMethod.isPresent()) %7B%0A%09%09%09return Optional.of(new JavaSource(testMethod.get()));%0A%09%09%7D%0A%09%09return getTestClass().map(JavaSource::new);%0A%09%7D%0A%0A%09default Optional%3CMethod%3E getTestMethod() %7B%0A%09%09Optional%3CClass%3C?%3E%3E testClass = getTestClass();%0A%09%09String methodName = getDescription().getMethodName();%0A%09%09if (testClass.isPresent() && methodName != null) %7B%0A%09%09%09List%3CMethod%3E methods = findMethods(testClass.get(), method -%3E methodName.equals(method.getName()));%0A%09%09%09if (methods.size() == 1) %7B%0A%09%09%09%09return Optional.of(methods.get(0));%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return Optional.empty();%0A%09%7D%0A%0A%09default Optional%3CClass%3C?%3E%3E getTestClass() %7B%0A%09%09return Optional.ofNullable(getDescription().getTestClass()
 );%0A%09

Implement TestDescriptor#getSource() for JUnit 4