From 9e1ef83669d9c0c44a88ec431984be0838af200d Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Wed, 5 Jun 2024 12:16:12 +0200 Subject: [PATCH] Avoid issues with system line separator in tests See f10caf6aa6a3ec5bfe229bf403a267d2e022f60e --- .../testfixture/codec/AbstractEncoderTests.java | 2 +- .../test/http/MediaTypeAssertTests.java | 14 +++++--------- .../http/codec/json/Jackson2JsonEncoderTests.java | 6 ++++-- .../MappingJackson2HttpMessageConverterTests.java | 8 ++++---- .../ResponseEntityResultHandlerTests.java | 10 ++++------ 5 files changed, 18 insertions(+), 22 deletions(-) diff --git a/spring-core/src/testFixtures/java/org/springframework/core/testfixture/codec/AbstractEncoderTests.java b/spring-core/src/testFixtures/java/org/springframework/core/testfixture/codec/AbstractEncoderTests.java index 8cab9edd3e2..fdc5c191c7f 100644 --- a/spring-core/src/testFixtures/java/org/springframework/core/testfixture/codec/AbstractEncoderTests.java +++ b/spring-core/src/testFixtures/java/org/springframework/core/testfixture/codec/AbstractEncoderTests.java @@ -251,7 +251,7 @@ public abstract class AbstractEncoderTests> extends Abstrac return dataBuffer -> { String actual = dataBuffer.toString(UTF_8); release(dataBuffer); - assertThat(actual).isEqualTo(expected); + assertThat(actual).isEqualToNormalizingNewlines(expected); }; } diff --git a/spring-test/src/test/java/org/springframework/test/http/MediaTypeAssertTests.java b/spring-test/src/test/java/org/springframework/test/http/MediaTypeAssertTests.java index c0dc306ff2b..3e0536dfdd5 100644 --- a/spring-test/src/test/java/org/springframework/test/http/MediaTypeAssertTests.java +++ b/spring-test/src/test/java/org/springframework/test/http/MediaTypeAssertTests.java @@ -62,12 +62,10 @@ class MediaTypeAssertTests { @Test void isEqualInvalidStringShouldFail() { - String ls = System.lineSeparator(); // output below is different between Unix and Windows assertThatExceptionOfType(AssertionError.class) .isThrownBy(() -> assertThat(mediaType("application/json")).isEqualTo("example of a bad value")) - .withMessageContaining("[Media type]") - .withMessageEndingWith("To be a valid media type but got:" + ls + - " \"Invalid mime type \"example of a bad value\": does not contain '/'\"" + ls); + .withMessageContainingAll("[Media type]", "To be a valid media type but got:", + "\"Invalid mime type \"example of a bad value\": does not contain '/'\""); } @Test @@ -108,12 +106,10 @@ class MediaTypeAssertTests { @Test void isNotEqualInvalidStringShouldFail() { - String ls = System.lineSeparator(); // output below is different between Unix and Windows assertThatExceptionOfType(AssertionError.class) .isThrownBy(() -> assertThat(mediaType("application/json")).isNotEqualTo("example of a bad value")) - .withMessageContaining("[Media type]") - .withMessageEndingWith("To be a valid media type but got:" + ls + - " \"Invalid mime type \"example of a bad value\": does not contain '/'\"" + ls); + .withMessageContainingAll("[Media type]", "To be a valid media type but got:", + "\"Invalid mime type \"example of a bad value\": does not contain '/'\""); } @Test @@ -169,7 +165,7 @@ class MediaTypeAssertTests { void isCompatibleWithStringAndEmptyExpected() { assertThatExceptionOfType(AssertionError.class) .isThrownBy(() -> assertThat(mediaType("application/json")).isCompatibleWith("")) - .withMessageContainingAll("Expecting:", "", "To be a valid media type but got:", + .withMessageContainingAll("Expecting:", "To be a valid media type but got:", "'mimeType' must not be empty"); } diff --git a/spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonEncoderTests.java b/spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonEncoderTests.java index a6652a5b97e..851b4406b90 100644 --- a/spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonEncoderTests.java +++ b/spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonEncoderTests.java @@ -262,9 +262,11 @@ class Jackson2JsonEncoderTests extends AbstractEncoderTests ObjectMapper mapper = new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, true); this.encoder.registerObjectMappersForType(JacksonViewBean.class, map -> map.put(halMediaType, mapper)); - String ls = System.lineSeparator(); // output below is different between Unix and Windows testEncode(Mono.just(jacksonValue), type, halMediaType, Collections.emptyMap(), step -> step - .consumeNextWith(expectString("{" + ls + " \"withView1\" : \"with\"" + ls + "}")) + .consumeNextWith(expectString(""" + { + \s "withView1" : "with" + }""")) .verifyComplete() ); } diff --git a/spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java index a894b4a207b..d62a9ce0212 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java @@ -63,8 +63,6 @@ import static org.assertj.core.api.Assertions.within; */ class MappingJackson2HttpMessageConverterTests { - protected static final String NEWLINE_SYSTEM_PROPERTY = System.lineSeparator(); - private final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); @@ -369,8 +367,10 @@ class MappingJackson2HttpMessageConverterTests { this.converter.writeInternal(bean, null, outputMessage); String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8); - assertThat(result).isEqualTo(("{" + NEWLINE_SYSTEM_PROPERTY + - " \"name\" : \"Jason\"" + NEWLINE_SYSTEM_PROPERTY + "}")); + assertThat(result).isEqualToNormalizingNewlines(""" + { + \s "name" : "Jason" + }"""); } @Test diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandlerTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandlerTests.java index 1384727693c..de0452b5868 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandlerTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandlerTests.java @@ -81,8 +81,6 @@ import static org.springframework.web.testfixture.method.ResolvableMethod.on; */ class ResponseEntityResultHandlerTests { - private static final String NEWLINE_SYSTEM_PROPERTY = System.lineSeparator(); - private final ResponseEntityResultHandler resultHandler = createHandler(); @@ -458,10 +456,10 @@ class ResponseEntityResultHandlerTests { handler.handleResult(exchange, result).block(); assertThat(exchange.getResponse().getHeaders().getContentType()).isEqualTo(halMediaType); - assertThat(exchange.getResponse().getBodyAsString().block()).isEqualTo( - "{" + NEWLINE_SYSTEM_PROPERTY + - " \"name\" : \"Jason\"" + NEWLINE_SYSTEM_PROPERTY + - "}"); + assertThat(exchange.getResponse().getBodyAsString().block()).isEqualToNormalizingNewlines(""" + { + \s "name" : "Jason" + }"""); } @Test // gh-24539