Class AbstractEntryBasedExtension<K, V, C extends Annotation, S extends Annotation, R extends 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 Annotation, S extends Annotation, R extends Annotation> extends 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.AfterAllCallback
An abstract base class for entry-based extensions, where entries (key-value pairs) can be cleared, set, or restored.
  • Field Details

    • INCREMENTAL_KEY

      private static final String INCREMENTAL_KEY
      Key to indicate storage is for an incremental backup object.
      See Also:
    • COMPLETE_KEY

      private static final String COMPLETE_KEY
      Key to indicate storage is for a complete backup object.
      See Also:
  • Constructor Details

    • AbstractEntryBasedExtension

      AbstractEntryBasedExtension()
  • Method Details

    • beforeAll

      public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      beforeAll in interface org.junit.jupiter.api.extension.BeforeAllCallback
    • beforeEach

      public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      beforeEach in interface org.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 Set<K> findEntriesToClear(AnnotatedElement element)
    • findEntriesToSet

      private Map<K,V> findEntriesToSet(AnnotatedElement element)
    • findAnnotations

      private <A extends Annotation> Stream<A> findAnnotations(AnnotatedElement element, Class<A> clazz)
    • getClearAnnotationType

      private Class<C> getClearAnnotationType()
    • getSetAnnotationType

      private Class<S> getSetAnnotationType()
    • getRestoreAnnotationType

      private Class<R> getRestoreAnnotationType()
    • getActualTypeArgumentAt

      private Type getActualTypeArgumentAt(int index)
    • preventClearAndSetSameEntries

      private void preventClearAndSetSameEntries(Collection<K> entriesToClear, Collection<K> entriesToSet)
    • storeOriginalIncrementalEntries

      private void storeOriginalIncrementalEntries(org.junit.jupiter.api.extension.ExtensionContext context, Collection<K> entriesToClear, Collection<K> entriesToSet)
    • storeOriginalCompleteEntries

      private void storeOriginalCompleteEntries(org.junit.jupiter.api.extension.ExtensionContext context, 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 this ExtensionContext, if the complete state was initially stored in a before all/each event.
      Parameters:
      context - The ExtensionContext which 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(Collection<K> entriesToClear)
    • setEntries

      private void setEntries(Map<K,V> entriesToSet)
    • afterEach

      public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
    • afterAll

      public void afterAll(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      afterAll in interface org.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 String getStoreKey(org.junit.jupiter.api.extension.ExtensionContext context, String discriminator)
    • clearKeyMapper

      protected abstract Function<C,K> clearKeyMapper()
      Returns:
      Mapper function to get the key from a clear annotation.
    • setKeyMapper

      protected abstract Function<S,K> setKeyMapper()
      Returns:
      Mapper function to get the key from a set annotation.
    • setValueMapper

      protected abstract 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.
    • getEntry

      protected abstract V getEntry(K key)
      Gets 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 Properties prepareToEnterRestorableContext()
      Prepare the entry-based environment for entering a context that must be restorable.

      Implementations may choose one of two strategies:

      The returned Properties must 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 System Properties may contain Objects.

      Returns:
      A non-null Properties that contains all entries of the entry environment.
    • prepareToExitRestorableContext

      protected abstract void prepareToExitRestorableContext(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. The Properties entries must follow the rules for entries of this environment, e.g., environment variables contain only Strings while System Properties may contain Objects.

      Parameters:
      entries - A non-null Properties that contains all entries of the entry environment.