Skip to content

Commit

Permalink
Added ThrowingConsumer for TestExtensionRegistry
Browse files Browse the repository at this point in the history
------------------------------------------------------------------------
On behalf of the community, the JUnit Lambda Team thanks
AdNovum Informatik AG (http://www.adnovum.ch)
JUnit crowdfunding campaign!
------------------------------------------------------------------------
  • Loading branch information
jlink committed Dec 14, 2015
1 parent 55a1054 commit 70f41ed
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 22 deletions.
Expand Up @@ -46,7 +46,7 @@ public void checkDefaultExtensions() {
}

@Test
public void newRegistryWithoutParentHasDefaultExtensions() throws Exception {
public void newRegistryWithoutParentHasDefaultExtensions() throws Throwable {
Set<Class<? extends TestExtension>> extensions = registry.getRegisteredExtensionClasses();

Assert.assertEquals(TestExtensionRegistry.getDefaultExtensionClasses().size(), extensions.size());
Expand All @@ -59,7 +59,7 @@ public void newRegistryWithoutParentHasDefaultExtensions() throws Exception {
}

@Test
public void registerExtensionByImplementingClass() throws Exception {
public void registerExtensionByImplementingClass() throws Throwable {

registry.addExtension(MyExtension.class);

Expand All @@ -80,7 +80,7 @@ public void registerExtensionByImplementingClass() throws Exception {
}

@Test
public void registerTestExtensionThatImplementsMultipleExtensionPoints() throws Exception {
public void registerTestExtensionThatImplementsMultipleExtensionPoints() throws Throwable {

registry.addExtension(MultipleExtension.class);

Expand All @@ -91,7 +91,7 @@ public void registerTestExtensionThatImplementsMultipleExtensionPoints() throws
}

@Test
public void extensionsAreInheritedFromParent() throws Exception {
public void extensionsAreInheritedFromParent() throws Throwable {

TestExtensionRegistry parent = new TestExtensionRegistry();
parent.addExtension(MyExtension.class);
Expand All @@ -110,7 +110,7 @@ public void extensionsAreInheritedFromParent() throws Exception {
}

@Test
public void registerExtensionPointsByExtensionRegistrar() throws Exception {
public void registerExtensionPointsByExtensionRegistrar() throws Throwable {

registry.addExtension(MyExtensionRegistrar.class);

Expand All @@ -120,7 +120,7 @@ public void registerExtensionPointsByExtensionRegistrar() throws Exception {
}

@Test
public void applyExtensionPointsInjectsCorrectRegisteredExceptionPoint() throws Exception {
public void applyExtensionPointsInjectsCorrectRegisteredExceptionPoint() throws Throwable {

registry.addExtension(MyExtension.class);

Expand All @@ -139,7 +139,7 @@ public void applyExtensionPointsInjectsCorrectRegisteredExceptionPoint() throws
}

@Test
public void registerExtensionPointDirectly() throws Exception {
public void registerExtensionPointDirectly() throws Throwable {

registry.registerExtension((MyExtensionPoint) test -> {
}, Position.INNERMOST, "anonymous extension");
Expand Down Expand Up @@ -173,13 +173,13 @@ public void exceptionInApplierCodeStopsIterationThroughExtensionPoints() {
throw new RuntimeException("should stop iteration");
});
}
catch (RuntimeException expected) {
catch (Throwable expected) {
}
Assert.assertEquals(1, countCalledExtensions.get());

}

private int countExtensionPoints(Class<? extends ExtensionPoint> extensionPointType) throws Exception {
private int countExtensionPoints(Class<? extends ExtensionPoint> extensionPointType) throws Throwable {
AtomicInteger counter = new AtomicInteger();
registry.applyExtensionPoints(extensionPointType, TestExtensionRegistry.ApplicationOrder.FORWARD,
registeredExtensionPoint -> counter.incrementAndGet());
Expand Down
Expand Up @@ -17,7 +17,6 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

import org.junit.gen5.api.AfterEach;
import org.junit.gen5.api.BeforeEach;
Expand All @@ -37,6 +36,7 @@
import org.junit.gen5.engine.junit5.execution.RegisteredExtensionPoint;
import org.junit.gen5.engine.junit5.execution.TestExtensionRegistry;
import org.junit.gen5.engine.junit5.execution.TestInstanceProvider;
import org.junit.gen5.engine.junit5.execution.ThrowingConsumer;

/**
* {@link TestDescriptor} for tests based on Java classes.
Expand Down Expand Up @@ -109,7 +109,7 @@ protected BeforeEachCallback beforeEachCallback(JUnit5EngineExecutionContext con
List<Method> beforeEachMethods = findAnnotatedMethods(testClass, BeforeEach.class,
MethodSortOrder.HierarchyDown);
return (testExtensionContext, testInstance) -> {
Consumer<RegisteredExtensionPoint<BeforeEachExtensionPoint>> applyBeforeEach = registeredExtensionPoint -> {
ThrowingConsumer<RegisteredExtensionPoint<BeforeEachExtensionPoint>> applyBeforeEach = registeredExtensionPoint -> {
try {
registeredExtensionPoint.getExtensionPoint().beforeEach(testExtensionContext);
}
Expand Down Expand Up @@ -146,7 +146,7 @@ protected AfterEachCallback afterEachCallback(JUnit5EngineExecutionContext conte
}
}

Consumer<RegisteredExtensionPoint<AfterEachExtensionPoint>> applyAfterEach = registeredExtensionPoint -> {
ThrowingConsumer<RegisteredExtensionPoint<AfterEachExtensionPoint>> applyAfterEach = registeredExtensionPoint -> {
try {
registeredExtensionPoint.getExtensionPoint().afterEach(testExtensionContext);
}
Expand Down
Expand Up @@ -64,12 +64,18 @@ private Object[] resolveParameters(MethodContext methodContext) throws Parameter
private Object resolveParameter(Parameter parameter, MethodContext methodContext) {
try {
final List<MethodParameterResolver> matchingResolvers = new ArrayList<>();
extensionRegistry.applyExtensionPoints(MethodParameterResolver.class, ApplicationOrder.FORWARD,
registeredExtensionPoint -> {
if (registeredExtensionPoint.getExtensionPoint().supports(parameter, methodContext,
testExtensionContext))
matchingResolvers.add(registeredExtensionPoint.getExtensionPoint());
});
try {
extensionRegistry.applyExtensionPoints(MethodParameterResolver.class, ApplicationOrder.FORWARD,
registeredExtensionPoint -> {
if (registeredExtensionPoint.getExtensionPoint().supports(parameter, methodContext,
testExtensionContext))
matchingResolvers.add(registeredExtensionPoint.getExtensionPoint());
});
}
catch (Throwable throwable) {
//TODO: How to handle exceptions during parameter resolution?
throwable.printStackTrace();
}

if (matchingResolvers.size() == 0) {
throw new ParameterResolutionException(
Expand Down
Expand Up @@ -17,7 +17,6 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;

import org.junit.gen5.api.extension.ExtensionPoint;
import org.junit.gen5.api.extension.ExtensionPoint.Position;
Expand Down Expand Up @@ -125,15 +124,16 @@ private <T extends ExtensionPoint> List<RegisteredExtensionPoint<T>> getRegister
* @param extensionPointApplier The code to execute for each extension point
*/
public <T extends ExtensionPoint> void applyExtensionPoints(Class<T> extensionClass, ApplicationOrder order,
Consumer<RegisteredExtensionPoint<T>> extensionPointApplier) {
ThrowingConsumer<RegisteredExtensionPoint<T>> extensionPointApplier) throws Throwable {
List<RegisteredExtensionPoint<T>> registeredExtensionPoints = getRegisteredExtensionPoints(extensionClass);
new ExtensionPointSorter().sort(registeredExtensionPoints);
if (order == ApplicationOrder.BACKWARD) {
Collections.reverse(registeredExtensionPoints);
}

registeredExtensionPoints.stream().forEach(
registeredExtensionPoint -> extensionPointApplier.accept(registeredExtensionPoint));
for (RegisteredExtensionPoint<T> rep : registeredExtensionPoints) {
extensionPointApplier.accept(rep);
}
}

/**
Expand Down
@@ -0,0 +1,23 @@
/*
* 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.junit5.execution;

@FunctionalInterface
public interface ThrowingConsumer<T> {

/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t) throws Throwable;

}

0 comments on commit 70f41ed

Please sign in to comment.