Search in sources :

Example 1 with DynamicTest

use of org.junit.jupiter.api.DynamicTest in project connect-utils by jcustenborder.

the class BaseDocumentationTest method schema.

@TestFactory
public Stream<DynamicTest> schema() throws IOException {
    final File parentDirectory = new File(outputDirectory, "schemas");
    if (!parentDirectory.exists()) {
        parentDirectory.mkdirs();
    }
    List<Schema> schemas = schemas();
    if (null != schemas && !schemas.isEmpty()) {
        final File schemaRstPath = new File(outputDirectory, "schemas.rst");
        final String schemaRst = "=======\n" + "Schemas\n" + "=======\n" + "\n" + ".. toctree::\n" + "    :maxdepth: 0\n" + "    :caption: Schemas:\n" + "    :glob:\n" + "\n" + "    schemas/*";
        Files.write(schemaRst, schemaRstPath, Charsets.UTF_8);
    }
    final String templateName = "rst/schema.rst.ftl";
    return this.schemas().stream().filter(schema -> !Strings.isNullOrEmpty(schema.name())).map(schema -> dynamicTest(String.format("%s.%s", schema.type(), schema.name()), () -> {
        StringBuilder filenameBuilder = new StringBuilder().append(schema.type().toString().toLowerCase());
        if (!Strings.isNullOrEmpty(schema.name())) {
            filenameBuilder.append('.').append(schema.name());
        }
        filenameBuilder.append(".rst");
        File outputFile = new File(parentDirectory, filenameBuilder.toString());
        Template template = configuration.getTemplate(templateName);
        log.info("Writing {}", outputFile);
        try (Writer writer = Files.newWriter(outputFile, Charsets.UTF_8)) {
            Map<String, Object> variables = ImmutableMap.of("input", TemplateSchema.of(schema), "helper", new RstTemplateHelper());
            template.process(variables, writer);
        }
    }));
}
Also used : Graphviz(guru.nidi.graphviz.engine.Graphviz) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) BeansWrapper(freemarker.ext.beans.BeansWrapper) TestFactory(org.junit.jupiter.api.TestFactory) Factory.node(guru.nidi.graphviz.model.Factory.node) LoggerFactory(org.slf4j.LoggerFactory) ConnectorTemplate(com.github.jcustenborder.kafka.connect.utils.templates.ConnectorTemplate) Reflections(org.reflections.Reflections) Schema(org.apache.kafka.connect.data.Schema) Transformation(org.apache.kafka.connect.transforms.Transformation) SourceConnector(org.apache.kafka.connect.source.SourceConnector) BeforeAll(org.junit.jupiter.api.BeforeAll) Factory.to(guru.nidi.graphviz.model.Factory.to) DynamicTest.dynamicTest(org.junit.jupiter.api.DynamicTest.dynamicTest) Map(java.util.Map) Graph(guru.nidi.graphviz.model.Graph) ConfigDef(org.apache.kafka.common.config.ConfigDef) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) Collectors(java.util.stream.Collectors) ClasspathHelper(org.reflections.util.ClasspathHelper) Test(org.junit.jupiter.api.Test) List(java.util.List) Factory.graph(guru.nidi.graphviz.model.Factory.graph) Stream(java.util.stream.Stream) Modifier(java.lang.reflect.Modifier) Writer(java.io.Writer) MarkdownTemplateHelper(com.github.jcustenborder.kafka.connect.utils.templates.markdown.MarkdownTemplateHelper) TemplateSchema(com.github.jcustenborder.kafka.connect.utils.templates.TemplateSchema) TemplateException(freemarker.template.TemplateException) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) Format(guru.nidi.graphviz.engine.Format) Files(com.google.common.io.Files) Template(freemarker.template.Template) Shape(guru.nidi.graphviz.attribute.Shape) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) TransformationTemplate(com.github.jcustenborder.kafka.connect.utils.templates.TransformationTemplate) SinkConnector(org.apache.kafka.connect.sink.SinkConnector) Charsets(com.google.common.base.Charsets) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) RankDir(guru.nidi.graphviz.attribute.RankDir) StringWriter(java.io.StringWriter) IOException(java.io.IOException) File(java.io.File) Label(guru.nidi.graphviz.model.Label) Configuration(freemarker.template.Configuration) PluginTemplate(com.github.jcustenborder.kafka.connect.utils.templates.PluginTemplate) SourceConnectorTemplate(com.github.jcustenborder.kafka.connect.utils.templates.SourceConnectorTemplate) DynamicTest(org.junit.jupiter.api.DynamicTest) RstTemplateHelper(com.github.jcustenborder.kafka.connect.utils.templates.rst.RstTemplateHelper) Comparator(java.util.Comparator) RstTemplateHelper(com.github.jcustenborder.kafka.connect.utils.templates.rst.RstTemplateHelper) Schema(org.apache.kafka.connect.data.Schema) TemplateSchema(com.github.jcustenborder.kafka.connect.utils.templates.TemplateSchema) File(java.io.File) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Writer(java.io.Writer) StringWriter(java.io.StringWriter) ConnectorTemplate(com.github.jcustenborder.kafka.connect.utils.templates.ConnectorTemplate) Template(freemarker.template.Template) TransformationTemplate(com.github.jcustenborder.kafka.connect.utils.templates.TransformationTemplate) PluginTemplate(com.github.jcustenborder.kafka.connect.utils.templates.PluginTemplate) SourceConnectorTemplate(com.github.jcustenborder.kafka.connect.utils.templates.SourceConnectorTemplate) TestFactory(org.junit.jupiter.api.TestFactory)

