Class AbstractEntryBasedExtension<K,​V,​C extends java.lang.annotation.Annotation,​S extends java.lang.annotation.Annotation,​R extends java.lang.annotation.Annotation>

  • 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.AfterAllCallback
    An abstract base class for entry-based extensions, where entries (key-value pairs) can be cleared, set, or restored.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String COMPLETE_KEY
      Key to indicate storage is for a complete backup object.
      private static java.lang.String INCREMENTAL_KEY
      Key to indicate storage is for an incremental backup object.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void afterAll​(org.junit.jupiter.api.extension.ExtensionContext context)  
      void afterEach​(org.junit.jupiter.api.extension.ExtensionContext context)  
      private void applyForAllContexts​(org.junit.jupiter.api.extension.ExtensionContext originalContext)  
      void beforeAll​(org.junit.jupiter.api.extension.ExtensionContext context)  
      void beforeEach​(org.junit.jupiter.api.extension.ExtensionContext context)  
      private void clearAndSetEntries​(org.junit.jupiter.api.extension.ExtensionContext currentContext, org.junit.jupiter.api.extension.ExtensionContext originalContext, boolean doIncrementalBackup)  
      private void clearEntries​(java.util.Collection<K> entriesToClear)  
      protected abstract void clearEntry​(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.Type getActualTypeArgumentAt​(int index)  
      private java.lang.Class<C> getClearAnnotationType()  
      protected abstract V getEntry​(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.Store getStore​(org.junit.jupiter.api.extension.ExtensionContext context)  
      private java.lang.String getStoreKey​(org.junit.jupiter.api.extension.ExtensionContext context, java.lang.String discriminator)  
      protected abstract java.util.Properties prepareToEnterRestorableContext()
      Prepare the entry-based environment for entering a context that must be restorable.
      protected abstract void prepareToExitRestorableContext​(java.util.Properties entries)
      Prepare to exit a restorable context for the entry based environment.
      private void preventClearAndSetSameEntries​(java.util.Collection<K> entriesToClear, java.util.Collection<K> entriesToSet)  
      protected void reportWarning​(org.junit.jupiter.api.extension.ExtensionContext context)
      Reports a warning about potentially unsafe practices.
      private void restoreForAllContexts​(org.junit.jupiter.api.extension.ExtensionContext originalContext)  
      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.
      private void restoreOriginalIncrementalEntries​(org.junit.jupiter.api.extension.ExtensionContext originalContext)  
      private void setEntries​(java.util.Map<K,​V> entriesToSet)  
      protected abstract void setEntry​(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 void storeOriginalCompleteEntries​(org.junit.jupiter.api.extension.ExtensionContext context, java.util.Properties originalEntries)  
      private void storeOriginalIncrementalEntries​(org.junit.jupiter.api.extension.ExtensionContext context, java.util.Collection<K> entriesToClear, java.util.Collection<K> entriesToSet)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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
    • Constructor Detail

      • AbstractEntryBasedExtension

        AbstractEntryBasedExtension()
    • Method Detail

      • 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 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 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​(java.util.Collection<K> entriesToClear)
      • setEntries

        private void setEntries​(java.util.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 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.
      • 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 java.util.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​(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. 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.