Skip to content

Commit 1f098e1

Browse files
committed
Polish DefaultPathContainerTests
1 parent 67fccc3 commit 1f098e1

File tree

1 file changed

+30
-35
lines changed

1 file changed

+30
-35
lines changed

spring-web/src/test/java/org/springframework/http/server/DefaultPathContainerTests.java

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,12 +13,10 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.http.server;
1718

18-
import java.util.Arrays;
19-
import java.util.Collections;
20-
import java.util.List;
21-
import java.util.stream.Collectors;
19+
import java.util.stream.Stream;
2220

2321
import org.junit.jupiter.api.Test;
2422

@@ -30,12 +28,14 @@
3028

3129
/**
3230
* Unit tests for {@link DefaultPathContainer}.
31+
*
3332
* @author Rossen Stoyanchev
33+
* @author Sam Brannen
3434
*/
35-
public class DefaultPathContainerTests {
35+
class DefaultPathContainerTests {
3636

3737
@Test
38-
public void pathSegment() {
38+
void pathSegment() {
3939
// basic
4040
testPathSegment("cars", "cars", new LinkedMultiValueMap<>());
4141

@@ -48,7 +48,7 @@ public void pathSegment() {
4848
}
4949

5050
@Test
51-
public void pathSegmentParams() throws Exception {
51+
void pathSegmentParams() {
5252
// basic
5353
LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>();
5454
params.add("colors", "red");
@@ -74,15 +74,14 @@ public void pathSegmentParams() throws Exception {
7474
}
7575

7676
private void testPathSegment(String rawValue, String valueToMatch, MultiValueMap<String, String> params) {
77-
7877
PathContainer container = PathContainer.parsePath(rawValue);
7978

8079
if ("".equals(rawValue)) {
81-
assertThat(container.elements().size()).isEqualTo(0);
80+
assertThat(container.elements()).isEmpty();
8281
return;
8382
}
8483

85-
assertThat(container.elements().size()).isEqualTo(1);
84+
assertThat(container.elements()).hasSize(1);
8685
PathSegment segment = (PathSegment) container.elements().get(0);
8786

8887
assertThat(segment.value()).as("value: '" + rawValue + "'").isEqualTo(rawValue);
@@ -91,40 +90,36 @@ private void testPathSegment(String rawValue, String valueToMatch, MultiValueMap
9190
}
9291

9392
@Test
94-
public void path() {
93+
void path() {
9594
// basic
96-
testPath("/a/b/c", "/a/b/c", Arrays.asList("/", "a", "/", "b", "/", "c"));
95+
testPath("/a/b/c", "/a/b/c", "/", "a", "/", "b", "/", "c");
9796

9897
// root path
99-
testPath("/", "/", Collections.singletonList("/"));
98+
testPath("/", "/", "/");
10099

101100
// empty path
102-
testPath("", "", Collections.emptyList());
103-
testPath("%20%20", "%20%20", Collections.singletonList("%20%20"));
101+
testPath("", "");
102+
testPath("%20%20", "%20%20", "%20%20");
104103

105104
// trailing slash
106-
testPath("/a/b/", "/a/b/", Arrays.asList("/", "a", "/", "b", "/"));
107-
testPath("/a/b//", "/a/b//", Arrays.asList("/", "a", "/", "b", "/", "/"));
105+
testPath("/a/b/", "/a/b/", "/", "a", "/", "b", "/");
106+
testPath("/a/b//", "/a/b//", "/", "a", "/", "b", "/", "/");
108107

109108
// extra slashes and spaces
110-
testPath("/%20", "/%20", Arrays.asList("/", "%20"));
111-
testPath("//%20/%20", "//%20/%20", Arrays.asList("/", "/", "%20", "/", "%20"));
109+
testPath("/%20", "/%20", "/", "%20");
110+
testPath("//%20/%20", "//%20/%20", "/", "/", "%20", "/", "%20");
112111
}
113112

114-
private void testPath(String input, PathContainer.Options options, String value, List<String> expectedElements) {
115-
PathContainer path = PathContainer.parsePath(input, options);
113+
private void testPath(String input, String value, String... expectedElements) {
114+
PathContainer path = PathContainer.parsePath(input, PathContainer.Options.HTTP_PATH);
116115

117116
assertThat(path.value()).as("value: '" + input + "'").isEqualTo(value);
118-
assertThat(path.elements().stream().map(PathContainer.Element::value).collect(Collectors.toList()))
119-
.as("elements: " + input).isEqualTo(expectedElements);
120-
}
121-
122-
private void testPath(String input, String value, List<String> expectedElements) {
123-
testPath(input, PathContainer.Options.HTTP_PATH, value, expectedElements);
117+
assertThat(path.elements()).map(PathContainer.Element::value).as("elements: " + input)
118+
.containsExactly(expectedElements);
124119
}
125120

126121
@Test
127-
public void subPath() {
122+
void subPath() {
128123
// basic
129124
PathContainer path = PathContainer.parsePath("/a/b/c");
130125
assertThat(path.subPath(0)).isSameAs(path);
@@ -141,15 +136,15 @@ public void subPath() {
141136
}
142137

143138
@Test // gh-23310
144-
public void pathWithCustomSeparator() {
139+
void pathWithCustomSeparator() {
145140
PathContainer path = PathContainer.parsePath("a.b%2Eb.c", PathContainer.Options.MESSAGE_ROUTE);
146141

147-
List<String> decodedSegments = path.elements().stream()
148-
.filter(e -> e instanceof PathSegment)
149-
.map(e -> ((PathSegment) e).valueToMatch())
150-
.collect(Collectors.toList());
142+
Stream<String> decodedSegments = path.elements().stream()
143+
.filter(PathSegment.class::isInstance)
144+
.map(PathSegment.class::cast)
145+
.map(PathSegment::valueToMatch);
151146

152-
assertThat(decodedSegments).isEqualTo(Arrays.asList("a", "b.b", "c"));
147+
assertThat(decodedSegments).containsExactly("a", "b.b", "c");
153148
}
154149

155150
}

0 commit comments

Comments
 (0)