@ThreadSafe @Immutable public class PodamFactoryImpl extends Object implements PodamFactory
Modifier and Type | Field and Description |
---|---|
static Comparator<Constructor<?>> |
ConstructorComparator
Comparator for sorting constructors.
|
private List<Class<? extends Annotation>> |
excludeAnnotations
A list of
Annotation s for attributes that PODAM shouldn't
consider |
private org.apache.log4j.Logger |
LOG
Application logger
|
private DataProviderStrategy |
strategy
The strategy to use to fill data.
|
Constructor and Description |
---|
PodamFactoryImpl()
Default constructor.
|
PodamFactoryImpl(DataProviderStrategy strategy)
Full constructor.
|
Modifier and Type | Method and Description |
---|---|
private PodamStrategyValue |
containsAttributeStrategyAnnotation(List<Annotation> annotations)
It returns a
PodamStrategyValue if one was specified, or
null otherwise. |
private Object |
createNewInstanceForClassWithoutConstructors(Class<?> pojoClass,
Class<?> clazz,
Type... genericTypeArgs)
It attempts to create an instance of the given class
|
private void |
fillCollection(Class<?> pojoClass,
String attributeName,
List<Annotation> annotations,
Collection<? super Object> collection,
Class<?> collectionElementType,
Type... genericTypeArgs)
It fills a collection with the required number of elements of the
required type.
|
private void |
fillMap(Class<?> pojoClass,
String attributeName,
List<Annotation> annotations,
Map<? super Object,? super Object> mapToBeFilled,
Class<?> keyClass,
Class<?> elementClass,
Type[] keyGenericTypeArgs,
Type[] elementGenericTypeArgs)
It fills a Map with the required number of elements of the required type.
|
private Type[] |
fillTypeArgMap(Map<String,Type> typeArgsMap,
TypeVariable<?>[] typeParameters,
Type[] genericTypeArgs)
Fills type agruments map
|
private Boolean |
getBooleanValueForAnnotation(List<Annotation> annotations)
It returns the boolean value indicated in the annotation.
|
private Byte |
getByteValueWithinRange(List<Annotation> annotations,
AttributeMetadata attributeMetadata)
It returns a random byte if the attribute was annotated with
PodamByteValue or null otherwise |
private Character |
getCharacterValueWithinRange(List<Annotation> annotations,
AttributeMetadata attributeMetadata)
It creates and returns a random
Character value |
private Double |
getDoubleValueWithinRange(List<Annotation> annotations,
AttributeMetadata attributeMetadata)
It creates and returns a random
Double value |
List<Class<? extends Annotation>> |
getExcludeAnnotations() |
private Float |
getFloatValueWithinRange(List<Annotation> annotations,
AttributeMetadata attributeMetadata)
Returns either a customised float value if a
PodamFloatValue
annotation was provided or a random float if this was not the case |
private Integer |
getIntegerValueWithinRange(List<Annotation> annotations,
AttributeMetadata attributeMetadata)
Returns either a customised int value if a
PodamIntValue
annotation was provided or a random integer if this was not the case |
private Long |
getLongValueWithinRange(List<Annotation> annotations,
AttributeMetadata attributeMetadata)
Returns either a customised long value if a
PodamLongValue
annotation was provided or a random long if this was not the case |
private Object |
getMapKeyOrElementValue(Class<?> pojoClass,
String attributeName,
List<Annotation> annotations,
Class<?> keyOrValueType,
PodamCollection collectionAnnotation,
AttributeStrategy<?> elementStrategy,
Type... genericTypeArgs)
It fills a Map key or value with the appropriate value, considering
attribute-level customisation.
|
private Object[] |
getParameterValuesForConstructor(Constructor<?> constructor,
Class<?> pojoClass,
Type... genericTypeArgs)
Given a constructor it manufactures and returns the parameter values
required to invoke it
|
private Short |
getShortValueWithinRange(List<Annotation> annotations,
AttributeMetadata attributeMetadata)
It returns a random short if the attribute was annotated with
PodamShortValue or null otherwise |
DataProviderStrategy |
getStrategy()
It returns the strategy for this factory.
|
private boolean |
isWrapper(Class<?> candidateWrapperClass)
It returns
true if this class is a wrapper class, false
otherwise |
private Object |
manufactureAttributeValue(Class<?> pojoClass,
Class<?> attributeType,
List<Annotation> annotations,
String attributeName,
Map<String,Type> typeArgsMap,
Type... genericTypeArgs)
It manufactures and returns the value for a POJO attribute.
|
private Object |
manufactureAttributeValue(Class<?> pojoClass,
Class<?> attributeType,
List<Annotation> annotations,
String attributeName,
Type... genericTypeArgs)
It manufactures and returns the value for a POJO attribute.
|
<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.
|
private <T> T |
manufacturePojoInternal(Class<T> pojoClass,
int depth,
Type... genericTypeArgs)
Generic method which returns an instance of the given class filled with
values dictated by the strategy
|
private Type[] |
mergeTypeArrays(Type[] original,
Type[] extra)
Utility method to merge two arrays
|
private Object |
resolveArrayElementValue(Class<?> attributeType,
List<Annotation> annotations,
Class<?> pojoClass,
String attributeName,
Map<String,Type> typeArgsMap)
It returns an Array with the first element set
|
private Collection<? super Object> |
resolveCollectionType(Class<?> collectionType)
Given a collection type it returns an instance
|
private Collection<? super Object> |
resolveCollectionValueWhenCollectionIsPojoAttribute(Class<?> pojoClass,
Class<?> collectionType,
String attributeName,
List<Annotation> annotations,
Map<String,Type> typeArgsMap,
Type... genericTypeArgs)
It returns a collection of some sort with some data in it.
|
private Class<?> |
resolveGenericParameter(Type paramType,
Map<String,Type> typeArgsMap,
AtomicReference<Type[]> methodGenericTypeArgs)
It resolves generic parameter type
|
private Map<? super Object,? super Object> |
resolveMapType(Class<?> attributeType)
It manufactures and returns a default instance for each map type
|
private Map<? super Object,? super Object> |
resolveMapValueWhenMapIsPojoAttribute(Class<?> pojoClass,
Class<?> attributeType,
String attributeName,
List<Annotation> annotations,
Map<String,Type> typeArgsMap,
Type... genericTypeArgs)
It manufactures and returns a Map with at least one element in it
|
private <T> T |
resolvePojoWithoutSetters(Class<T> pojoClass,
int depth,
Type... genericTypeArgs)
It creates and returns an instance of the given class if at least one of
its constructors has been annotated with
PodamConstructor |
private Object |
resolvePrimitiveValue(Class<?> primitiveClass,
List<Annotation> annotations,
AttributeMetadata attributeMetadata)
It resolves and returns the primitive value depending on the type
|
private String |
resolveStringValue(List<Annotation> annotations,
AttributeMetadata attributeMetadata)
It creates and returns a String value, eventually customised by
annotations
|
private Object |
resolveWrapperValue(Class<?> candidateWrapperClass,
List<Annotation> annotations,
AttributeMetadata attributeMetadata)
It attempts to resolve the given class as a wrapper class and if this is
the case it assigns a random value
|
private 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 Object |
returnAttributeDataStrategyValue(Class<?> attributeType,
AttributeStrategy<?> attributeStrategy)
It retrieves the value for the
PodamStrategyValue annotation with
which the attribute was annotated |
void |
setExcludeAnnotations(List<Class<? extends Annotation>> excludeAnnotations) |
private void |
validateAttributeName(String attributeName)
It validates that the attribute name is not null or empty
|
private final org.apache.log4j.Logger LOG
private final DataProviderStrategy strategy
The default is RandomDataProviderStrategy
.
private List<Class<? extends Annotation>> excludeAnnotations
Annotation
s for attributes that PODAM shouldn't
considerpublic static Comparator<Constructor<?>> ConstructorComparator
We would like to have constructor with less parameters to speed up creation.
public PodamFactoryImpl()
public PodamFactoryImpl(DataProviderStrategy strategy)
strategy
- The strategy to use to fill datapublic <T> T manufacturePojo(Class<T> pojoClass, Type... genericTypeArgs)
This method uses RandomDataProviderStrategy
as the default
implementation.
manufacturePojo
in interface PodamFactory
T
- The type for which a filled instance is requiredpojoClass
- The name of the class for which an instance filled with values
is requiredgenericTypeArgs
- The generic Type arguments for a generic class instancepublic DataProviderStrategy getStrategy()
getStrategy
in interface PodamFactory
private Type[] fillTypeArgMap(Map<String,Type> typeArgsMap, TypeVariable<?>[] typeParameters, Type[] genericTypeArgs)
This method places required and provided types for object creation into a map, which will be used for type mapping.
typeArgsMap
- a map to filltypeParameters
- Type arguments needed for a generics object creationgenericTypeArgs
- Type arguments provided for a generics object by callerprivate Object createNewInstanceForClassWithoutConstructors(Class<?> pojoClass, Class<?> clazz, Type... genericTypeArgs) throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException
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 , no-args, factory method (e.g. getInstance()) and it invokes it
clazz
- The class for which a new instance is requiredgenericTypeArgs
- The generic type arguments for the current generic class
instanceIllegalArgumentException
- If an illegal argument was passed to the constructorInstantiationException
- If an exception occurred during instantiationIllegalAccessException
- If security was violated while creating the objectInvocationTargetException
- If an exception occurred while invoking the constructor or
factory methodClassNotFoundException
- If it was not possible to create a class from a stringprivate Class<?> resolveGenericParameter(Type paramType, Map<String,Type> typeArgsMap, AtomicReference<Type[]> methodGenericTypeArgs)
paramType
- The generic parameter typetypeArgsMap
- A map of resolved typesmethodGenericTypeArgs
- Return value posible generic types of the generic parameter
typeprivate Object resolvePrimitiveValue(Class<?> primitiveClass, List<Annotation> annotations, AttributeMetadata attributeMetadata)
primitiveClass
- The primitive type classannotations
- The annotations to consider for this attributeattributeMetadata
- IllegalArgumentException
- If a specific value was set in an annotation but it was not
possible to convert such value in the desired typeprivate Boolean getBooleanValueForAnnotation(List<Annotation> annotations)
annotations
- The collection of annotations for the annotated attributeprivate Byte getByteValueWithinRange(List<Annotation> annotations, AttributeMetadata attributeMetadata)
PodamByteValue
or null
otherwiseannotations
- The list of annotations for this attributeattributeMetadata
- The attribute's metadata, if any, used for customisationIllegalArgumentException
- If the PodamByteValue.numValue()
value has been set
and it is not convertible to a byte typeprivate Short getShortValueWithinRange(List<Annotation> annotations, AttributeMetadata attributeMetadata)
PodamShortValue
or null
otherwiseannotations
- The annotations with which the attribute was annotatedattributeMetadata
- The attribute's metadata, if any, used for customisationPodamShortValue
or null
otherwiseIllegalArgumentException
- If PodamShortValue.numValue()
was set and its value
could not be converted to a Short typeprivate Character getCharacterValueWithinRange(List<Annotation> annotations, AttributeMetadata attributeMetadata)
Character
valueannotations
- The list of annotations which might customise the return valueattributeMetadata
- The attribute's metadata, if any, used for customisationCharacter
valueprivate Integer getIntegerValueWithinRange(List<Annotation> annotations, AttributeMetadata attributeMetadata)
PodamIntValue
annotation was provided or a random integer if this was not the caseannotations
- The list of annotations for the int attributeattributeMetadata
- The attribute's metadata, if any, used for customisationPodamIntValue
annotation was provided or a random integer if this was not the
caseIllegalArgumentException
- If it was not possible to convert the
PodamIntValue.numValue()
to an Integerprivate Float getFloatValueWithinRange(List<Annotation> annotations, AttributeMetadata attributeMetadata)
PodamFloatValue
annotation was provided or a random float if this was not the caseannotations
- The list of annotations for the int attributeattributeMetadata
- The attribute's metadata, if any, used for customisationPodamFloatValue
annotation was provided or a random float if this was not the
caseIllegalArgumentException
- If PodamFloatValue.numValue()
contained a value not
convertible to a Float typeprivate Double getDoubleValueWithinRange(List<Annotation> annotations, AttributeMetadata attributeMetadata)
Double
valueannotations
- The list of annotations which might customise the return valueattributeMetadata
- The attribute's metadata, if any, used for customisation *Double
valueprivate Long getLongValueWithinRange(List<Annotation> annotations, AttributeMetadata attributeMetadata)
PodamLongValue
annotation was provided or a random long if this was not the caseannotations
- The list of annotations for the int attributeattributeMetadata
- The attribute's metadata, if any, used for customisationPodamLongValue
annotation was provided or a random long if this was not the caseIllegalArgumentException
- If it was not possible to convert
PodamLongValue.numValue()
to a Longprivate Object resolveWrapperValue(Class<?> candidateWrapperClass, List<Annotation> annotations, AttributeMetadata attributeMetadata)
candidateWrapperClass
- The class which might be a wrapper classattributeMetadata
- The attribute's metadata, if any, used for customisationnull
if this is not a wrapper class, otherwise an Object
with the value for the wrapper classprivate <T> T resolvePojoWithoutSetters(Class<T> pojoClass, int depth, Type... genericTypeArgs) throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException
PodamConstructor
T
- The type of the instance to returnpojoClass
- The class of which an instance is requireddepth
- How many instances of the same class have been created so fargenericTypeArgs
- The generic type arguments for the current generic class
instancePodamConstructor
IllegalArgumentException
- If an illegal argument was passed to the constructorInstantiationException
- If an exception occurred during instantiationIllegalAccessException
- If security was violated while creating the objectInvocationTargetException
- If an exception occurred while invoking the constructor or
factory methodClassNotFoundException
- If it was not possible to create a class from a stringprivate <T> T manufacturePojoInternal(Class<T> pojoClass, int depth, Type... genericTypeArgs)
T
- The type for which a filled instance is requiredpojoClass
- The name of the class for which an instance filled with values
is requireddepth
- How many times pojoClass
has been found. This will be
used for reentrant objectsgenericTypeArgs
- The generic type arguments for the current generic class
instancePodamMockeryException
- if a problem occurred while creating a POJO instance or while
setting its stateprivate Object manufactureAttributeValue(Class<?> pojoClass, Class<?> attributeType, List<Annotation> annotations, String attributeName, Type... genericTypeArgs) throws InstantiationException, IllegalAccessException, InvocationTargetException, IllegalArgumentException, ClassNotFoundException
pojoClass
- The POJO class being filled with valuesattributeType
- The type of the attribute for which a value is being
manufacturedannotations
- The annotations for the attribute being consideredattributeName
- The attribute namegenericTypeArgs
- The generic type arguments for the current generic class
instanceInstantiationException
- If an exception occurred during instantiationIllegalAccessException
- If security was violated while creating the objectInvocationTargetException
- If an exception occurred while invoking the constructor or
factory methodClassNotFoundException
- If it was not possible to create a class from a stringIllegalArgumentException
- private Object manufactureAttributeValue(Class<?> pojoClass, Class<?> attributeType, List<Annotation> annotations, String attributeName, Map<String,Type> typeArgsMap, Type... genericTypeArgs) throws InstantiationException, IllegalAccessException, InvocationTargetException, IllegalArgumentException, ClassNotFoundException
pojoClass
- The POJO class being filled with valuesattributeType
- The type of the attribute for which a value is being
manufacturedannotations
- The annotations for the attribute being consideredattributeName
- The attribute nametypeArgsMap
- a map relating the generic class arguments ("genericTypeArgs
- The generic type arguments for the current generic class
instanceInstantiationException
- If an exception occurred during instantiationIllegalAccessException
- If security was violated while creating the objectInvocationTargetException
- If an exception occurred while invoking the constructor or
factory methodClassNotFoundException
- If it was not possible to create a class from a stringIllegalArgumentException
- private String resolveStringValue(List<Annotation> annotations, AttributeMetadata attributeMetadata) throws InstantiationException, IllegalAccessException
annotations
- The list of annotations used to customise the String value, if
anyattributeMetadata
- IllegalAccessException
- If an exception occurred while creating an instance of the
strategyInstantiationException
- If an exception occurred while creating an instance of the
strategyIllegalArgumentException
- If PodamStrategyValue
was specified but the type was
not correct for the attribute being setprivate PodamStrategyValue containsAttributeStrategyAnnotation(List<Annotation> annotations)
PodamStrategyValue
if one was specified, or
null
otherwise.annotations
- The list of annotationstrue
if the list of annotations contains at least one
PodamStrategyValue
annotation.private boolean isWrapper(Class<?> candidateWrapperClass)
true
if this class is a wrapper class, false
otherwisecandidateWrapperClass
- The class to checktrue
if this class is a wrapper class, false
otherwiseprivate List<Annotation> retrieveFieldAnnotations(Class<?> clazz, Method setter)
clazz
- The class containing the annotated attributesetter
- The setter methodNoSuchFieldException
- If the field could not be foundSecurityException
- if a security exception occurredprivate Collection<? super Object> resolveCollectionValueWhenCollectionIsPojoAttribute(Class<?> pojoClass, Class<?> collectionType, String attributeName, List<Annotation> annotations, Map<String,Type> typeArgsMap, Type... genericTypeArgs)
pojoClass
- The POJO being analysedcollectionType
- The type of the attribute being evaluatedannotations
- The set of annotations for the annotated attribute. It might
be emptyattributeName
- The name of the field being settypeArgsMap
- a map relating the generic class arguments ("genericTypeArgs
- The generic type arguments for the current generic class
instancePodamMockeryException
- An exception occurred while resolving the collectionIllegalArgumentException
- If the field name is null or emptyprivate void fillCollection(Class<?> pojoClass, String attributeName, List<Annotation> annotations, Collection<? super Object> collection, Class<?> collectionElementType, Type... genericTypeArgs) throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException
This method has a so-called side effect. It updates the collection passed as argument.
pojoClass
- The POJO where the collection attribute is definedattributeName
- The attribute nameannotations
- The annotations for this attributecollection
- The Collection to be filledcollectionElementType
- The type of the collection elementgenericTypeArgs
- The generic type arguments for the current generic class
instanceInstantiationException
- If an exception occurred during instantiationIllegalAccessException
- If security was violated while creating the objectInvocationTargetException
- If an exception occurred while invoking the constructor or
factory methodClassNotFoundException
- If it was not possible to create a class from a stringprivate Map<? super Object,? super Object> resolveMapValueWhenMapIsPojoAttribute(Class<?> pojoClass, Class<?> attributeType, String attributeName, List<Annotation> annotations, Map<String,Type> typeArgsMap, Type... genericTypeArgs)
pojoClass
- The POJO being initialisedattributeType
- The type of the POJO map attributeattributeName
- The POJO attribute nameannotations
- The annotations specified for this attributetypeArgsMap
- a map relating the generic class arguments ("genericTypeArgs
- The generic type arguments for the current generic class
instanceIllegalArgumentException
- PodamMockeryException
- If an error occurred while creating the Map objectprivate void fillMap(Class<?> pojoClass, String attributeName, List<Annotation> annotations, Map<? super Object,? super Object> mapToBeFilled, Class<?> keyClass, Class<?> elementClass, Type[] keyGenericTypeArgs, Type[] elementGenericTypeArgs) throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException
This method has a so-called side-effect. It updates the Map given as argument.
pojoClass
- The POJO where the Map attribute has been declaredattributeName
- The attribute nameannotations
- The annotations for the attributemapToBeFilled
- The Map to be returnedkeyClass
- The type of the Map keyelementClass
- The type of the Map elementkeyGenericTypeArgs
- The generic type arguments for the current key generic class
instanceelementGenericTypeArgs
- The generic type arguments for the current element generic
class instanceInstantiationException
- If an exception occurred during instantiationIllegalAccessException
- If security was violated while creating the objectInvocationTargetException
- If an exception occurred while invoking the constructor or
factory methodClassNotFoundException
- If it was not possible to create a class from a stringprivate Object getMapKeyOrElementValue(Class<?> pojoClass, String attributeName, List<Annotation> annotations, Class<?> keyOrValueType, PodamCollection collectionAnnotation, AttributeStrategy<?> elementStrategy, Type... genericTypeArgs) throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException
pojoClass
- The class containing the annotated attributeattributeName
- The attribute nameannotations
- The list of annotations for this attributekeyOrValueType
- The Map key / element typecollectionAnnotation
- The PodamCollection
annotationelementStrategy
- The strategy to use to fill the Map key or value elementgenericTypeArgs
- The generic type arguments for the current generic class
instanceInstantiationException
- If an exception occurred during instantiationIllegalAccessException
- If security was violated while creating the objectInvocationTargetException
- If an exception occurred while invoking the constructor or
factory methodIllegalArgumentException
- ClassNotFoundException
private Object resolveArrayElementValue(Class<?> attributeType, List<Annotation> annotations, Class<?> pojoClass, String attributeName, Map<String,Type> typeArgsMap) throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException
attributeType
- The array typeannotations
- The annotations to be consideredpojoClass
- attributeName
- typeArgsMap
- a map relating the generic class arguments ("IllegalArgumentException
- If an illegal argument was passed to the constructorInstantiationException
- If an exception occurred during instantiationIllegalAccessException
- If security was violated while creating the objectInvocationTargetException
- If an exception occurred while invoking the constructor or
factory methodClassNotFoundException
- If it was not possible to create a class from a stringprivate Collection<? super Object> resolveCollectionType(Class<?> collectionType)
collectionType
- The collection type *private Map<? super Object,? super Object> resolveMapType(Class<?> attributeType)
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
attributeType
- The attribute typeprivate void validateAttributeName(String attributeName)
attributeName
- The attribute to be validatedIllegalArgumentException
- If the attribute name is null or emptyprivate Object[] getParameterValuesForConstructor(Constructor<?> constructor, Class<?> pojoClass, Type... genericTypeArgs) throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException
constructor
- The constructor for which parameter values are requiredpojoClass
- The POJO class containing the constructorgenericTypeArgs
- The generic type arguments for the current generic class
instanceIllegalArgumentException
- If an illegal argument was passed to the constructorInstantiationException
- If an exception occurred during instantiationIllegalAccessException
- If security was violated while creating the objectInvocationTargetException
- If an exception occurred while invoking the constructor or
factory methodClassNotFoundException
- If it was not possible to create a class from a stringprivate Type[] mergeTypeArrays(Type[] original, Type[] extra)
original
- The main arrayextra
- The additional array, optionally may be nullprivate Object returnAttributeDataStrategyValue(Class<?> attributeType, AttributeStrategy<?> attributeStrategy) throws InstantiationException, IllegalAccessException
PodamStrategyValue
annotation with
which the attribute was annotatedattributeType
- The attribute type, used for type checkingattributeStrategy
- The AttributeStrategy
to usePodamStrategyValue
annotation with
which the attribute was annotatedInstantiationException
- If an exception occurred while creating an instance of the
strategy contained within the PodamStrategyValue
annotationIllegalAccessException
- If an exception occurred while creating an instance of the
strategy contained within the PodamStrategyValue
annotationIllegalArgumentException
- If the type of the data strategy defined for the
PodamStrategyValue
annotation is not assignable to
the annotated attribute. This de facto guarantees type
safety.public List<Class<? extends Annotation>> getExcludeAnnotations()
public void setExcludeAnnotations(List<Class<? extends Annotation>> excludeAnnotations)
excludeAnnotations
- the excludeAnnotations to setCopyright © 2013. All rights reserved.