Use new Java features (switch expressions, text blocks, new JDK methods)

Closes gh-29747
This commit is contained in:
Krzysztof Krason
2022-12-28 08:59:08 +01:00
committed by Sam Brannen
parent 48abd493fe
commit afb8a0d1b1
53 changed files with 498 additions and 552 deletions

View File

@@ -201,34 +201,22 @@ public final class CloseStatus {
*/
public static CloseStatus create(int code, @Nullable String reason) {
if (!StringUtils.hasText(reason)) {
switch (code) {
case 1000:
return NORMAL;
case 1001:
return GOING_AWAY;
case 1002:
return PROTOCOL_ERROR;
case 1003:
return NOT_ACCEPTABLE;
case 1005:
return NO_STATUS_CODE;
case 1006:
return NO_CLOSE_FRAME;
case 1007:
return BAD_DATA;
case 1008:
return POLICY_VIOLATION;
case 1009:
return TOO_BIG_TO_PROCESS;
case 1010:
return REQUIRED_EXTENSION;
case 1011:
return SERVER_ERROR;
case 1012:
return SERVICE_RESTARTED;
case 1013:
return SERVICE_OVERLOAD;
}
return switch (code) {
case 1000 -> NORMAL;
case 1001 -> GOING_AWAY;
case 1002 -> PROTOCOL_ERROR;
case 1003 -> NOT_ACCEPTABLE;
case 1005 -> NO_STATUS_CODE;
case 1006 -> NO_CLOSE_FRAME;
case 1007 -> BAD_DATA;
case 1008 -> POLICY_VIOLATION;
case 1009 -> TOO_BIG_TO_PROCESS;
case 1010 -> REQUIRED_EXTENSION;
case 1011 -> SERVER_ERROR;
case 1012 -> SERVICE_RESTARTED;
case 1013 -> SERVICE_OVERLOAD;
default -> new CloseStatus(code, reason);
};
}
return new CloseStatus(code, reason);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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.
@@ -125,26 +125,20 @@ class FlushingIntegrationTests extends AbstractHttpHandlerIntegrationTests {
@Override
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
String path = request.getURI().getPath();
switch (path) {
case "/write-and-flush":
return response.writeAndFlushWith(
testInterval(Duration.ofMillis(50), 2)
.map(longValue -> wrap("data" + longValue + "\n", response))
.map(Flux::just)
.mergeWith(Flux.never()));
case "/write-and-complete":
return response.writeWith(
chunks1K().take(64).map(s -> wrap(s, response)));
case "/write-and-never-complete":
return switch (path) {
case "/write-and-flush" -> response.writeAndFlushWith(
testInterval(Duration.ofMillis(50), 2)
.map(longValue -> wrap("data" + longValue + "\n", response))
.map(Flux::just)
.mergeWith(Flux.never()));
case "/write-and-complete" -> response.writeWith(
chunks1K().take(64).map(s -> wrap(s, response)));
case "/write-and-never-complete" ->
// Reactor requires at least 50 to flush, Tomcat/Undertow 8, Jetty 1
return response.writeWith(
chunks1K().take(64).map(s -> wrap(s, response)).mergeWith(Flux.never()));
default:
return response.writeWith(Flux.empty());
}
response.writeWith(
chunks1K().take(64).map(s -> wrap(s, response)).mergeWith(Flux.never()));
default -> response.writeWith(Flux.empty());
};
}
private Flux<String> chunks1K() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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.
@@ -336,23 +336,25 @@ public class BodyExtractorsTests {
public void toParts() {
BodyExtractor<Flux<Part>, ServerHttpRequest> extractor = BodyExtractors.toParts();
String bodyContents = "-----------------------------9051914041544843365972754266\r\n" +
"Content-Disposition: form-data; name=\"text\"\r\n" +
"\r\n" +
"text default\r\n" +
"-----------------------------9051914041544843365972754266\r\n" +
"Content-Disposition: form-data; name=\"file1\"; filename=\"a.txt\"\r\n" +
"Content-Type: text/plain\r\n" +
"\r\n" +
"Content of a.txt.\r\n" +
"\r\n" +
"-----------------------------9051914041544843365972754266\r\n" +
"Content-Disposition: form-data; name=\"file2\"; filename=\"a.html\"\r\n" +
"Content-Type: text/html\r\n" +
"\r\n" +
"<!DOCTYPE html><title>Content of a.html.</title>\r\n" +
"\r\n" +
"-----------------------------9051914041544843365972754266--\r\n";
String bodyContents = """
-----------------------------9051914041544843365972754266\r
Content-Disposition: form-data; name="text"\r
\r
text default\r
-----------------------------9051914041544843365972754266\r
Content-Disposition: form-data; name="file1"; filename="a.txt"\r
Content-Type: text/plain\r
\r
Content of a.txt.\r
\r
-----------------------------9051914041544843365972754266\r
Content-Disposition: form-data; name="file2"; filename="a.html"\r
Content-Type: text/html\r
\r
<!DOCTYPE html><title>Content of a.html.</title>\r
\r
-----------------------------9051914041544843365972754266--\r
""";
byte[] bytes = bodyContents.getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));

View File

@@ -726,8 +726,8 @@ class WebClientIntegrationTests {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
request.getBody().copyTo(bos);
String actual = bos.toString("UTF-8");
String expected = new String(Files.readAllBytes(resource.getFile().toPath()), StandardCharsets.UTF_8);
String actual = bos.toString(StandardCharsets.UTF_8);
String expected = Files.readString(resource.getFile().toPath(), StandardCharsets.UTF_8);
assertThat(actual).isEqualTo(expected);
}
catch (IOException ex) {

View File

@@ -412,15 +412,17 @@ public class DefaultServerRequestTests {
@Test
public void multipartData() {
String data = "--12345\r\n" +
"Content-Disposition: form-data; name=\"foo\"\r\n" +
"\r\n" +
"bar\r\n" +
"--12345\r\n" +
"Content-Disposition: form-data; name=\"baz\"\r\n" +
"\r\n" +
"qux\r\n" +
"--12345--\r\n";
String data = """
--12345\r
Content-Disposition: form-data; name="foo"\r
\r
bar\r
--12345\r
Content-Disposition: form-data; name="baz"\r
\r
qux\r
--12345--\r
""";
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@@ -80,13 +80,17 @@ public class CssLinkResourceTransformerTests {
MockServerWebExchange exchange = MockServerWebExchange.from(get("/static/main.css"));
Resource css = getResource("main.css");
String expected = "\n" +
"@import url(\"/static/bar-11e16cf79faee7ac698c805cf28248d2.css?#iefix\");\n" +
"@import url('/static/bar-11e16cf79faee7ac698c805cf28248d2.css#bla-normal');\n" +
"@import url(/static/bar-11e16cf79faee7ac698c805cf28248d2.css);\n\n" +
"@import \"/static/foo-e36d2e05253c6c7085a91522ce43a0b4.css\";\n" +
"@import '/static/foo-e36d2e05253c6c7085a91522ce43a0b4.css';\n\n" +
"body { background: url(\"/static/images/image-f448cd1d5dba82b774f3202c878230b3.png?#iefix\") }\n";
String expected = """
@import url("/static/bar-11e16cf79faee7ac698c805cf28248d2.css?#iefix");
@import url('/static/bar-11e16cf79faee7ac698c805cf28248d2.css#bla-normal');
@import url(/static/bar-11e16cf79faee7ac698c805cf28248d2.css);
@import "/static/foo-e36d2e05253c6c7085a91522ce43a0b4.css";
@import '/static/foo-e36d2e05253c6c7085a91522ce43a0b4.css';
body { background: url("/static/images/image-f448cd1d5dba82b774f3202c878230b3.png?#iefix") }
""";
StepVerifier.create(this.transformerChain.transform(exchange, css)
.cast(TransformedResource.class))
@@ -118,9 +122,10 @@ public class CssLinkResourceTransformerTests {
ResourceTransformerChain chain = new DefaultResourceTransformerChain(mockChain, transformers);
Resource resource = getResource("external.css");
String expected = "@import url(\"https://example.org/fonts/css\");\n" +
"body { background: url(\"file:///home/spring/image.png\") }\n" +
"figure { background: url(\"//example.org/style.css\")}";
String expected = """
@import url("https://example.org/fonts/css");
body { background: url("file:///home/spring/image.png") }
figure { background: url("//example.org/style.css")}""";
StepVerifier.create(chain.transform(exchange, resource)
.cast(TransformedResource.class))
@@ -167,10 +172,10 @@ public class CssLinkResourceTransformerTests {
public void transformEmptyUrlFunction() throws Exception {
MockServerWebExchange exchange = MockServerWebExchange.from(get("/static/empty_url_function.css"));
Resource css = getResource("empty_url_function.css");
String expected =
".fooStyle {\n" +
"\tbackground: transparent url() no-repeat left top;\n" +
"}";
String expected = """
.fooStyle {
\tbackground: transparent url() no-repeat left top;
}""";
StepVerifier.create(this.transformerChain.transform(exchange, css)
.cast(TransformedResource.class))

View File

@@ -45,7 +45,6 @@ import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
import org.springframework.web.testfixture.server.MockServerWebExchange;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.singletonMap;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
@@ -357,8 +356,9 @@ public class FreeMarkerMacroTests {
}
private void storeTemplateInTempDir(String macro) throws IOException {
Files.write(this.templateLoaderPath.resolve("tmp.ftl"),
("<#import \"spring.ftl\" as spring />\n" + macro).getBytes(UTF_8));
Files.writeString(this.templateLoaderPath.resolve("tmp.ftl"),
"<#import \"spring.ftl\" as spring />\n" + macro
);
}
private List<String> getOutput() {