Skip to content

Commit 274fba4

Browse files
committed
Additional unit tests for operations on empty UriTemplate
See gh-32432 (cherry picked from commit 54a6d89)
1 parent 5dfec09 commit 274fba4

File tree

1 file changed

+61
-31
lines changed

1 file changed

+61
-31
lines changed

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

+61-31
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
* @author Juergen Hoeller
3535
* @author Rossen Stoyanchev
3636
*/
37-
public class UriTemplateTests {
37+
class UriTemplateTests {
3838

3939
@Test
4040
void emptyPathDoesNotThrowException() {
@@ -47,70 +47,84 @@ void nullPathThrowsException() {
4747
}
4848

4949
@Test
50-
public void getVariableNames() throws Exception {
50+
void getVariableNames() {
5151
UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}");
5252
List<String> variableNames = template.getVariableNames();
5353
assertThat(variableNames).as("Invalid variable names").isEqualTo(Arrays.asList("hotel", "booking"));
5454
}
5555

5656
@Test
57-
public void expandVarArgs() throws Exception {
57+
void getVariableNamesFromEmpty() {
58+
UriTemplate template = new UriTemplate("");
59+
List<String> variableNames = template.getVariableNames();
60+
assertThat(variableNames).isEmpty();
61+
}
62+
63+
@Test
64+
void expandVarArgs() {
5865
UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}");
5966
URI result = template.expand("1", "42");
60-
assertThat(result).as("Invalid expanded template").isEqualTo(new URI("/hotels/1/bookings/42"));
67+
assertThat(result).as("Invalid expanded template").isEqualTo(URI.create("/hotels/1/bookings/42"));
68+
}
69+
70+
@Test
71+
void expandVarArgsFromEmpty() {
72+
UriTemplate template = new UriTemplate("");
73+
URI result = template.expand();
74+
assertThat(result).as("Invalid expanded template").isEqualTo(URI.create(""));
6175
}
6276

6377
@Test // SPR-9712
64-
public void expandVarArgsWithArrayValue() throws Exception {
78+
void expandVarArgsWithArrayValue() {
6579
UriTemplate template = new UriTemplate("/sum?numbers={numbers}");
6680
URI result = template.expand(new int[] {1, 2, 3});
67-
assertThat(result).isEqualTo(new URI("/sum?numbers=1,2,3"));
81+
assertThat(result).isEqualTo(URI.create("/sum?numbers=1,2,3"));
6882
}
6983

7084
@Test
71-
public void expandVarArgsNotEnoughVariables() throws Exception {
85+
void expandVarArgsNotEnoughVariables() {
7286
UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}");
7387
assertThatIllegalArgumentException().isThrownBy(() -> template.expand("1"));
7488
}
7589

7690
@Test
77-
public void expandMap() throws Exception {
91+
void expandMap() {
7892
Map<String, String> uriVariables = new HashMap<>(2);
7993
uriVariables.put("booking", "42");
8094
uriVariables.put("hotel", "1");
8195
UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}");
8296
URI result = template.expand(uriVariables);
83-
assertThat(result).as("Invalid expanded template").isEqualTo(new URI("/hotels/1/bookings/42"));
97+
assertThat(result).as("Invalid expanded template").isEqualTo(URI.create("/hotels/1/bookings/42"));
8498
}
8599

86100
@Test
87-
public void expandMapDuplicateVariables() throws Exception {
101+
void expandMapDuplicateVariables() {
88102
UriTemplate template = new UriTemplate("/order/{c}/{c}/{c}");
89103
assertThat(template.getVariableNames()).isEqualTo(Arrays.asList("c", "c", "c"));
90104
URI result = template.expand(Collections.singletonMap("c", "cheeseburger"));
91-
assertThat(result).isEqualTo(new URI("/order/cheeseburger/cheeseburger/cheeseburger"));
105+
assertThat(result).isEqualTo(URI.create("/order/cheeseburger/cheeseburger/cheeseburger"));
92106
}
93107

94108
@Test
95-
public void expandMapNonString() throws Exception {
109+
void expandMapNonString() {
96110
Map<String, Integer> uriVariables = new HashMap<>(2);
97111
uriVariables.put("booking", 42);
98112
uriVariables.put("hotel", 1);
99113
UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}");
100114
URI result = template.expand(uriVariables);
101-
assertThat(result).as("Invalid expanded template").isEqualTo(new URI("/hotels/1/bookings/42"));
115+
assertThat(result).as("Invalid expanded template").isEqualTo(URI.create("/hotels/1/bookings/42"));
102116
}
103117

104118
@Test
105-
public void expandMapEncoded() throws Exception {
119+
void expandMapEncoded() {
106120
Map<String, String> uriVariables = Collections.singletonMap("hotel", "Z\u00fcrich");
107121
UriTemplate template = new UriTemplate("/hotel list/{hotel}");
108122
URI result = template.expand(uriVariables);
109-
assertThat(result).as("Invalid expanded template").isEqualTo(new URI("/hotel%20list/Z%C3%BCrich"));
123+
assertThat(result).as("Invalid expanded template").isEqualTo(URI.create("/hotel%20list/Z%C3%BCrich"));
110124
}
111125

112126
@Test
113-
public void expandMapUnboundVariables() throws Exception {
127+
void expandMapUnboundVariables() {
114128
Map<String, String> uriVariables = new HashMap<>(2);
115129
uriVariables.put("booking", "42");
116130
uriVariables.put("bar", "1");
@@ -120,14 +134,14 @@ public void expandMapUnboundVariables() throws Exception {
120134
}
121135

122136
@Test
123-
public void expandEncoded() throws Exception {
137+
void expandEncoded() {
124138
UriTemplate template = new UriTemplate("/hotel list/{hotel}");
125139
URI result = template.expand("Z\u00fcrich");
126-
assertThat(result).as("Invalid expanded template").isEqualTo(new URI("/hotel%20list/Z%C3%BCrich"));
140+
assertThat(result).as("Invalid expanded template").isEqualTo(URI.create("/hotel%20list/Z%C3%BCrich"));
127141
}
128142

129143
@Test
130-
public void matches() throws Exception {
144+
void matches() {
131145
UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}");
132146
assertThat(template.matches("/hotels/1/bookings/42")).as("UriTemplate does not match").isTrue();
133147
assertThat(template.matches("/hotels/bookings")).as("UriTemplate matches").isFalse();
@@ -136,14 +150,23 @@ public void matches() throws Exception {
136150
}
137151

138152
@Test
139-
public void matchesCustomRegex() throws Exception {
153+
void matchesAgainstEmpty() {
154+
UriTemplate template = new UriTemplate("");
155+
assertThat(template.matches("/hotels/1/bookings/42")).as("UriTemplate matches").isFalse();
156+
assertThat(template.matches("/hotels/bookings")).as("UriTemplate matches").isFalse();
157+
assertThat(template.matches("")).as("UriTemplate does not match").isTrue();
158+
assertThat(template.matches(null)).as("UriTemplate matches").isFalse();
159+
}
160+
161+
@Test
162+
void matchesCustomRegex() {
140163
UriTemplate template = new UriTemplate("/hotels/{hotel:\\d+}");
141164
assertThat(template.matches("/hotels/42")).as("UriTemplate does not match").isTrue();
142165
assertThat(template.matches("/hotels/foo")).as("UriTemplate matches").isFalse();
143166
}
144167

145168
@Test
146-
public void match() throws Exception {
169+
void match() {
147170
Map<String, String> expected = new HashMap<>(2);
148171
expected.put("booking", "42");
149172
expected.put("hotel", "1");
@@ -154,7 +177,14 @@ public void match() throws Exception {
154177
}
155178

156179
@Test
157-
public void matchCustomRegex() throws Exception {
180+
void matchAgainstEmpty() {
181+
UriTemplate template = new UriTemplate("");
182+
Map<String, String> result = template.match("/hotels/1/bookings/42");
183+
assertThat(result).as("Invalid match").isEmpty();
184+
}
185+
186+
@Test
187+
void matchCustomRegex() {
158188
Map<String, String> expected = new HashMap<>(2);
159189
expected.put("booking", "42");
160190
expected.put("hotel", "1");
@@ -165,22 +195,22 @@ public void matchCustomRegex() throws Exception {
165195
}
166196

167197
@Test // SPR-13627
168-
public void matchCustomRegexWithNestedCurlyBraces() throws Exception {
198+
void matchCustomRegexWithNestedCurlyBraces() {
169199
UriTemplate template = new UriTemplate("/site.{domain:co.[a-z]{2}}");
170200
Map<String, String> result = template.match("/site.co.eu");
171201
assertThat(result).as("Invalid match").isEqualTo(Collections.singletonMap("domain", "co.eu"));
172202
}
173203

174204
@Test
175-
public void matchDuplicate() throws Exception {
205+
void matchDuplicate() {
176206
UriTemplate template = new UriTemplate("/order/{c}/{c}/{c}");
177207
Map<String, String> result = template.match("/order/cheeseburger/cheeseburger/cheeseburger");
178208
Map<String, String> expected = Collections.singletonMap("c", "cheeseburger");
179209
assertThat(result).as("Invalid match").isEqualTo(expected);
180210
}
181211

182212
@Test
183-
public void matchMultipleInOneSegment() throws Exception {
213+
void matchMultipleInOneSegment() {
184214
UriTemplate template = new UriTemplate("/{foo}-{bar}");
185215
Map<String, String> result = template.match("/12-34");
186216
Map<String, String> expected = new HashMap<>(2);
@@ -190,19 +220,19 @@ public void matchMultipleInOneSegment() throws Exception {
190220
}
191221

192222
@Test // SPR-16169
193-
public void matchWithMultipleSegmentsAtTheEnd() throws Exception {
223+
void matchWithMultipleSegmentsAtTheEnd() {
194224
UriTemplate template = new UriTemplate("/account/{accountId}");
195225
assertThat(template.matches("/account/15/alias/5")).isFalse();
196226
}
197227

198228
@Test
199-
public void queryVariables() throws Exception {
229+
void queryVariables() {
200230
UriTemplate template = new UriTemplate("/search?q={query}");
201231
assertThat(template.matches("/search?q=foo")).isTrue();
202232
}
203233

204234
@Test
205-
public void fragments() throws Exception {
235+
void fragments() {
206236
UriTemplate template = new UriTemplate("/search#{fragment}");
207237
assertThat(template.matches("/search#foo")).isTrue();
208238

@@ -211,19 +241,19 @@ public void fragments() throws Exception {
211241
}
212242

213243
@Test // SPR-13705
214-
public void matchesWithSlashAtTheEnd() throws Exception {
244+
void matchesWithSlashAtTheEnd() {
215245
assertThat(new UriTemplate("/test/").matches("/test/")).isTrue();
216246
}
217247

218248
@Test
219-
public void expandWithDollar() throws Exception {
249+
void expandWithDollar() {
220250
UriTemplate template = new UriTemplate("/{a}");
221251
URI uri = template.expand("$replacement");
222252
assertThat(uri.toString()).isEqualTo("/$replacement");
223253
}
224254

225255
@Test
226-
public void expandWithAtSign() throws Exception {
256+
void expandWithAtSign() {
227257
UriTemplate template = new UriTemplate("http://localhost/query={query}");
228258
URI uri = template.expand("foo@bar");
229259
assertThat(uri.toString()).isEqualTo("http://localhost/query=foo@bar");

0 commit comments

Comments
 (0)