Skip to content

Commit 7184ab8

Browse files
dpezzoli-partecNylle
authored andcommitted
Collect annotations also on getter and setter methods
1 parent fea00d7 commit 7184ab8

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

src/main/java/com/github/nylle/javafixture/Reflector.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package com.github.nylle.javafixture;
22

3+
import java.beans.IntrospectionException;
4+
import java.beans.Introspector;
5+
import java.lang.annotation.Annotation;
36
import java.lang.reflect.Field;
47
import java.lang.reflect.InaccessibleObjectException;
58
import java.lang.reflect.Modifier;
9+
import java.util.Arrays;
10+
import java.util.Objects;
611
import java.util.Optional;
712
import java.util.stream.Stream;
813

@@ -64,6 +69,26 @@ private Stream<Field> getDeclaredFields(Class<?> type) {
6469
.orElse(Stream.of()));
6570
}
6671

72+
public Annotation[] getFieldAnnotations(Field field) {
73+
try {
74+
return getFieldAnnotations(field, clazz).toArray(Annotation[]::new);
75+
} catch (IntrospectionException e) {
76+
return field.getAnnotations();
77+
}
78+
}
79+
80+
public Stream<Annotation> getFieldAnnotations(Field field, Class<?> type) throws IntrospectionException {
81+
82+
return Stream.concat(Arrays.stream(Introspector.getBeanInfo(type).getPropertyDescriptors())
83+
.filter(property -> !Modifier.isStatic(field.getModifiers()))
84+
.filter(property -> property.getName().equals(field.getName()))
85+
.flatMap(propertyDescriptor -> Stream.of(propertyDescriptor.getReadMethod(), propertyDescriptor.getWriteMethod())
86+
.filter(Objects::nonNull)
87+
.flatMap(method -> Stream.of(method.getAnnotations()))
88+
), Arrays.stream(field.getAnnotations()));
89+
}
90+
91+
6792
public void setField(Field field, Object value) {
6893
try {
6994
field.setAccessible(true);

src/main/java/com/github/nylle/javafixture/specimen/ObjectSpecimen.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,16 @@ private T populate(CustomizationContext customizationContext) {
6161
var result = context.cached(type, instanceFactory.instantiate(type));
6262
var reflector = new Reflector<>(result).validateCustomization(customizationContext, type);
6363
try {
64+
6465
reflector.getDeclaredFields()
6566
.filter(field -> !customizationContext.getIgnoredFields().contains(field.getName()))
6667
.forEach(field -> reflector.setField(field,
6768
customizationContext.getCustomFields().getOrDefault(
6869
field.getName(),
6970
Map.<String, ISpecimen<?>>of().getOrDefault(
7071
field.getGenericType().getTypeName(),
71-
specimenFactory.build(SpecimenType.fromClass(field.getGenericType()))).create(new CustomizationContext(List.of(), Map.of(), false), field.getAnnotations()))));
72+
specimenFactory.build(SpecimenType.fromClass(field.getGenericType())))
73+
.create(new CustomizationContext(List.of(), Map.of(), false), reflector.getFieldAnnotations(field)))));
7274
} catch (SpecimenException ex) {
7375
return context.overwrite(type, instanceFactory.construct(type, customizationContext));
7476
}

src/test/java/com/github/nylle/javafixture/FixtureWithValidationTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.github.nylle.javafixture.annotations.fixture.TestWithFixture;
44
import com.github.nylle.javafixture.testobjects.TestObjectWithJakartaValidationAnnotations;
5+
import com.github.nylle.javafixture.testobjects.TestObjectWithJakartaValidationAnnotationsOnMethod;
56
import com.github.nylle.javafixture.testobjects.TestObjectWithJavaxValidationAnnotations;
67
import org.junit.jupiter.api.Test;
78

@@ -29,6 +30,18 @@ void jakartaSizeAnnotationIsSupported() {
2930
assertThat(sut.getWithMaxAnnotation().length()).isLessThanOrEqualTo(100);
3031
assertThat(sut.getWithMinMaxAnnotation().length()).isBetween(3, 6);
3132
assertThat(sut.getWithColumnLengthAnnotation().length()).isBetween(0, 5);
33+
assertThat(sut.getWithColumnLengthAnnotationAndMaxAnnotation().length()).isBetween(0, 5);
34+
}
35+
36+
@Test
37+
void jakartaSizeAnnotationOnMethodIsSupported() {
38+
var sut = new Fixture().create(TestObjectWithJakartaValidationAnnotationsOnMethod.class);
39+
40+
assertThat(sut.getWithMinAnnotation().length()).isGreaterThanOrEqualTo(5);
41+
assertThat(sut.getWithMaxAnnotation().length()).isLessThanOrEqualTo(100);
42+
assertThat(sut.getWithMinMaxAnnotation().length()).isBetween(3, 6);
43+
assertThat(sut.getWithColumnLengthAnnotation().length()).isBetween(0, 5);
44+
assertThat(sut.getWithMaxAnnotationAndColumnLengthAnnotation().length()).isBetween(0, 5);
3245
}
3346

3447
@Test
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.github.nylle.javafixture.testobjects;
2+
3+
4+
import jakarta.persistence.Column;
5+
import jakarta.validation.constraints.Size;
6+
7+
public class TestObjectWithJakartaValidationAnnotationsOnMethod {
8+
9+
private String withMinMaxAnnotation;
10+
11+
12+
private String withMinAnnotation;
13+
14+
15+
private String withMaxAnnotation;
16+
17+
18+
private String withColumnLengthAnnotation;
19+
20+
21+
private String withMaxAnnotationAndColumnLengthAnnotation;
22+
23+
@Size(min = 3, max = 6)
24+
public String getWithMinMaxAnnotation() {
25+
return withMinMaxAnnotation;
26+
}
27+
@Size(min = 5)
28+
public String getWithMinAnnotation() {
29+
return withMinAnnotation;
30+
}
31+
@Size(max = 100)
32+
public String getWithMaxAnnotation() {
33+
return withMaxAnnotation;
34+
}
35+
@Column(length = 5)
36+
public String getWithColumnLengthAnnotation() {
37+
return withColumnLengthAnnotation;
38+
}
39+
@Size(max = 100)
40+
@Column(length = 5)
41+
public String getWithMaxAnnotationAndColumnLengthAnnotation() {
42+
return withMaxAnnotationAndColumnLengthAnnotation;
43+
}
44+
}

0 commit comments

Comments
 (0)