uk.co.jemos.podam.api
Class PodamFactory

java.lang.Object
  extended by uk.co.jemos.podam.api.PodamFactory

public class PodamFactory
extends Object

Author:
mtedone

Field Summary
private static org.apache.log4j.Logger LOG
          Application logger
 
Constructor Summary
PodamFactory()
           
 
Method Summary
private static Object createNewInstanceForClassWithoutSetters(DataProviderStrategy strategy, Class<?> clazz)
          It attempts to create an instance of the given class
private static Byte getByteValueWithinRange(DataProviderStrategy strategy, List<Annotation> annotations)
          It returns a random byte if the attribute was annotated with PodamByteValue or null otherwise
private static Character getCharacterValueWithinRange(DataProviderStrategy strategy, List<Annotation> annotations)
          It creates and returns a random Character value
private static Double getDoubleValueWithinRange(DataProviderStrategy strategy, List<Annotation> annotations)
          It creates and returns a random Double value
private static Float getFloatValueWithinRange(DataProviderStrategy strategy, List<Annotation> annotations)
          Returns either a customised float value if a PodamFloatValue annotation was provided or a random float if this was not the case
private static Integer getIntValueWithinRange(DataProviderStrategy strategy, List<Annotation> annotations)
          Returns either a customised int value if a PodamIntValue annotation was provided or a random integer if this was not the case
private static Long getLongValueWithinRange(DataProviderStrategy strategy, List<Annotation> annotations)
          Returns either a customised long value if a PodamLongValue annotation was provided or a random long if this was not the case
private static Short getShortValueWithinRange(DataProviderStrategy strategy, List<Annotation> annotations)
          It returns a random short if the attribute was annotated with PodamShortValue or null otherwise
private static boolean isWrapper(Class<?> candidateWrapperClass)
          It returns true if this class is a wrapper class, false otherwise
private static Object manufactureAttributeValue(DataProviderStrategy strategy, Class<?> pojoClass, Class<?> attributeType, List<Annotation> annotations, String attributeName)
          It manufactures and returns the value for a POJO attribute.
static
<T> T
manufacturePojo(Class<T> pojoClass)
          Generic method which returns an instance of the given class filled with dummy values, using the default data provider strategy.
static
<T> T
manufacturePojo(Class<T> pojoClass, DataProviderStrategy strategy)
          Generic method which returns an instance of the given class filled with dummy values
private static
<T> T
manufacturePojoInternal(DataProviderStrategy strategy, Class<T> pojoClass, int depth)
          Generic method which returns an instance of the given class filled with dummy values
private static Object resolveArrayElementValue(DataProviderStrategy strategy, Class<?> attributeType, List<Annotation> annotations, Class<?> pojoClass, String attributeName)
          It returns an Array with the first element set
private static int resolveCollectionNbrElementsFromAnnotations(List<Annotation> annotations)
          Given a list of annotations, it scans whether there is one to customise the number of elements in a collection
private static Collection<? super Object> resolveCollectionType(Class<?> collectionType)
          Given a collection type it returns an instance
private static Collection<? super Object> resolveCollectionValueWhenCollectionIsPojoAttribute(DataProviderStrategy strategy, Class<?> pojoClass, Class<?> collectionType, String attributeName, List<Annotation> annotations)
          It returns a collection of some sort with some data in it.
private static Map<? super Object,? super Object> resolveMapType(Class<?> attributeType)
          It manufactures and returns a default instance for each map type
private static Map<? super Object,? super Object> resolveMapValueWhenMapIsPojoAttribute(DataProviderStrategy strategy, Class<?> pojoClass, Class<?> attributeType, String attributeName, List<Annotation> annotations)
          It manufactures and returns a Map with at least one element in it
private static
<T> T
resolvePojoWithoutDefaultConstructor(DataProviderStrategy strategy, Class<T> pojoClass, int depth)
          It creates and returns an instance of the given class if at least one of its constructors has been annotated with PodamConstructor
private static Object resolvePrimitiveValue(DataProviderStrategy strategy, Class<?> primitiveClass, List<Annotation> annotations)
          It resolves and returns the primitive value depending on the type
private static String resolveStringValue(DataProviderStrategy strategy, List<Annotation> annotations)
          It creates and returns a String value, eventually customised by annotations
private static Object resolveWrapperValue(DataProviderStrategy strategy, Class<?> candidateWrapperClass, List<Annotation> annotations)
          It attempts to resolve the given class as a wrapper class and if this is the case it assigns a random value
