From a307b23f5a306348ef50a8ed361b58ae3f524004 Mon Sep 17 00:00:00 2001 From: Augustine Cui Date: Sat, 23 Oct 2021 18:40:05 -0500 Subject: [PATCH 1/2] Fixed flaky tests due to `class.getMethod` --- .../feign/TypesResolveReturnTypeTest.java | 46 ++++++++++++------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/core/src/test/java/feign/TypesResolveReturnTypeTest.java b/core/src/test/java/feign/TypesResolveReturnTypeTest.java index d322837d1..27fbc5480 100644 --- a/core/src/test/java/feign/TypesResolveReturnTypeTest.java +++ b/core/src/test/java/feign/TypesResolveReturnTypeTest.java @@ -39,6 +39,18 @@ interface OverridingConcreteSimple extends Simple { String get(); } + public Method[] getMethods(Class c) { + Method[] methods = c.getMethods(); + Arrays.sort(methods, new Comparator() { + @Override + public int compare(Method o1, Method o2) { + return (o1.getName() + o1.getGenericReturnType().getTypeName()).compareTo( + (o2.getName() + o2.getGenericReturnType().getTypeName())); + } + }); + return methods; + } + @Test public void simple() { Method[] methods = Simple.class.getMethods(); @@ -159,7 +171,7 @@ public void concreteSimpleClassGenericSecondLevel() { @Test public void overridingConcreteSimpleClassGenericSecondLevel() { - Method[] methods = OverridingConcreteSimpleClassGenericSecondLevel.class.getMethods(); + Method[] methods = getMethods(OverridingConcreteSimpleClassGenericSecondLevel.class); Assertions.assertThat(methods.length).isEqualTo(2); Type resolved = Types.resolve(OverridingConcreteSimpleClassGenericSecondLevel.class, OverridingConcreteSimpleClassGenericSecondLevel.class, methods[0].getGenericReturnType()); @@ -221,7 +233,7 @@ public void concreteSimpleClassMultipleGenericSecondLevel() { @Test public void overridingConcreteSimpleClassMultipleGenericSecondLevel() { - Method[] methods = OverridingConcreteSimpleClassMultipleGenericSecondLevel.class.getMethods(); + Method[] methods = getMethods(OverridingConcreteSimpleClassMultipleGenericSecondLevel.class); Assertions.assertThat(methods.length).isEqualTo(2); Type resolved = Types.resolve(OverridingConcreteSimpleClassMultipleGenericSecondLevel.class, @@ -255,7 +267,7 @@ public void realizingSimpleClassGenericThirdLevel() { @Test public void realizingSimpleClassMultipleGenericThirdLevel() { - Method[] methods = RealizingSimpleClassMultipleGenericThirdLevel.class.getMethods(); + Method[] methods = getMethods(RealizingSimpleClassMultipleGenericThirdLevel.class); Assertions.assertThat(methods.length).isEqualTo(2); Type resolved = Types.resolve(RealizingSimpleClassMultipleGenericThirdLevel.class, RealizingSimpleClassMultipleGenericThirdLevel.class, methods[0].getGenericReturnType()); @@ -269,7 +281,7 @@ public void realizingSimpleClassMultipleGenericThirdLevel() { @Test public void realizingOverridingSimpleClassGenericThirdLevel() { - Method[] methods = RealizingOverridingSimpleClassGenericThirdLevel.class.getMethods(); + Method[] methods = getMethods(RealizingOverridingSimpleClassGenericThirdLevel.class); Assertions.assertThat(methods.length).isEqualTo(2); Type resolved = Types.resolve(RealizingOverridingSimpleClassGenericThirdLevel.class, RealizingOverridingSimpleClassGenericThirdLevel.class, methods[0].getGenericReturnType()); @@ -283,7 +295,7 @@ public void realizingOverridingSimpleClassGenericThirdLevel() { @Test public void realizingOverridingSimpleClassMultipleGenericThirdLevel() { - Method[] methods = RealizingOverridingSimpleClassMultipleGenericThirdLevel.class.getMethods(); + Method[] methods = getMethods(RealizingOverridingSimpleClassMultipleGenericThirdLevel.class); Assertions.assertThat(methods.length).isEqualTo(2); Type resolved = Types.resolve(RealizingOverridingSimpleClassMultipleGenericThirdLevel.class, @@ -354,7 +366,7 @@ public void concreteSimpleClassGenericThirdLevel() { @Test public void OverridingConcreteSimpleClassGenericThirdLevel() { - Method[] methods = OverridingConcreteSimpleClassGenericThirdLevel.class.getMethods(); + Method[] methods = getMethods(OverridingConcreteSimpleClassGenericThirdLevel.class); Assertions.assertThat(methods.length).isEqualTo(2); Type resolved = Types.resolve(OverridingConcreteSimpleClassGenericThirdLevel.class, OverridingConcreteSimpleClassGenericThirdLevel.class, methods[0].getGenericReturnType()); @@ -452,13 +464,13 @@ public void concreteCollectionGenericFourthLevel() { @Test public void overridingConcreteCollectionGenericFourthLevel() { - Method[] methods = OverridingConcreteCollectionGenericFourthLevel.class.getMethods(); + Method[] methods = getMethods(OverridingConcreteCollectionGenericFourthLevel.class); Assertions.assertThat(methods.length).isEqualTo(2); Type resolved = Types.resolve(OverridingConcreteCollectionGenericFourthLevel.class, - OverridingConcreteCollectionGenericFourthLevel.class, methods[0].getGenericReturnType()); + OverridingConcreteCollectionGenericFourthLevel.class, methods[1].getGenericReturnType()); Assertions.assertThat(resolved instanceof ParameterizedType).isTrue(); Type resolved2 = Types.resolve(OverridingConcreteCollectionGenericFourthLevel.class, - OverridingConcreteCollectionGenericFourthLevel.class, methods[1].getGenericReturnType()); + OverridingConcreteCollectionGenericFourthLevel.class, methods[0].getGenericReturnType()); Assertions.assertThat(resolved2).isEqualTo(Object.class); Type resolvedType = Types.resolveReturnType(resolved, resolved2); Assertions.assertThat(resolvedType).isEqualTo(resolved); @@ -466,17 +478,17 @@ public void overridingConcreteCollectionGenericFourthLevel() { @Test public void overrideOverridingConcreteCollectionGenericFourthLevel() { - Method[] methods = OverrideOverridingConcreteCollectionGenericFourthLevel.class.getMethods(); + Method[] methods = getMethods(OverrideOverridingConcreteCollectionGenericFourthLevel.class); Assertions.assertThat(methods.length).isEqualTo(2); Type resolved = Types.resolve(OverrideOverridingConcreteCollectionGenericFourthLevel.class, OverrideOverridingConcreteCollectionGenericFourthLevel.class, - methods[0].getGenericReturnType()); + methods[1].getGenericReturnType()); Assertions.assertThat(resolved instanceof ParameterizedType).isTrue(); Type resolved2 = Types.resolve(OverrideOverridingConcreteCollectionGenericFourthLevel.class, OverrideOverridingConcreteCollectionGenericFourthLevel.class, - methods[1].getGenericReturnType()); + methods[0].getGenericReturnType()); Assertions.assertThat(resolved2).isEqualTo(Object.class); Type resolvedType = Types.resolveReturnType(resolved, resolved2); Assertions.assertThat(resolvedType).isEqualTo(resolved); @@ -505,20 +517,20 @@ public void concreteGenericCollectionGenericFifthLevel() { @Test public void overridingConcreteGenericCollectionGenericFifthLevel() { - Method[] methods = OverridingConcreteGenericCollectionGenericFifthLevel.class.getMethods(); + Method[] methods = getMethods(OverridingConcreteGenericCollectionGenericFifthLevel.class); Assertions.assertThat(methods.length).isEqualTo(2); Type resolved = Types.resolve(OverridingConcreteGenericCollectionGenericFifthLevel.class, OverridingConcreteGenericCollectionGenericFifthLevel.class, - methods[0].getGenericReturnType()); + methods[1].getGenericReturnType()); Assertions.assertThat(resolved instanceof ParameterizedType).isTrue(); Type resolved2 = Types.resolve(OverridingConcreteGenericCollectionGenericFifthLevel.class, OverridingConcreteGenericCollectionGenericFifthLevel.class, - methods[1].getGenericReturnType()); + methods[0].getGenericReturnType()); Assertions.assertThat(resolved2).isEqualTo(Object.class); - Type resolvedType = Types.resolveReturnType(methods[0].getGenericReturnType(), - methods[1].getGenericReturnType()); + Type resolvedType = Types.resolveReturnType(methods[1].getGenericReturnType(), + methods[0].getGenericReturnType()); Assertions.assertThat(resolvedType).isEqualTo(resolved); } From c786c414e37ac1b8e2a96b470a510bc81ec96144 Mon Sep 17 00:00:00 2001 From: Augustine Cui Date: Sun, 24 Oct 2021 21:00:37 -0500 Subject: [PATCH 2/2] Simplify Comparator code --- .../test/java/feign/TypesResolveReturnTypeTest.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/core/src/test/java/feign/TypesResolveReturnTypeTest.java b/core/src/test/java/feign/TypesResolveReturnTypeTest.java index 27fbc5480..7a502e48e 100644 --- a/core/src/test/java/feign/TypesResolveReturnTypeTest.java +++ b/core/src/test/java/feign/TypesResolveReturnTypeTest.java @@ -19,8 +19,10 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; +import java.util.Arrays; import java.util.ArrayList; import java.util.Collection; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -41,13 +43,8 @@ interface OverridingConcreteSimple extends Simple { public Method[] getMethods(Class c) { Method[] methods = c.getMethods(); - Arrays.sort(methods, new Comparator() { - @Override - public int compare(Method o1, Method o2) { - return (o1.getName() + o1.getGenericReturnType().getTypeName()).compareTo( - (o2.getName() + o2.getGenericReturnType().getTypeName())); - } - }); + Arrays.sort(methods, + Comparator.comparing(o -> (o.getName() + o.getGenericReturnType().getTypeName()))); return methods; }