Module org.junitpioneer
Package org.junitpioneer.jupiter
Class AbstractEntryBasedExtension<K,V,C extends java.lang.annotation.Annotation,S extends java.lang.annotation.Annotation,R extends java.lang.annotation.Annotation>
- java.lang.Object
-
- org.junitpioneer.jupiter.AbstractEntryBasedExtension<K,V,C,S,R>
-
- Type Parameters:
K- The entry key type.V- The entry value type.C- The clear annotation type.S- The set annotation type.R- The restore annotation type.
- 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
- Direct Known Subclasses:
EnvironmentVariableExtension,SystemPropertyExtension
abstract class AbstractEntryBasedExtension<K,V,C extends java.lang.annotation.Annotation,S extends java.lang.annotation.Annotation,R extends java.lang.annotation.Annotation> extends java.lang.Object implements org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.AfterAllCallbackAn abstract base class for entry-based extensions, where entries (key-value pairs) can be cleared, set, or restored.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classAbstractEntryBasedExtension.EntriesBackup
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringCOMPLETE_KEYKey to indicate storage is for a complete backup object.private static java.lang.StringINCREMENTAL_KEYKey to indicate storage is for an incremental backup object.
-
Constructor Summary
Constructors Constructor Description AbstractEntryBasedExtension()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidafterAll(org.junit.jupiter.api.extension.ExtensionContext context)voidafterEach(org.junit.jupiter.api.extension.ExtensionContext context)private voidapplyForAllContexts(org.junit.jupiter.api.extension.ExtensionContext originalContext)voidbeforeAll(org.junit.jupiter.api.extension.ExtensionContext context)voidbeforeEach(org.junit.jupiter.api.extension.ExtensionContext context)private voidclearAndSetEntries(org.junit.jupiter.api.extension.ExtensionContext currentContext, org.junit.jupiter.api.extension.ExtensionContext originalContext, boolean doIncrementalBackup)private voidclearEntries(java.util.Collection<K> entriesToClear)protected abstract voidclearEntry(K key)Removes the entry indicated by the specified key.protected abstract java.util.function.Function<C,K>clearKeyMapper()private <A extends java.lang.annotation.Annotation>
java.util.stream.Stream<A>findAnnotations(java.lang.reflect.AnnotatedElement element, java.lang.Class<A> clazz)private java.util.Set<K>findEntriesToClear(java.lang.reflect.AnnotatedElement element)private java.util.Map<K,V>findEntriesToSet(java.lang.reflect.AnnotatedElement element)private java.lang.reflect.TypegetActualTypeArgumentAt(int index)private java.lang.Class<C>getClearAnnotationType()protected abstract VgetEntry(K key)Gets the entry indicated by the specified key.private java.lang.Class<R>getRestoreAnnotationType()private java.lang.Class<S>getSetAnnotationType()private org.junit.jupiter.api.extension.ExtensionContext.StoregetStore(org.junit.jupiter.api.extension.ExtensionContext context)private java.lang.StringgetStoreKey(org.junit.jupiter.api.extension.ExtensionContext context, java.lang.String discriminator)protected abstract java.util.PropertiesprepareToEnterRestorableContext()Prepare the entry-based environment for entering a context that must be restorable.protected abstract voidprepareToExitRestorableContext(java.util.Properties entries)Prepare to exit a restorable context for the entry based environment.private voidpreventClearAndSetSameEntries(java.util.Collection<K> entriesToClear, java.util.Collection<K> entriesToSet)protected voidreportWarning(org.junit.jupiter.api.extension.ExtensionContext context)Reports a warning about potentially unsafe practices.private voidrestoreForAllContexts(org.junit.jupiter.api.extension.ExtensionContext originalContext)private booleanrestoreOriginalCompleteEntries(org.junit.jupiter.api.extension.ExtensionContext context)Restore the complete original state of the entries as they were prior to thisExtensionContext, if the complete state was initially stored in a before all/each event.private voidrestoreOriginalIncrementalEntries(org.junit.jupiter.api.extension.ExtensionContext originalContext)private voidsetEntries(java.util.Map<K,V> entriesToSet)protected abstract voidsetEntry(K key, V value)Sets the entry indicated by the specified key.protected abstract java.util.function.Function<S,K>setKeyMapper()protected abstract java.util.function.Function<S,V>setValueMapper()private voidstoreOriginalCompleteEntries(org.junit.jupiter.api.extension.ExtensionContext context, java.util.Properties originalEntries)private voidstoreOriginalIncrementalEntries(org.junit.jupiter.api.extension.ExtensionContext context, java.util.Collection<K> entriesToClear, java.util.Collection<K> entriesToSet)
-
-
-
Field Detail
-
INCREMENTAL_KEY
private static final java.lang.String INCREMENTAL_KEY
Key to indicate storage is for an incremental backup object.- See Also:
- Constant Field Values
-
COMPLETE_KEY
private static final java.lang.String COMPLETE_KEY
Key to indicate storage is for a complete backup object.- See Also:
- Constant Field Values
-
-
Method Detail
-
beforeAll
public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context)
- Specified by:
beforeAllin interfaceorg.junit.jupiter.api.extension.BeforeAllCallback
-
beforeEach
public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
- Specified by:
beforeEachin interfaceorg.junit.jupiter.api.extension.BeforeEachCallback
-
applyForAllContexts
private void applyForAllContexts(org.junit.jupiter.api.extension.ExtensionContext originalContext)
-
clearAndSetEntries
private void clearAndSetEntries(org.junit.jupiter.api.extension.ExtensionContext currentContext, org.junit.jupiter.api.extension.ExtensionContext originalContext, boolean doIncrementalBackup)
-
findEntriesToClear
private java.util.Set<K> findEntriesToClear(java.lang.reflect.AnnotatedElement element)
-
findEntriesToSet
private java.util.Map<K,V> findEntriesToSet(java.lang.reflect.AnnotatedElement element)
-
findAnnotations
private <A extends java.lang.annotation.Annotation> java.util.stream.Stream<A> findAnnotations(java.lang.reflect.AnnotatedElement element, java.lang.Class<A> clazz)
-
getClearAnnotationType
private java.lang.Class<C> getClearAnnotationType()
-
getSetAnnotationType
private java.lang.Class<S> getSetAnnotationType()
-
getRestoreAnnotationType
private java.lang.Class<R> getRestoreAnnotationType()
-
getActualTypeArgumentAt
private java.lang.reflect.Type getActualTypeArgumentAt(int index)
-
preventClearAndSetSameEntries
private void preventClearAndSetSameEntries(java.util.Collection<K> entriesToClear, java.util.Collection<K> entriesToSet)
-
storeOriginalIncrementalEntries
private void storeOriginalIncrementalEntries(org.junit.jupiter.api.extension.ExtensionContext context, java.util.Collection<K> entriesToClear, java.util.Collection<K> entriesToSet)
-
storeOriginalCompleteEntries
private void storeOriginalCompleteEntries(org.junit.jupiter.api.extension.ExtensionContext context, java.util.Properties originalEntries)
-
restoreOriginalCompleteEntries
private boolean restoreOriginalCompleteEntries(org.junit.jupiter.api.extension.ExtensionContext context)
Restore the complete original state of the entries as they were prior to thisExtensionContext, if the complete state was initially stored in a before all/each event.- Parameters:
context- TheExtensionContextwhich may have a bulk backup stored.- Returns:
- true if a complete backup exists and was used to restore, false if not.
-
clearEntries
private void clearEntries(java.util.Collection<K> entriesToClear)
-
afterEach
public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context)
- Specified by:
afterEachin interfaceorg.junit.jupiter.api.extension.AfterEachCallback
-
afterAll
public void afterAll(org.junit.jupiter.api.extension.ExtensionContext context)
- Specified by:
afterAllin interfaceorg.junit.jupiter.api.extension.AfterAllCallback
-
restoreForAllContexts
private void restoreForAllContexts(org.junit.jupiter.api.extension.ExtensionContext originalContext)
-
restoreOriginalIncrementalEntries
private void restoreOriginalIncrementalEntries(org.junit.jupiter.api.extension.ExtensionContext originalContext)
-
getStore
private org.junit.jupiter.api.extension.ExtensionContext.Store getStore(org.junit.jupiter.api.extension.ExtensionContext context)
-
getStoreKey
private java.lang.String getStoreKey(org.junit.jupiter.api.extension.ExtensionContext context, java.lang.String discriminator)
-
clearKeyMapper
protected abstract java.util.function.Function<C,K> clearKeyMapper()
- Returns:
- Mapper function to get the key from a clear annotation.
-
setKeyMapper
protected abstract java.util.function.Function<S,K> setKeyMapper()
- Returns:
- Mapper function to get the key from a set annotation.
-
setValueMapper
protected abstract java.util.function.Function<S,V> setValueMapper()
- Returns:
- Mapper function to get the value from a set annotation.
-
clearEntry
protected abstract void clearEntry(K key)
Removes the entry indicated by the specified key.
-
setEntry
protected abstract void setEntry(K key, V value)
Sets the entry indicated by the specified key.
-
reportWarning
protected void reportWarning(org.junit.jupiter.api.extension.ExtensionContext context)
Reports a warning about potentially unsafe practices.
-
prepareToEnterRestorableContext
protected abstract java.util.Properties prepareToEnterRestorableContext()
Prepare the entry-based environment for entering a context that must be restorable.Implementations may choose one of two strategies:
- Post swap, where the original entry-based environment is left in place and a clone is returned.
In this case
prepareToExitRestorableContext(java.util.Properties)will restore the clone. - Preemptive swap, where the current entry-based environment is replaced with a clone and the
original is returned.
In this case the
prepareToExitRestorableContext(java.util.Properties)will restore the original environment.
The returned
Propertiesmust not be null and its key-value pairs must follow the rules for entries of its type. E.g., environment variables contain only Strings while SystemPropertiesmay contain Objects.- Returns:
- A non-null
Propertiesthat contains all entries of the entry environment.
- Post swap, where the original entry-based environment is left in place and a clone is returned.
In this case
-
prepareToExitRestorableContext
protected abstract void prepareToExitRestorableContext(java.util.Properties entries)
Prepare to exit a restorable context for the entry based environment.The entry environment will be restored to the state passed in as
Properties. ThePropertiesentries must follow the rules for entries of this environment, e.g., environment variables contain only Strings while SystemPropertiesmay contain Objects.- Parameters:
entries- A non-nullPropertiesthat contains all entries of the entry environment.
-
-