Example 2 with DynamicTest

use of org.junit.jupiter.api.DynamicTest in project junit5 by junit-team.

the class ScriptTests method preconditionsAreChecked.

@TestFactory
Stream<DynamicTest> preconditionsAreChecked(TestInfo info) {
    Annotation annotation = info.getTestMethod().orElseThrow(Error::new).getAnnotation(TestFactory.class);
    Class<JUnitException> expected = JUnitException.class;
    return // 
    Stream.of(// 
    dynamicTest("0", () -> assertNotNull(new Script(annotation, "e", "s", "r"))), // 
    dynamicTest("1", () -> assertThrows(expected, () -> new Script(null, "e", "s", "r"))), // 
    dynamicTest("2", () -> assertNotNull(new Script(Test.class, "a", "e", "s", "r"))), // 
    dynamicTest("3", () -> assertThrows(expected, () -> new Script(null, "a", "e", "s", "r"))), // 
    dynamicTest("4", () -> assertThrows(expected, () -> new Script(Test.class, null, "e", "s", "r"))), // 
    dynamicTest("5", () -> assertThrows(expected, () -> new Script(Test.class, "a", null, "s", "r"))), // 
    dynamicTest("6", () -> assertThrows(expected, () -> new Script(Test.class, "a", "e", null, "r"))), // 
    dynamicTest("7", () -> assertThrows(expected, () -> new Script(Test.class, "a", "e", "s", null))), // 
    dynamicTest("8", () -> assertNotNull(new Script(Test.class, "", "e", "s", ""))), // 
    dynamicTest("9", () -> assertThrows(expected, () -> new Script(Test.class, "", "", "s", ""))), // 
    dynamicTest("A", () -> assertThrows(expected, () -> new Script(Test.class, "", "e", "", ""))));
}
Also used : Test(org.junit.jupiter.api.Test) DynamicTest.dynamicTest(org.junit.jupiter.api.DynamicTest.dynamicTest) DynamicTest(org.junit.jupiter.api.DynamicTest) JUnitException(org.junit.platform.commons.JUnitException) Annotation(java.lang.annotation.Annotation) TestFactory(org.junit.jupiter.api.TestFactory)

Example 3 with DynamicTest

use of org.junit.jupiter.api.DynamicTest in project junit5 by junit-team.

the class DynamicTestsDemo method generateRandomNumberOfTests.

@TestFactory
Stream<DynamicTest> generateRandomNumberOfTests() {
    // Generates random positive integers between 0 and 100 until
    // a number evenly divisible by 7 is encountered.
    Iterator<Integer> inputGenerator = new Iterator<Integer>() {

        Random random = new Random();

        // end::user_guide[]
        {
            // Use fixed seed to always produce the same number of tests for execution on the CI server
            random = new Random(23);
        }

        // tag::user_guide[]
        int current;

        @Override
        public boolean hasNext() {
            current = random.nextInt(100);
            return current % 7 != 0;
        }

        @Override
        public Integer next() {
            return current;
        }
    };
    // Generates display names like: input:5, input:37, input:85, etc.
    Function<Integer, String> displayNameGenerator = (input) -> "input:" + input;
    // Executes tests based on the current input value.
    ThrowingConsumer<Integer> testExecutor = (input) -> assertTrue(input % 7 != 0);
    // Returns a stream of dynamic tests.
    return DynamicTest.stream(inputGenerator, displayNameGenerator, testExecutor);
}
Also used : IntStream(java.util.stream.IntStream) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) TestFactory(org.junit.jupiter.api.TestFactory) Iterator(java.util.Iterator) Collection(java.util.Collection) Random(java.util.Random) DynamicNode(org.junit.jupiter.api.DynamicNode) Function(java.util.function.Function) List(java.util.List) Stream(java.util.stream.Stream) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) DynamicTest.dynamicTest(org.junit.jupiter.api.DynamicTest.dynamicTest) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) DynamicContainer.dynamicContainer(org.junit.jupiter.api.DynamicContainer.dynamicContainer) DynamicTest(org.junit.jupiter.api.DynamicTest) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Tag(org.junit.jupiter.api.Tag) ThrowingConsumer(org.junit.jupiter.api.function.ThrowingConsumer) Random(java.util.Random) Iterator(java.util.Iterator) TestFactory(org.junit.jupiter.api.TestFactory)

