Skip to content

Commit

Permalink
#413: Add requirements & coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Jun 22, 2024
1 parent 4850fee commit 84f357d
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import org.itsallcode.openfasttrace.core.importer.ImporterServiceImpl;
import org.itsallcode.openfasttrace.core.report.ReportService;
import org.itsallcode.openfasttrace.core.report.ReporterFactoryLoader;
import org.itsallcode.openfasttrace.core.serviceloader.InitializingServiceLoader;
import org.itsallcode.openfasttrace.core.serviceloader.Loader;

class ServiceFactory
{
Expand All @@ -26,9 +24,7 @@ ExporterService createExporterService()
ImporterService createImporterService(final ImportSettings settings)
{
final ImporterContext context = new ImporterContext(settings);
final Loader<ImporterFactory> serviceLoader = InitializingServiceLoader.load(ImporterFactory.class, context);
final ImporterService service = new ImporterServiceImpl(
new ImporterFactoryLoader(serviceLoader), settings);
final ImporterService service = new ImporterServiceImpl(new ImporterFactoryLoader(context), settings);
context.setImporterService(service);
return service;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class ExporterFactoryLoader
*/
public ExporterFactoryLoader(final ExporterContext context)
{
// [impl->dsn~plugins.loading.plugin_types~1]
this(InitializingServiceLoader.load(ExporterFactory.class, context));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import java.util.Optional;
import java.util.logging.Logger;

import org.itsallcode.openfasttrace.api.importer.ImporterException;
import org.itsallcode.openfasttrace.api.importer.ImporterFactory;
import org.itsallcode.openfasttrace.api.importer.*;
import org.itsallcode.openfasttrace.api.importer.input.InputFile;
import org.itsallcode.openfasttrace.core.serviceloader.InitializingServiceLoader;
import org.itsallcode.openfasttrace.core.serviceloader.Loader;

/**
Expand All @@ -26,11 +26,23 @@ public class ImporterFactoryLoader
* @param serviceLoader
* the loader used for locating importers.
*/
public ImporterFactoryLoader(final Loader<ImporterFactory> serviceLoader)
ImporterFactoryLoader(final Loader<ImporterFactory> serviceLoader)
{
this.serviceLoader = serviceLoader;
}

/**
* Create a new loader for the given context.
*
* @param context
* the context for the new loader.
*/
public ImporterFactoryLoader(final ImporterContext context)
{
// [impl->dsn~plugins.loading.plugin_types~1]
this(InitializingServiceLoader.load(ImporterFactory.class, context));
}

/**
* Finds a matching {@link ImporterFactory} that can handle the given
* {@link Path}. If no or more than one {@link ImporterFactory} is found,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class ReporterFactoryLoader
*/
public ReporterFactoryLoader(final ReporterContext context)
{
// [impl->dsn~plugins.loading.plugin_types~1]
this(InitializingServiceLoader.load(ReporterFactory.class, context));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class ServiceLoaderFactory
*/
static ServiceLoaderFactory createDefault()
{
return new ServiceLoaderFactory(getHomeDirectory().resolve(".oft").resolve("plugins"), true);
final Path pluginsDirectory = getHomeDirectory().resolve(".oft").resolve("plugins");
return new ServiceLoaderFactory(pluginsDirectory, true);
}

private static Path getHomeDirectory()
Expand Down Expand Up @@ -76,6 +77,7 @@ private static <T> Loader<T> createLoader(final Class<T> serviceType, final List
*
* @return a list of service origins
*/
// [impl->dsn~plugins.loading~1]
List<ServiceOrigin> findServiceOrigins()
{
final List<ServiceOrigin> origins = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/**
* Origin of a service, either the current classpath or a list of jar files.
*/
// [impl->dsn~plugins.loading~1]
interface ServiceOrigin extends AutoCloseable
{
/**
Expand Down Expand Up @@ -45,6 +46,7 @@ static ServiceOrigin forJar(final Path jar)
* list of paths to jar files
* @return a service origin for the jar files
*/
// [impl->dsn~plugins.loading.separate_classloader~1]
static ServiceOrigin forJars(final List<Path> jars)
{
return new JarFileOrigin(jars, createClassLoader(jars));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.junit.jupiter.api.io.TempDir;
import org.opentest4j.MultipleFailuresError;

// [utest->dsn~plugins.loading~1]
class ServiceLoaderFactoryTest
{
@TempDir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

// [utest->dsn~plugins.loading~1]
@ExtendWith(MockitoExtension.class)
class ServiceOriginTest
{
Expand All @@ -34,6 +35,7 @@ void currentClassPathOriginToString()
assertThat(origin, hasToString(startsWith("CurrentClassPathOrigin [classLoader=")));
}

// [utest->dsn~plugins.loading.separate_classloader~1]
@Test
void currentClassPathGetClassLoader()
{
Expand Down
6 changes: 3 additions & 3 deletions doc/spec/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Separating plugins from each other avoids conflicts between potentially duplicat
Covers:
* [`req~plugins.loading~1`](system_requirements.md#loading-plugins)

Needs: impl, utest, itest
Needs: impl, utest

### Loader Supports Plugin Types
`dsn~plugins.loading.plugin_types~1`
Expand All @@ -165,7 +165,7 @@ The Plugin loader supports loading factories the following plugin types:
Covers:
* [`req~plugins.types~1`](system_requirements.md#supported-plugin-types)

Needs: impl, utest, itest
Needs: impl, itest

## Import

Expand Down Expand Up @@ -1031,7 +1031,7 @@ The CLI allows listing available plugins in OFT.
Covers:
- [req~plugins.list~1](system_requirements.md#listing-available-plugins)

Needs: impl, utest, itest
#Needs: impl, utest, itest

# Design Decisions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* must be located in module {@code product} (which includes all plugin modules)
* so that it can access all plugin services.
*/
// [itest->dsn~plugins.loading~1]
class ServiceLoaderFactoryIT
{
@TempDir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
import org.itsallcode.openfasttrace.api.exporter.ExporterFactory;
import org.itsallcode.openfasttrace.api.importer.ImporterContext;
import org.itsallcode.openfasttrace.api.importer.ImporterFactory;
import org.itsallcode.openfasttrace.api.report.ReporterContext;
import org.itsallcode.openfasttrace.api.report.ReporterFactory;
import org.itsallcode.openfasttrace.exporter.specobject.SpecobjectExporterFactory;
import org.itsallcode.openfasttrace.importer.markdown.MarkdownImporterFactory;
import org.itsallcode.openfasttrace.importer.restructuredtext.RestructuredTextImporterFactory;
import org.itsallcode.openfasttrace.importer.specobject.SpecobjectImporterFactory;
import org.itsallcode.openfasttrace.importer.tag.TagImporterFactory;
import org.itsallcode.openfasttrace.importer.zip.ZipFileImporterFactory;
import org.itsallcode.openfasttrace.report.aspec.ASpecReporterFactory;
import org.itsallcode.openfasttrace.report.html.HtmlReporterFactory;
import org.itsallcode.openfasttrace.report.plaintext.PlaintextReporterFactory;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -36,6 +41,7 @@ void testNoServicesRegistered()
assertThat(voidServiceLoader.load().toList(), emptyIterable());
}

// [itest->dsn~plugins.loading.plugin_types~1]
@Test
void testImporterFactoriesRegistered()
{
Expand All @@ -55,20 +61,38 @@ void testImporterFactoriesRegistered()
}
}

// [itest->dsn~plugins.loading.plugin_types~1]
@Test
void testExporterFactoriesRegistered()
{
final ExporterContext context = new ExporterContext();
final List<ExporterFactory> services = getRegisteredServices(ExporterFactory.class,
context);
assertThat(services, hasSize(1));
assertThat(services, contains(instanceOf(SpecobjectExporterFactory.class)));
assertThat(services, containsInAnyOrder(instanceOf(SpecobjectExporterFactory.class)));
for (final ExporterFactory factory : services)
{
assertThat(factory.getContext(), sameInstance(context));
}
}

// [itest->dsn~plugins.loading.plugin_types~1]
@Test
void testReporterFactoriesRegistered()
{
final ReporterContext context = new ReporterContext(null);
final List<ReporterFactory> services = getRegisteredServices(ReporterFactory.class,
context);
assertThat(services, hasSize(3));
assertThat(services, containsInAnyOrder(instanceOf(PlaintextReporterFactory.class),
instanceOf(ASpecReporterFactory.class),
instanceOf(HtmlReporterFactory.class)));
for (final ReporterFactory factory : services)
{
assertThat(factory.getContext(), sameInstance(context));
}
}

private <T extends Initializable<C>, C> List<T> getRegisteredServices(final Class<T> type,
final C context)
{
Expand Down

0 comments on commit 84f357d

Please sign in to comment.