Skip to content

Commit

Permalink
Replace Consumer<TestDescriptor> with custom interface
Browse files Browse the repository at this point in the history
Issue: #14
  • Loading branch information
marcphilipp committed Feb 24, 2017
1 parent 0448e81 commit fc3c61b
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 25 deletions.
Expand Up @@ -10,8 +10,6 @@


package org.junit.jupiter.engine.descriptor; package org.junit.jupiter.engine.descriptor;


import java.util.function.Consumer;

import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext; import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext;
import org.junit.platform.engine.TestDescriptor; import org.junit.platform.engine.TestDescriptor;
Expand Down Expand Up @@ -45,7 +43,7 @@ public boolean isContainer() {


@Override @Override
public JupiterEngineExecutionContext execute(JupiterEngineExecutionContext context, public JupiterEngineExecutionContext execute(JupiterEngineExecutionContext context,
Consumer<TestDescriptor> dynamicTestExecutor) throws Exception { DynamicTestExecutor dynamicTestExecutor) throws Exception {
executeAndMaskThrowable(dynamicTest.getExecutable()); executeAndMaskThrowable(dynamicTest.getExecutable());
return context; return context;
} }
Expand Down
Expand Up @@ -17,7 +17,6 @@
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Consumer;


import org.junit.jupiter.api.extension.AfterEachCallback; import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.AfterTestExecutionCallback; import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
Expand Down Expand Up @@ -144,7 +143,7 @@ public SkipResult shouldBeSkipped(JupiterEngineExecutionContext context) throws


@Override @Override
public JupiterEngineExecutionContext execute(JupiterEngineExecutionContext context, public JupiterEngineExecutionContext execute(JupiterEngineExecutionContext context,
Consumer<TestDescriptor> dynamicTestExecutor) throws Exception { DynamicTestExecutor dynamicTestExecutor) throws Exception {
ThrowableCollector throwableCollector = context.getThrowableCollector(); ThrowableCollector throwableCollector = context.getThrowableCollector();


// @formatter:off // @formatter:off
Expand Down Expand Up @@ -203,8 +202,7 @@ private <T extends Extension> void invokeBeforeMethodsOrCallbacksUntilExceptionO
} }
} }