private static Class<?> retrieveClassFromCollectionTypeInConstructor(String typeStr)
          Given a String representing a type for a collection or map in a constructor, it returns the class of that collection or map
private static Class<?>[] retrieveClassFromMapTypeInConstructor(String typeStr)
          Given a string representing a Map in a constructor, it returns an array identifying the classes for the key and value in the map
private static List<Annotation> retrieveFieldAnnotations(Class<?> clazz, Method setter)
          Given the original class and the setter method, it returns all annotations for the field or an empty collection if no custom annotations were found on the field
private static void validateAttributeName(String attributeName)
          It validates that the attribute name is not null or empty
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG

private static final org.apache.log4j.Logger LOG
Application logger

Constructor Detail

PodamFactory

public PodamFactory()
Method Detail

manufacturePojo

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

This method uses RandomDataProviderStrategy as the default implementation.

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
Returns:
An instance of filled with dummy values
Throws:
PodamMockeryException - if a problem occurred while creating a POJO instance or while setting its state

manufacturePojo

public static <T> T manufacturePojo(Class<T> pojoClass,
                                    DataProviderStrategy strategy)
Generic method which returns an instance of the given class filled with dummy values

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
strategy - The DataProviderStrategy that PODAM will use to fill data
Returns:
An instance of filled with dummy values
Throws:
PodamMockeryException - if a problem occurred while creating a POJO instance or while setting its state

createNewInstanceForClassWithoutSetters

private static Object createNewInstanceForClassWithoutSetters(DataProviderStrategy strategy,
                                                              Class<?> clazz)
                                                       throws IllegalArgumentException,
                                                              InstantiationException,
                                                              IllegalAccessException,
                                                              InvocationTargetException,
                                                              ClassNotFoundException
It attempts to create an instance of the given class

This method attempts to create an instance of the given argument for classes without setters. These may be either immutable classes (e.g. with final attributes and no setters) or Java classes (e.g. belonging to the java / javax namespace). In case the class does not provide a public, no-arg constructor (e.g. Calendar), this method attempts to find a static, no-args, factory method (e.g. getInstance()) and it invokes it

Parameters:
clazz - The class for which a new instance is required
strategy - The DataProviderStrategy that PODAM will use to fill data
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 static factory method
ClassNotFoundException - If it was not possible to create a class from a string

resolvePrimitiveValue

private static Object resolvePrimitiveValue(DataProviderStrategy strategy,
                                            Class<?> primitiveClass,
                                            List<Annotation> annotations)
It resolves and returns the primitive value depending on the type

Parameters:
strategy - The DataProviderStrategy that PODAM will use to fill data
primitiveClass - The primitive type class
annotations - The annotations to consider for this attribute
Returns:
the primitive value depending on the type

getByteValueWithinRange

private static Byte getByteValueWithinRange(DataProviderStrategy strategy,
                                            List<Annotation> annotations)
It returns a random byte if the attribute was annotated with PodamByteValue or null otherwise

Parameters:
annotations - The list of annotations for this attribute
strategy - The DataProviderStrategy to use to retrieve the data.
Returns:
A random byte if the attribute was annotated with

getShortValueWithinRange

private static Short getShortValueWithinRange(DataProviderStrategy strategy,
                                              List<Annotation> annotations)
It returns a random short if the attribute was annotated with PodamShortValue or null otherwise

Parameters:
annotations - The annotations with which the attribute was annotated
strategy - The DataProviderStrategy to use to retrieve the data.
Returns:
A random short if the attribute was annotated with PodamShortValue or null otherwise

getCharacterValueWithinRange

private static Character getCharacterValueWithinRange(DataProviderStrategy strategy,
                                                      List<Annotation> annotations)
It creates and returns a random Character value

Parameters:
annotations - The list of annotations which might customise the return value
strategy - The DataProviderStrategy to use to retrieve the data.
Returns:
A random Character value

getIntValueWithinRange

private static Integer getIntValueWithinRange(DataProviderStrategy strategy,
                                              List<Annotation> annotations)
Returns either a customised int value if a PodamIntValue annotation was provided or a random integer if this was not the case

Parameters:
annotations - The list of annotations for the int attribute
strategy - The DataProviderStrategy to use to retrieve the data.
Returns:
Either a customised int value if a PodamIntValue annotation was provided or a random integer if this was not the case

getFloatValueWithinRange

private static Float getFloatValueWithinRange(DataProviderStrategy strategy,
                                              List<Annotation> annotations)
