Skip to content

Commit 5053ee3

Browse files
committed
Add alternative mapping from attributes to attribute strategies
1 parent a7929fd commit 5053ee3

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

src/main/java/uk/co/jemos/podam/api/AbstractRandomDataProviderStrategy.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import java.lang.annotation.Annotation;
3030
import java.lang.reflect.Constructor;
31+
import java.lang.reflect.Field;
3132
import java.lang.reflect.Method;
3233
import java.lang.reflect.Type;
3334
import java.util.ArrayDeque;
@@ -108,6 +109,12 @@ public abstract class AbstractRandomDataProviderStrategy implements RandomDataPr
108109
private final Map<Class<? extends Annotation>, AttributeStrategy<?>> attributeStrategies
109110
= new ConcurrentHashMap<Class<? extends Annotation>, AttributeStrategy<?>>();
110111

112+
/**
113+
* Mapping between attributes and attribute strategies
114+
*/
115+
private final Map<Class<?>, Map<String,AttributeStrategy<?>>> attributeClassStrategies
116+
= new ConcurrentHashMap<Class<?>, Map<String,AttributeStrategy<?>>>();
117+
111118
/** The constructor comparator */
112119
private AbstractConstructorComparator constructorHeavyComparator =
113120
ConstructorHeavyFirstComparator.INSTANCE;
@@ -546,6 +553,59 @@ public AttributeStrategy<?> getStrategyForAnnotation(
546553

547554
}
548555

556+
/**
557+
* {@inheritDoc}
558+
*/
559+
@Override
560+
public RandomDataProviderStrategy addOrReplaceAttributeStrategy(
561+
final Class<?> type, final String attributeName,
562+
final AttributeStrategy<?> attributeStrategy) {
563+
564+
Map<String,AttributeStrategy<?>> classStrategies = attributeClassStrategies.get(type);
565+
if (null == classStrategies) {
566+
classStrategies = new ConcurrentHashMap<String,AttributeStrategy<?>>();
567+
attributeClassStrategies.put(type, classStrategies);
568+
}
569+
classStrategies.put(attributeName, attributeStrategy);
570+
571+
return this;
572+
}
573+
574+
/**
575+
* {@inheritDoc}
576+
*/
577+
@Override
578+
public RandomDataProviderStrategy removeAttributeStrategy(
579+
final Class<?> type, String attributeName) {
580+
581+
Map<String,AttributeStrategy<?>> classStrategies = attributeClassStrategies.get(type);
582+
if (null != classStrategies) {
583+
classStrategies.remove(attributeName);
584+
}
585+
586+
return this;
587+
}
588+
589+
/**
590+
* {@inheritDoc}
591+
*/
592+
@Override
593+
public AttributeStrategy<?> getStrategyForAttribute(
594+
final ClassAttribute attribute) {
595+
596+
AttributeStrategy<?> attributeStrategy = null;
597+
Field field = attribute.getAttribute();
598+
if (null != field) {
599+
Class<?> type = field.getDeclaringClass();
600+
Map<String,AttributeStrategy<?>> classStrategies = attributeClassStrategies.get(type);
601+
if (null != classStrategies) {
602+
attributeStrategy = classStrategies.get(attribute.getName());
603+
}
604+
}
605+
return attributeStrategy;
606+
607+
}
608+
549609
/**
550610
* {@inheritDoc}
551611
*/

src/main/java/uk/co/jemos/podam/api/DataProviderStrategy.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,46 @@ public enum Order {
262262
*/
263263
AttributeStrategy<?> getStrategyForAnnotation(Class<? extends Annotation> annotationClass);
264264

265+
/**
266+
* Finds attribute strategies for attribute.
267+
* <p>
268+
* Searches for mapping between class attribute and attribute strategies,
269+
* which will be used then for populating fields or constructor parameters.
270+
* </p>
271+
*
272+
* @param attribute
273+
* attribute to be manufactured
274+
* @return attribute strategy associated with given attribute
275+
*/
276+
AttributeStrategy<?> getStrategyForAttribute(ClassAttribute attribute);
277+
278+
/**
279+
* Registers @AttributeStrategy implementation, which will be used to
280+
* instantiate objects of a specified type. Use this to alter factory
281+
* behaviour.
282+
*
283+
* @param type
284+
* the specific class type the specified manufacturer
285+
* will instantiate.
286+
* @param attributeName
287+
* attribute name to use attributeStrategy for
288+
* @param attributeStrategy
289+
* attribute strategy to be registered
290+
* @return itself
291+
*/
292+
DataProviderStrategy addOrReplaceAttributeStrategy(
293+
Class<?> type, String attributeName, AttributeStrategy<?> attributeStrategy);
294+
295+
/**
296+
* Removes @AttributeStrategy implementation from for the specific attribute
297+
*
298+
* @param type
299+
* the specific class type the specified manufacturer
300+
* will instantiate.
301+
* @param attributeName
302+
* attribute name to use attributeStrategy for
303+
* @return itself
304+
*/
305+
DataProviderStrategy removeAttributeStrategy(
306+
Class<?> type, String attributeName);
265307
}

src/main/java/uk/co/jemos/podam/api/PodamFactoryImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,9 @@ private <T> boolean populateReadWriteField(T pojo, ClassAttribute attribute,
803803

804804
AttributeStrategy<?> attributeStrategy
805805
= TypeManufacturerUtil.findAttributeStrategy(strategy, pojoAttributeAnnotations, attributeType);
806+
if (null == attributeStrategy) {
807+
attributeStrategy = strategy.getStrategyForAttribute(attribute);
808+
}
806809
Object setterArg = null;
807810
if (null != attributeStrategy) {
808811

0 commit comments

Comments
 (0)