Skip to content

Commit

Permalink
#336: Add provides tags for ServiceLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Aug 28, 2022
1 parent b74acee commit c1f00de
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 22 deletions.
7 changes: 5 additions & 2 deletions exporter/specobject/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/**
* This provides an exporter for the SpecObject XML format.
*
* @provides org.itsallcode.openfasttrace.api.exporter.ExporterFactory
*/
module org.itsallcode.openfasttrace.exporter.specobject
{
exports org.itsallcode.openfasttrace.exporter.specobject;

requires java.logging;
requires java.xml;
requires org.itsallcode.openfasttrace.api;
requires org.itsallcode.openfasttrace.exporter.common;

provides org.itsallcode.openfasttrace.api.exporter.ExporterFactory
with org.itsallcode.openfasttrace.exporter.specobject.SpecobjectExporterFactory;
}
7 changes: 5 additions & 2 deletions importer/markdown/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/**
* This provides an importer for the MarkDown format.
*
* @provides org.itsallcode.openfasttrace.api.importer.ImporterFactory
*/
module org.itsallcode.openfasttrace.importer.markdown
{
exports org.itsallcode.openfasttrace.importer.markdown;

requires java.logging;
requires transitive org.itsallcode.openfasttrace.api;

provides org.itsallcode.openfasttrace.api.importer.ImporterFactory
with org.itsallcode.openfasttrace.importer.markdown.MarkdownImporterFactory;
}
11 changes: 5 additions & 6 deletions importer/specobject/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
/**
* This provides an importer for the SpecObject XML format.
*
* @provides org.itsallcode.openfasttrace.api.importer.ImporterFactory
*/
module org.itsallcode.openfasttrace.importer.specobject
{
exports org.itsallcode.openfasttrace.importer.specobject;
exports org.itsallcode.openfasttrace.importer.specobject.handler;
exports org.itsallcode.openfasttrace.importer.specobject.xml;
exports org.itsallcode.openfasttrace.importer.specobject.xml.tree;
exports org.itsallcode.openfasttrace.importer.specobject.xml.event;

requires java.logging;
requires transitive java.xml;
requires transitive org.itsallcode.openfasttrace.api;

provides org.itsallcode.openfasttrace.api.importer.ImporterFactory
with org.itsallcode.openfasttrace.importer.specobject.SpecobjectImporterFactory;
}
6 changes: 5 additions & 1 deletion importer/tag/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
/**
* This provides an importer for coverage tags.
*
* @provides org.itsallcode.openfasttrace.api.importer.ImporterFactory
*/
module org.itsallcode.openfasttrace.importer.tag
{
exports org.itsallcode.openfasttrace.importer.tag;

requires java.logging;
requires transitive org.itsallcode.openfasttrace.api;

provides org.itsallcode.openfasttrace.api.importer.ImporterFactory
with org.itsallcode.openfasttrace.importer.tag.TagImporterFactory;
}
8 changes: 5 additions & 3 deletions importer/zip/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/**
* This provides an importer for the ZIP files.
*
* @provides org.itsallcode.openfasttrace.api.importer.ImporterFactory
*/
module org.itsallcode.openfasttrace.importer.zip
{
exports org.itsallcode.openfasttrace.importer.zip;
exports org.itsallcode.openfasttrace.importer.zip.input;

requires transitive org.itsallcode.openfasttrace.api;

provides org.itsallcode.openfasttrace.api.importer.ImporterFactory
with org.itsallcode.openfasttrace.importer.zip.ZipFileImporterFactory;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package org.itsallcode.openfasttrace;

import static java.util.stream.Collectors.toList;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.withSettings;

import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.stream.Stream;

import org.itsallcode.openfasttrace.api.core.Newline;
import org.itsallcode.openfasttrace.api.core.Trace;
import org.itsallcode.openfasttrace.api.exporter.Exporter;
import org.itsallcode.openfasttrace.api.exporter.ExporterContext;
import org.itsallcode.openfasttrace.api.exporter.ExporterFactory;
import org.itsallcode.openfasttrace.api.importer.ImportEventListener;
import org.itsallcode.openfasttrace.api.importer.Importer;
import org.itsallcode.openfasttrace.api.importer.ImporterContext;
import org.itsallcode.openfasttrace.api.importer.ImporterFactory;
import org.itsallcode.openfasttrace.api.importer.input.InputFile;
import org.itsallcode.openfasttrace.api.importer.input.RealFileInput;
import org.itsallcode.openfasttrace.api.report.Reportable;
import org.itsallcode.openfasttrace.api.report.ReporterContext;
import org.itsallcode.openfasttrace.api.report.ReporterFactory;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.Answers;

class TestAllServicesAvailable
{
private static List<ImporterFactory> importerFactories;
private static List<ExporterFactory> exporterFactories;
private static List<ReporterFactory> reporterFactories;

@BeforeAll
static void loadFactories()
{
importerFactories = loadImporterFactories();
exporterFactories = loadExporterFactories();
reporterFactories = loadReporterFactories();
}

private static List<ImporterFactory> loadImporterFactories()
{
final ServiceLoader<ImporterFactory> loader = ServiceLoader.load(ImporterFactory.class);
final ImporterContext contextMock = mock(ImporterContext.class,
withSettings().defaultAnswer(Answers.RETURNS_DEEP_STUBS));
final List<ImporterFactory> importerFactories = loader.stream() //
.map(ServiceLoader.Provider::get).collect(toList());
importerFactories.forEach(factory -> factory.init(contextMock));
return importerFactories;
}

private static List<ExporterFactory> loadExporterFactories()
{
final ServiceLoader<ExporterFactory> loader = ServiceLoader.load(ExporterFactory.class);
final ExporterContext contextMock = mock(ExporterContext.class,
withSettings().defaultAnswer(Answers.RETURNS_DEEP_STUBS));
final List<ExporterFactory> exporterFactories = loader.stream() //
.map(ServiceLoader.Provider::get).collect(toList());
exporterFactories.forEach(factory -> factory.init(contextMock));
return exporterFactories;
}

private static List<ReporterFactory> loadReporterFactories()
{
final ServiceLoader<ReporterFactory> loader = ServiceLoader.load(ReporterFactory.class);
final ReporterContext contextMock = mock(ReporterContext.class,
withSettings().defaultAnswer(Answers.RETURNS_DEEP_STUBS));
final List<ReporterFactory> reporterFactories = loader.stream() //
.map(ServiceLoader.Provider::get).collect(toList());
reporterFactories.forEach(factory -> factory.init(contextMock));
return reporterFactories;
}

@ParameterizedTest
@CsvSource(
{ "md", "oreqm", "java", "zip" })
void importerAvailable(final String suffix)
{
final InputFile file = RealFileInput.forPath(Paths.get("file." + suffix));
final Optional<ImporterFactory> factory = importerFactories.stream() //
.filter(f -> f.supportsFile(file)) //
.findAny();
if (factory.isEmpty())
{
fail("No importer found for file name suffix '" + suffix + "'");
}
final Importer importer = factory.get().createImporter(file, mock(ImportEventListener.class));
assertNotNull(importer);
}

@ParameterizedTest
@CsvSource(
{ "specobject" })
void exporterAvailable(final String format)
{
final Optional<ExporterFactory> factory = exporterFactories.stream() //
.filter(f -> f.supportsFormat(format)) //
.findAny();
if (factory.isEmpty())
{
fail("No exporter found for format '" + format + "'");
}
final Exporter exporter = factory.get().createExporter(Path.of("file"), format, StandardCharsets.UTF_8,
Newline.UNIX, Stream.empty());
assertNotNull(exporter);
}

@ParameterizedTest
@CsvSource(
{ "aspec", "html", "plain" })
void reporterAvailable(final String format)
{
final Optional<ReporterFactory> factory = reporterFactories.stream() //
.filter(f -> f.supportsFormat(format)) //
.findAny();
if (factory.isEmpty())
{
fail("No reporter found for format '" + format + "'");
}
final Reportable reportable = factory.get().createImporter(Trace.builder().build());
assertNotNull(reportable);
}
}
7 changes: 5 additions & 2 deletions reporter/aspec/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/**
* This provides an report generator for the Aspec format.
*
* @provides org.itsallcode.openfasttrace.api.report.ReporterFactory
*/
module org.itsallcode.openfasttrace.report.aspec
{
exports org.itsallcode.openfasttrace.report.aspec;

requires java.xml;
requires java.logging;
requires org.itsallcode.openfasttrace.api;
requires org.itsallcode.openfasttrace.exporter.common;

provides org.itsallcode.openfasttrace.api.report.ReporterFactory
with org.itsallcode.openfasttrace.report.aspec.ASpecReporterFactory;
}
9 changes: 5 additions & 4 deletions reporter/html/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/**
* This provides an report generator for the HTML format.
*
* @provides org.itsallcode.openfasttrace.api.report.ReporterFactory
*/
module org.itsallcode.openfasttrace.report.html
{
exports org.itsallcode.openfasttrace.report.html;
exports org.itsallcode.openfasttrace.report.html.view;
exports org.itsallcode.openfasttrace.report.html.view.html;

requires transitive org.itsallcode.openfasttrace.api;

provides org.itsallcode.openfasttrace.api.report.ReporterFactory
with org.itsallcode.openfasttrace.report.html.HtmlReporterFactory;
}
7 changes: 5 additions & 2 deletions reporter/plaintext/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/**
* This provides an report generator for the plain text format.
*
* @provides org.itsallcode.openfasttrace.api.report.ReporterFactory
*/
module org.itsallcode.openfasttrace.report.plaintext
{
exports org.itsallcode.openfasttrace.report.plaintext;

requires transitive org.itsallcode.openfasttrace.api;

provides org.itsallcode.openfasttrace.api.report.ReporterFactory
with org.itsallcode.openfasttrace.report.plaintext.PlaintextReporterFactory;
}

0 comments on commit c1f00de

Please sign in to comment.