Class JdutYamlFactory
- java.lang.Object
-
- guru.mikelue.jdut.junit5.JdutYamlFactory
-
- All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterAllCallback,org.junit.jupiter.api.extension.AfterEachCallback,org.junit.jupiter.api.extension.BeforeAllCallback,org.junit.jupiter.api.extension.BeforeEachCallback,org.junit.jupiter.api.extension.Extension
public abstract class JdutYamlFactory extends Object implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback
This factory sets up loading convention of YAML file, in order to prepare and to clean up data for testing.This class can be used in
ExtendWithor JUnit 5 extension mechanism. And writing a class inheriting this one to apply the conventions of loading YAML file.To use this class, you should implement
getYamlConductorFactory(org.junit.jupiter.api.extension.ExtensionContext, guru.mikelue.jdut.junit5.JdutYamlFactory.Event)method to set-upDataSourceand related affairs of your test data.By
Example of your implementation:BeforeEachCallbackandAfterAllCallback, a testing class loads and cleans up data while aJdutResourceis applied on it. ByBeforeEachCallbackandAfterEachCallback, a testing method loads and cleans up data while aJdutResourceis applied on it.
Usage on your testing class:public class YourFactory extends JdutYamlFactory { @Override protected YamlConductorFactory getYamlConductorFactory(ExtensionContext context, Event event) { // Your implementation return null; } }package guru.mikelue.nova; // Loading: classpath:guru/mikelue/nova/Hydrogen.yaml @JdutResource @ExtendWith(YourFactory.class) public class Hydrogen { // Loading: classpath:guru/mikelue/nova/Hydrogen-explode.yaml @Test @JdutResource void explode() { } }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classJdutYamlFactory.EventDefins the class-level or method-level event of loading test data.
-
Constructor Summary
Constructors Modifier Constructor Description protectedJdutYamlFactory()This class can only be used by implementation of subclass.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidafterAll(org.junit.jupiter.api.extension.ExtensionContext context)Checks if anyDuetConductorinExtensionContext.Store.voidafterEach(org.junit.jupiter.api.extension.ExtensionContext context)Checks if anyDuetConductorinExtensionContext.Store.voidbeforeAll(org.junit.jupiter.api.extension.ExtensionContext context)Checks ifJdutResourceis on testing class.voidbeforeEach(org.junit.jupiter.api.extension.ExtensionContext context)Checks ifJdutResourceis on testing method.static JdutYamlFactorybuildByDataSource(Supplier<DataSource> supplier)Builds a new object bySupplierofDataSource.static JdutYamlFactorybuildByFactory(Supplier<YamlConductorFactory> supplier)Builds a new object bySupplierofYamlConductorFactory.protected DuetConductorbuildDuetConductor(org.junit.jupiter.api.extension.ExtensionContext context, JdutYamlFactory.Event event)Builds theDuetConductorby default convention(defaultBuilderOfConductorConfig(org.junit.jupiter.api.extension.ExtensionContext)).static Consumer<ConductorConfig.Builder>defaultBuilderOfConductorConfig(org.junit.jupiter.api.extension.ExtensionContext context)Constructs a builder forConductorConfigby default convention for resource loading(from testing class).protected org.slf4j.LoggergetLogger()The retrievedLoggerhas the name of implementing class.protected abstract YamlConductorFactorygetYamlConductorFactory(org.junit.jupiter.api.extension.ExtensionContext context, JdutYamlFactory.Event event)protected booleanneedConductData(org.junit.jupiter.api.extension.ExtensionContext context, JdutYamlFactory.Event event)Checks if the test should conduct data.
-
-
-
Method Detail
-
buildByFactory
public static JdutYamlFactory buildByFactory(Supplier<YamlConductorFactory> supplier)
Builds a new object bySupplierofYamlConductorFactory.- Parameters:
supplier- The functional interface of supplyingYamlConductorFactory- Returns:
- new object
-
buildByDataSource
public static JdutYamlFactory buildByDataSource(Supplier<DataSource> supplier)
Builds a new object bySupplierofDataSource.- Parameters:
supplier- The functional interface of supplyingDataSource- Returns:
- new object
-
defaultBuilderOfConductorConfig
public static Consumer<ConductorConfig.Builder> defaultBuilderOfConductorConfig(org.junit.jupiter.api.extension.ExtensionContext context)
Constructs a builder forConductorConfigby default convention for resource loading(from testing class).- Parameters:
context- The context provided by JUnit 5- Returns:
- new builder
-
beforeAll
public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context) throws ExceptionChecks ifJdutResourceis on testing class. If it is, loading the YAML file from classpaht:{package}/{classname}.yaml. For example:package guru.mikelue.cassia; // Loading: classpath:guru/mikelue/cassia/Verum.yaml @JdutResource public class Verum { }This callback would put the
DuetConductorin class-ExtensionContext.NamespaceExtensionContext.Store. TheDuetConductoris constructed fromgetYamlConductorFactory(org.junit.jupiter.api.extension.ExtensionContext, guru.mikelue.jdut.junit5.JdutYamlFactory.Event).- Specified by:
beforeAllin interfaceorg.junit.jupiter.api.extension.BeforeAllCallback- Parameters:
context- The context provided by JUnit 5- Throws:
Exception- SeeBeforeAllCallback.beforeAll(org.junit.jupiter.api.extension.ExtensionContext)
-
afterAll
public void afterAll(org.junit.jupiter.api.extension.ExtensionContext context) throws ExceptionChecks if anyDuetConductorinExtensionContext.Store. If it is, use the conductor to clean up data. For example:package guru.mikelue.cassia; // Loading: classpath:guru/mikelue/cassia/Verum.yaml @JdutResource public class Verum { }This callback would remove the
DuetConductorin class-ExtensionContext.NamespaceExtensionContext.Store.- Specified by:
afterAllin interfaceorg.junit.jupiter.api.extension.AfterAllCallback- Parameters:
context- The context provided by JUnit 5- Throws:
Exception- SeeBeforeAllCallback.beforeAll(org.junit.jupiter.api.extension.ExtensionContext)
-
beforeEach
public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) throws ExceptionChecks ifJdutResourceis on testing method. If it is, loading the YAML file from classpaht:{package}/{classname}-{methodname}.yaml. For example:package guru.mikelue.cassia; public class Verum { // Loading: classpath:guru/mikelue/cassia/Verum-grind.yaml @Test @JdutResource void grind() { } }This callback would put the
DuetConductorin method-ExtensionContext.NamespaceExtensionContext.Store. TheDuetConductoris constructed fromgetYamlConductorFactory(org.junit.jupiter.api.extension.ExtensionContext, guru.mikelue.jdut.junit5.JdutYamlFactory.Event).- Specified by:
beforeEachin interfaceorg.junit.jupiter.api.extension.BeforeEachCallback- Parameters:
context- The context provided by JUnit 5- Throws:
Exception- SeeBeforeAllCallback.beforeAll(org.junit.jupiter.api.extension.ExtensionContext)
-
afterEach
public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context) throws ExceptionChecks if anyDuetConductorinExtensionContext.Store. If it is, use the conductor to clean up data. For example:package guru.mikelue.cassia; public class Verum { // Loading: classpath:guru/mikelue/cassia/Verum-grind.yaml @Test @JdutResource void grind() { } }This callback would remove the
DuetConductorin method-ExtensionContext.NamespaceExtensionContext.Store.- Specified by:
afterEachin interfaceorg.junit.jupiter.api.extension.AfterEachCallback- Parameters:
context- The context provided by JUnit 5- Throws:
Exception- SeeBeforeAllCallback.beforeAll(org.junit.jupiter.api.extension.ExtensionContext)
-
getYamlConductorFactory
protected abstract YamlConductorFactory getYamlConductorFactory(org.junit.jupiter.api.extension.ExtensionContext context, JdutYamlFactory.Event event)
-
getLogger
protected final org.slf4j.Logger getLogger()
The retrievedLoggerhas the name of implementing class.- Returns:
- A logger having the name of implementing class.
-
needConductData
protected boolean needConductData(org.junit.jupiter.api.extension.ExtensionContext context, JdutYamlFactory.Event event)Checks if the test should conduct data. You can override this method to control whether or not to conduct data for testing.- Parameters:
context- The context provided by JUnit 5event- The event for scope of current triggered.- Returns:
- true if the conduction should be executed
-
buildDuetConductor
protected DuetConductor buildDuetConductor(org.junit.jupiter.api.extension.ExtensionContext context, JdutYamlFactory.Event event)
Builds theDuetConductorby default convention(defaultBuilderOfConductorConfig(org.junit.jupiter.api.extension.ExtensionContext)). You can override this method to customize your own arsenal of test data.- Parameters:
context- The context provided by JUnit 5event- The event for scope of current triggered.- Returns:
- The initialized conductor for testing
-
-