Browse Source

ToStringVisitor consistently returns platform-independent line breaks

Issue: SPR-17322
pull/2025/head
Juergen Hoeller 6 years ago
parent
commit
b1b28d4641
  1. 25
      spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ToStringVisitor.java
  2. 13
      spring-webflux/src/test/java/org/springframework/web/reactive/function/server/ToStringVisitorTests.java

25
spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ToStringVisitor.java

@ -26,16 +26,14 @@ import org.springframework.http.HttpMethod; @@ -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 @@ -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 @@ -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 @@ -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<ServerRequest, Mono<Resource>> 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 @@ -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 @@ -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;
}
}

13
spring-webflux/src/test/java/org/springframework/web/reactive/function/server/ToStringVisitorTests.java

@ -23,14 +23,8 @@ import org.springframework.http.HttpMethod; @@ -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 { @@ -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 { @@ -91,6 +85,7 @@ public class ToStringVisitorTests {
assertEquals(expected, result);
}
private static class SimpleHandlerFunction implements HandlerFunction<ServerResponse> {
@Override

Loading…
Cancel
Save