Skip to content

Commit 9e1ef83

Browse files
committed
Avoid issues with system line separator in tests
See f10caf6
1 parent f4f89aa commit 9e1ef83

File tree

5 files changed

+18
-22
lines changed

5 files changed

+18
-22
lines changed

spring-core/src/testFixtures/java/org/springframework/core/testfixture/codec/AbstractEncoderTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ protected Consumer<DataBuffer> expectString(String expected) {
251251
return dataBuffer -> {
252252
String actual = dataBuffer.toString(UTF_8);
253253
release(dataBuffer);
254-
assertThat(actual).isEqualTo(expected);
254+
assertThat(actual).isEqualToNormalizingNewlines(expected);
255255
};
256256
}
257257

spring-test/src/test/java/org/springframework/test/http/MediaTypeAssertTests.java

+5-9
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,10 @@ void isEqualWhenDifferentStringShouldFail() {
6262

6363
@Test
6464
void isEqualInvalidStringShouldFail() {
65-
String ls = System.lineSeparator(); // output below is different between Unix and Windows
6665
assertThatExceptionOfType(AssertionError.class)
6766
.isThrownBy(() -> assertThat(mediaType("application/json")).isEqualTo("example of a bad value"))
68-
.withMessageContaining("[Media type]")
69-
.withMessageEndingWith("To be a valid media type but got:" + ls +
70-
" \"Invalid mime type \"example of a bad value\": does not contain '/'\"" + ls);
67+
.withMessageContainingAll("[Media type]", "To be a valid media type but got:",
68+
"\"Invalid mime type \"example of a bad value\": does not contain '/'\"");
7169
}
7270

7371
@Test
@@ -108,12 +106,10 @@ void isNotEqualWhenSameStringShouldFail() {
108106

109107
@Test
110108
void isNotEqualInvalidStringShouldFail() {
111-
String ls = System.lineSeparator(); // output below is different between Unix and Windows
112109
assertThatExceptionOfType(AssertionError.class)
113110
.isThrownBy(() -> assertThat(mediaType("application/json")).isNotEqualTo("example of a bad value"))
114-
.withMessageContaining("[Media type]")
115-
.withMessageEndingWith("To be a valid media type but got:" + ls +
116-
" \"Invalid mime type \"example of a bad value\": does not contain '/'\"" + ls);
111+
.withMessageContainingAll("[Media type]", "To be a valid media type but got:",
112+
"\"Invalid mime type \"example of a bad value\": does not contain '/'\"");
117113
}
118114

119115
@Test
@@ -169,7 +165,7 @@ void isCompatibleWithStringAndNullExpected() {
169165
void isCompatibleWithStringAndEmptyExpected() {
170166
assertThatExceptionOfType(AssertionError.class)
171167
.isThrownBy(() -> assertThat(mediaType("application/json")).isCompatibleWith(""))
172-
.withMessageContainingAll("Expecting:", "", "To be a valid media type but got:",
168+
.withMessageContainingAll("Expecting:", "To be a valid media type but got:",
173169
"'mimeType' must not be empty");
174170
}
175171

spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonEncoderTests.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,11 @@ public void jacksonValueUnwrappedBeforeObjectMapperSelection() {
262262
ObjectMapper mapper = new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, true);
263263
this.encoder.registerObjectMappersForType(JacksonViewBean.class, map -> map.put(halMediaType, mapper));
264264

265-
String ls = System.lineSeparator(); // output below is different between Unix and Windows
266265
testEncode(Mono.just(jacksonValue), type, halMediaType, Collections.emptyMap(), step -> step
267-
.consumeNextWith(expectString("{" + ls + " \"withView1\" : \"with\"" + ls + "}"))
266+
.consumeNextWith(expectString("""
267+
{
268+
\s "withView1" : "with"
269+
}"""))
268270
.verifyComplete()
269271
);
270272
}

spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@
6363
*/
6464
class MappingJackson2HttpMessageConverterTests {
6565

66-
protected static final String NEWLINE_SYSTEM_PROPERTY = System.lineSeparator();
67-
6866
private final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
6967

7068

@@ -369,8 +367,10 @@ void prettyPrint() throws Exception {
369367
this.converter.writeInternal(bean, null, outputMessage);
370368
String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
371369

372-
assertThat(result).isEqualTo(("{" + NEWLINE_SYSTEM_PROPERTY +
373-
" \"name\" : \"Jason\"" + NEWLINE_SYSTEM_PROPERTY + "}"));
370+
assertThat(result).isEqualToNormalizingNewlines("""
371+
{
372+
\s "name" : "Jason"
373+
}""");
374374
}
375375

376376
@Test

spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandlerTests.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@
8181
*/
8282
class ResponseEntityResultHandlerTests {
8383

84-
private static final String NEWLINE_SYSTEM_PROPERTY = System.lineSeparator();
85-
8684
private final ResponseEntityResultHandler resultHandler = createHandler();
8785

8886

@@ -458,10 +456,10 @@ void handleWithObjectMapperByTypeRegistration() {
458456
handler.handleResult(exchange, result).block();
459457

460458
assertThat(exchange.getResponse().getHeaders().getContentType()).isEqualTo(halMediaType);
461-
assertThat(exchange.getResponse().getBodyAsString().block()).isEqualTo(
462-
"{" + NEWLINE_SYSTEM_PROPERTY +
463-
" \"name\" : \"Jason\"" + NEWLINE_SYSTEM_PROPERTY +
464-
"}");
459+
assertThat(exchange.getResponse().getBodyAsString().block()).isEqualToNormalizingNewlines("""
460+
{
461+
\s "name" : "Jason"
462+
}""");
465463
}
466464

467465
@Test // gh-24539

0 commit comments

Comments
 (0)