[LANG-1612] Fix flaky tests in FieldUtilsTest.

This commit is contained in:
Gary Gregory 2020-11-15 12:24:13 -05:00
parent 3bda8be036
commit 08d1f66236
2 changed files with 14 additions and 7 deletions

View File

@ -59,6 +59,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="LANG-1541" type="fix" dev="ggregory" due-to="Arturo Bernal, Gary Gregory">ArrayUtils.contains() and indexOf() fails to handle Double.NaN #647.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix potential NPE in TypeUtils.isAssignable(Type, ParameterizedType, Map, Type>).</action>
<action issue="LANG-1420" type="fix" dev="ggregory" due-to="Gordon Fraser, Rostislav Krasny, Arturo Bernal, Gary Gregory">TypeUtils.isAssignable returns wrong result for GenericArrayType and ParameterizedType, #643.</action>
<action issue="LANG-1612" type="fix" dev="ggregory" due-to="XinT, Gary Gregory">testGetAllFields and testGetFieldsWithAnnotation sometimes fail.</action>
<!-- ADDS -->
<action type="add" dev="ggregory" due-to="Gary Gregory">Add BooleanUtils.booleanValues().</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add BooleanUtils.primitiveValues().</action>

View File

@ -37,6 +37,7 @@ import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.JavaVersion;
import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.lang3.compare.ObjectToStringComparator;
import org.apache.commons.lang3.reflect.testbed.Ambig;
import org.apache.commons.lang3.reflect.testbed.Annotated;
import org.apache.commons.lang3.reflect.testbed.Foo;
@ -165,10 +166,10 @@ public class FieldUtilsTest {
@Test
public void testGetAllFields() {
assertArrayEquals(new Field[0], FieldUtils.getAllFields(Object.class));
final Field[] fieldsNumber = Number.class.getDeclaredFields();
assertArrayEquals(fieldsNumber, FieldUtils.getAllFields(Number.class));
final Field[] fieldsNumber = sort(Number.class.getDeclaredFields());
assertArrayEquals(fieldsNumber, sort(FieldUtils.getAllFields(Number.class)));
final Field[] fieldsInteger = Integer.class.getDeclaredFields();
assertArrayEquals(ArrayUtils.addAll(fieldsInteger, fieldsNumber), FieldUtils.getAllFields(Integer.class));
assertArrayEquals(sort(ArrayUtils.addAll(fieldsInteger, fieldsNumber)), sort(FieldUtils.getAllFields(Integer.class)));
final Field[] allFields = FieldUtils.getAllFields(PublicChild.class);
// Under Jacoco,0.8.1 and Java 10, the field count is 7.
int expected = 5;
@ -180,6 +181,11 @@ public class FieldUtilsTest {
assertEquals(expected, allFields.length, Arrays.toString(allFields));
}
private Field[] sort(Field[] fields) {
// Field does not implement Comparable, so we use a KISS solution here.
return ArrayUtils.sort(fields, ObjectToStringComparator.INSTANCE);
}
@Test
public void testGetAllFieldsList() {
assertEquals(0, FieldUtils.getAllFieldsList(Object.class).size());
@ -204,11 +210,11 @@ public class FieldUtilsTest {
@Test
public void testGetFieldsWithAnnotation() throws NoSuchFieldException {
assertArrayEquals(new Field[0], FieldUtils.getFieldsWithAnnotation(Object.class, Annotated.class));
final Field[] annotatedFields = new Field[]{
final Field[] annotatedFields = sort(new Field[] {
FieldUtilsTest.class.getDeclaredField("publicChild"),
FieldUtilsTest.class.getDeclaredField("privatelyShadowedChild")
};
assertArrayEquals(annotatedFields, FieldUtils.getFieldsWithAnnotation(FieldUtilsTest.class, Annotated.class));
FieldUtilsTest.class.getDeclaredField("privatelyShadowedChild") });
assertArrayEquals(annotatedFields,
sort(FieldUtils.getFieldsWithAnnotation(FieldUtilsTest.class, Annotated.class)));
}
@Test