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
ExtendWith
or 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-upDataSource
and related affairs of your test data.By
Example of your implementation:BeforeEachCallback
andAfterAllCallback
, a testing class loads and cleans up data while aJdutResource
is applied on it. ByBeforeEachCallback
andAfterEachCallback
, a testing method loads and cleans up data while aJdutResource
is 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 class
JdutYamlFactory.Event
Defins the class-level or method-level event of loading test data.
-
Constructor Summary
Constructors Modifier Constructor Description protected
JdutYamlFactory()
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 void
afterAll(org.junit.jupiter.api.extension.ExtensionContext context)
Checks if anyDuetConductor
inExtensionContext.Store
.void
afterEach(org.junit.jupiter.api.extension.ExtensionContext context)
Checks if anyDuetConductor
inExtensionContext.Store
.void
beforeAll(org.junit.jupiter.api.extension.ExtensionContext context)
Checks ifJdutResource
is on testing class.void
beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
Checks ifJdutResource
is on testing method.static JdutYamlFactory
buildByDataSource(Supplier<DataSource> supplier)
Builds a new object bySupplier
ofDataSource
.static JdutYamlFactory
buildByFactory(Supplier<YamlConductorFactory> supplier)
Builds a new object bySupplier
ofYamlConductorFactory
.protected DuetConductor
buildDuetConductor(org.junit.jupiter.api.extension.ExtensionContext context, JdutYamlFactory.Event event)
Builds theDuetConductor
by default convention(defaultBuilderOfConductorConfig(org.junit.jupiter.api.extension.ExtensionContext)
).static Consumer<ConductorConfig.Builder>
defaultBuilderOfConductorConfig(org.junit.jupiter.api.extension.ExtensionContext context)
Constructs a builder forConductorConfig
by default convention for resource loading(from testing class).protected org.slf4j.Logger
getLogger()
The retrievedLogger
has the name of implementing class.protected abstract YamlConductorFactory
getYamlConductorFactory(org.junit.jupiter.api.extension.ExtensionContext context, JdutYamlFactory.Event event)
protected boolean
needConductData(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 bySupplier
ofYamlConductorFactory
.- Parameters:
supplier
- The functional interface of supplyingYamlConductorFactory
- Returns:
- new object
-
buildByDataSource
public static JdutYamlFactory buildByDataSource(Supplier<DataSource> supplier)
Builds a new object bySupplier
ofDataSource
.- 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 forConductorConfig
by 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 Exception
Checks ifJdutResource
is 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
DuetConductor
in class-ExtensionContext.Namespace
ExtensionContext.Store
. TheDuetConductor
is constructed fromgetYamlConductorFactory(org.junit.jupiter.api.extension.ExtensionContext, guru.mikelue.jdut.junit5.JdutYamlFactory.Event)
.- Specified by:
beforeAll
in 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 Exception
Checks if anyDuetConductor
inExtensionContext.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
DuetConductor
in class-ExtensionContext.Namespace
ExtensionContext.Store
.- Specified by:
afterAll
in 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 Exception
Checks ifJdutResource
is 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
DuetConductor
in method-ExtensionContext.Namespace
ExtensionContext.Store
. TheDuetConductor
is constructed fromgetYamlConductorFactory(org.junit.jupiter.api.extension.ExtensionContext, guru.mikelue.jdut.junit5.JdutYamlFactory.Event)
.- Specified by:
beforeEach
in 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 Exception
Checks if anyDuetConductor
inExtensionContext.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
DuetConductor
in method-ExtensionContext.Namespace
ExtensionContext.Store
.- Specified by:
afterEach
in 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 retrievedLogger
has 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 theDuetConductor
by 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
-
-