Skip to content

Commit

Permalink
Search for annotation mapped to AttributeStrategy using breadth-first…
Browse files Browse the repository at this point in the history
… instead of depth-first for Issue #279
  • Loading branch information
daivanov committed Jul 26, 2020
1 parent e612e42 commit f7464a8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -542,20 +542,7 @@ public RandomDataProviderStrategy removeAttributeStrategy(
public AttributeStrategy<?> getStrategyForAnnotation(
final Class<? extends Annotation> annotationClass) {

AttributeStrategy<?> strategy = attributeStrategies.get(annotationClass);
if (strategy == null) {
for (Class<?> iface : annotationClass.getInterfaces()) {
if (Annotation.class.isAssignableFrom(iface)) {
@SuppressWarnings("unchecked")
Class<? extends Annotation> annotationIface = (Class<? extends Annotation>)iface;
strategy = getStrategyForAnnotation(annotationIface);
if (strategy != null) {
break;
}
}
}
}
return strategy;
return attributeStrategies.get(annotationClass);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public static AttributeStrategy<?> findAttributeStrategy(DataProviderStrategy st
throws InstantiationException, IllegalAccessException, SecurityException, IllegalArgumentException, InvocationTargetException {

List<Annotation> localAnnotations = new ArrayList<Annotation>(annotations);
List<Class<? extends Annotation>> annotationsToCheck = new ArrayList<Class<? extends Annotation>>();
Iterator<Annotation> iter = localAnnotations.iterator();
while (iter.hasNext()) {
Annotation annotation = iter.next();
Expand Down Expand Up @@ -87,6 +88,14 @@ public static AttributeStrategy<?> findAttributeStrategy(DataProviderStrategy st
AttributeStrategy<?> attrStrategy = strategy.getStrategyForAnnotation(annotationClass);
if (null != attrStrategy) {
return attrStrategy;
} else {
for (Class<?> iface : annotationClass.getInterfaces()) {
if (Annotation.class.isAssignableFrom(iface)) {
@SuppressWarnings("unchecked")
Class<? extends Annotation> tmp = (Class<? extends Annotation>) iface;
annotationsToCheck.add(tmp);
}
}
}

if (annotation.annotationType().getAnnotation(Constraint.class) != null) {
Expand All @@ -107,6 +116,23 @@ public static AttributeStrategy<?> findAttributeStrategy(DataProviderStrategy st
}
}

Iterator<Class<? extends Annotation>> iter2 = annotationsToCheck.iterator();
while (iter2.hasNext()) {
Class<? extends Annotation> annotationClass = iter2.next();
AttributeStrategy<?> attrStrategy = strategy.getStrategyForAnnotation(annotationClass);
if (null != attrStrategy) {
return attrStrategy;
} else {
for (Class<?> iface : annotationClass.getInterfaces()) {
if (Annotation.class.isAssignableFrom(iface)) {
@SuppressWarnings("unchecked")
Class<? extends Annotation> tmp = (Class<? extends Annotation>) iface;
annotationsToCheck.add(tmp);
}
}
}
}

AttributeStrategy<?> retValue = null;
if (!localAnnotations.isEmpty()
&& !Collection.class.isAssignableFrom(attributeType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,20 +543,14 @@ public void podamShouldHandlePojosWithAnnotatedFieldsAndCustomGenericAnnotationS
SimplePojoWithMultipleAnnotationsToAttribute pojo = podamInvocationSteps.whenIInvokeTheFactoryForClass(SimplePojoWithMultipleAnnotationsToAttribute.class, podamFactory);
podamValidationSteps.theObjectShouldNotBeNull(pojo);
podamValidationSteps.theCollectionShouldNotBeNullOrEmptyAndShouldHaveExactlyTheExpectedNumberOfElements(
annotationStrategy.getRecordedCalls(), List.class, 5);
annotationStrategy.getRecordedCalls(), List.class, 1);
podamValidationSteps.theCollectionShouldNotBeNullOrEmptyAndShouldHaveExactlyTheExpectedNumberOfElements(
annotationStrategy.getRecordedCalls().get(0), Annotation.class, 1);
podamValidationSteps.theCollectionShouldNotBeNullOrEmptyAndShouldHaveExactlyTheExpectedNumberOfElements(
annotationStrategy.getRecordedCalls().get(1), Annotation.class, 3);
podamValidationSteps.theCollectionShouldNotBeNullOrEmptyAndShouldHaveExactlyTheExpectedNumberOfElements(
annotationStrategy.getRecordedCalls().get(2), Annotation.class, 2);
podamValidationSteps.theCollectionShouldNotBeNullOrEmptyAndShouldHaveExactlyTheExpectedNumberOfElements(
annotationStrategy.getRecordedCalls().get(3), Annotation.class, 3);
podamValidationSteps.theCollectionShouldNotBeNullOrEmptyAndShouldHaveExactlyTheExpectedNumberOfElements(
annotationStrategy.getRecordedCalls().get(4), Annotation.class, 3);
podamValidationSteps.theCollectionShouldNotBeNullOrEmptyAndShouldHaveExactlyTheExpectedNumberOfElements(
basicAnnotationStrategy.getRecordedCalls(), List.class, 1);
basicAnnotationStrategy.getRecordedCalls(), List.class, 2);
podamValidationSteps.theCollectionShouldNotBeNullOrEmptyAndShouldHaveExactlyTheExpectedNumberOfElements(
basicAnnotationStrategy.getRecordedCalls().get(0), Annotation.class, 2);
podamValidationSteps.theCollectionShouldNotBeNullOrEmptyAndShouldHaveExactlyTheExpectedNumberOfElements(
basicAnnotationStrategy.getRecordedCalls().get(1), Annotation.class, 2);
}
}

0 comments on commit f7464a8

Please sign in to comment.