|
1 | 1 | package com.github.nylle.javafixture; |
2 | 2 |
|
| 3 | +import com.github.nylle.javafixture.testobjects.TestObjectWithJakartaValidationAnnotations; |
| 4 | +import com.github.nylle.javafixture.testobjects.TestObjectWithJakartaValidationAnnotationsOnMethod; |
3 | 5 | import com.github.nylle.javafixture.testobjects.inheritance.GenericChild; |
4 | 6 | import org.junit.jupiter.api.DisplayName; |
5 | 7 | import org.junit.jupiter.api.Nested; |
6 | 8 | import org.junit.jupiter.api.Test; |
7 | 9 | import org.mockito.Mockito; |
8 | 10 |
|
| 11 | +import jakarta.validation.constraints.Size; |
| 12 | + |
| 13 | +import java.beans.IntrospectionException; |
9 | 14 | import java.lang.reflect.Field; |
10 | 15 | import java.lang.reflect.InaccessibleObjectException; |
| 16 | +import java.util.Arrays; |
11 | 17 | import java.util.List; |
12 | 18 | import java.util.Map; |
| 19 | +import java.util.stream.Collectors; |
13 | 20 |
|
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
14 | 22 | import static org.assertj.core.api.Assertions.assertThatCode; |
15 | 23 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
16 | 24 | import static org.mockito.ArgumentMatchers.any; |
17 | 25 | import static org.mockito.Mockito.doThrow; |
| 26 | +import static org.mockito.Mockito.mock; |
| 27 | +import static org.mockito.Mockito.when; |
18 | 28 |
|
19 | 29 | class ReflectorTest { |
20 | 30 |
|
@@ -120,4 +130,46 @@ void catchInaccessibleObjectException() { |
120 | 130 | } |
121 | 131 | } |
122 | 132 |
|
| 133 | + @Nested |
| 134 | + class GetFieldAnnotations { |
| 135 | + |
| 136 | + @Test |
| 137 | + void returnsSizeAnnotationOnPrivateField() { |
| 138 | + var sut = new Reflector<>(new TestObjectWithJakartaValidationAnnotations()); |
| 139 | + |
| 140 | + var field = sut.getDeclaredFields().filter(x -> x.getName().equals("withMinMaxAnnotation")).findFirst().get(); |
| 141 | + |
| 142 | + var actual = Arrays.stream(sut.getFieldAnnotations(field)).collect(Collectors.toList()); |
| 143 | + |
| 144 | + assertThat(actual).hasSize(1); |
| 145 | + assertThat(actual.get(0).annotationType()).isEqualTo(Size.class); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + void returnsSizeAnnotationOnGetter() { |
| 150 | + var sut = new Reflector<>(new TestObjectWithJakartaValidationAnnotationsOnMethod()); |
| 151 | + |
| 152 | + var field = sut.getDeclaredFields().filter(x -> x.getName().equals("withMinMaxAnnotation")).findFirst().get(); |
| 153 | + |
| 154 | + var actual = Arrays.stream(sut.getFieldAnnotations(field)).collect(Collectors.toList()); |
| 155 | + |
| 156 | + assertThat(actual).hasSize(1); |
| 157 | + assertThat(actual.get(0).annotationType()).isEqualTo(Size.class); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + void returnsSizeAnnotationOnFieldIfGetterThrowsIntrospectionException() { |
| 162 | + var sut = new Reflector<>(new TestObjectWithJakartaValidationAnnotations()); |
| 163 | + var expected = sut.getDeclaredFields().filter(x -> x.getName().equals("withMinMaxAnnotation")).findFirst().get().getAnnotations(); |
| 164 | + |
| 165 | + var throwingField = mock(Field.class); |
| 166 | + when(throwingField.getModifiers()).thenAnswer(x -> { throw new IntrospectionException("expected for test"); }); |
| 167 | + when(throwingField.getAnnotations()).thenReturn(expected); |
| 168 | + |
| 169 | + var actual = Arrays.stream(sut.getFieldAnnotations(throwingField)).collect(Collectors.toList()); |
| 170 | + |
| 171 | + assertThat(actual).hasSize(1); |
| 172 | + assertThat(actual.get(0).annotationType()).isEqualTo(Size.class); |
| 173 | + } |
| 174 | + } |
123 | 175 | } |
0 commit comments