Returns either a customised float value if a PodamFloatValue annotation was provided or a random float if this was not the case

Parameters:
annotations - The list of annotations for the int attribute
strategy - The DataProviderStrategy to use to retrieve the data.
Returns:
Either a customised float value if a PodamFloatValue annotation was provided or a random float if this was not the case

getDoubleValueWithinRange

private static Double getDoubleValueWithinRange(DataProviderStrategy strategy,
                                                List<Annotation> annotations)
It creates and returns a random Double value

Parameters:
annotations - The list of annotations which might customise the return value
strategy - The DataProviderStrategy to use to retrieve the data.
Returns:
a random Double value

getLongValueWithinRange

private static Long getLongValueWithinRange(DataProviderStrategy strategy,
                                            List<Annotation> annotations)
Returns either a customised long value if a PodamLongValue annotation was provided or a random long if this was not the case

Parameters:
annotations - The list of annotations for the int attribute
strategy - The DataProviderStrategy to use to retrieve the data.
Returns:
Either a customised long value if a PodamLongValue annotation was provided or a random long if this was not the case

resolveWrapperValue

private static Object resolveWrapperValue(DataProviderStrategy strategy,
                                          Class<?> candidateWrapperClass,
                                          List<Annotation> annotations)
It attempts to resolve the given class as a wrapper class and if this is the case it assigns a random value

Parameters:
strategy - The DataProviderStrategy that PODAM will use to fill data
candidateWrapperClass - The class which might be a wrapper class
Returns:
null if this is not a wrapper class, otherwise an Object with the value for the wrapper class

resolvePojoWithoutDefaultConstructor

private static <T> T resolvePojoWithoutDefaultConstructor(DataProviderStrategy strategy,
                                                          Class<T> pojoClass,
                                                          int depth)
                                               throws IllegalArgumentException,
                                                      InstantiationException,
                                                      IllegalAccessException,
                                                      InvocationTargetException,
                                                      ClassNotFoundException
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:
strategy - The DataProviderStrategy that PODAM will use to fill data
pojoClass - The class of which an instance is required
depth - How many instances of the same class have been created so far
Returns:
an instance of the given class if at least one of its constructors has been annotated with PodamConstructor
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 static factory method
ClassNotFoundException - If it was not possible to create a class from a string

retrieveClassFromCollectionTypeInConstructor

private static Class<?> retrieveClassFromCollectionTypeInConstructor(String typeStr)
Given a String representing a type for a collection or map in a constructor, it returns the class of that collection or map

Parameters:
typeStr - The String identifying the collection type
Returns:
The class of that collection or map
Throws:
IllegalStateException - If the class identified by typeStr was not found

retrieveClassFromMapTypeInConstructor

private static Class<?>[] retrieveClassFromMapTypeInConstructor(String typeStr)
Given a string representing a Map in a constructor, it returns an array identifying the classes for the key and value in the map

Parameters:
typeStr - The type of the Map. It can be non-generic
Returns:
An array identifying the classes for the key and value in the map
Throws:
IllegalStateException - If either the key or value classes could not be loaded because not found

manufacturePojoInternal

private static <T> T manufacturePojoInternal(DataProviderStrategy strategy,
                                             Class<T> pojoClass,
                                             int depth)
Generic method which returns an instance of the given class filled with dummy values

Type Parameters:
T - The type for which a filled instance is required
Parameters:
strategy - The DataProviderStrategy that PODAM will use to fill data
pojoClass - The name of the class for which an instance filled with values is required
depth - How many times dtoClass has been found
Returns:
An instance of filled with dummy values
Throws:
PodamMockeryException - if a problem occurred while creating a POJO instance or while setting its state

manufactureAttributeValue

private static Object manufactureAttributeValue(DataProviderStrategy strategy,
                                                Class<?> pojoClass,
                                                Class<?> attributeType,
                                                List<Annotation> annotations,
                                                String attributeName)
                                         throws InstantiationException,
                                                IllegalAccessException,
                                                InvocationTargetException,
                                                IllegalArgumentException,
                                                ClassNotFoundException
It manufactures and returns the value for a POJO attribute.

Parameters:
strategy - The DataProviderStrategy that PODAM will use to fill data
pojoClass - The POJO class being filled with values
attributeType - The type of the attribute for which a value is being manufactured
annotations - The annotations for the attribute being considered
attributeName - The attribute name
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 static factory method
ClassNotFoundException - If it was not possible to create a class from a string
IllegalArgumentException - If an illegal argument was passed

