From b1b28d46414c6aff08684705fc3997718dcf5205 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 22 Nov 2018 16:11:59 +0100 Subject: [PATCH] ToStringVisitor consistently returns platform-independent line breaks Issue: SPR-17322 --- .../function/server/ToStringVisitor.java | 25 ++++++++----------- .../function/server/ToStringVisitorTests.java | 15 ++++------- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ToStringVisitor.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ToStringVisitor.java index b5a072f1b5..8e13095641 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ToStringVisitor.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ToStringVisitor.java @@ -26,16 +26,14 @@ import org.springframework.http.HttpMethod; import org.springframework.lang.Nullable; /** - * Implementation of {@link RouterFunctions.Visitor} that creates a formatted string representation - * of router functions. + * Implementation of {@link RouterFunctions.Visitor} that creates a formatted + * string representation of router functions. * * @author Arjen Poutsma * @since 5.0 */ class ToStringVisitor implements RouterFunctions.Visitor, RequestPredicates.Visitor { - private static final String NEW_LINE = System.getProperty("line.separator", "\\n"); - private final StringBuilder builder = new StringBuilder(); private int indent = 0; @@ -43,14 +41,14 @@ class ToStringVisitor implements RouterFunctions.Visitor, RequestPredicates.Visi @Nullable private String infix; + // RouterFunctions.Visitor @Override public void startNested(RequestPredicate predicate) { indent(); predicate.accept(this); - this.builder.append(" => {"); - this.builder.append(NEW_LINE); + this.builder.append(" => {\n"); this.indent++; } @@ -58,8 +56,7 @@ class ToStringVisitor implements RouterFunctions.Visitor, RequestPredicates.Visi public void endNested(RequestPredicate predicate) { this.indent--; indent(); - this.builder.append('}'); - this.builder.append(NEW_LINE); + this.builder.append("}\n"); } @Override @@ -67,15 +64,13 @@ class ToStringVisitor implements RouterFunctions.Visitor, RequestPredicates.Visi indent(); predicate.accept(this); this.builder.append(" -> "); - this.builder.append(handlerFunction); - this.builder.append(NEW_LINE); + this.builder.append(handlerFunction).append('\n'); } @Override public void resources(Function> lookupFunction) { indent(); - this.builder.append(lookupFunction); - this.builder.append(NEW_LINE); + this.builder.append(lookupFunction).append('\n'); } @Override @@ -90,6 +85,7 @@ class ToStringVisitor implements RouterFunctions.Visitor, RequestPredicates.Visi } } + // RequestPredicates.Visitor @Override @@ -178,9 +174,10 @@ class ToStringVisitor implements RouterFunctions.Visitor, RequestPredicates.Visi @Override public String toString() { String result = this.builder.toString(); - if (result.endsWith(NEW_LINE)) { - result = result.substring(0, result.length() - NEW_LINE.length()); + if (result.endsWith("\n")) { + result = result.substring(0, result.length() - 1); } return result; } + } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/ToStringVisitorTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/ToStringVisitorTests.java index 3469fab36d..38952d8c69 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/ToStringVisitorTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/ToStringVisitorTests.java @@ -23,14 +23,8 @@ import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import static org.junit.Assert.*; -import static org.springframework.web.reactive.function.server.RequestPredicates.accept; -import static org.springframework.web.reactive.function.server.RequestPredicates.contentType; -import static org.springframework.web.reactive.function.server.RequestPredicates.method; -import static org.springframework.web.reactive.function.server.RequestPredicates.methods; -import static org.springframework.web.reactive.function.server.RequestPredicates.path; -import static org.springframework.web.reactive.function.server.RequestPredicates.pathExtension; -import static org.springframework.web.reactive.function.server.RequestPredicates.queryParam; -import static org.springframework.web.reactive.function.server.RouterFunctions.route; +import static org.springframework.web.reactive.function.server.RequestPredicates.*; +import static org.springframework.web.reactive.function.server.RouterFunctions.*; /** * @author Arjen Poutsma @@ -50,7 +44,7 @@ public class ToStringVisitorTests { ToStringVisitor visitor = new ToStringVisitor(); routerFunction.accept(visitor); - String result = visitor.toString(); + String result = visitor.toString(); String expected = "/foo => {\n" + " /bar => {\n" + @@ -91,6 +85,7 @@ public class ToStringVisitorTests { assertEquals(expected, result); } + private static class SimpleHandlerFunction implements HandlerFunction { @Override @@ -104,4 +99,4 @@ public class ToStringVisitorTests { } } -} \ No newline at end of file +}