From b60340b102ac1858c1527faab4c848561a860898 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 11 Feb 2022 15:33:14 +0100 Subject: [PATCH] Simplify tests for synthesized annotation toString() See gh-28015 --- .../annotation/MergedAnnotationsTests.java | 79 +++++++------------ 1 file changed, 30 insertions(+), 49 deletions(-) diff --git a/spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java b/spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java index acd8fc9abaf..02ec8f0980e 100644 --- a/spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java +++ b/spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java @@ -38,7 +38,6 @@ import java.util.stream.Stream; import javax.annotation.Resource; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.JRE; import org.springframework.core.Ordered; import org.springframework.core.annotation.MergedAnnotation.Adapt; @@ -1872,60 +1871,42 @@ class MergedAnnotationsTests { assertThat(webMappingWithAliases.toString()).isNotEqualTo(synthesizedWebMapping1.toString()); - if (JRE.currentVersion().ordinal() > JRE.JAVA_8.ordinal()) { - // The unsynthesized annotation for handleMappedWithSamePathAndValueAttributes() - // should produce the same toString() results as synthesized annotations for - // handleMappedWithPathAttribute() on Java 9 or higher - assertToStringForWebMappingWithPathAndValue(webMappingWithPathAndValue); - } + // The unsynthesized annotation for handleMappedWithSamePathAndValueAttributes() + // should produce almost the same toString() results as synthesized annotations for + // handleMappedWithPathAttribute() on Java 9 or higher; however, due to multiple changes + // in the JDK's toString() implementation for annotations in JDK 9, 14, and 19, + // we do not test the JDK implementation. + // assertToStringForWebMappingWithPathAndValue(webMappingWithPathAndValue); + assertToStringForWebMappingWithPathAndValue(synthesizedWebMapping1); assertToStringForWebMappingWithPathAndValue(synthesizedWebMapping2); } private void assertToStringForWebMappingWithPathAndValue(RequestMapping webMapping) { - String string = webMapping.toString(); - - // Formatting common to Spring and JDK 9+ - assertThat(string) - .contains("value={\"/test\"}", "path={\"/test\"}", "name=\"bar\"", "ch='X'", "chars={'X'}") + assertThat(webMapping.toString()) + .startsWith("@org.springframework.core.annotation.MergedAnnotationsTests.RequestMapping(") + .contains( + // Strings + "value={\"/test\"}", "path={\"/test\"}", "name=\"bar\"", + // Characters + "ch='X'", "chars={'X'}", + // Enums + "method={GET, POST}", + // Classes + "clazz=org.springframework.core.annotation.MergedAnnotationsTests.RequestMethod.class", + "classes={int[][].class, org.springframework.core.annotation.MergedAnnotationsTests.RequestMethod[].class}", + // Bytes + "byteValue=(byte) 0xFF", "bytes={(byte) 0xFF}", + // Shorts + "shortValue=9876", "shorts={9876}", + // Longs + "longValue=42L", "longs={42L}", + // Floats + "floatValue=3.14f", "floats={3.14f}", + // Doubles + "doubleValue=99.999d", "doubles={99.999d}" + ) .endsWith(")"); - - if (webMapping instanceof SynthesizedAnnotation) { - assertThat(string).as("Spring formatting") - .startsWith("@org.springframework.core.annotation.MergedAnnotationsTests.RequestMapping(") - .contains("method={GET, POST}", - "clazz=org.springframework.core.annotation.MergedAnnotationsTests.RequestMethod.class", - "classes={int[][].class, org.springframework.core.annotation.MergedAnnotationsTests.RequestMethod[].class}", - "byteValue=(byte) 0xFF", "bytes={(byte) 0xFF}", - "shortValue=9876", "shorts={9876}", - "longValue=42L", "longs={42L}", - "floatValue=3.14f", "floats={3.14f}", - "doubleValue=99.999d", "doubles={99.999d}" - ); - } - else { - assertThat(string).as("JDK 9-18 formatting") - .startsWith("@org.springframework.core.annotation.MergedAnnotationsTests$RequestMapping(") - .contains("method={method: get, method: post}", - "clazz=org.springframework.core.annotation.MergedAnnotationsTests$RequestMethod.class", - "classes={int[][].class, org.springframework.core.annotation.MergedAnnotationsTests$RequestMethod[].class}", - "shortValue=9876", "shorts={9876}", - "floatValue=3.14f", "floats={3.14f}", - "doubleValue=99.999", "doubles={99.999}" - ); - if (JRE.currentVersion().ordinal() < JRE.JAVA_14.ordinal()) { - assertThat(string).as("JDK 9-13 formatting") - .contains("longValue=42", "longs={42}", - "byteValue=-1", "bytes={-1}" - ); - } - else { - assertThat(string).as("JDK 14+ formatting") - .contains("longValue=42L", "longs={42L}", - "byteValue=(byte)0xff", "bytes={(byte)0xff}" - ); - } - } } @Test