From 08d1f6623678e8cd0b37a9d2d721b95545bcc151 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sun, 15 Nov 2020 12:24:13 -0500 Subject: [PATCH] [LANG-1612] Fix flaky tests in FieldUtilsTest. --- src/changes/changes.xml | 1 + .../commons/lang3/reflect/FieldUtilsTest.java | 20 ++++++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index ef11ac6b9..d933a3b9f 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -59,6 +59,7 @@ The type attribute can be add,update,fix,remove. ArrayUtils.contains() and indexOf() fails to handle Double.NaN #647. Fix potential NPE in TypeUtils.isAssignable(Type, ParameterizedType, Map, Type>). TypeUtils.isAssignable returns wrong result for GenericArrayType and ParameterizedType, #643. + testGetAllFields and testGetFieldsWithAnnotation sometimes fail. Add BooleanUtils.booleanValues(). Add BooleanUtils.primitiveValues(). diff --git a/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java b/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java index f52de064e..15fe8c32c 100644 --- a/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java @@ -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