forked from mtedone/podam
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass selected annotation to AttributeStrategy constructor to allow cu…
…stomization for Issue mtedone#237
- Loading branch information
Showing
4 changed files
with
156 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,7 @@ public abstract class TypeManufacturerUtil { | |
* @param strategy | ||
* The data provider strategy | ||
* @param annotations | ||
* The list of annotations | ||
* The list of annotations, irrelevant annotations will be removed | ||
* @param attributeType | ||
* Type of attribute expected to be returned | ||
* @return {@link AttributeStrategy}, if {@link PodamStrategyValue} or bean | ||
|
@@ -47,13 +47,18 @@ public abstract class TypeManufacturerUtil { | |
* if attribute strategy cannot be instantiated | ||
* @throws InstantiationException | ||
* if attribute strategy cannot be instantiated | ||
* @throws SecurityException | ||
* if access security is violated | ||
* @throws InvocationTargetException | ||
* if invocation failed | ||
* @throws IllegalArgumentException | ||
* if illegal argument provided to a constructor | ||
*/ | ||
public static AttributeStrategy<?> findAttributeStrategy(DataProviderStrategy strategy, | ||
List<Annotation> annotations, Class<?> attributeType) | ||
throws InstantiationException, IllegalAccessException { | ||
throws InstantiationException, IllegalAccessException, SecurityException, IllegalArgumentException, InvocationTargetException { | ||
|
||
List<Annotation> localAnnotations = new ArrayList<Annotation>(annotations); | ||
Iterator<Annotation> iter = localAnnotations.iterator(); | ||
Iterator<Annotation> iter = annotations.iterator(); | ||
while (iter.hasNext()) { | ||
Annotation annotation = iter.next(); | ||
if (annotation instanceof PodamStrategyValue) { | ||
|
@@ -79,7 +84,13 @@ public static AttributeStrategy<?> findAttributeStrategy(DataProviderStrategy st | |
|
||
Class<AttributeStrategy<?>> attrStrategyClass; | ||
if ((attrStrategyClass = strategy.getStrategyForAnnotation(annotationClass)) != null) { | ||
return attrStrategyClass.newInstance(); | ||
Constructor<AttributeStrategy<?>> ctor; | ||
try { | ||
ctor = attrStrategyClass.getConstructor(Annotation.class); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
daivanov
Author
Owner
|
||
return ctor.newInstance(annotation); | ||
} catch(NoSuchMethodException e) { | ||
return attrStrategyClass.newInstance(); | ||
} | ||
} | ||
|
||
if (annotation.annotationType().getAnnotation(Constraint.class) != null) { | ||
|
@@ -99,12 +110,12 @@ public static AttributeStrategy<?> findAttributeStrategy(DataProviderStrategy st | |
} | ||
|
||
AttributeStrategy<?> retValue = null; | ||
if (!localAnnotations.isEmpty() | ||
if (!annotations.isEmpty() | ||
&& !Collection.class.isAssignableFrom(attributeType) | ||
&& !Map.class.isAssignableFrom(attributeType) | ||
&& !attributeType.isArray()) { | ||
|
||
retValue = new BeanValidationStrategy(localAnnotations, attributeType); | ||
retValue = new BeanValidationStrategy(annotations, attributeType); | ||
} | ||
|
||
return retValue; | ||
|
38 changes: 38 additions & 0 deletions
38
src/test/java/uk/co/jemos/podam/test/dto/ValidatedPatternPojo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* | ||
*/ | ||
package uk.co.jemos.podam.test.dto; | ||
|
||
|
||
import javax.validation.constraints.Pattern; | ||
|
||
/** | ||
* POJO to test bean validation API | ||
* | ||
* @author daivanov | ||
* | ||
*/ | ||
public class ValidatedPatternPojo { | ||
|
||
@Pattern(regexp = "^[0-9]{1,45}$") | ||
private String number; | ||
|
||
@Pattern(regexp = "^[a-zA-Z0-9_]*$") | ||
private String identifier; | ||
|
||
public String getNumber() { | ||
return number; | ||
} | ||
|
||
public void setNumber(String number) { | ||
this.number = number; | ||
} | ||
|
||
public String getIdentifier() { | ||
return identifier; | ||
} | ||
|
||
public void setIdentifier(String identifier) { | ||
this.identifier = identifier; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/test/java/uk/co/jemos/podam/test/strategies/PatternStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* | ||
*/ | ||
package uk.co.jemos.podam.test.strategies; | ||
|
||
import java.lang.annotation.Annotation; | ||
|
||
import javax.validation.constraints.Pattern; | ||
|
||
import uk.co.jemos.podam.api.PodamUtils; | ||
import uk.co.jemos.podam.common.AttributeStrategy; | ||
|
||
/** | ||
* @author daivanov | ||
* | ||
*/ | ||
public class PatternStrategy implements AttributeStrategy<String> { | ||
|
||
/** validation annotation */ | ||
private Pattern patternAnnotation; | ||
|
||
/** | ||
* Constructor for the strategy | ||
* | ||
* @param annotation | ||
* validation annotation | ||
*/ | ||
public PatternStrategy(Annotation annotation) { | ||
if (annotation instanceof Pattern) { | ||
this.patternAnnotation = (Pattern)annotation; | ||
} else { | ||
throw new IllegalArgumentException(annotation.getClass().getSimpleName() + " is not Pattern"); | ||
} | ||
} | ||
|
||
/** | ||
* Produces valid patterns. | ||
* | ||
* {@inheritDoc} | ||
*/ | ||
public String getValue() { | ||
|
||
StringBuffer sb = new StringBuffer(); | ||
|
||
if ("^[0-9]{1,45}$".equals(patternAnnotation.regexp())) { | ||
|
||
sb.append(Integer.toString(PodamUtils.getIntegerInRange(0, 1000000))); | ||
} else if ("^[a-zA-Z0-9_]*$".equals(patternAnnotation.regexp())) { | ||
|
||
for (int i = 0; i < 2; i++) { | ||
sb.append(PodamUtils.getNiceCharacter()); | ||
} | ||
sb.append(Integer.toString(PodamUtils.getIntegerInRange(0, 10))); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
// ------------------->> Constants | ||
|
||
// ------------------->> Instance / Static variables | ||
|
||
// ------------------->> Constructors | ||
|
||
// ------------------->> Public methods | ||
|
||
// ------------------->> Getters / Setters | ||
|
||
// ------------------->> Private methods | ||
|
||
// ------------------->> equals() / hashcode() / toString() | ||
|
||
// ------------------->> Inner classes | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Why do you rely on constructor injection for the Annotation instance instead of extending the
T getValue()
method of the AttributeStrategy with a parameter:T getValue(Annotation annotation);
? This way it is more explicit and people unfamiliar with the code don't have to ensure that a constructor with a parameter is present, which they will probably only find out through trial and error..