Skip to content

Commit b1b28d4

Browse files
committed
ToStringVisitor consistently returns platform-independent line breaks
Issue: SPR-17322
1 parent 17b2b8a commit b1b28d4

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,56 +26,51 @@
2626
import org.springframework.lang.Nullable;
2727

2828
/**
29-
* Implementation of {@link RouterFunctions.Visitor} that creates a formatted string representation
30-
* of router functions.
29+
* Implementation of {@link RouterFunctions.Visitor} that creates a formatted
30+
* string representation of router functions.
3131
*
3232
* @author Arjen Poutsma
3333
* @since 5.0
3434
*/
3535
class ToStringVisitor implements RouterFunctions.Visitor, RequestPredicates.Visitor {
3636

37-
private static final String NEW_LINE = System.getProperty("line.separator", "\\n");
38-
3937
private final StringBuilder builder = new StringBuilder();
4038

4139
private int indent = 0;
4240

4341
@Nullable
4442
private String infix;
4543

44+
4645
// RouterFunctions.Visitor
4746

4847
@Override
4948
public void startNested(RequestPredicate predicate) {
5049
indent();
5150
predicate.accept(this);
52-
this.builder.append(" => {");
53-
this.builder.append(NEW_LINE);
51+
this.builder.append(" => {\n");
5452
this.indent++;
5553
}
5654

5755
@Override
5856
public void endNested(RequestPredicate predicate) {
5957
this.indent--;
6058
indent();
61-
this.builder.append('}');
62-
this.builder.append(NEW_LINE);
59+
this.builder.append("}\n");
6360
}
6461

6562
@Override
6663
public void route(RequestPredicate predicate, HandlerFunction<?> handlerFunction) {
6764
indent();
6865
predicate.accept(this);
6966
this.builder.append(" -> ");
70-
this.builder.append(handlerFunction);
71-
this.builder.append(NEW_LINE);
67+
this.builder.append(handlerFunction).append('\n');
7268
}
7369

7470
@Override
7571
public void resources(Function<ServerRequest, Mono<Resource>> lookupFunction) {
7672
indent();
77-
this.builder.append(lookupFunction);
78-
this.builder.append(NEW_LINE);
73+
this.builder.append(lookupFunction).append('\n');
7974
}
8075

8176
@Override
@@ -90,6 +85,7 @@ private void indent() {
9085
}
9186
}
9287

88+
9389
// RequestPredicates.Visitor
9490

9591
@Override
@@ -178,9 +174,10 @@ private void infix() {
178174
@Override
179175
public String toString() {
180176
String result = this.builder.toString();
181-
if (result.endsWith(NEW_LINE)) {
182-
result = result.substring(0, result.length() - NEW_LINE.length());
177+
if (result.endsWith("\n")) {
178+
result = result.substring(0, result.length() - 1);
183179
}
184180
return result;
185181
}
182+
186183
}

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,8 @@
2323
import org.springframework.http.MediaType;
2424

2525
import static org.junit.Assert.*;
26-
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
27-
import static org.springframework.web.reactive.function.server.RequestPredicates.contentType;
28-
import static org.springframework.web.reactive.function.server.RequestPredicates.method;
29-
import static org.springframework.web.reactive.function.server.RequestPredicates.methods;
30-
import static org.springframework.web.reactive.function.server.RequestPredicates.path;
31-
import static org.springframework.web.reactive.function.server.RequestPredicates.pathExtension;
32-
import static org.springframework.web.reactive.function.server.RequestPredicates.queryParam;
33-
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
26+
import static org.springframework.web.reactive.function.server.RequestPredicates.*;
27+
import static org.springframework.web.reactive.function.server.RouterFunctions.*;
3428

3529
/**
3630
* @author Arjen Poutsma
@@ -50,7 +44,7 @@ public void nested() {
5044

5145
ToStringVisitor visitor = new ToStringVisitor();
5246
routerFunction.accept(visitor);
53-
String result = visitor.toString();
47+
String result = visitor.toString();
5448

5549
String expected = "/foo => {\n" +
5650
" /bar => {\n" +
@@ -91,6 +85,7 @@ private void testPredicate(RequestPredicate predicate, String expected) {
9185
assertEquals(expected, result);
9286
}
9387

88+
9489
private static class SimpleHandlerFunction implements HandlerFunction<ServerResponse> {
9590

9691
@Override
@@ -104,4 +99,4 @@ public String toString() {
10499
}
105100
}
106101

107-
}
102+
}

0 commit comments

Comments
 (0)