Class AbstractRandomDataProviderStrategy

    • Field Detail

      • LOG

        private static final org.slf4j.Logger LOG
        Application logger
      • maxDepth

        private int maxDepth
        How many times it is allowed to PODAM to create an instance of the same class in a recursive hierarchy
      • nbrOfCollectionElements

        private final AtomicInteger nbrOfCollectionElements
        The number of collection elements.
      • isMemoizationEnabled

        private final AtomicBoolean isMemoizationEnabled
        Flag to enable/disable the memoization setting.
      • memoizationTable

        private final Map<Class<?>,​Map<Type[],​Object>> memoizationTable
        A map to keep one object for each class. If memoization is enabled, the factory will use this table to avoid creating objects of the same class multiple times.
      • specificTypes

        private final Map<Class<?>,​Class<?>> specificTypes
        A list of user-submitted specific implementations for interfaces and abstract classes
      • factoryTypes

        private final Map<Class<?>,​Class<?>> factoryTypes
        A list of user-submitted factories to build interfaces and abstract classes
      • attributeClassStrategies

        private final Map<Class<?>,​Map<String,​AttributeStrategy<?>>> attributeClassStrategies
        Mapping between attributes and attribute strategies
    • Constructor Detail

      • AbstractRandomDataProviderStrategy

        public AbstractRandomDataProviderStrategy()
        Implementation of the Singleton pattern
      • AbstractRandomDataProviderStrategy

        public AbstractRandomDataProviderStrategy​(int nbrOfCollectionElements)
    • Method Detail

      • getNumberOfCollectionElements

        public int getNumberOfCollectionElements​(Class<?> type)
        Returns the number of default collection elements.

        Implementations of this interface need to provide this value.

        Specified by:
        getNumberOfCollectionElements in interface DataProviderStrategy
        Parameters:
        type - Type of collection's elements
        Returns:
        The number of default collection elements
      • setDefaultNumberOfCollectionElements

        public void setDefaultNumberOfCollectionElements​(int newNumberOfCollectionElements)
        Sets the new default number of default collection elements.
        Specified by:
        setDefaultNumberOfCollectionElements in interface DataProviderStrategy
        Parameters:
        newNumberOfCollectionElements - The new number of collection elements.
      • getMaxDepth

        public int getMaxDepth​(Class<?> type)
        Returns how many times it is allowed to PODAM to create an instance of the same class in a recursive hierarchy

        Implementations of this interface need to provide this value.

        Specified by:
        getMaxDepth in interface DataProviderStrategy
        Parameters:
        type - Type of POJO to be created
        Returns:
        How many times it is allowed to create an instance of the same class in a recursive hierarchy
      • setMaxDepth

        public void setMaxDepth​(int maxDepth)
        Max depth setter
        Parameters:
        maxDepth - defines new max depth
      • isMemoizationEnabled

        public boolean isMemoizationEnabled()
        Returns whether the memoization setting is enabled. When memoization is enabled, only one object will be created for each type. Every next property of the same type will be a reference to the same object. This can dramatically improve performance but with the expense of not having objects with different values.

        Implementations of this interface need to provide this value.

        Specified by:
        isMemoizationEnabled in interface DataProviderStrategy
        Returns:
        Whether the memoization setting is enabled.
      • setMemoization

        public void setMemoization​(boolean isMemoizationEnabled)
        Sets whether memoization is enabled
        Specified by:
        setMemoization in interface DataProviderStrategy
        Parameters:
        isMemoizationEnabled - whether memoization is enabled
      • getMemoizedObject

        public Object getMemoizedObject​(AttributeMetadata attributeMetadata)
        Obtains object from memoization cache
        Specified by:
        getMemoizedObject in interface DataProviderStrategy
        Parameters:
        attributeMetadata - attribute metadata for instance to be fetched
        Returns:
        object instance from memoization cache
      • cacheMemoizedObject

        public void cacheMemoizedObject​(AttributeMetadata attributeMetadata,
                                        Object instance)
        Saves object in memoization cache
        Specified by:
        cacheMemoizedObject in interface DataProviderStrategy
        Parameters:
        attributeMetadata - attribute metadata for instance being memoized
        instance - object instance to save in memoization cache
      • sort

        public void sort​(Constructor<?>[] constructors,
                         DataProviderStrategy.Order order)
        Rearranges POJO's constructors in order they will be tried to produce the POJO. Default strategy consist of putting constructors with less parameters to be tried first.
        Specified by:
        sort in interface DataProviderStrategy
        Parameters:
        constructors - Array of POJO's constructors
        order - DataProviderStrategy.Order how to sort constructors
      • addOrReplaceTypeManufacturer

        public <T> DataProviderStrategy addOrReplaceTypeManufacturer​(Class<? extends T> type,
                                                                     TypeManufacturer<T> typeManufacturer)
        Registers @TypeManufacturer implementation, which will be used to instantiate objects of a specified type. Use this to alter factory behaviour.
        Specified by:
        addOrReplaceTypeManufacturer in interface DataProviderStrategy
        Type Parameters:
        T - type of objects to be produced by the manufacturer
        Parameters:
        type - the specific class type the specified manufacturer will instantiate.
        typeManufacturer - type manufacturer implementation to be registered
        Returns:
        itself
      • removeTypeManufacturer

        public <T> DataProviderStrategy removeTypeManufacturer​(Class<T> type)
        Remove binding of a class type to a specific implementation of type manufacturer
        Specified by:
        removeTypeManufacturer in interface DataProviderStrategy
        Type Parameters:
        T - type of objects to be produced by the manufacturer
        Parameters:
        type - the specific class type to remove binding
        Returns:
        itself
      • getTypeValue

        public <T> T getTypeValue​(AttributeMetadata attributeMetadata,
                                  Map<String,​Type> genericTypesArgumentsMap,
                                  Class<T> pojoType)
        Obtains a type value
        Specified by:
        getTypeValue in interface DataProviderStrategy
        Type Parameters:
        T - type of returned object
        Parameters:
        attributeMetadata - The AttributeMetadata information
        genericTypesArgumentsMap - The generic attribute type argument types
        pojoType - The class of the requested type
        Returns:
        The type value
      • addOrReplaceFactory

        public <T> AbstractRandomDataProviderStrategy addOrReplaceFactory​(Class<T> abstractClass,
                                                                          Class<?> factoryClass)
        Binds an interface/abstract class to its factory. If the strategy previously contained a binding for the interface/abstract class, the old value is replaced by the new value. If you want to implement more sophisticated binding strategy, override this class.
        Specified by:
        addOrReplaceFactory in interface RandomDataProviderStrategy
        Type Parameters:
        T - return type
        Parameters:
        abstractClass - the interface/abstract class to bind
        factoryClass - factory class for instantiation of abstractClass.
        Returns:
        itself
      • getFactoryClass

        public Class<?> getFactoryClass​(Class<?> nonInstantiatableClass)
        Resolves factory classes for abstract classes and interfaces.

        Should return factory class type, which can be used to instantiate an abstract class type or interface.

        Specified by:
        getFactoryClass in interface DataProviderStrategy
        Parameters:
        nonInstantiatableClass - Abstract class type or interface
        Returns:
        Non-abstract factory class type to instantiate nonInstantiatableClass.
      • addOrReplaceSpecific

        public <T> DataProviderStrategy addOrReplaceSpecific​(Class<T> abstractClass,
                                                             Class<? extends T> specificClass)
        Bind an interface/abstract class to a specific implementation. If the strategy previously contained a binding for the interface/abstract class, the old value will not be replaced by the new value. If you want to force the value replacement, invoke removeSpecific before invoking this method. If you want to implement more sophisticated binding strategy, override this class.
        Specified by:
        addOrReplaceSpecific in interface DataProviderStrategy
        Type Parameters:
        T - return type
        Parameters:
        abstractClass - the interface/abstract class to bind
        specificClass - the specific class implementing or extending abstractClass.
        Returns:
        itself
      • removeSpecific

        public <T> DataProviderStrategy removeSpecific​(Class<T> abstractClass)
        Remove binding of an interface/abstract class to a specific implementation
        Specified by:
        removeSpecific in interface DataProviderStrategy
        Type Parameters:
        T - return type
        Parameters:
        abstractClass - the interface/abstract class to remove binding
        Returns:
        itself
      • getSpecificClass

        public <T> Class<? extends T> getSpecificClass​(Class<T> nonInstantiatableClass)
        Resolves abstract classes and interfaces.

        Should return specific class type, which can be instantiated and assigned to abstract class type or interface.

        Specified by:
        getSpecificClass in interface DataProviderStrategy
        Type Parameters:
        T - The type of class being resolved
        Parameters:
        nonInstantiatableClass - Abstract class type or interface
        Returns:
        Non-abstract class type derived from nonInstantiatableClass.
      • addOrReplaceAttributeStrategy

        public RandomDataProviderStrategy addOrReplaceAttributeStrategy​(Class<? extends Annotation> annotationClass,
                                                                        AttributeStrategy<?> attributeStrategy)
        Bind an annotation to attribute strategy class. If the strategy previously contained a binding for the annotation, the old will be replaced. If you want to implement more sophisticated binding strategy, override this class.
        Specified by:
        addOrReplaceAttributeStrategy in interface RandomDataProviderStrategy
        Parameters:
        annotationClass - the annotation class
        attributeStrategy - the attribute strategy
        Returns:
        itself
      • getStrategyForAnnotation

        public AttributeStrategy<?> getStrategyForAnnotation​(Class<? extends Annotation> annotationClass)
        Finds attribute strategies for annotations.

        Searches for mapping between annotations and attribute strategies, which will be used then for populating fields or constructor parameters.

        Specified by:
        getStrategyForAnnotation in interface DataProviderStrategy
        Parameters:
        annotationClass - Annotation class to inspect
        Returns:
        attribute strategy associated with given annotation
      • addOrReplaceAttributeStrategy

        public RandomDataProviderStrategy addOrReplaceAttributeStrategy​(Class<?> type,
                                                                        String attributeName,
                                                                        AttributeStrategy<?> attributeStrategy)
        Registers @AttributeStrategy implementation, which will be used to instantiate objects of a specified type. Use this to alter factory behaviour.
        Specified by:
        addOrReplaceAttributeStrategy in interface DataProviderStrategy
        Parameters:
        type - the specific class type the specified manufacturer will instantiate.
        attributeName - attribute name to use attributeStrategy for
        attributeStrategy - attribute strategy to be registered
        Returns:
        itself
      • removeAttributeStrategy

        public RandomDataProviderStrategy removeAttributeStrategy​(Class<?> type,
                                                                  String attributeName)
        Removes @AttributeStrategy implementation from for the specific attribute
        Specified by:
        removeAttributeStrategy in interface DataProviderStrategy
        Parameters:
        type - the specific class type the specified manufacturer will instantiate.
        attributeName - attribute name to use attributeStrategy for
        Returns:
        itself
      • getStrategyForAttribute

        public AttributeStrategy<?> getStrategyForAttribute​(ClassAttribute attribute)
        Finds attribute strategies for attribute.

        Searches for mapping between class attribute and attribute strategies, which will be used then for populating fields or constructor parameters.

        Specified by:
        getStrategyForAttribute in interface DataProviderStrategy
        Parameters:
        attribute - attribute to be manufactured
        Returns:
        attribute strategy associated with given attribute