From 7aa06b86102eea887e5d6b307b4be227436099b1 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 26 Mar 2020 21:13:29 +0000 Subject: [PATCH] Polishing contribution See gh-24786 --- .../core/io/buffer/DefaultDataBuffer.java | 2 +- .../codec/AbstractEncoderTests.java | 2 +- .../io/buffer/DataBufferTestUtils.java | 2 +- .../reactive/MockClientHttpRequest.java | 19 +++++++---------- .../reactive/MockClientHttpResponse.java | 20 +++++++----------- .../reactive/MockServerHttpResponse.java | 21 +++++++------------ .../reactive/MockClientHttpRequest.java | 19 +++++++---------- .../reactive/MockClientHttpResponse.java | 20 +++++++----------- .../reactive/MockServerHttpResponse.java | 21 +++++++------------ 9 files changed, 45 insertions(+), 81 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBuffer.java b/spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBuffer.java index d1f5e9d0e3..be2155f343 100644 --- a/spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBuffer.java +++ b/spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBuffer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. 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 74d930f67b..106cc114e8 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-core/src/testFixtures/java/org/springframework/core/testfixture/io/buffer/DataBufferTestUtils.java b/spring-core/src/testFixtures/java/org/springframework/core/testfixture/io/buffer/DataBufferTestUtils.java index 12ab9ad8c6..b1e96092f0 100644 --- a/spring-core/src/testFixtures/java/org/springframework/core/testfixture/io/buffer/DataBufferTestUtils.java +++ b/spring-core/src/testFixtures/java/org/springframework/core/testfixture/io/buffer/DataBufferTestUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpRequest.java b/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpRequest.java index 638a2446f1..c6568c1fca 100644 --- a/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -149,18 +149,13 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest { Charset charset = Optional.ofNullable(getHeaders().getContentType()).map(MimeType::getCharset) .orElse(StandardCharsets.UTF_8); - return getBody() - .reduce(bufferFactory().allocateBuffer(), (previous, current) -> { - previous.write(current); - DataBufferUtils.release(current); - return previous; + return DataBufferUtils.join(getBody()) + .map(buffer -> { + String s = buffer.toString(charset); + DataBufferUtils.release(buffer); + return s; }) - .map(buffer -> bufferToString(buffer, charset)); - } - - private static String bufferToString(DataBuffer buffer, Charset charset) { - Assert.notNull(charset, "'charset' must not be null"); - return buffer.toString(charset); + .defaultIfEmpty(""); } } diff --git a/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpResponse.java b/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpResponse.java index b553fc9ac7..4e3d1a76ad 100644 --- a/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpResponse.java +++ b/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -122,19 +122,13 @@ public class MockClientHttpResponse implements ClientHttpResponse { * charset of the Content-Type response or otherwise as "UTF-8". */ public Mono getBodyAsString() { - Charset charset = getCharset(); - return Flux.from(getBody()) - .reduce(this.bufferFactory.allocateBuffer(), (previous, current) -> { - previous.write(current); - DataBufferUtils.release(current); - return previous; + return DataBufferUtils.join(getBody()) + .map(buffer -> { + String s = buffer.toString(getCharset()); + DataBufferUtils.release(buffer); + return s; }) - .map(buffer -> dumpString(buffer, charset)); - } - - private static String dumpString(DataBuffer buffer, Charset charset) { - Assert.notNull(charset, "'charset' must not be null"); - return buffer.toString(charset); + .defaultIfEmpty(""); } private Charset getCharset() { diff --git a/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpResponse.java b/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpResponse.java index 905ea67baf..af068c8a90 100644 --- a/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpResponse.java +++ b/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -142,20 +142,13 @@ public class MockServerHttpResponse extends AbstractServerHttpResponse { Charset charset = Optional.ofNullable(getHeaders().getContentType()).map(MimeType::getCharset) .orElse(StandardCharsets.UTF_8); - return getBody() - .reduce(bufferFactory().allocateBuffer(), (previous, current) -> { - previous.write(current); - DataBufferUtils.release(current); - return previous; + return DataBufferUtils.join(getBody()) + .map(buffer -> { + String s = buffer.toString(charset); + DataBufferUtils.release(buffer); + return s; }) - .map(buffer -> bufferToString(buffer, charset)); - } - - private static String bufferToString(DataBuffer buffer, Charset charset) { - Assert.notNull(charset, "'charset' must not be null"); - String str = buffer.toString(charset); - DataBufferUtils.release(buffer); - return str; + .defaultIfEmpty(""); } } diff --git a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/client/reactive/MockClientHttpRequest.java b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/client/reactive/MockClientHttpRequest.java index 321b48e745..1fdd0edcd3 100644 --- a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/client/reactive/MockClientHttpRequest.java +++ b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/client/reactive/MockClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -149,18 +149,13 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest { Charset charset = Optional.ofNullable(getHeaders().getContentType()).map(MimeType::getCharset) .orElse(StandardCharsets.UTF_8); - return getBody() - .reduce(bufferFactory().allocateBuffer(), (previous, current) -> { - previous.write(current); - DataBufferUtils.release(current); - return previous; + return DataBufferUtils.join(getBody()) + .map(buffer -> { + String s = buffer.toString(charset); + DataBufferUtils.release(buffer); + return s; }) - .map(buffer -> bufferToString(buffer, charset)); - } - - private static String bufferToString(DataBuffer buffer, Charset charset) { - Assert.notNull(charset, "'charset' must not be null"); - return buffer.toString(charset); + .defaultIfEmpty(""); } } diff --git a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/client/reactive/MockClientHttpResponse.java b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/client/reactive/MockClientHttpResponse.java index 65815cf78f..7ee7e6afb9 100644 --- a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/client/reactive/MockClientHttpResponse.java +++ b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/client/reactive/MockClientHttpResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -122,19 +122,13 @@ public class MockClientHttpResponse implements ClientHttpResponse { * charset of the Content-Type response or otherwise as "UTF-8". */ public Mono getBodyAsString() { - Charset charset = getCharset(); - return Flux.from(getBody()) - .reduce(this.bufferFactory.allocateBuffer(), (previous, current) -> { - previous.write(current); - DataBufferUtils.release(current); - return previous; + return DataBufferUtils.join(getBody()) + .map(buffer -> { + String s = buffer.toString(getCharset()); + DataBufferUtils.release(buffer); + return s; }) - .map(buffer -> dumpString(buffer, charset)); - } - - private static String dumpString(DataBuffer buffer, Charset charset) { - Assert.notNull(charset, "'charset' must not be null"); - return buffer.toString(charset); + .defaultIfEmpty(""); } private Charset getCharset() { diff --git a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/server/reactive/MockServerHttpResponse.java b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/server/reactive/MockServerHttpResponse.java index 5a1f2b438a..b3f95854e4 100644 --- a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/server/reactive/MockServerHttpResponse.java +++ b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/server/reactive/MockServerHttpResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -142,20 +142,13 @@ public class MockServerHttpResponse extends AbstractServerHttpResponse { Charset charset = Optional.ofNullable(getHeaders().getContentType()).map(MimeType::getCharset) .orElse(StandardCharsets.UTF_8); - return getBody() - .reduce(bufferFactory().allocateBuffer(), (previous, current) -> { - previous.write(current); - DataBufferUtils.release(current); - return previous; + return DataBufferUtils.join(getBody()) + .map(buffer -> { + String s = buffer.toString(charset); + DataBufferUtils.release(buffer); + return s; }) - .map(buffer -> bufferToString(buffer, charset)); - } - - private static String bufferToString(DataBuffer buffer, Charset charset) { - Assert.notNull(charset, "'charset' must not be null"); - String s = buffer.toString(charset); - DataBufferUtils.release(buffer); - return s; + .defaultIfEmpty(""); } }