BAEL-6896: Add test for public static final field lookup. (#14668)

This commit is contained in:
Harry9656 2023-09-09 21:28:24 +02:00 committed by GitHub
parent e6a231d824
commit 4609814e3c
2 changed files with 133 additions and 89 deletions

View File

@ -2,6 +2,7 @@ package com.baeldung.reflection;
public class Employee extends Person { public class Employee extends Person {
public static final String LABEL = "employee";
public int employeeId; public int employeeId;
} }

View File

@ -1,6 +1,7 @@
package com.baeldung.reflection; package com.baeldung.reflection;
import org.junit.Test; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
@ -9,128 +10,170 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.Assert.*; import org.junit.Test;
public class PersonAndEmployeeReflectionUnitTest { public class PersonAndEmployeeReflectionUnitTest {
// Fields names
private static final String LAST_NAME_FIELD = "lastName"; private static final String LAST_NAME_FIELD = "lastName";
private static final String FIRST_NAME_FIELD = "firstName"; private static final String FIRST_NAME_FIELD = "firstName";
private static final String EMPLOYEE_ID_FIELD = "employeeId"; private static final String EMPLOYEE_ID_FIELD = "employeeId";
private static final String EMPLOYEE_TYPE_FIELD = "LABEL";
private static final String MONTH_EMPLOYEE_REWARD_FIELD = "reward"; private static final String MONTH_EMPLOYEE_REWARD_FIELD = "reward";
@Test @Test
public void givenPersonClass_whenGetDeclaredFields_thenTwoFields() { public void givenPersonClass_whenGetDeclaredFields_thenTwoFields() {
// When List<Field> allFields = Arrays.asList(Person.class.getDeclaredFields());
Field[] allFields = Person.class.getDeclaredFields();
// Then assertEquals(2, allFields.size());
assertEquals(2, allFields.length); Field lastName = allFields.stream()
.filter(field -> field.getName()
assertTrue(Arrays.stream(allFields).anyMatch(field -> .equals(LAST_NAME_FIELD))
field.getName().equals(LAST_NAME_FIELD) .findFirst()
&& field.getType().equals(String.class)) .orElseThrow(() -> new RuntimeException("Field not found"));
); assertEquals(String.class, lastName.getType());
assertTrue(Arrays.stream(allFields).anyMatch(field -> Field firstName = allFields.stream()
field.getName().equals(FIRST_NAME_FIELD) .filter(field -> field.getName()
&& field.getType().equals(String.class)) .equals(FIRST_NAME_FIELD))
); .findFirst()
.orElseThrow(() -> new RuntimeException("Field not found"));
assertEquals(String.class, firstName.getType());
} }
@Test @Test
public void givenEmployeeClass_whenGetDeclaredFields_thenOneField() { public void givenEmployeeClass_whenGetDeclaredFields_thenFilterAndReturnStaticField() {
// When List<Field> publicStaticField = Arrays.stream(Employee.class.getDeclaredFields())
Field[] allFields = Employee.class.getDeclaredFields(); .filter(field -> Modifier.isStatic(field.getModifiers()) && Modifier.isPublic(field.getModifiers()) && Modifier.isFinal(field.getModifiers()))
.collect(Collectors.toList());
// Then assertEquals(1, publicStaticField.size());
assertEquals(1, allFields.length); Field employeeTypeField = publicStaticField.get(0);
assertEquals(EMPLOYEE_TYPE_FIELD, employeeTypeField.getName());
assertTrue(Arrays.stream(allFields).anyMatch(field ->
field.getName().equals(EMPLOYEE_ID_FIELD)
&& field.getType().equals(int.class))
);
} }
@Test @Test
public void givenEmployeeClass_whenSuperClassGetDeclaredFields_thenOneField() { public void givenEmployeeClass_whenGetDeclaredFields_thenTwoField() {
// When List<Field> allFields = Arrays.asList(Employee.class.getDeclaredFields());
Field[] allFields = Employee.class.getSuperclass().getDeclaredFields();
// Then assertEquals(2, allFields.size());
assertEquals(2, allFields.length); Field employeeIdField = allFields.stream()
.filter(field -> field.getName()
assertTrue(Arrays.stream(allFields).anyMatch(field -> .equals(EMPLOYEE_ID_FIELD))
field.getName().equals(LAST_NAME_FIELD) .findFirst()
&& field.getType().equals(String.class)) .orElseThrow(() -> new RuntimeException("Field not found"));
); assertEquals(int.class, employeeIdField.getType());
assertTrue(Arrays.stream(allFields).anyMatch(field -> Field employeeTypeField = allFields.stream()
field.getName().equals(FIRST_NAME_FIELD) .filter(field -> field.getName()
&& field.getType().equals(String.class)) .equals(EMPLOYEE_TYPE_FIELD))
); .findFirst()
.orElseThrow(() -> new RuntimeException("Field not found"));
assertEquals(String.class, employeeTypeField.getType());
} }
@Test @Test
public void givenEmployeeClass_whenGetDeclaredFieldsOnBothClasses_thenThreeFields() { public void givenEmployeeClass_whenSuperClassGetDeclaredFields_thenTwoField() {
// When List<Field> allFields = Arrays.asList(Employee.class.getSuperclass()
Field[] personFields = Employee.class.getSuperclass().getDeclaredFields(); .getDeclaredFields());
Field[] employeeFields = Employee.class.getDeclaredFields();
Field[] allFields = new Field[employeeFields.length + personFields.length];
Arrays.setAll(allFields, i -> (i < personFields.length ? personFields[i] : employeeFields[i - personFields.length]));
// Then assertEquals(2, allFields.size());
assertEquals(3, allFields.length); Field lastNameField = allFields.stream()
.filter(field -> field.getName()
.equals(LAST_NAME_FIELD))
.findFirst()
.orElseThrow(() -> new RuntimeException("Field not found"));
assertEquals(String.class, lastNameField.getType());
Field firstNameField = allFields.stream()
.filter(field -> field.getName()
.equals(FIRST_NAME_FIELD))
.findFirst()
.orElseThrow(() -> new RuntimeException("Field not found"));
assertEquals(String.class, firstNameField.getType());
}
assertTrue(Arrays.stream(allFields).anyMatch(field -> @Test
field.getName().equals(LAST_NAME_FIELD) public void givenEmployeeClass_whenGetDeclaredFieldsOnBothClasses_thenFourFields() {
&& field.getType().equals(String.class)) List<Field> personFields = Arrays.asList(Employee.class.getSuperclass()
); .getDeclaredFields());
assertTrue(Arrays.stream(allFields).anyMatch(field -> List<Field> employeeFields = Arrays.asList(Employee.class.getDeclaredFields());
field.getName().equals(FIRST_NAME_FIELD) List<Field> allFields = Stream.concat(personFields.stream(), employeeFields.stream())
&& field.getType().equals(String.class)) .collect(Collectors.toList());
);
assertTrue(Arrays.stream(allFields).anyMatch(field -> assertEquals(4, allFields.size());
field.getName().equals(EMPLOYEE_ID_FIELD) Field lastNameField = allFields.stream()
&& field.getType().equals(int.class)) .filter(field -> field.getName()
); .equals(LAST_NAME_FIELD))
.findFirst()
.orElseThrow(() -> new RuntimeException("Field not found"));
assertEquals(String.class, lastNameField.getType());
Field firstNameField = allFields.stream()
.filter(field -> field.getName()
.equals(FIRST_NAME_FIELD))
.findFirst()
.orElseThrow(() -> new RuntimeException("Field not found"));
assertEquals(String.class, firstNameField.getType());
Field employeeIdField = allFields.stream()
.filter(field -> field.getName()
.equals(EMPLOYEE_ID_FIELD))
.findFirst()
.orElseThrow(() -> new RuntimeException("Field not found"));
assertEquals(int.class, employeeIdField.getType());
Field employeeTypeField = allFields.stream()
.filter(field -> field.getName()
.equals(EMPLOYEE_TYPE_FIELD))
.findFirst()
.orElseThrow(() -> new RuntimeException("Field not found"));
assertEquals(String.class, employeeTypeField.getType());
} }
@Test @Test
public void givenEmployeeClass_whenGetDeclaredFieldsOnEmployeeSuperclassWithModifiersFilter_thenOneFields() { public void givenEmployeeClass_whenGetDeclaredFieldsOnEmployeeSuperclassWithModifiersFilter_thenOneFields() {
// When List<Field> personFields = Arrays.stream(Employee.class.getSuperclass()
List<Field> personFields = Arrays.stream(Employee.class.getSuperclass().getDeclaredFields()) .getDeclaredFields())
.filter(f -> Modifier.isPublic(f.getModifiers()) || Modifier.isProtected(f.getModifiers())) .filter(f -> Modifier.isPublic(f.getModifiers()) || Modifier.isProtected(f.getModifiers()))
.collect(Collectors.toList()); .collect(Collectors.toList());
// Then
assertEquals(1, personFields.size()); assertEquals(1, personFields.size());
Field personField = personFields.stream()
assertTrue(personFields.stream().anyMatch(field -> .filter(field -> field.getName()
field.getName().equals(LAST_NAME_FIELD) .equals(LAST_NAME_FIELD))
&& field.getType().equals(String.class)) .findFirst()
); .orElseThrow(() -> new RuntimeException("Field not found"));
assertEquals(String.class, personField.getType());
} }
@Test @Test
public void givenMonthEmployeeClass_whenGetAllFields_thenThreeFields() { public void givenMonthEmployeeClass_whenGetAllFields_thenFourFields() {
// When
List<Field> allFields = getAllFields(MonthEmployee.class); List<Field> allFields = getAllFields(MonthEmployee.class);
// Then assertEquals(4, allFields.size());
assertEquals(3, allFields.size()); assertFalse(allFields.stream()
.anyMatch(field -> field.getName()
assertTrue(allFields.stream().anyMatch(field -> .equals(FIRST_NAME_FIELD)));
field.getName().equals(LAST_NAME_FIELD) assertEquals(String.class, allFields.stream()
&& field.getType().equals(String.class)) .filter(field -> field.getName()
); .equals(LAST_NAME_FIELD))
assertTrue(allFields.stream().anyMatch(field -> .findFirst()
field.getName().equals(EMPLOYEE_ID_FIELD) .orElseThrow(() -> new RuntimeException("Field not found"))
&& field.getType().equals(int.class)) .getType());
); assertEquals(int.class, allFields.stream()
assertTrue(allFields.stream().anyMatch(field -> .filter(field -> field.getName()
field.getName().equals(MONTH_EMPLOYEE_REWARD_FIELD) .equals(EMPLOYEE_ID_FIELD))
&& field.getType().equals(double.class)) .findFirst()
); .orElseThrow(() -> new RuntimeException("Field not found"))
.getType());
assertEquals(double.class, allFields.stream()
.filter(field -> field.getName()
.equals(MONTH_EMPLOYEE_REWARD_FIELD))
.findFirst()
.orElseThrow(() -> new RuntimeException("Field not found"))
.getType());
assertEquals(String.class, allFields.stream()
.filter(field -> field.getName()
.equals(EMPLOYEE_TYPE_FIELD))
.findFirst()
.orElseThrow(() -> new RuntimeException("Field not found"))
.getType());
} }
public List<Field> getAllFields(Class clazz) { public List<Field> getAllFields(Class clazz) {