Class PodamFactoryImpl

  • All Implemented Interfaces:
    PodamFactory

    @NotThreadSafe
    @Immutable
    public class PodamFactoryImpl
    extends Object
    implements PodamFactory
    The PODAM factory implementation
    Since:
    1.0.0
    Author:
    mtedone
    • Constructor Detail

      • PodamFactoryImpl

        public PodamFactoryImpl()
        Default constructor.
      • PodamFactoryImpl

        public PodamFactoryImpl​(DataProviderStrategy strategy)
        Constructor with non-default strategy
        Parameters:
        strategy - The strategy to use to fill data
      • PodamFactoryImpl

        public PodamFactoryImpl​(PodamFactory externalFactory)
        Constructor with non-default external factory
        Parameters:
        externalFactory - External factory to delegate production this factory cannot handle
      • PodamFactoryImpl

        public PodamFactoryImpl​(PodamFactory externalFactory,
                                DataProviderStrategy strategy)
        Full constructor.
        Parameters:
        externalFactory - External factory to delegate production this factory cannot handle
        strategy - The strategy to use to fill data
    • Method Detail

      • manufacturePojoWithFullData

        public <T> T manufacturePojoWithFullData​(Class<T> pojoClass,
                                                 Type... genericTypeArgs)
        Generic method which returns an instance of the given class filled with dummy values, using the default data provider strategy. In order to instantiate class, constructor with the most parameters is used.

        This method uses RandomDataProviderStrategyImpl as the default implementation.

        Specified by:
        manufacturePojoWithFullData in interface PodamFactory
        Type Parameters:
        T - The type for which a filled instance is required
        Parameters:
        pojoClass - The name of the class for which an instance filled with values is required
        genericTypeArgs - The generic Type arguments for a generic class instance
        Returns:
        An instance of <T> filled with dummy values
      • manufacturePojo

        public <T> T manufacturePojo​(Class<T> pojoClass,
                                     Type... genericTypeArgs)
        Generic method which returns an instance of the given class filled with dummy values, using the default data provider strategy.

        This method uses RandomDataProviderStrategyImpl as the default implementation.

        Specified by:
        manufacturePojo in interface PodamFactory
        Type Parameters:
        T - The type for which a filled instance is required
        Parameters:
        pojoClass - The name of the class for which an instance filled with values is required
        genericTypeArgs - The generic Type arguments for a generic class instance
        Returns:
        An instance of <T> filled with dummy values
      • populatePojo

        public <T> T populatePojo​(T pojo,
                                  Type... genericTypeArgs)
        Generic method which populates an instance of the given class with dummy values, using the default data provider strategy.

        This method uses RandomDataProviderStrategyImpl as the default implementation.

        Specified by:
        populatePojo in interface PodamFactory
        Type Parameters:
        T - The type for which a filled instance is required
        Parameters:
        pojo - The instance to be filled with values
        genericTypeArgs - The generic Type arguments for a generic class instance
        Returns:
        The same instance of object for chaining
      • setStrategy

        public PodamFactory setStrategy​(DataProviderStrategy strategy)
        Sets the data provider strategy for this factory
        Specified by:
        setStrategy in interface PodamFactory
        Parameters:
        strategy - A data provider strategy to be used by this factory
        Returns:
        instance of the factory for chaining
      • getClassStrategy

        public ClassInfoStrategy getClassStrategy()
        It returns the class info strategy for this factory.
        Specified by:
        getClassStrategy in interface PodamFactory
        Returns:
        the class info strategy for this factory
      • setClassStrategy

        public PodamFactory setClassStrategy​(ClassInfoStrategy classInfoStrategy)
        Sets the class info strategy for this factory
        Specified by:
        setClassStrategy in interface PodamFactory
        Parameters:
        classInfoStrategy - A class info strategy to be used by this factory
        Returns:
        instance of the factory for chaining
      • setExternalFactory

        public PodamFactory setExternalFactory​(PodamFactory externalFactory)
        Sets an external factory for this factory to delegate requests it cannot process
        Specified by:
        setExternalFactory in interface PodamFactory
        Parameters:
        externalFactory - An external factory to be used by this factory
        Returns:
        instance of this factory for chaining
      • instantiatePojoWithFactory

        private <T> T instantiatePojoWithFactory​(Class<?> factoryClass,
                                                 Class<T> pojoClass,
                                                 ManufacturingContext manufacturingCtx,
                                                 Type... genericTypeArgs)
                                          throws InstantiationException,
                                                 IllegalAccessException,
                                                 InvocationTargetException,
                                                 ClassNotFoundException
        It attempts to create an instance of the given class with a static method of the factory

        This method attempts to instantiate POJO with a static method of provided factory, for example, getInstance().

        Type Parameters:
        T - The type of Pojo class
        Parameters:
        factoryClass - The factory class, which will be used for POJO instantiation
        pojoClass - The name of the class for which an instance filled with values
        manufacturingCtx - the manufacturing context
        genericTypeArgs - The generic type arguments for the current generic class instance
        Returns:
        An instance of the given class
        Throws:
        IllegalArgumentException - If an illegal argument was passed to the constructor
        InstantiationException - If an exception occurred during instantiation
        IllegalAccessException - If security was violated while creating the object
        InvocationTargetException - If an exception occurred while invoking the constructor or factory method
        ClassNotFoundException - If it was not possible to create a class from a string
      • instantiatePojo

        private <T> T instantiatePojo​(Class<T> pojoClass,
                                      ManufacturingContext manufacturingCtx,
                                      Type... genericTypeArgs)
                               throws SecurityException
        It creates and returns an instance of the given class if at least one of its constructors has been annotated with PodamConstructor
        Type Parameters:
        T - The type of the instance to return
        Parameters:
        pojoClass - The class of which an instance is required
        manufacturingCtx - the manufacturing context
        genericTypeArgs - The generic type arguments for the current generic class instance
        Returns:
        an instance of the given class if at least one of its constructors has been annotated with PodamConstructor
        Throws:
        SecurityException - If an security was violated
      • doManufacturePojo

        private <T> T doManufacturePojo​(Class<T> pojoClass,
                                        ManufacturingContext manufacturingCtx,
                                        Type... genericTypeArgs)
        Manufactures and populates the pojo class
        Type Parameters:
        T - The type of the instance to return
        Parameters:
        pojoClass - the class to instantiate
        manufacturingCtx - the initialized manufacturing context
        genericTypeArgs - generic arguments for the pojo class
        Returns:
        instance of @pojoClass or null in case it cannot be instantiated
      • manufactureAttributeValue

        private Object manufactureAttributeValue​(Object pojo,
                                                 ManufacturingContext manufacturingCtx,
                                                 Class<?> attributeType,
                                                 Type genericAttributeType,
                                                 List<Annotation> annotations,
                                                 String attributeName,
                                                 Type... genericTypeArgs)
                                          throws InstantiationException,
                                                 IllegalAccessException,
                                                 InvocationTargetException,
                                                 ClassNotFoundException
        It manufactures and returns the value for a POJO attribute.
        Parameters:
        pojo - The POJO being filled with values
        manufacturingCtx - the manufacturing context
        attributeType - The type of the attribute for which a value is being manufactured
        genericAttributeType - The generic type of the attribute for which a value is being manufactured
        annotations - The annotations for the attribute being considered
        attributeName - The attribute name
        genericTypeArgs - The generic type arguments for the current generic class instance
        Returns:
        The value for an attribute
        Throws:
        InstantiationException - If an exception occurred during instantiation
        IllegalAccessException - If security was violated while creating the object
        InvocationTargetException - If an exception occurred while invoking the constructor or factory method
        IllegalArgumentException -
        • If an illegal argument was passed
        • If an invalid value was set for a precise value in an annotation and such value could not be converted to the desired type
        ClassNotFoundException - If class being manufactured cannot be loaded
      • resortToExternalFactory

        private <T> T resortToExternalFactory​(ManufacturingContext manufacturingCtx,
                                              String msg,
                                              Class<T> pojoClass,
                                              Type... genericTypeArgs)
        Delegates POJO manufacturing to an external factory
        Type Parameters:
        T - The type of the instance to return
        Parameters:
        manufacturingCtx - the manufacturing context
        msg - Message to log, must contain two parameters
        pojoClass - The class of which an instance is required
        genericTypeArgs - The generic type arguments for the current generic class instance
        Returns:
        instance of POJO produced by external factory or null
      • resolveCollectionValueWhenCollectionIsPojoAttribute

        private Collection<? super Object> resolveCollectionValueWhenCollectionIsPojoAttribute​(Object pojo,
                                                                                               ManufacturingContext manufacturingCtx,
                                                                                               AttributeMetadata attributeMetadata)
        It returns a collection of some sort with some data in it.
        Parameters:
        pojo - The POJO being analyzed
        manufacturingCtx - the manufacturing context
        attributeMetadata - The attribute's metadata
        Returns:
        a collection of some sort with some data in it
        Throws:
        PodamMockeryException - An exception occurred while resolving the collection
        IllegalArgumentException - If the field name is null or empty
      • findInheretedCollectionElementType

        private Class<?> findInheretedCollectionElementType​(Collection<Object> collection,
                                                            ManufacturingContext manufacturingCtx,
                                                            AtomicReference<Type[]> elementGenericTypeArgs,
                                                            Type... genericTypeArgs)
        Tries to find collection element type from collection object
        Parameters:
        collection - The collection to be filled
        manufacturingCtx - the manufacturing context
        elementGenericTypeArgs - parameter to return generic arguments of collection element
        genericTypeArgs - The generic type arguments for the current generic class instance
        Returns:
        class type of collection element
      • resolveMapValueWhenMapIsPojoAttribute

        private Map<? super Object,​? super Object> resolveMapValueWhenMapIsPojoAttribute​(Object pojo,
                                                                                               ManufacturingContext manufacturingCtx,
                                                                                               AttributeMetadata attributeMetadata)
        It manufactures and returns a Map with at least one element in it
        Parameters:
        pojo - The POJO being initialized
        manufacturingCtx - the manufacturing context
        attributeMetadata - The attribute's metadata
        Returns:
        Map with at least one element in it
        Throws:
        IllegalArgumentException -
        • If the attribute name is null or empty
        • If the array of types of the Map has length different from 2
        PodamMockeryException - If an error occurred while creating the Map object
      • findInheretedMapElementType

        private MapArguments findInheretedMapElementType​(Map<Object,​Object> map,
                                                         ManufacturingContext manufacturingCtx,
                                                         Type... genericTypeArgs)
        Finds key and element type arguments
        Parameters:
        map - The map being initialized
        manufacturingCtx - the manufacturing context
        genericTypeArgs - The generic type arguments for the current generic class instance
        Returns:
        Inherited map key and element types