Skip to content

Commit

Permalink
Renamed @dynamic to avoid confusion between factory and products (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Merdes authored and marcphilipp committed May 23, 2016
1 parent e8eeefa commit 77f7820
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 50 deletions.
16 changes: 8 additions & 8 deletions documentation/src/test/java/example/DynamicTestsDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@
import java.util.stream.Stream;

import org.junit.gen5.api.Assertions;
import org.junit.gen5.api.Dynamic;
import org.junit.gen5.api.DynamicTest;
import org.junit.gen5.api.Tag;
import org.junit.gen5.api.TestFactory;
import org.junit.gen5.junit4.runner.JUnit5;
import org.junit.runner.RunWith;

@RunWith(JUnit5.class)
@Tag("exclude")
public class DynamicTestsDemo {

// @Dynamic
// @TestFactory
List<String> dynamicTestsWithWrongReturnType() {
List<String> tests = new ArrayList<>();
tests.add("Hallo");
return tests;
}

@Dynamic
@TestFactory
List<DynamicTest> dynamicTestsFromList() {
List<DynamicTest> tests = new ArrayList<>();

Expand All @@ -45,30 +45,30 @@ List<DynamicTest> dynamicTestsFromList() {
return tests;
}

@Dynamic
@TestFactory
Stream<DynamicTest> dynamicTestsFromStream() {
String[] testNames = new String[] { "test1", "test2" };
return Arrays.stream(testNames).map(name -> new DynamicTest(name, () -> {
}));
}

@Dynamic
@TestFactory
Iterator<DynamicTest> dynamicTestStreamFromIterator() {
List<DynamicTest> tests = new ArrayList<>();
tests.add(new DynamicTest("succeedingTest", () -> Assertions.assertTrue(true, "succeeding")));
tests.add(new DynamicTest("failingTest", () -> Assertions.assertTrue(false, "failing")));
return tests.iterator();
}

@Dynamic
@TestFactory
Iterable<DynamicTest> dynamicTestStreamFromIterable() {
List<DynamicTest> tests = new ArrayList<>();
tests.add(new DynamicTest("succeedingTest", () -> Assertions.assertTrue(true, "succeeding")));
tests.add(new DynamicTest("failingTest", () -> Assertions.assertTrue(false, "failing")));
return tests;
}

@Dynamic
@TestFactory
Iterator<DynamicTest> generatedTestsFromGeneratorFunction() {
Iterator<DynamicTest> generator = new Iterator<DynamicTest>() {
int counter = 0;
Expand All @@ -87,7 +87,7 @@ public DynamicTest next() {
return generator;
}

@Dynamic
@TestFactory
Stream<DynamicTest> generatedRandomNumberOfTests() {
final int AVERAGE = 49;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@
import java.util.List;
import java.util.stream.Stream;

import org.junit.gen5.api.Dynamic;
import org.junit.gen5.api.DynamicTest;
import org.junit.gen5.api.Test;
import org.junit.gen5.api.TestFactory;
import org.junit.gen5.engine.ExecutionEventRecorder;
import org.junit.gen5.engine.TestDescriptor;
import org.junit.gen5.launcher.TestDiscoveryRequest;

class DynamicTestGenerationTests extends AbstractJUnit5TestEngineTests {

@Test
public void dynamicTestMethodsAreCorrectlyDiscoveredForClassSelector() {
public void testFactoryMethodsAreCorrectlyDiscoveredForClassSelector() {
TestDiscoveryRequest request = request().select(forClass(MyDynamicTestCase.class)).build();
TestDescriptor engineDescriptor = discoverTests(request);
assertEquals(5, engineDescriptor.allDescendants().size(), "# resolved test descriptors");
}

@Test
public void dynamicTestMethodIsCorrectlyDiscoveredForMethodSelector() {
public void testFactoryMethodIsCorrectlyDiscoveredForMethodSelector() {
TestDiscoveryRequest request = request().select(forMethod(MyDynamicTestCase.class, "dynamicStream")).build();
TestDescriptor engineDescriptor = discoverTests(request);
assertEquals(2, engineDescriptor.allDescendants().size(), "# resolved test descriptors");
Expand Down Expand Up @@ -113,7 +113,7 @@ public void dynamicTestsAreExecutedFromIterable() {

private static class MyDynamicTestCase {

@Dynamic
@TestFactory
Stream<DynamicTest> dynamicStream() {
List<DynamicTest> tests = new ArrayList<>();

Expand All @@ -123,7 +123,7 @@ Stream<DynamicTest> dynamicStream() {
return tests.stream();
}

@Dynamic
@TestFactory
Collection<DynamicTest> dynamicCollection() {
List<DynamicTest> tests = new ArrayList<>();

Expand All @@ -133,7 +133,7 @@ Collection<DynamicTest> dynamicCollection() {
return tests;
}

@Dynamic
@TestFactory
Iterator<DynamicTest> dynamicIterator() {
List<DynamicTest> tests = new ArrayList<>();

Expand All @@ -143,7 +143,7 @@ Iterator<DynamicTest> dynamicIterator() {
return tests.iterator();
}

@Dynamic
@TestFactory
Iterable<DynamicTest> dynamicIterable() {
return this::dynamicIterator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import static org.junit.gen5.api.Assertions.expectThrows;
import static org.junit.gen5.engine.junit5.discovery.JUnit5UniqueIdBuilder.engineId;
import static org.junit.gen5.engine.junit5.discovery.JUnit5UniqueIdBuilder.uniqueIdForClass;
import static org.junit.gen5.engine.junit5.discovery.JUnit5UniqueIdBuilder.uniqueIdForDynamicMethod;
import static org.junit.gen5.engine.junit5.discovery.JUnit5UniqueIdBuilder.uniqueIdForMethod;
import static org.junit.gen5.engine.junit5.discovery.JUnit5UniqueIdBuilder.uniqueIdForTestFactoryMethod;
import static org.junit.gen5.launcher.main.TestDiscoveryRequestBuilder.request;

import java.io.File;
Expand All @@ -28,10 +28,10 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.gen5.api.Dynamic;
import org.junit.gen5.api.DynamicTest;
import org.junit.gen5.api.Nested;
import org.junit.gen5.api.Test;
import org.junit.gen5.api.TestFactory;
import org.junit.gen5.commons.JUnitException;
import org.junit.gen5.commons.util.PreconditionViolationException;
import org.junit.gen5.engine.DiscoverySelector;
Expand Down Expand Up @@ -63,7 +63,7 @@ public void singleClassResolution() {
assertTrue(uniqueIds.contains(uniqueIdForClass(MyTestClass.class)));
assertTrue(uniqueIds.contains(uniqueIdForMethod(MyTestClass.class, "test1()")));
assertTrue(uniqueIds.contains(uniqueIdForMethod(MyTestClass.class, "test2()")));
assertTrue(uniqueIds.contains(uniqueIdForDynamicMethod(MyTestClass.class, "dynamicTest()")));
assertTrue(uniqueIds.contains(uniqueIdForTestFactoryMethod(MyTestClass.class, "dynamicTest()")));
}

@Test
Expand All @@ -78,7 +78,7 @@ public void duplicateClassSelectorOnlyResolvesOnce() {
assertTrue(uniqueIds.contains(uniqueIdForClass(MyTestClass.class)));
assertTrue(uniqueIds.contains(uniqueIdForMethod(MyTestClass.class, "test1()")));
assertTrue(uniqueIds.contains(uniqueIdForMethod(MyTestClass.class, "test2()")));
assertTrue(uniqueIds.contains(uniqueIdForDynamicMethod(MyTestClass.class, "dynamicTest()")));
assertTrue(uniqueIds.contains(uniqueIdForTestFactoryMethod(MyTestClass.class, "dynamicTest()")));
}

@Test
Expand All @@ -93,7 +93,7 @@ public void twoClassesResolution() {
assertTrue(uniqueIds.contains(uniqueIdForClass(MyTestClass.class)));
assertTrue(uniqueIds.contains(uniqueIdForMethod(MyTestClass.class, "test1()")));
assertTrue(uniqueIds.contains(uniqueIdForMethod(MyTestClass.class, "test2()")));
assertTrue(uniqueIds.contains(uniqueIdForDynamicMethod(MyTestClass.class, "dynamicTest()")));
assertTrue(uniqueIds.contains(uniqueIdForTestFactoryMethod(MyTestClass.class, "dynamicTest()")));
assertTrue(uniqueIds.contains(uniqueIdForClass(YourTestClass.class)));
assertTrue(uniqueIds.contains(uniqueIdForMethod(YourTestClass.class, "test3()")));
assertTrue(uniqueIds.contains(uniqueIdForMethod(YourTestClass.class, "test4()")));
Expand Down Expand Up @@ -158,7 +158,7 @@ public void classResolutionByUniqueId() {
assertTrue(uniqueIds.contains(uniqueIdForClass(MyTestClass.class)));
assertTrue(uniqueIds.contains(uniqueIdForMethod(MyTestClass.class, "test1()")));
assertTrue(uniqueIds.contains(uniqueIdForMethod(MyTestClass.class, "test2()")));
assertTrue(uniqueIds.contains(uniqueIdForDynamicMethod(MyTestClass.class, "dynamicTest()")));
assertTrue(uniqueIds.contains(uniqueIdForTestFactoryMethod(MyTestClass.class, "dynamicTest()")));
}

@Test
Expand Down Expand Up @@ -493,7 +493,7 @@ void notATest() {

}

@Dynamic
@TestFactory
Stream<DynamicTest> dynamicTest() {
return new ArrayList<DynamicTest>().stream();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public static UniqueId uniqueIdForMethod(Class<?> clazz, String methodPart) {
return uniqueIdForClass(clazz).append(TestMethodResolver.SEGMENT_TYPE, methodPart);
}

public static UniqueId uniqueIdForDynamicMethod(Class<?> clazz, String methodPart) {
return uniqueIdForClass(clazz).append(DynamicTestMethodResolver.SEGMENT_TYPE, methodPart);
public static UniqueId uniqueIdForTestFactoryMethod(Class<?> clazz, String methodPart) {
return uniqueIdForClass(clazz).append(TestFactoryMethodResolver.SEGMENT_TYPE, methodPart);
}

public static UniqueId engineId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
@Retention(RetentionPolicy.RUNTIME)
@Documented
@API(Experimental)
public @interface Dynamic {
public @interface TestFactory {
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
import org.junit.gen5.engine.support.hierarchical.SingleTestExecutor;

@API(Internal)
public class DynamicMethodTestDescriptor extends MethodTestDescriptor implements Leaf<JUnit5EngineExecutionContext> {
public class TestFactoryTestDescriptor extends MethodTestDescriptor implements Leaf<JUnit5EngineExecutionContext> {

public DynamicMethodTestDescriptor(UniqueId uniqueId, Class<?> testClass, Method testMethod) {
public TestFactoryTestDescriptor(UniqueId uniqueId, Class<?> testClass, Method testMethod) {
super(uniqueId, testClass, testMethod);
}

Expand All @@ -64,8 +64,8 @@ protected void invokeTestMethod(JUnit5EngineExecutionContext context, TestExtens
testExtensionContext.getTestInstance(), testExtensionContext.getTestMethod());

MethodInvoker methodInvoker = new MethodInvoker(testExtensionContext, context.getExtensionRegistry());
Object dynamicMethodResult = methodInvoker.invoke(methodInvocationContext);
Stream<? extends DynamicTest> dynamicTestStream = toDynamicTestStream(dynamicMethodResult);
Object testFactoryMethodResult = methodInvoker.invoke(methodInvocationContext);
Stream<? extends DynamicTest> dynamicTestStream = toDynamicTestStream(testFactoryMethodResult);

AtomicInteger index = new AtomicInteger();
try {
Expand All @@ -81,27 +81,28 @@ protected void invokeTestMethod(JUnit5EngineExecutionContext context, TestExtens
}

@SuppressWarnings("unchecked")
private Stream<? extends DynamicTest> toDynamicTestStream(Object dynamicMethodResult) {
private Stream<? extends DynamicTest> toDynamicTestStream(Object testFactoryMethodResult) {

if (dynamicMethodResult instanceof Stream) {
return (Stream<? extends DynamicTest>) dynamicMethodResult;
if (testFactoryMethodResult instanceof Stream) {
return (Stream<? extends DynamicTest>) testFactoryMethodResult;
}
// use Collection's stream() implementation even though it implements Iterable
if (dynamicMethodResult instanceof Collection) {
Collection<? extends DynamicTest> dynamicTestCollection = (Collection<? extends DynamicTest>) dynamicMethodResult;
if (testFactoryMethodResult instanceof Collection) {
Collection<? extends DynamicTest> dynamicTestCollection = (Collection<? extends DynamicTest>) testFactoryMethodResult;
return dynamicTestCollection.stream();
}
if (dynamicMethodResult instanceof Iterable) {
Iterable<? extends DynamicTest> dynamicTestIterable = (Iterable<? extends DynamicTest>) dynamicMethodResult;
if (testFactoryMethodResult instanceof Iterable) {
Iterable<? extends DynamicTest> dynamicTestIterable = (Iterable<? extends DynamicTest>) testFactoryMethodResult;
return StreamSupport.stream(dynamicTestIterable.spliterator(), false);
}
if (dynamicMethodResult instanceof Iterator) {
Iterator<? extends DynamicTest> dynamicTestIterator = (Iterator<? extends DynamicTest>) dynamicMethodResult;
if (testFactoryMethodResult instanceof Iterator) {
Iterator<? extends DynamicTest> dynamicTestIterator = (Iterator<? extends DynamicTest>) testFactoryMethodResult;
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(dynamicTestIterator, Spliterator.ORDERED),
false);
}

throw new JUnitException("Dynamic test must return Stream, Iterable, or Iterator of " + DynamicTest.class);
throw new JUnitException(
"Test factory method must return Stream, Iterable, or Iterator of " + DynamicTest.class);
}

private void registerAndExecute(DynamicTest dynamicTest, int index, EngineExecutionListener listener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private JavaElementsResolver createJavaElementsResolver(TestDescriptor engineDes
resolvers.add(new TestContainerResolver());
resolvers.add(new NestedTestsResolver());
resolvers.add(new TestMethodResolver());
resolvers.add(new DynamicTestMethodResolver());
resolvers.add(new TestFactoryMethodResolver());
return new JavaElementsResolver(engineDescriptor, resolvers);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
public class IsTestClassWithTests implements Predicate<Class<?>> {

private static final IsTestMethod isTestMethod = new IsTestMethod();
private static final IsDynamicTestMethod isDynamicTestMethod = new IsDynamicTestMethod();
private static final IsTestFactoryMethod IS_TEST_FACTORY_METHOD = new IsTestFactoryMethod();

private static final IsPotentialTestContainer isPotentialTestContainer = new IsPotentialTestContainer();

Expand All @@ -46,7 +46,7 @@ private boolean hasTestMethods(Class<?> candidate) {
}

private boolean hasDynamicTests(Class<?> candidate) {
return !ReflectionUtils.findMethods(candidate, isDynamicTestMethod, HierarchyDown).isEmpty();
return !ReflectionUtils.findMethods(candidate, IS_TEST_FACTORY_METHOD, HierarchyDown).isEmpty();
}

private boolean hasNestedTests(Class<?> candidate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.lang.reflect.Method;
import java.util.function.Predicate;

import org.junit.gen5.api.Dynamic;
import org.junit.gen5.api.TestFactory;
import org.junit.gen5.commons.meta.API;

/**
Expand All @@ -28,7 +28,7 @@
* @since 5.0
*/
@API(Internal)
public class IsDynamicTestMethod implements Predicate<Method> {
public class IsTestFactoryMethod implements Predicate<Method> {

@Override
public boolean test(Method candidate) {
Expand All @@ -40,7 +40,7 @@ public boolean test(Method candidate) {
if (isAbstract(candidate))
return false;

return isAnnotated(candidate, Dynamic.class);
return isAnnotated(candidate, TestFactory.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
import org.junit.gen5.engine.TestDescriptor;
import org.junit.gen5.engine.UniqueId;
import org.junit.gen5.engine.junit5.descriptor.ClassTestDescriptor;
import org.junit.gen5.engine.junit5.descriptor.DynamicMethodTestDescriptor;
import org.junit.gen5.engine.junit5.descriptor.TestFactoryTestDescriptor;

public class DynamicTestMethodResolver extends TestMethodResolver {
public class TestFactoryMethodResolver extends TestMethodResolver {

public static final String SEGMENT_TYPE = "dynamic";
public static final String SEGMENT_TYPE = "test-factory";

protected boolean isTestMethod(Method candidate) {
return new IsDynamicTestMethod().test(candidate);
return new IsTestFactoryMethod().test(candidate);
}

protected UniqueId createUniqueId(Method testMethod, TestDescriptor parent) {
Expand All @@ -34,7 +34,7 @@ protected UniqueId createUniqueId(Method testMethod, TestDescriptor parent) {

protected TestDescriptor resolveMethod(Method testMethod, ClassTestDescriptor parentClassDescriptor,
UniqueId uniqueId) {
return new DynamicMethodTestDescriptor(uniqueId, parentClassDescriptor.getTestClass(), testMethod);
return new TestFactoryTestDescriptor(uniqueId, parentClassDescriptor.getTestClass(), testMethod);
}

}

0 comments on commit 77f7820

Please sign in to comment.