Skip to content

Commit

Permalink
ToStringVisitor consistently returns platform-independent line breaks
Browse files Browse the repository at this point in the history
Issue: SPR-17322
  • Loading branch information
jhoeller committed Nov 22, 2018
1 parent 17b2b8a commit b1b28d4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
Expand Up @@ -26,56 +26,51 @@
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;

@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++;
}

@Override
public void endNested(RequestPredicate predicate) {
this.indent--;
indent();
this.builder.append('}');
this.builder.append(NEW_LINE);
this.builder.append("}\n");
}

@Override
public void route(RequestPredicate predicate, HandlerFunction<?> handlerFunction) {
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
Expand All @@ -90,6 +85,7 @@ private void indent() {
}
}


// RequestPredicates.Visitor

@Override
Expand Down Expand Up @@ -178,9 +174,10 @@ private void infix() {
@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;
}

}
Expand Up @@ -23,14 +23,8 @@
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
Expand All @@ -50,7 +44,7 @@ public void nested() {

ToStringVisitor visitor = new ToStringVisitor();
routerFunction.accept(visitor);
String result = visitor.toString();
String result = visitor.toString();

String expected = "/foo => {\n" +
" /bar => {\n" +
Expand Down Expand Up @@ -91,6 +85,7 @@ private void testPredicate(RequestPredicate predicate, String expected) {
assertEquals(expected, result);
}


private static class SimpleHandlerFunction implements HandlerFunction<ServerResponse> {

@Override
Expand All @@ -104,4 +99,4 @@ public String toString() {
}
}

}
}

0 comments on commit b1b28d4

Please sign in to comment.