resolveStringValue

private static String resolveStringValue(DataProviderStrategy strategy,
                                         List<Annotation> annotations)
It creates and returns a String value, eventually customised by annotations

Parameters:
strategy - The DataProviderStrategy that PODAM will use to fill data
annotations - The list of annotations used to customise the String value, if any
Returns:
a String value, eventually customised by annotations

isWrapper

private static boolean isWrapper(Class<?> candidateWrapperClass)
It returns true if this class is a wrapper class, false otherwise

Parameters:
candidateWrapperClass - The class to check
Returns:
true if this class is a wrapper class, false otherwise

retrieveFieldAnnotations

private static List<Annotation> retrieveFieldAnnotations(Class<?> clazz,
                                                         Method setter)
Given the original class and the setter method, it returns all annotations for the field or an empty collection if no custom annotations were found on the field

Parameters:
clazz - The class containing the annotated attribute
setter - The setter method
Returns:
all annotations for the field
Throws:
NoSuchFieldException - If the field could not be found
SecurityException - if a security exception occurred

resolveCollectionValueWhenCollectionIsPojoAttribute

private static Collection<? super Object> resolveCollectionValueWhenCollectionIsPojoAttribute(DataProviderStrategy strategy,
                                                                                              Class<?> pojoClass,
                                                                                              Class<?> collectionType,
                                                                                              String attributeName,
                                                                                              List<Annotation> annotations)
It returns a collection of some sort with some data in it.

Parameters:
strategy - The DataProviderStrategy that PODAM will use to fill data
pojoClass - The POJO being analysed
collectionType - The type of the attribute being evaluated
annotations - The set of annotations for the annotated attribute. It might be empty
attributeName - The name of the field being set
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

resolveMapValueWhenMapIsPojoAttribute

private static Map<? super Object,? super Object> resolveMapValueWhenMapIsPojoAttribute(DataProviderStrategy strategy,
                                                                                        Class<?> pojoClass,
                                                                                        Class<?> attributeType,
                                                                                        String attributeName,
                                                                                        List<Annotation> annotations)
It manufactures and returns a Map with at least one element in it

Parameters:
strategy - The DataProviderStrategy that PODAM will use to fill data
pojoClass - The POJO being initialised
attributeType - The type of the POJO map attribute
attributeName - The POJO attribute name
annotations - The annotations specified for this attribute
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

resolveArrayElementValue

private static Object resolveArrayElementValue(DataProviderStrategy strategy,
                                               Class<?> attributeType,
                                               List<Annotation> annotations,
                                               Class<?> pojoClass,
                                               String attributeName)
                                        throws IllegalArgumentException,
                                               InstantiationException,
                                               IllegalAccessException,
                                               InvocationTargetException,
                                               ClassNotFoundException
It returns an Array with the first element set

Parameters:
strategy - The DataProviderStrategy that PODAM will use to fill data
attributeType - The array type
annotations - The annotations to be considered
pojoClass -
attributeName -
Returns:
Array with the first element set
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 static factory method
ClassNotFoundException - If it was not possible to create a class from a string

resolveCollectionType

private static Collection<? super Object> resolveCollectionType(Class<?> collectionType)
Given a collection type it returns an instance

Parameters:
collectionType - The collection type *
Returns:
an instance of the collection type

resolveMapType

private static Map<? super Object,? super Object> resolveMapType(Class<?> attributeType)
It manufactures and returns a default instance for each map type

The default implementation for a ConcurrentMap is ConcurrentHashMap

The default implementation for a SortedMap is a TreeMap

The default Map is none of the above was recognised is a HashMap

Parameters:
attributeType - The attribute type
Returns:
A default instance for each map type

validateAttributeName

private static void validateAttributeName(String attributeName)
It validates that the attribute name is not null or empty

Parameters:
attributeName - The attribute to be validated
Throws:
IllegalArgumentException - If the attribute name is null or empty

resolveCollectionNbrElementsFromAnnotations

private static int resolveCollectionNbrElementsFromAnnotations(List<Annotation> annotations)
Given a list of annotations, it scans whether there is one to customise the number of elements in a collection

The number of elements defaults to PodamConstants.ANNOTATION_COLLECTION_DEFAULT_NBR_ELEMENTS

Parameters:
annotations - The list of annotations to scan for PodamCollection
Returns:
The number of elements to add to the collection

Jemos 2002 - 2011: All Rights Reserved. Podam runs under the MIT license

Copyright © 2011. All Rights Reserved.