Skip to content

Commit

Permalink
Cleanup after pull-request review junit-team#3890
Browse files Browse the repository at this point in the history
Make error messaging even more precise. Print out the provider class
that did not return any template invocation contexts.
  • Loading branch information
nskvortsov committed Oct 13, 2024
1 parent e6fc9f4 commit 8e0ae1b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

package org.junit.jupiter.engine.descriptor;

import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static org.apiguardian.api.API.Status.INTERNAL;
import static org.junit.jupiter.engine.descriptor.ExtensionUtils.populateNewExtensionRegistryFromExtendWithAnnotation;
Expand All @@ -19,6 +18,7 @@
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

import org.apiguardian.api.API;
import org.junit.jupiter.api.extension.ExecutableInvoker;
Expand Down Expand Up @@ -101,18 +101,22 @@ public JupiterEngineExecutionContext execute(JupiterEngineExecutionContext conte
List<TestTemplateInvocationContextProvider> providers = validateProviders(extensionContext,
context.getExtensionRegistry());
AtomicInteger invocationIndex = new AtomicInteger();
// @formatter:off
providers.stream()
.flatMap(provider -> provider.provideTestTemplateInvocationContexts(extensionContext))
.map(invocationContext -> createInvocationTestDescriptor(invocationContext, invocationIndex.incrementAndGet()))
.filter(Optional::isPresent)
.map(Optional::get)
.forEach(invocationTestDescriptor -> execute(dynamicTestExecutor, invocationTestDescriptor));
// @formatter:on
boolean allProvidersMayBeEmpty = providers.stream().allMatch(
p -> p.mayReturnZeroInvocationContexts(extensionContext));
if (!allProvidersMayBeEmpty) {
validateWasAtLeastInvokedOnce(invocationIndex.get(), extensionContext, providers);
for (TestTemplateInvocationContextProvider provider : providers) {
int initialValue = invocationIndex.get();
try (Stream<TestTemplateInvocationContext> templateInvocationContexts = //
provider.provideTestTemplateInvocationContexts(extensionContext)) {
// @formatter:off
templateInvocationContexts
.map(invocationContext -> createInvocationTestDescriptor(invocationContext, invocationIndex.incrementAndGet()))
.filter(Optional::isPresent)
.map(Optional::get)
.forEach(invocationTestDescriptor -> execute(dynamicTestExecutor, invocationTestDescriptor));
// @formatter:on
}
Preconditions.condition(
invocationIndex.get() != initialValue || provider.mayReturnZeroInvocationContexts(extensionContext),
"Provider [" + provider.getClass().getSimpleName() //
+ "] did not provide invocation contexts, but is expected to do so");
}
return context;
}
Expand Down Expand Up @@ -145,18 +149,4 @@ private void execute(DynamicTestExecutor dynamicTestExecutor, TestDescriptor tes
testDescriptor.setParent(this);
dynamicTestExecutor.execute(testDescriptor);
}

private void validateWasAtLeastInvokedOnce(int invocationIndex, ExtensionContext extensionContext,
List<TestTemplateInvocationContextProvider> providers) {

boolean allMayReturnEmptyContext = providers.stream().allMatch(
p -> p.mayReturnZeroInvocationContexts(extensionContext));

Preconditions.condition(invocationIndex > 0 || allMayReturnEmptyContext,
() -> "None of the supporting " + TestTemplateInvocationContextProvider.class.getSimpleName() + "s "
+ providers.stream().filter(p -> !p.mayReturnZeroInvocationContexts(extensionContext)).map(
provider -> provider.getClass().getSimpleName()).collect(joining(", ", "[", "]"))
+ " provided a non-empty stream");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,9 @@ void templateWithSupportingProviderButNoInvocationsReportsFailure() {
wrappedInContainerEvents(MyTestTemplateTestCase.class, //
event(container("templateWithSupportingProviderButNoInvocations"), started()), //
event(container("templateWithSupportingProviderButNoInvocations"),
finishedWithFailure(message("None of the supporting TestTemplateInvocationContextProviders ["
finishedWithFailure(message("Provider ["
+ InvocationContextProviderThatSupportsEverythingButProvidesNothing.class.getSimpleName()
+ "] provided a non-empty stream")))));
+ "] did not provide invocation contexts, but is expected to do so")))));
}

@Test
Expand All @@ -382,9 +382,9 @@ void templateWithMixedProvidersNoInvocationReportsFailure() {
wrappedInContainerEvents(MyTestTemplateTestCase.class, //
event(container("templateWithMultipleProvidersAllowingAndRestrictingToProvideNothing"), started()), //
event(container("templateWithMultipleProvidersAllowingAndRestrictingToProvideNothing"),
finishedWithFailure(message("None of the supporting TestTemplateInvocationContextProviders ["
finishedWithFailure(message("Provider ["
+ InvocationContextProviderThatSupportsEverythingButProvidesNothing.class.getSimpleName()
+ "] provided a non-empty stream")))));
+ "] did not provide invocation contexts, but is expected to do so")))));
}

@Test
Expand Down

0 comments on commit 8e0ae1b

Please sign in to comment.