Skip to content

Commit

Permalink
TestFactoryTestDescriptors can be discovered by their unique IDs (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink authored and marcphilipp committed May 24, 2016
1 parent 77f7820 commit 509f287
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.junit.gen5.engine.discovery.MethodSelector;
import org.junit.gen5.engine.discovery.PackageSelector;
import org.junit.gen5.engine.discovery.UniqueIdSelector;
import org.junit.gen5.engine.junit5.descriptor.TestFactoryTestDescriptor;

/**
* @since 5.0
Expand Down Expand Up @@ -322,6 +323,33 @@ public void twoMethodResolutionsByUniqueId() {
assertSame(classFromMethod1, classFromMethod2);
}

@Test
public void resolvingDynamicTestByUniqueIdResolvesOnlyUpToParentTestFactory() {
UniqueIdSelector selector = UniqueIdSelector.forUniqueId(
uniqueIdForTestFactoryMethod(MyTestClass.class, "dynamicTest()").append(
TestFactoryTestDescriptor.DYNAMIC_TEST_SEGMENT_TYPE, "%1"));

resolver.resolveSelectors(request().select(selector).build(), engineDescriptor);

assertThat(engineDescriptor.allDescendants()).hasSize(2);

assertThat(uniqueIds()).containsSequence(uniqueIdForClass(MyTestClass.class),
uniqueIdForTestFactoryMethod(MyTestClass.class, "dynamicTest()"));
}

@Test
public void resolvingTestFactoryMethodByUniqueId() {
UniqueIdSelector selector = UniqueIdSelector.forUniqueId(
uniqueIdForTestFactoryMethod(MyTestClass.class, "dynamicTest()"));

resolver.resolveSelectors(request().select(selector).build(), engineDescriptor);

assertThat(engineDescriptor.allDescendants()).hasSize(2);

assertThat(uniqueIds()).containsSequence(uniqueIdForClass(MyTestClass.class),
uniqueIdForTestFactoryMethod(MyTestClass.class, "dynamicTest()"));
}

@Test
public void packageResolution() {
PackageSelector selector = PackageSelector.forPackageName("org.junit.gen5.engine.junit5.descriptor.subpackage");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
@API(Internal)
public class TestFactoryTestDescriptor extends MethodTestDescriptor implements Leaf<JUnit5EngineExecutionContext> {

public static final String DYNAMIC_TEST_SEGMENT_TYPE = "dynamic-test";

public TestFactoryTestDescriptor(UniqueId uniqueId, Class<?> testClass, Method testMethod) {
super(uniqueId, testClass, testMethod);
}
Expand Down Expand Up @@ -106,7 +108,7 @@ private Stream<? extends DynamicTest> toDynamicTestStream(Object testFactoryMeth
}

private void registerAndExecute(DynamicTest dynamicTest, int index, EngineExecutionListener listener) {
UniqueId uniqueId = getUniqueId().append("dynamic-test", "%" + index);
UniqueId uniqueId = getUniqueId().append(DYNAMIC_TEST_SEGMENT_TYPE, "%" + index);
DynamicTestTestDescriptor dynamicTestTestDescriptor = new DynamicTestTestDescriptor(uniqueId, dynamicTest,
getSource().get());
addChild(dynamicTestTestDescriptor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import java.lang.reflect.Method;

import org.junit.gen5.commons.util.StringUtils;
import org.junit.gen5.engine.TestDescriptor;
import org.junit.gen5.engine.UniqueId;
import org.junit.gen5.engine.junit5.descriptor.ClassTestDescriptor;
Expand All @@ -22,14 +21,14 @@ public class TestFactoryMethodResolver extends TestMethodResolver {

public static final String SEGMENT_TYPE = "test-factory";

protected boolean isTestMethod(Method candidate) {
return new IsTestFactoryMethod().test(candidate);
private final IsTestFactoryMethod isTestFactoryMethod = new IsTestFactoryMethod();

public TestFactoryMethodResolver() {
super(SEGMENT_TYPE);
}

protected UniqueId createUniqueId(Method testMethod, TestDescriptor parent) {
String methodId = String.format("%s(%s)", testMethod.getName(),
StringUtils.nullSafeToString(testMethod.getParameterTypes()));
return parent.getUniqueId().append(SEGMENT_TYPE, methodId);
protected boolean isTestMethod(Method candidate) {
return isTestFactoryMethod.test(candidate);
}

protected TestDescriptor resolveMethod(Method testMethod, ClassTestDescriptor parentClassDescriptor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public class TestMethodResolver implements ElementResolver {

public static final String SEGMENT_TYPE = "method";

private final String segmentType;

public TestMethodResolver() {
this(SEGMENT_TYPE);
}

protected TestMethodResolver(String segmentType) {
this.segmentType = segmentType;
}

@Override
public Set<TestDescriptor> resolveElement(AnnotatedElement element, TestDescriptor parent) {
if (!(element instanceof Method))
Expand All @@ -51,7 +61,7 @@ public Set<TestDescriptor> resolveElement(AnnotatedElement element, TestDescript

@Override
public Optional<TestDescriptor> resolveUniqueId(UniqueId.Segment segment, TestDescriptor parent) {
if (!segment.getType().equals(SEGMENT_TYPE))
if (!segment.getType().equals(segmentType))
return Optional.empty();

if (!(parent instanceof ClassTestDescriptor))
Expand All @@ -73,10 +83,10 @@ protected boolean isTestMethod(Method candidate) {
return new IsTestMethod().test(candidate);
}

protected UniqueId createUniqueId(Method testMethod, TestDescriptor parent) {
String methodId = String.format("%s(%s)", testMethod.getName(),
StringUtils.nullSafeToString(testMethod.getParameterTypes()));
return parent.getUniqueId().append(SEGMENT_TYPE, methodId);
private UniqueId createUniqueId(Method testMethod, TestDescriptor parent) {
Class<?>[] parameterTypes = testMethod.getParameterTypes();
String methodId = String.format("%s(%s)", testMethod.getName(), StringUtils.nullSafeToString(parameterTypes));
return parent.getUniqueId().append(segmentType, methodId);
}

private Optional<Method> findMethod(UniqueId.Segment segment, ClassTestDescriptor parent) {
Expand Down

0 comments on commit 509f287

Please # to comment.