Skip to content

Commit f2f84bf

Browse files
committed
Polishing
1 parent 6d67bdf commit f2f84bf

File tree

3 files changed

+60
-62
lines changed

3 files changed

+60
-62
lines changed

spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ class UriComponentsBuilderTests {
5555
void plain() throws URISyntaxException {
5656
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
5757
UriComponents result = builder.scheme("https").host("example.com")
58-
.path("foo").queryParam("bar").fragment("baz")
59-
.build();
58+
.path("foo").queryParam("bar").fragment("baz").build();
59+
6060
assertThat(result.getScheme()).isEqualTo("https");
6161
assertThat(result.getHost()).isEqualTo("example.com");
6262
assertThat(result.getPath()).isEqualTo("foo");
@@ -93,10 +93,10 @@ void multipleFromSameBuilder() throws URISyntaxException {
9393
@Test
9494
void fromPath() throws URISyntaxException {
9595
UriComponents result = UriComponentsBuilder.fromPath("foo").queryParam("bar").fragment("baz").build();
96+
9697
assertThat(result.getPath()).isEqualTo("foo");
9798
assertThat(result.getQuery()).isEqualTo("bar");
9899
assertThat(result.getFragment()).isEqualTo("baz");
99-
100100
assertThat(result.toUriString()).as("Invalid result URI String").isEqualTo("foo?bar#baz");
101101

102102
URI expected = new URI("foo?bar#baz");
@@ -113,23 +113,23 @@ void fromPath() throws URISyntaxException {
113113
void fromHierarchicalUri() throws URISyntaxException {
114114
URI uri = new URI("https://example.com/foo?bar#baz");
115115
UriComponents result = UriComponentsBuilder.fromUri(uri).build();
116+
116117
assertThat(result.getScheme()).isEqualTo("https");
117118
assertThat(result.getHost()).isEqualTo("example.com");
118119
assertThat(result.getPath()).isEqualTo("/foo");
119120
assertThat(result.getQuery()).isEqualTo("bar");
120121
assertThat(result.getFragment()).isEqualTo("baz");
121-
122122
assertThat(result.toUri()).as("Invalid result URI").isEqualTo(uri);
123123
}
124124

125125
@Test
126126
void fromOpaqueUri() throws URISyntaxException {
127127
URI uri = new URI("mailto:[email protected]#baz");
128128
UriComponents result = UriComponentsBuilder.fromUri(uri).build();
129+
129130
assertThat(result.getScheme()).isEqualTo("mailto");
130131
assertThat(result.getSchemeSpecificPart()).isEqualTo("[email protected]");
131132
assertThat(result.getFragment()).isEqualTo("baz");
132-
133133
assertThat(result.toUri()).as("Invalid result URI").isEqualTo(uri);
134134
}
135135

@@ -1195,14 +1195,15 @@ void uriComponentsWithMergedQueryParams() {
11951195
assertThat(uri).isEqualTo("http://localhost:8081/{path}?sort={sort}&sort=another_value");
11961196
}
11971197

1198-
@Test // SPR-17630
1198+
@Test // SPR-17630
11991199
void toUriStringWithCurlyBraces() {
12001200
assertThat(UriComponentsBuilder.fromUriString("/path?q={asa}asa").toUriString()).isEqualTo("/path?q=%7Basa%7Dasa");
12011201
}
12021202

1203-
@Test
1203+
@Test // gh-26012
12041204
void verifyDoubleSlashReplacedWithSingleOne() {
12051205
String path = UriComponentsBuilder.fromPath("/home/").path("/path").build().getPath();
12061206
assertThat(path).isEqualTo("/home/path");
12071207
}
1208+
12081209
}

spring-web/src/test/java/org/springframework/web/util/UriComponentsTests.java

Lines changed: 41 additions & 40 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-2020 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.
@@ -43,7 +43,6 @@ public class UriComponentsTests {
4343

4444
@Test
4545
public void expandAndEncode() {
46-
4746
UriComponents uri = UriComponentsBuilder
4847
.fromPath("/hotel list/{city} specials").queryParam("q", "{value}").build()
4948
.expand("Z\u00fcrich", "a+b").encode();
@@ -53,7 +52,6 @@ public void expandAndEncode() {
5352

5453
@Test
5554
public void encodeAndExpand() {
56-
5755
UriComponents uri = UriComponentsBuilder
5856
.fromPath("/hotel list/{city} specials").queryParam("q", "{value}").encode().build()
5957
.expand("Z\u00fcrich", "a+b");
@@ -63,88 +61,86 @@ public void encodeAndExpand() {
6361

6462
@Test
6563
public void encodeAndExpandPartially() {
66-
6764
UriComponents uri = UriComponentsBuilder
6865
.fromPath("/hotel list/{city} specials").queryParam("q", "{value}").encode()
69-
.uriVariables(Collections.singletonMap("city", "Z\u00fcrich"))
70-
.build();
66+
.uriVariables(Collections.singletonMap("city", "Z\u00fcrich")).build();
7167

7268
assertThat(uri.expand("a+b").toString()).isEqualTo("/hotel%20list/Z%C3%BCrich%20specials?q=a%2Bb");
7369
}
7470

75-
@Test // SPR-17168
71+
@Test // SPR-17168
7672
public void encodeAndExpandWithDollarSign() {
7773
UriComponents uri = UriComponentsBuilder.fromPath("/path").queryParam("q", "{value}").encode().build();
7874
assertThat(uri.expand("JavaClass$1.class").toString()).isEqualTo("/path?q=JavaClass%241.class");
7975
}
8076

8177
@Test
8278
public void toUriEncoded() throws URISyntaxException {
83-
UriComponents uriComponents = UriComponentsBuilder.fromUriString(
84-
"https://example.com/hotel list/Z\u00fcrich").build();
85-
assertThat(uriComponents.encode().toUri()).isEqualTo(new URI("https://example.com/hotel%20list/Z%C3%BCrich"));
79+
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/hotel list/Z\u00fcrich").build();
80+
assertThat(uri.encode().toUri()).isEqualTo(new URI("https://example.com/hotel%20list/Z%C3%BCrich"));
8681
}
8782

8883
@Test
8984
public void toUriNotEncoded() throws URISyntaxException {
90-
UriComponents uriComponents = UriComponentsBuilder.fromUriString(
91-
"https://example.com/hotel list/Z\u00fcrich").build();
92-
assertThat(uriComponents.toUri()).isEqualTo(new URI("https://example.com/hotel%20list/Z\u00fcrich"));
85+
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/hotel list/Z\u00fcrich").build();
86+
assertThat(uri.toUri()).isEqualTo(new URI("https://example.com/hotel%20list/Z\u00fcrich"));
9387
}
9488

9589
@Test
9690
public void toUriAlreadyEncoded() throws URISyntaxException {
97-
UriComponents uriComponents = UriComponentsBuilder.fromUriString(
98-
"https://example.com/hotel%20list/Z%C3%BCrich").build(true);
99-
UriComponents encoded = uriComponents.encode();
100-
assertThat(encoded.toUri()).isEqualTo(new URI("https://example.com/hotel%20list/Z%C3%BCrich"));
91+
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/hotel%20list/Z%C3%BCrich").build(true);
92+
assertThat(uri.encode().toUri()).isEqualTo(new URI("https://example.com/hotel%20list/Z%C3%BCrich"));
10193
}
10294

10395
@Test
10496
public void toUriWithIpv6HostAlreadyEncoded() throws URISyntaxException {
105-
UriComponents uriComponents = UriComponentsBuilder.fromUriString(
97+
UriComponents uri = UriComponentsBuilder.fromUriString(
10698
"http://[1abc:2abc:3abc::5ABC:6abc]:8080/hotel%20list/Z%C3%BCrich").build(true);
107-
UriComponents encoded = uriComponents.encode();
108-
assertThat(encoded.toUri()).isEqualTo(new URI("http://[1abc:2abc:3abc::5ABC:6abc]:8080/hotel%20list/Z%C3%BCrich"));
99+
100+
assertThat(uri.encode().toUri()).isEqualTo(
101+
new URI("http://[1abc:2abc:3abc::5ABC:6abc]:8080/hotel%20list/Z%C3%BCrich"));
109102
}
110103

111104
@Test
112105
public void expand() {
113-
UriComponents uriComponents = UriComponentsBuilder.fromUriString(
114-
"https://example.com").path("/{foo} {bar}").build();
115-
uriComponents = uriComponents.expand("1 2", "3 4");
116-
assertThat(uriComponents.getPath()).isEqualTo("/1 2 3 4");
117-
assertThat(uriComponents.toUriString()).isEqualTo("https://example.com/1 2 3 4");
106+
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com").path("/{foo} {bar}").build();
107+
uri = uri.expand("1 2", "3 4");
108+
109+
assertThat(uri.getPath()).isEqualTo("/1 2 3 4");
110+
assertThat(uri.toUriString()).isEqualTo("https://example.com/1 2 3 4");
118111
}
119112

120-
@Test // SPR-13311
113+
@Test // SPR-13311
121114
public void expandWithRegexVar() {
122115
String template = "/myurl/{name:[a-z]{1,5}}/show";
123-
UriComponents uriComponents = UriComponentsBuilder.fromUriString(template).build();
124-
uriComponents = uriComponents.expand(Collections.singletonMap("name", "test"));
125-
assertThat(uriComponents.getPath()).isEqualTo("/myurl/test/show");
116+
UriComponents uri = UriComponentsBuilder.fromUriString(template).build();
117+
uri = uri.expand(Collections.singletonMap("name", "test"));
118+
119+
assertThat(uri.getPath()).isEqualTo("/myurl/test/show");
126120
}
127121

128-
@Test // SPR-17630
122+
@Test // SPR-17630
129123
public void uirTemplateExpandWithMismatchedCurlyBraces() {
130-
assertThat(UriComponentsBuilder.fromUriString("/myurl/?q={{{{").encode().build().toUriString()).isEqualTo("/myurl/?q=%7B%7B%7B%7B");
124+
UriComponents uri = UriComponentsBuilder.fromUriString("/myurl/?q={{{{").encode().build();
125+
assertThat(uri.toUriString()).isEqualTo("/myurl/?q=%7B%7B%7B%7B");
131126
}
132127

133-
@Test // gh-22447
128+
@Test // gh-22447
134129
public void expandWithFragmentOrder() {
135-
UriComponents uriComponents = UriComponentsBuilder
130+
UriComponents uri = UriComponentsBuilder
136131
.fromUriString("https://{host}/{path}#{fragment}").build()
137132
.expand("example.com", "foo", "bar");
138133

139-
assertThat(uriComponents.toUriString()).isEqualTo("https://example.com/foo#bar");
134+
assertThat(uri.toUriString()).isEqualTo("https://example.com/foo#bar");
140135
}
141136

142-
@Test // SPR-12123
137+
@Test // SPR-12123
143138
public void port() {
144139
UriComponents uri1 = fromUriString("https://example.com:8080/bar").build();
145140
UriComponents uri2 = fromUriString("https://example.com/bar").port(8080).build();
146141
UriComponents uri3 = fromUriString("https://example.com/bar").port("{port}").build().expand(8080);
147142
UriComponents uri4 = fromUriString("https://example.com/bar").port("808{digit}").build().expand(0);
143+
148144
assertThat(uri1.getPort()).isEqualTo(8080);
149145
assertThat(uri1.toUriString()).isEqualTo("https://example.com:8080/bar");
150146
assertThat(uri2.getPort()).isEqualTo(8080);
@@ -175,20 +171,22 @@ public void invalidEncodedSequence() {
175171

176172
@Test
177173
public void normalize() {
178-
UriComponents uriComponents = UriComponentsBuilder.fromUriString("https://example.com/foo/../bar").build();
179-
assertThat(uriComponents.normalize().toString()).isEqualTo("https://example.com/bar");
174+
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/foo/../bar").build();
175+
assertThat(uri.normalize().toString()).isEqualTo("https://example.com/bar");
180176
}
181177

182178
@Test
183179
public void serializable() throws Exception {
184-
UriComponents uriComponents = UriComponentsBuilder.fromUriString(
180+
UriComponents uri = UriComponentsBuilder.fromUriString(
185181
"https://example.com").path("/{foo}").query("bar={baz}").build();
182+
186183
ByteArrayOutputStream bos = new ByteArrayOutputStream();
187184
ObjectOutputStream oos = new ObjectOutputStream(bos);
188-
oos.writeObject(uriComponents);
185+
oos.writeObject(uri);
189186
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
190187
UriComponents readObject = (UriComponents) ois.readObject();
191-
assertThat(uriComponents.toString()).isEqualTo(readObject.toString());
188+
189+
assertThat(uri.toString()).isEqualTo(readObject.toString());
192190
}
193191

194192
@Test
@@ -197,6 +195,7 @@ public void copyToUriComponentsBuilder() {
197195
UriComponentsBuilder targetBuilder = UriComponentsBuilder.newInstance();
198196
source.copyToUriComponentsBuilder(targetBuilder);
199197
UriComponents result = targetBuilder.build().encode();
198+
200199
assertThat(result.getPath()).isEqualTo("/foo/bar/ba%2Fz");
201200
assertThat(result.getPathSegments()).isEqualTo(Arrays.asList("foo", "bar", "ba%2Fz"));
202201
}
@@ -207,6 +206,7 @@ public void equalsHierarchicalUriComponents() {
207206
UriComponents uric1 = UriComponentsBuilder.fromUriString(url).path("/{foo}").query("bar={baz}").build();
208207
UriComponents uric2 = UriComponentsBuilder.fromUriString(url).path("/{foo}").query("bar={baz}").build();
209208
UriComponents uric3 = UriComponentsBuilder.fromUriString(url).path("/{foo}").query("bin={baz}").build();
209+
210210
assertThat(uric1).isInstanceOf(HierarchicalUriComponents.class);
211211
assertThat(uric1).isEqualTo(uric1);
212212
assertThat(uric1).isEqualTo(uric2);
@@ -219,6 +219,7 @@ public void equalsOpaqueUriComponents() {
219219
UriComponents uric1 = UriComponentsBuilder.fromUriString(baseUrl + "/foo/bar").build();
220220
UriComponents uric2 = UriComponentsBuilder.fromUriString(baseUrl + "/foo/bar").build();
221221
UriComponents uric3 = UriComponentsBuilder.fromUriString(baseUrl + "/foo/bin").build();
222+
222223
assertThat(uric1).isInstanceOf(OpaqueUriComponents.class);
223224
assertThat(uric1).isEqualTo(uric1);
224225
assertThat(uric1).isEqualTo(uric2);

spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java

Lines changed: 11 additions & 15 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-2020 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.
@@ -49,9 +49,7 @@ public void expandVarArgs() throws Exception {
4949
assertThat(result).as("Invalid expanded template").isEqualTo(new URI("/hotels/1/bookings/42"));
5050
}
5151

52-
// SPR-9712
53-
54-
@Test
52+
@Test // SPR-9712
5553
public void expandVarArgsWithArrayValue() throws Exception {
5654
UriTemplate template = new UriTemplate("/sum?numbers={numbers}");
5755
URI result = template.expand(new int[] {1, 2, 3});
@@ -61,8 +59,7 @@ public void expandVarArgsWithArrayValue() throws Exception {
6159
@Test
6260
public void expandVarArgsNotEnoughVariables() throws Exception {
6361
UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}");
64-
assertThatIllegalArgumentException().isThrownBy(() ->
65-
template.expand("1"));
62+
assertThatIllegalArgumentException().isThrownBy(() -> template.expand("1"));
6663
}
6764

6865
@Test
@@ -156,7 +153,7 @@ public void matchCustomRegex() throws Exception {
156153
assertThat(result).as("Invalid match").isEqualTo(expected);
157154
}
158155

159-
@Test // SPR-13627
156+
@Test // SPR-13627
160157
public void matchCustomRegexWithNestedCurlyBraces() throws Exception {
161158
UriTemplate template = new UriTemplate("/site.{domain:co.[a-z]{2}}");
162159
Map<String, String> result = template.match("/site.co.eu");
@@ -181,8 +178,8 @@ public void matchMultipleInOneSegment() throws Exception {
181178
assertThat(result).as("Invalid match").isEqualTo(expected);
182179
}
183180

184-
@Test // SPR-16169
185-
public void matchWithMultipleSegmentsAtTheEnd() {
181+
@Test // SPR-16169
182+
public void matchWithMultipleSegmentsAtTheEnd() throws Exception {
186183
UriTemplate template = new UriTemplate("/account/{accountId}");
187184
assertThat(template.matches("/account/15/alias/5")).isFalse();
188185
}
@@ -202,21 +199,20 @@ public void fragments() throws Exception {
202199
assertThat(template.matches("/search?query=foo#bar")).isTrue();
203200
}
204201

205-
@Test // SPR-13705
206-
public void matchesWithSlashAtTheEnd() {
207-
UriTemplate uriTemplate = new UriTemplate("/test/");
208-
assertThat(uriTemplate.matches("/test/")).isTrue();
202+
@Test // SPR-13705
203+
public void matchesWithSlashAtTheEnd() throws Exception {
204+
assertThat(new UriTemplate("/test/").matches("/test/")).isTrue();
209205
}
210206

211207
@Test
212-
public void expandWithDollar() {
208+
public void expandWithDollar() throws Exception {
213209
UriTemplate template = new UriTemplate("/{a}");
214210
URI uri = template.expand("$replacement");
215211
assertThat(uri.toString()).isEqualTo("/$replacement");
216212
}
217213

218214
@Test
219-
public void expandWithAtSign() {
215+
public void expandWithAtSign() throws Exception {
220216
UriTemplate template = new UriTemplate("http://localhost/query={query}");
221217
URI uri = template.expand("foo@bar");
222218
assertThat(uri.toString()).isEqualTo("http://localhost/query=foo@bar");

0 commit comments

Comments
 (0)