From 69515138bb7d562d1050a99d9d7f768c3676e727 Mon Sep 17 00:00:00 2001 From: Giorgos Gaganis Date: Sat, 11 Mar 2017 19:08:19 +0200 Subject: [PATCH] Replace AtomicInteger with int in TestTemplateTestDescriptor.execute() An atomic integer is not really required to count the invocationIndex. A local int variable should shuffice. Rewrite using java5 for-each and while loop instead of lambdas to remove the need for an effectively final mutable number. --- .../descriptor/TestTemplateTestDescriptor.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateTestDescriptor.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateTestDescriptor.java index aa840626828e..3dcf926348ad 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateTestDescriptor.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateTestDescriptor.java @@ -16,7 +16,6 @@ import java.lang.reflect.Method; import java.util.Iterator; import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; import org.junit.jupiter.api.extension.ContainerExtensionContext; import org.junit.jupiter.api.extension.TestTemplateInvocationContext; @@ -90,16 +89,17 @@ public JupiterEngineExecutionContext execute(JupiterEngineExecutionContext conte ContainerExtensionContext containerExtensionContext = (ContainerExtensionContext) context.getExtensionContext(); List providers = validateProviders(containerExtensionContext, context.getExtensionRegistry()); - AtomicInteger invocationIndex = new AtomicInteger(); - providers.forEach(provider -> { + int invocationIndex = 0; + for (TestTemplateInvocationContextProvider provider : providers) { Iterator contextIterator = provider.provide(containerExtensionContext); - contextIterator.forEachRemaining(invocationContext -> { - int index = invocationIndex.incrementAndGet(); + while (contextIterator.hasNext()) { + TestTemplateInvocationContext invocationContext = contextIterator.next(); + int index = ++invocationIndex; TestDescriptor invocationTestDescriptor = createInvocationTestDescriptor(invocationContext, index); addChild(invocationTestDescriptor); dynamicTestExecutor.execute(invocationTestDescriptor); - }); - }); + } + } validateWasAtLeastInvokedOnce(invocationIndex); return context; } @@ -126,8 +126,8 @@ private TestDescriptor createInvocationTestDescriptor(TestTemplateInvocationCont invocationContext, index); } - private void validateWasAtLeastInvokedOnce(AtomicInteger invocationIndex) { - if (invocationIndex.get() == 0) { + private void validateWasAtLeastInvokedOnce(int invocationIndex) { + if (invocationIndex == 0) { throw new TestAbortedException("No supporting " + TestTemplateInvocationContextProvider.class.getSimpleName() + " provided an invocation context"); }