Skip to content

Commit

Permalink
Resolving method selector works
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink authored and bechte committed Apr 29, 2016
1 parent e71ca17 commit dd501e4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
Expand Up @@ -52,7 +52,7 @@ public void testSingleClassResolution() {
assertTrue(uniqueIds.contains(uniqueIdForMethod(MyTestClass.class, "test2()")));
}

// @Test
@Test
public void testTwoClassesResolution() {
ClassSelector selector1 = ClassSelector.forClass(MyTestClass.class);
ClassSelector selector2 = ClassSelector.forClass(YourTestClass.class);
Expand Down Expand Up @@ -82,7 +82,7 @@ public void testClassResolutionOfNestedClass() {
assertTrue(uniqueIds.contains(uniqueIdForMethod(OtherTestClass.NestedTestClass.class, "test6()")));
}

// @Test
@Test
public void testMethodResolution() throws NoSuchMethodException {
MethodSelector selector = MethodSelector.forMethod(
MyTestClass.class.getDeclaredMethod("test1").getDeclaringClass(),
Expand All @@ -96,7 +96,7 @@ public void testMethodResolution() throws NoSuchMethodException {
assertTrue(uniqueIds.contains(uniqueIdForMethod(MyTestClass.class, "test1()")));
}

// @Test
@Test
public void testMethodResolutionFromInheritedMethod() throws NoSuchMethodException {
MethodSelector selector = MethodSelector.forMethod(HerTestClass.class,
MyTestClass.class.getDeclaredMethod("test1"));
Expand All @@ -119,15 +119,6 @@ public void resolvingSelectorOfNonTestMethodResolvesNothing() throws NoSuchMetho
assertTrue(engineDescriptor.allDescendants().isEmpty());
}

// @Test
public void testResolutionOfNotTestMethod() throws NoSuchMethodException {
MethodSelector selector = MethodSelector.forMethod(
MyTestClass.class.getDeclaredMethod("notATest").getDeclaringClass(),
MyTestClass.class.getDeclaredMethod("notATest"));
EngineDiscoveryRequest request = request().select(selector).build();
assertThrows(PreconditionViolationException.class, () -> resolver.resolveSelectors(request));
}

// @Test
public void testClassResolutionByUniqueId() {
UniqueIdSelector selector = UniqueIdSelector.forUniqueId(uniqueIdForClass(MyTestClass.class).getUniqueString());
Expand Down
Expand Up @@ -14,12 +14,14 @@

import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;

import org.junit.gen5.commons.util.ReflectionUtils;
import org.junit.gen5.engine.EngineDiscoveryRequest;
import org.junit.gen5.engine.TestDescriptor;
import org.junit.gen5.engine.UniqueId;
import org.junit.gen5.engine.discovery.ClassSelector;
import org.junit.gen5.engine.discovery.MethodSelector;
import org.junit.gen5.engine.junit5.descriptor.ClassTestDescriptor;
import org.junit.gen5.engine.junit5.descriptor.MethodTestDescriptor;
import org.junit.gen5.engine.junit5.discovery.IsPotentialTestContainer;
Expand All @@ -37,33 +39,46 @@ public void resolveSelectors(EngineDiscoveryRequest request) {
request.getSelectorsByType(ClassSelector.class).forEach(selector -> {
resolveClass(selector.getTestClass());
});
request.getSelectorsByType(MethodSelector.class).forEach(selector -> {
resolveMethod(selector.getTestClass(), selector.getTestMethod());
});

}

private void resolveMethod(Class<?> testClass, Method testMethod) {
Optional<TestDescriptor> optionalParent = resolve(testClass, engineDescriptor, false);
optionalParent.ifPresent(parent -> {
resolve(testMethod, parent, true);
});
}

private void resolveClass(Class<?> testClass) {
TestDescriptor parent = engineDescriptor;
resolve(testClass, parent, true);
}

private void resolve(Class<?> testClass, TestDescriptor parent, boolean withChildren) {
private Optional<TestDescriptor> resolve(Class<?> testClass, TestDescriptor parent, boolean withChildren) {
if (!new IsPotentialTestContainer().test(testClass))
return;
return Optional.empty();
UniqueId uniqueId = parent.getUniqueId().append("class", testClass.getName());
ClassTestDescriptor descriptor = new ClassTestDescriptor(uniqueId, testClass);
parent.addChild(descriptor);
ClassTestDescriptor classTestDescriptor = new ClassTestDescriptor(uniqueId, testClass);
parent.addChild(classTestDescriptor);

if (withChildren) {
List<Method> testMethodCandidates = findMethods(testClass, new IsTestMethod(),
ReflectionUtils.MethodSortOrder.HierarchyDown);
testMethodCandidates.forEach(method -> resolve(method, descriptor, true));
testMethodCandidates.forEach(method -> resolve(method, classTestDescriptor, true));
}

return Optional.of(classTestDescriptor);
}

private void resolve(Method testMethod, TestDescriptor parent, boolean withChildren) {
private Optional<TestDescriptor> resolve(Method testMethod, TestDescriptor parent, boolean withChildren) {
ClassTestDescriptor parentClassDescriptor = (ClassTestDescriptor) parent;
UniqueId uniqueId = parentClassDescriptor.getUniqueId().append("method", testMethod.getName() + "()");
MethodTestDescriptor methodTestDescriptor = new MethodTestDescriptor(uniqueId,
parentClassDescriptor.getTestClass(), testMethod);
parentClassDescriptor.addChild(methodTestDescriptor);
return Optional.of(methodTestDescriptor);
}
}

0 comments on commit dd501e4

Please sign in to comment.