Skip to content

Commit

Permalink
#171 Unresolvable Unique IDs result only in logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink committed Mar 17, 2016
1 parent 0e169e5 commit 0c59d4c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
Expand Up @@ -19,7 +19,6 @@


import org.junit.gen5.api.Nested; import org.junit.gen5.api.Nested;
import org.junit.gen5.api.Test; import org.junit.gen5.api.Test;
import org.junit.gen5.commons.util.PreconditionViolationException;
import org.junit.gen5.engine.EngineDiscoveryRequest; import org.junit.gen5.engine.EngineDiscoveryRequest;
import org.junit.gen5.engine.TestDescriptor; import org.junit.gen5.engine.TestDescriptor;
import org.junit.gen5.engine.UniqueId; import org.junit.gen5.engine.UniqueId;
Expand Down Expand Up @@ -155,15 +154,12 @@ public void testMethodOfInnerClassByUniqueId() {
} }


@Test @Test
public void testNonResolvableUniqueId() { public void testNonResolvableUniqueIdResolvesNothing() {
UniqueIdSelector selector = UniqueIdSelector.forUniqueId(engineId().append("poops", "machine")); UniqueIdSelector selector = UniqueIdSelector.forUniqueId(engineId().append("poops", "machine"));
EngineDiscoveryRequest request = request().select(selector).build(); EngineDiscoveryRequest request = request().select(selector).build();


PreconditionViolationException exception = expectThrows(PreconditionViolationException.class, () -> { resolver.resolveSelectors(request);
resolver.resolveSelectors(request); assertTrue(engineDescriptor.allDescendants().isEmpty());
});
assertTrue(exception.getMessage().contains("Cannot resolve part '[poops:machine]'"),
"Exception message wrong: " + exception.getMessage());
} }


@Test @Test
Expand Down Expand Up @@ -196,7 +192,6 @@ public void testMethodResolutionByUniqueIdFromInheritedClass() {
assertEquals(2, engineDescriptor.allDescendants().size()); assertEquals(2, engineDescriptor.allDescendants().size());
List<UniqueId> uniqueIds = uniqueIds(); List<UniqueId> uniqueIds = uniqueIds();


// System.out.println(uniqueIds);
assertTrue(uniqueIds.contains(uniqueIdForClass(HerTestClass.class))); assertTrue(uniqueIds.contains(uniqueIdForClass(HerTestClass.class)));
assertTrue(uniqueIds.contains(uniqueIdForMethod(HerTestClass.class, "test1()"))); assertTrue(uniqueIds.contains(uniqueIdForMethod(HerTestClass.class, "test1()")));
} }
Expand All @@ -215,12 +210,13 @@ public void testMethodResolutionByUniqueIdWithParams() {
} }


@Test @Test
public void testMethodResolutionByUniqueIdWithWrongParams() { public void testMethodResolutionByUniqueIdWithWrongParamsResolvesNothing() {
UniqueIdSelector selector = UniqueIdSelector.forUniqueId( UniqueIdSelector selector = UniqueIdSelector.forUniqueId(
uniqueIdForMethod(HerTestClass.class, "test7(java.math.BigDecimal)")); uniqueIdForMethod(HerTestClass.class, "test7(java.math.BigDecimal)"));
EngineDiscoveryRequest request = request().select(selector).build(); EngineDiscoveryRequest request = request().select(selector).build();


assertThrows(PreconditionViolationException.class, () -> resolver.resolveSelectors(request)); resolver.resolveSelectors(request);
assertTrue(engineDescriptor.allDescendants().isEmpty());
} }


@Test @Test
Expand Down
Expand Up @@ -14,11 +14,9 @@
import static org.junit.gen5.commons.util.ReflectionUtils.loadClass; import static org.junit.gen5.commons.util.ReflectionUtils.loadClass;


import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.stream.Collectors;


import org.junit.gen5.commons.util.PreconditionViolationException; import org.junit.gen5.commons.util.PreconditionViolationException;
import org.junit.gen5.commons.util.Preconditions; import org.junit.gen5.commons.util.Preconditions;
Expand Down Expand Up @@ -106,18 +104,25 @@ private JUnit5Testable createTestable(UniqueId uniqueId, UniqueId engineId, List
} }
case TYPE_METHOD: { case TYPE_METHOD: {
Class<?> container = ((JUnit5Class) last).getJavaClass(); Class<?> container = ((JUnit5Class) last).getJavaClass();
next = fromMethod(findMethod(head.getValue(), container, uniqueId), container, engineId); Optional<Method> optionalMethod = findMethod(head.getValue(), container);
if (!optionalMethod.isPresent()) {
LOG.warning(() -> String.format("Cannot resolve part '%s' of unique ID '%s'", head,
uniqueId.getUniqueString()));
return JUnit5Testable.doNothing();
}
next = fromMethod(optionalMethod.get(), container, engineId);
break; break;
} }
default: default:
throw createCannotResolveUniqueIdException(uniqueId, head.toString()); LOG.warning(() -> String.format("Cannot resolve part '%s' of unique ID '%s'", head,
uniqueId.getUniqueString()));
return JUnit5Testable.doNothing();
} }
return createTestable(uniqueId, engineId, parts, next); return createTestable(uniqueId, engineId, parts, next);
} }


private Method findMethod(String methodSpecPart, Class<?> clazz, UniqueId uniqueId) { private Optional<Method> findMethod(String methodSpecPart, Class<?> clazz) {
Optional<Method> optionalMethod = new MethodFinder().findMethod(methodSpecPart, clazz); return new MethodFinder().findMethod(methodSpecPart, clazz);
return optionalMethod.orElseThrow(() -> createCannotResolveUniqueIdException(uniqueId, methodSpecPart));
} }


private Class<?> findNestedClass(String nameExtension, Class<?> containerClass) { private Class<?> findNestedClass(String nameExtension, Class<?> containerClass) {
Expand Down
Expand Up @@ -15,6 +15,7 @@
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;

import org.junit.gen5.commons.util.ReflectionUtils; import org.junit.gen5.commons.util.ReflectionUtils;


public class MethodFinder { public class MethodFinder {
Expand Down

0 comments on commit 0c59d4c

Please sign in to comment.