Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace AtomicInteger with int in TestTemplateTestDescriptor.execute() #725

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
import static org.junit.platform.commons.meta.API.Usage.Internal;

import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

import org.junit.jupiter.api.extension.ContainerExtensionContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
Expand Down Expand Up @@ -89,13 +90,18 @@ public JupiterEngineExecutionContext execute(JupiterEngineExecutionContext conte
ContainerExtensionContext containerExtensionContext = (ContainerExtensionContext) context.getExtensionContext();
List<TestTemplateInvocationContextProvider> providers = validateProviders(containerExtensionContext,
context.getExtensionRegistry());
AtomicInteger invocationIndex = new AtomicInteger();
// @formatter:off
providers.stream()
.flatMap(provider -> provider.provide(containerExtensionContext))
.map(invocationContext -> createInvocationTestDescriptor(invocationContext, invocationIndex.incrementAndGet()))
.forEach(invocationTestDescriptor -> execute(dynamicTestExecutor, invocationTestDescriptor));
// @formatter:on

int invocationIndex = 0;
for (TestTemplateInvocationContextProvider provider : providers) {
Stream<TestTemplateInvocationContext> invocationContextStream = provider.provide(containerExtensionContext);
Iterator<TestTemplateInvocationContext> contextIterator = invocationContextStream.iterator();
while (contextIterator.hasNext()) {
TestDescriptor invocationTestDescriptor = createInvocationTestDescriptor(contextIterator.next(),
++invocationIndex);
execute(dynamicTestExecutor, invocationTestDescriptor);
}
invocationContextStream.close();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stream should be used within a try-with-resources statement to ensure it's closed in case odf an exception.

}
validateWasAtLeastInvokedOnce(invocationIndex);
return context;
}
Expand Down Expand Up @@ -127,8 +133,8 @@ private void execute(DynamicTestExecutor dynamicTestExecutor, TestDescriptor tes
dynamicTestExecutor.execute(testDescriptor);
}

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");
}
Expand Down