protected void invokeTestMethod(JupiterEngineExecutionContext context, protected void invokeTestMethod(JupiterEngineExecutionContext context, DynamicTestExecutor dynamicTestExecutor) {
Consumer<TestDescriptor> dynamicTestExecutor) {
TestExtensionContext testExtensionContext = (TestExtensionContext) context.getExtensionContext(); TestExtensionContext testExtensionContext = (TestExtensionContext) context.getExtensionContext();
ThrowableCollector throwableCollector = context.getThrowableCollector(); ThrowableCollector throwableCollector = context.getThrowableCollector();


Expand Down
Expand Up @@ -19,7 +19,6 @@
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.stream.Stream; import java.util.stream.Stream;


import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.DynamicTest;
Expand Down Expand Up @@ -73,8 +72,7 @@ public boolean isLeaf() {
} }


@Override @Override
protected void invokeTestMethod(JupiterEngineExecutionContext context, protected void invokeTestMethod(JupiterEngineExecutionContext context, DynamicTestExecutor dynamicTestExecutor) {
Consumer<TestDescriptor> dynamicTestExecutor) {
TestExtensionContext testExtensionContext = (TestExtensionContext) context.getExtensionContext(); TestExtensionContext testExtensionContext = (TestExtensionContext) context.getExtensionContext();


context.getThrowableCollector().execute(() -> { context.getThrowableCollector().execute(() -> {
Expand Down Expand Up @@ -118,11 +116,11 @@ private Stream<DynamicTest> toDynamicTestStream(TestExtensionContext testExtensi
throw invalidReturnTypeException(testExtensionContext); throw invalidReturnTypeException(testExtensionContext);
} }


private void registerAndExecute(DynamicTest dynamicTest, int index, Consumer<TestDescriptor> dynamicTestExecutor) { private void registerAndExecute(DynamicTest dynamicTest, int index, DynamicTestExecutor dynamicTestExecutor) {
UniqueId uniqueId = getUniqueId().append(DYNAMIC_TEST_SEGMENT_TYPE, "#" + index); UniqueId uniqueId = getUniqueId().append(DYNAMIC_TEST_SEGMENT_TYPE, "#" + index);
TestDescriptor descriptor = new DynamicTestTestDescriptor(uniqueId, dynamicTest, getSource().get()); TestDescriptor descriptor = new DynamicTestTestDescriptor(uniqueId, dynamicTest, getSource().get());
addChild(descriptor); addChild(descriptor);
dynamicTestExecutor.accept(descriptor); dynamicTestExecutor.execute(descriptor);
} }


private JUnitException invalidReturnTypeException(TestExtensionContext testExtensionContext) { private JUnitException invalidReturnTypeException(TestExtensionContext testExtensionContext) {
Expand Down
Expand Up @@ -25,11 +25,11 @@
* @since 5.0 * @since 5.0
*/ */
@API(Internal) @API(Internal)
public final class TestTemplateContainerExtensionContext extends AbstractExtensionContext<TestTemplateTestDescriptor> final class TestTemplateContainerExtensionContext extends AbstractExtensionContext<TestTemplateTestDescriptor>
implements ContainerExtensionContext { implements ContainerExtensionContext {


public TestTemplateContainerExtensionContext(ExtensionContext parent, TestTemplateContainerExtensionContext(ExtensionContext parent, EngineExecutionListener engineExecutionListener,
EngineExecutionListener engineExecutionListener, TestTemplateTestDescriptor testDescriptor) { TestTemplateTestDescriptor testDescriptor) {
super(parent, engineExecutionListener, testDescriptor); super(parent, engineExecutionListener, testDescriptor);
} }


Expand Down
Expand Up @@ -18,7 +18,6 @@
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;


import org.junit.jupiter.api.extension.ContainerExtensionContext; import org.junit.jupiter.api.extension.ContainerExtensionContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContext; import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
Expand Down Expand Up @@ -116,7 +115,7 @@ public SkipResult shouldBeSkipped(JupiterEngineExecutionContext context) throws


@Override @Override
public JupiterEngineExecutionContext execute(JupiterEngineExecutionContext context, public JupiterEngineExecutionContext execute(JupiterEngineExecutionContext context,
Consumer<TestDescriptor> dynamicTestExecutor) throws Exception { DynamicTestExecutor dynamicTestExecutor) throws Exception {
ContainerExtensionContext containerExtensionContext = (ContainerExtensionContext) context.getExtensionContext(); ContainerExtensionContext containerExtensionContext = (ContainerExtensionContext) context.getExtensionContext();
List<TestTemplateInvocationContextProvider> providers = validateProviders(containerExtensionContext, List<TestTemplateInvocationContextProvider> providers = validateProviders(containerExtensionContext,
context.getExtensionRegistry()); context.getExtensionRegistry());
Expand All @@ -127,7 +126,7 @@ public JupiterEngineExecutionContext execute(JupiterEngineExecutionContext conte
int index = invocationIndex.incrementAndGet(); int index = invocationIndex.incrementAndGet();
TestDescriptor invocationTestDescriptor = createInvocationTestDescriptor(invocationContext, index); TestDescriptor invocationTestDescriptor = createInvocationTestDescriptor(invocationContext, index);
addChild(invocationTestDescriptor); addChild(invocationTestDescriptor);
dynamicTestExecutor.accept(invocationTestDescriptor); dynamicTestExecutor.execute(invocationTestDescriptor);
}); });
}); });
validateWasAtLeastInvokedOnce(invocationIndex); validateWasAtLeastInvokedOnce(invocationIndex);
Expand Down
Expand Up @@ -13,7 +13,6 @@
import static org.junit.platform.commons.meta.API.Usage.Experimental; import static org.junit.platform.commons.meta.API.Usage.Experimental;


import java.util.Optional; import java.util.Optional;
import java.util.function.Consumer;


import org.junit.platform.commons.meta.API; import org.junit.platform.commons.meta.API;
import org.junit.platform.commons.util.ToStringBuilder; import org.junit.platform.commons.util.ToStringBuilder;
Expand Down Expand Up @@ -91,7 +90,7 @@ default C before(C context) throws Exception {
* @see #before * @see #before
* @see #after * @see #after
*/ */
default C execute(C context, Consumer<TestDescriptor> dynamicTestExecutor) throws Exception { default C execute(C context, DynamicTestExecutor dynamicTestExecutor) throws Exception {
return execute(context); return execute(context);
} }


Expand All @@ -107,7 +106,8 @@ default C execute(C context, Consumer<TestDescriptor> dynamicTestExecutor) throw
* *
* @see #before * @see #before
* @see #after * @see #after
* @deprecated Please use {@link #execute(EngineExecutionContext, Consumer)} instead. * @deprecated Please use
* {@link #execute(EngineExecutionContext, DynamicTestExecutor)} instead.
*/ */
@Deprecated @Deprecated
default C execute(C context) throws Exception { default C execute(C context) throws Exception {
Expand Down Expand Up @@ -197,4 +197,27 @@ public String toString() {
} }
} }


/**
* Executor for additional, dynamic test descriptors discovered during
* execution of a {@link Node}.
*
* <p>The test descriptors will be executed by the same
* {@link HierarchicalTestExecutor} that executes the submitting node.
*
* <p>This interface is not intended to be implemented by clients.
*
* @see Node#execute(EngineExecutionContext, DynamicTestExecutor)
* @see HierarchicalTestExecutor
*/
interface DynamicTestExecutor {

/**
* Submit a dynamic test descriptor for immediate execution.
*
* @param testDescriptor the test descriptor to be executed
*/
void execute(TestDescriptor testDescriptor);

}

} }
Expand Up @@ -69,7 +69,7 @@ public SkipResult shouldBeSkipped(DemoEngineExecutionContext context) throws Exc


@Override @Override
public DemoEngineExecutionContext execute(DemoEngineExecutionContext context, public DemoEngineExecutionContext execute(DemoEngineExecutionContext context,
Consumer<TestDescriptor> dynamicTestExecutor) { DynamicTestExecutor dynamicTestExecutor) {
if (this.executeBlock != null) { if (this.executeBlock != null) {
this.executeBlock.run(); this.executeBlock.run();
} }
Expand Down
Expand Up @@ -35,6 +35,7 @@
import org.junit.platform.engine.TestExecutionResult; import org.junit.platform.engine.TestExecutionResult;
import org.junit.platform.engine.UniqueId; import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.support.descriptor.AbstractTestDescriptor; import org.junit.platform.engine.support.descriptor.AbstractTestDescriptor;
import org.junit.platform.engine.support.hierarchical.Node.DynamicTestExecutor;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.InOrder; import org.mockito.InOrder;
import org.opentest4j.TestAbortedException; import org.opentest4j.TestAbortedException;
Expand Down Expand Up @@ -337,8 +338,8 @@ public void executesDynamicTestDescriptors() throws Exception {
MyLeaf dynamicTestDescriptor = spy(new MyLeaf(leafUniqueId.append("dynamic", "child"))); MyLeaf dynamicTestDescriptor = spy(new MyLeaf(leafUniqueId.append("dynamic", "child")));


when(child.execute(any(), any())).thenAnswer(invocation -> { when(child.execute(any(), any())).thenAnswer(invocation -> {
Consumer<TestDescriptor> dynamicTestExecutor = invocation.getArgument(1); DynamicTestExecutor dynamicTestExecutor = invocation.getArgument(1);
dynamicTestExecutor.accept(dynamicTestDescriptor); dynamicTestExecutor.execute(dynamicTestDescriptor);
return invocation.getArgument(0); return invocation.getArgument(0);
}); });
root.addChild(child); root.addChild(child);
Expand Down Expand Up @@ -430,7 +431,7 @@ protected MyLeaf(UniqueId uniqueId) {


@Override @Override
public MyEngineExecutionContext execute(MyEngineExecutionContext context, public MyEngineExecutionContext execute(MyEngineExecutionContext context,
Consumer<TestDescriptor> dynamicTestExecutor) throws Exception { DynamicTestExecutor dynamicTestExecutor) throws Exception {
return context; return context;
} }


Expand Down

0 comments on commit fc3c61b

Please sign in to comment.