Example 4 with DynamicTest

use of org.junit.jupiter.api.DynamicTest in project junit5 by junit-team.

the class TestClassWithTemplate method singleClassResolution.

@Test
void singleClassResolution() {
    ClassSelector selector = selectClass(MyTestClass.class);
    resolver.resolveSelectors(request().selectors(selector).build(), engineDescriptor);
    assertEquals(4, engineDescriptor.getDescendants().size());
    List<UniqueId> uniqueIds = uniqueIds();
    assertThat(uniqueIds).contains(uniqueIdForClass(MyTestClass.class));
    assertThat(uniqueIds).contains(uniqueIdForMethod(MyTestClass.class, "test1()"));
    assertThat(uniqueIds).contains(uniqueIdForMethod(MyTestClass.class, "test2()"));
    assertThat(uniqueIds).contains(uniqueIdForTestFactoryMethod(MyTestClass.class, "dynamicTest()"));
}
Also used : DiscoverySelectors.selectUniqueId(org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId) UniqueId(org.junit.platform.engine.UniqueId) ClassSelector(org.junit.platform.engine.discovery.ClassSelector) Test(org.junit.jupiter.api.Test) DynamicTest(org.junit.jupiter.api.DynamicTest)

Example 5 with DynamicTest

use of org.junit.jupiter.api.DynamicTest in project junit5 by junit-team.

the class TestClassWithTemplate method twoClassesResolution.

@Test
void twoClassesResolution() {
    ClassSelector selector1 = selectClass(MyTestClass.class);
    ClassSelector selector2 = selectClass(YourTestClass.class);
    resolver.resolveSelectors(request().selectors(selector1, selector2).build(), engineDescriptor);
    assertEquals(7, engineDescriptor.getDescendants().size());
    List<UniqueId> uniqueIds = uniqueIds();
    assertThat(uniqueIds).contains(uniqueIdForClass(MyTestClass.class));
    assertThat(uniqueIds).contains(uniqueIdForMethod(MyTestClass.class, "test1()"));
    assertThat(uniqueIds).contains(uniqueIdForMethod(MyTestClass.class, "test2()"));
    assertThat(uniqueIds).contains(uniqueIdForTestFactoryMethod(MyTestClass.class, "dynamicTest()"));
    assertThat(uniqueIds).contains(uniqueIdForClass(YourTestClass.class));
    assertThat(uniqueIds).contains(uniqueIdForMethod(YourTestClass.class, "test3()"));
    assertThat(uniqueIds).contains(uniqueIdForMethod(YourTestClass.class, "test4()"));
}
Also used : DiscoverySelectors.selectUniqueId(org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId) UniqueId(org.junit.platform.engine.UniqueId) ClassSelector(org.junit.platform.engine.discovery.ClassSelector) Test(org.junit.jupiter.api.Test) DynamicTest(org.junit.jupiter.api.DynamicTest)

Aggregations

DynamicTest (org.junit.jupiter.api.DynamicTest)25 Test (org.junit.jupiter.api.Test)16 TestFactory (org.junit.jupiter.api.TestFactory)14 Stream (java.util.stream.Stream)10 RecommendedLoan (com.github.robozonky.api.strategies.RecommendedLoan)7 DynamicTest.dynamicTest (org.junit.jupiter.api.DynamicTest.dynamicTest)7 ReturnCode (com.github.robozonky.api.ReturnCode)6 Event (com.github.robozonky.api.notifications.Event)6 EventListener (com.github.robozonky.api.notifications.EventListener)6 EventListenerSupplier (com.github.robozonky.api.notifications.EventListenerSupplier)6 ExecutionCompletedEvent (com.github.robozonky.api.notifications.ExecutionCompletedEvent)6 InvestmentDelegatedEvent (com.github.robozonky.api.notifications.InvestmentDelegatedEvent)6 InvestmentMadeEvent (com.github.robozonky.api.notifications.InvestmentMadeEvent)6 InvestmentPurchasedEvent (com.github.robozonky.api.notifications.InvestmentPurchasedEvent)6 InvestmentRejectedEvent (com.github.robozonky.api.notifications.InvestmentRejectedEvent)6 PurchasingCompletedEvent (com.github.robozonky.api.notifications.PurchasingCompletedEvent)6 PurchasingStartedEvent (com.github.robozonky.api.notifications.PurchasingStartedEvent)6 RoboZonkyEndingEvent (com.github.robozonky.api.notifications.RoboZonkyEndingEvent)6 SaleOfferedEvent (com.github.robozonky.api.notifications.SaleOfferedEvent)6 SellingCompletedEvent (com.github.robozonky.api.notifications.SellingCompletedEvent)6