From 70f41edef4e26f84337cf46f8d34fb4a8fdf4af6 Mon Sep 17 00:00:00 2001 From: Johannes Link Date: Mon, 14 Dec 2015 08:55:14 +0100 Subject: [PATCH] Added ThrowingConsumer for TestExtensionRegistry ------------------------------------------------------------------------ On behalf of the community, the JUnit Lambda Team thanks AdNovum Informatik AG (http://www.adnovum.ch) JUnit crowdfunding campaign! ------------------------------------------------------------------------ --- .../execution/TestExtensionRegistryTest.java | 18 +++++++-------- .../descriptor/ClassTestDescriptor.java | 6 ++--- .../junit5/execution/MethodInvoker.java | 18 ++++++++++----- .../execution/TestExtensionRegistry.java | 8 +++---- .../junit5/execution/ThrowingConsumer.java | 23 +++++++++++++++++++ 5 files changed, 51 insertions(+), 22 deletions(-) create mode 100644 junit5-engine/src/main/java/org/junit/gen5/engine/junit5/execution/ThrowingConsumer.java diff --git a/junit-tests/src/test/java/org/junit/gen5/engine/junit5/execution/TestExtensionRegistryTest.java b/junit-tests/src/test/java/org/junit/gen5/engine/junit5/execution/TestExtensionRegistryTest.java index 4987721d1e2..ec0eb2fe7ce 100644 --- a/junit-tests/src/test/java/org/junit/gen5/engine/junit5/execution/TestExtensionRegistryTest.java +++ b/junit-tests/src/test/java/org/junit/gen5/engine/junit5/execution/TestExtensionRegistryTest.java @@ -46,7 +46,7 @@ public void checkDefaultExtensions() { } @Test - public void newRegistryWithoutParentHasDefaultExtensions() throws Exception { + public void newRegistryWithoutParentHasDefaultExtensions() throws Throwable { Set> extensions = registry.getRegisteredExtensionClasses(); Assert.assertEquals(TestExtensionRegistry.getDefaultExtensionClasses().size(), extensions.size()); @@ -59,7 +59,7 @@ public void newRegistryWithoutParentHasDefaultExtensions() throws Exception { } @Test - public void registerExtensionByImplementingClass() throws Exception { + public void registerExtensionByImplementingClass() throws Throwable { registry.addExtension(MyExtension.class); @@ -80,7 +80,7 @@ public void registerExtensionByImplementingClass() throws Exception { } @Test - public void registerTestExtensionThatImplementsMultipleExtensionPoints() throws Exception { + public void registerTestExtensionThatImplementsMultipleExtensionPoints() throws Throwable { registry.addExtension(MultipleExtension.class); @@ -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); @@ -110,7 +110,7 @@ public void extensionsAreInheritedFromParent() throws Exception { } @Test - public void registerExtensionPointsByExtensionRegistrar() throws Exception { + public void registerExtensionPointsByExtensionRegistrar() throws Throwable { registry.addExtension(MyExtensionRegistrar.class); @@ -120,7 +120,7 @@ public void registerExtensionPointsByExtensionRegistrar() throws Exception { } @Test - public void applyExtensionPointsInjectsCorrectRegisteredExceptionPoint() throws Exception { + public void applyExtensionPointsInjectsCorrectRegisteredExceptionPoint() throws Throwable { registry.addExtension(MyExtension.class); @@ -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"); @@ -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 extensionPointType) throws Exception { + private int countExtensionPoints(Class extensionPointType) throws Throwable { AtomicInteger counter = new AtomicInteger(); registry.applyExtensionPoints(extensionPointType, TestExtensionRegistry.ApplicationOrder.FORWARD, registeredExtensionPoint -> counter.incrementAndGet()); diff --git a/junit5-engine/src/main/java/org/junit/gen5/engine/junit5/descriptor/ClassTestDescriptor.java b/junit5-engine/src/main/java/org/junit/gen5/engine/junit5/descriptor/ClassTestDescriptor.java index 750993a4017..df810b50d7c 100644 --- a/junit5-engine/src/main/java/org/junit/gen5/engine/junit5/descriptor/ClassTestDescriptor.java +++ b/junit5-engine/src/main/java/org/junit/gen5/engine/junit5/descriptor/ClassTestDescriptor.java @@ -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; @@ -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. @@ -109,7 +109,7 @@ protected BeforeEachCallback beforeEachCallback(JUnit5EngineExecutionContext con List beforeEachMethods = findAnnotatedMethods(testClass, BeforeEach.class, MethodSortOrder.HierarchyDown); return (testExtensionContext, testInstance) -> { - Consumer> applyBeforeEach = registeredExtensionPoint -> { + ThrowingConsumer> applyBeforeEach = registeredExtensionPoint -> { try { registeredExtensionPoint.getExtensionPoint().beforeEach(testExtensionContext); } @@ -146,7 +146,7 @@ protected AfterEachCallback afterEachCallback(JUnit5EngineExecutionContext conte } } - Consumer> applyAfterEach = registeredExtensionPoint -> { + ThrowingConsumer> applyAfterEach = registeredExtensionPoint -> { try { registeredExtensionPoint.getExtensionPoint().afterEach(testExtensionContext); } diff --git a/junit5-engine/src/main/java/org/junit/gen5/engine/junit5/execution/MethodInvoker.java b/junit5-engine/src/main/java/org/junit/gen5/engine/junit5/execution/MethodInvoker.java index 9539bc1ef9e..18d952cb779 100644 --- a/junit5-engine/src/main/java/org/junit/gen5/engine/junit5/execution/MethodInvoker.java +++ b/junit5-engine/src/main/java/org/junit/gen5/engine/junit5/execution/MethodInvoker.java @@ -64,12 +64,18 @@ private Object[] resolveParameters(MethodContext methodContext) throws Parameter private Object resolveParameter(Parameter parameter, MethodContext methodContext) { try { final List 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( diff --git a/junit5-engine/src/main/java/org/junit/gen5/engine/junit5/execution/TestExtensionRegistry.java b/junit5-engine/src/main/java/org/junit/gen5/engine/junit5/execution/TestExtensionRegistry.java index 8ca8ad73540..f8221fbb53f 100644 --- a/junit5-engine/src/main/java/org/junit/gen5/engine/junit5/execution/TestExtensionRegistry.java +++ b/junit5-engine/src/main/java/org/junit/gen5/engine/junit5/execution/TestExtensionRegistry.java @@ -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; @@ -125,15 +124,16 @@ private List> getRegister * @param extensionPointApplier The code to execute for each extension point */ public void applyExtensionPoints(Class extensionClass, ApplicationOrder order, - Consumer> extensionPointApplier) { + ThrowingConsumer> extensionPointApplier) throws Throwable { List> registeredExtensionPoints = getRegisteredExtensionPoints(extensionClass); new ExtensionPointSorter().sort(registeredExtensionPoints); if (order == ApplicationOrder.BACKWARD) { Collections.reverse(registeredExtensionPoints); } - registeredExtensionPoints.stream().forEach( - registeredExtensionPoint -> extensionPointApplier.accept(registeredExtensionPoint)); + for (RegisteredExtensionPoint rep : registeredExtensionPoints) { + extensionPointApplier.accept(rep); + } } /** diff --git a/junit5-engine/src/main/java/org/junit/gen5/engine/junit5/execution/ThrowingConsumer.java b/junit5-engine/src/main/java/org/junit/gen5/engine/junit5/execution/ThrowingConsumer.java new file mode 100644 index 00000000000..35b8ec0391a --- /dev/null +++ b/junit5-engine/src/main/java/org/junit/gen5/engine/junit5/execution/ThrowingConsumer.java @@ -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 { + + /** + * Performs this operation on the given argument. + * + * @param t the input argument + */ + void accept(T t) throws Throwable; + +}