ToStringVisitor consistently returns platform-independent line breaks
Issue: SPR-17322
This commit is contained in:
@@ -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<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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<ServerResponse> {
|
||||
|
||||
@Override
|
||||
@@ -104,4 +99,4 @@ public class ToStringVisitorTests {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user