34
34
* @author Juergen Hoeller
35
35
* @author Rossen Stoyanchev
36
36
*/
37
- public class UriTemplateTests {
37
+ class UriTemplateTests {
38
38
39
39
@ Test
40
40
void emptyPathDoesNotThrowException () {
@@ -47,70 +47,84 @@ void nullPathThrowsException() {
47
47
}
48
48
49
49
@ Test
50
- public void getVariableNames () throws Exception {
50
+ void getVariableNames () {
51
51
UriTemplate template = new UriTemplate ("/hotels/{hotel}/bookings/{booking}" );
52
52
List <String > variableNames = template .getVariableNames ();
53
53
assertThat (variableNames ).as ("Invalid variable names" ).isEqualTo (Arrays .asList ("hotel" , "booking" ));
54
54
}
55
55
56
56
@ 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 () {
58
65
UriTemplate template = new UriTemplate ("/hotels/{hotel}/bookings/{booking}" );
59
66
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 ("" ));
61
75
}
62
76
63
77
@ Test // SPR-9712
64
- public void expandVarArgsWithArrayValue () throws Exception {
78
+ void expandVarArgsWithArrayValue () {
65
79
UriTemplate template = new UriTemplate ("/sum?numbers={numbers}" );
66
80
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" ));
68
82
}
69
83
70
84
@ Test
71
- public void expandVarArgsNotEnoughVariables () throws Exception {
85
+ void expandVarArgsNotEnoughVariables () {
72
86
UriTemplate template = new UriTemplate ("/hotels/{hotel}/bookings/{booking}" );
73
87
assertThatIllegalArgumentException ().isThrownBy (() -> template .expand ("1" ));
74
88
}
75
89
76
90
@ Test
77
- public void expandMap () throws Exception {
91
+ void expandMap () {
78
92
Map <String , String > uriVariables = new HashMap <>(2 );
79
93
uriVariables .put ("booking" , "42" );
80
94
uriVariables .put ("hotel" , "1" );
81
95
UriTemplate template = new UriTemplate ("/hotels/{hotel}/bookings/{booking}" );
82
96
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" ));
84
98
}
85
99
86
100
@ Test
87
- public void expandMapDuplicateVariables () throws Exception {
101
+ void expandMapDuplicateVariables () {
88
102
UriTemplate template = new UriTemplate ("/order/{c}/{c}/{c}" );
89
103
assertThat (template .getVariableNames ()).isEqualTo (Arrays .asList ("c" , "c" , "c" ));
90
104
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" ));
92
106
}
93
107
94
108
@ Test
95
- public void expandMapNonString () throws Exception {
109
+ void expandMapNonString () {
96
110
Map <String , Integer > uriVariables = new HashMap <>(2 );
97
111
uriVariables .put ("booking" , 42 );
98
112
uriVariables .put ("hotel" , 1 );
99
113
UriTemplate template = new UriTemplate ("/hotels/{hotel}/bookings/{booking}" );
100
114
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" ));
102
116
}
103
117
104
118
@ Test
105
- public void expandMapEncoded () throws Exception {
119
+ void expandMapEncoded () {
106
120
Map <String , String > uriVariables = Collections .singletonMap ("hotel" , "Z\u00fc rich" );
107
121
UriTemplate template = new UriTemplate ("/hotel list/{hotel}" );
108
122
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" ));
110
124
}
111
125
112
126
@ Test
113
- public void expandMapUnboundVariables () throws Exception {
127
+ void expandMapUnboundVariables () {
114
128
Map <String , String > uriVariables = new HashMap <>(2 );
115
129
uriVariables .put ("booking" , "42" );
116
130
uriVariables .put ("bar" , "1" );
@@ -120,14 +134,14 @@ public void expandMapUnboundVariables() throws Exception {
120
134
}
121
135
122
136
@ Test
123
- public void expandEncoded () throws Exception {
137
+ void expandEncoded () {
124
138
UriTemplate template = new UriTemplate ("/hotel list/{hotel}" );
125
139
URI result = template .expand ("Z\u00fc rich" );
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" ));
127
141
}
128
142
129
143
@ Test
130
- public void matches () throws Exception {
144
+ void matches () {
131
145
UriTemplate template = new UriTemplate ("/hotels/{hotel}/bookings/{booking}" );
132
146
assertThat (template .matches ("/hotels/1/bookings/42" )).as ("UriTemplate does not match" ).isTrue ();
133
147
assertThat (template .matches ("/hotels/bookings" )).as ("UriTemplate matches" ).isFalse ();
@@ -136,14 +150,23 @@ public void matches() throws Exception {
136
150
}
137
151
138
152
@ 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 () {
140
163
UriTemplate template = new UriTemplate ("/hotels/{hotel:\\ d+}" );
141
164
assertThat (template .matches ("/hotels/42" )).as ("UriTemplate does not match" ).isTrue ();
142
165
assertThat (template .matches ("/hotels/foo" )).as ("UriTemplate matches" ).isFalse ();
143
166
}
144
167
145
168
@ Test
146
- public void match () throws Exception {
169
+ void match () {
147
170
Map <String , String > expected = new HashMap <>(2 );
148
171
expected .put ("booking" , "42" );
149
172
expected .put ("hotel" , "1" );
@@ -154,7 +177,14 @@ public void match() throws Exception {
154
177
}
155
178
156
179
@ 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 () {
158
188
Map <String , String > expected = new HashMap <>(2 );
159
189
expected .put ("booking" , "42" );
160
190
expected .put ("hotel" , "1" );
@@ -165,22 +195,22 @@ public void matchCustomRegex() throws Exception {
165
195
}
166
196
167
197
@ Test // SPR-13627
168
- public void matchCustomRegexWithNestedCurlyBraces () throws Exception {
198
+ void matchCustomRegexWithNestedCurlyBraces () {
169
199
UriTemplate template = new UriTemplate ("/site.{domain:co.[a-z]{2}}" );
170
200
Map <String , String > result = template .match ("/site.co.eu" );
171
201
assertThat (result ).as ("Invalid match" ).isEqualTo (Collections .singletonMap ("domain" , "co.eu" ));
172
202
}
173
203
174
204
@ Test
175
- public void matchDuplicate () throws Exception {
205
+ void matchDuplicate () {
176
206
UriTemplate template = new UriTemplate ("/order/{c}/{c}/{c}" );
177
207
Map <String , String > result = template .match ("/order/cheeseburger/cheeseburger/cheeseburger" );
178
208
Map <String , String > expected = Collections .singletonMap ("c" , "cheeseburger" );
179
209
assertThat (result ).as ("Invalid match" ).isEqualTo (expected );
180
210
}
181
211
182
212
@ Test
183
- public void matchMultipleInOneSegment () throws Exception {
213
+ void matchMultipleInOneSegment () {
184
214
UriTemplate template = new UriTemplate ("/{foo}-{bar}" );
185
215
Map <String , String > result = template .match ("/12-34" );
186
216
Map <String , String > expected = new HashMap <>(2 );
@@ -190,19 +220,19 @@ public void matchMultipleInOneSegment() throws Exception {
190
220
}
191
221
192
222
@ Test // SPR-16169
193
- public void matchWithMultipleSegmentsAtTheEnd () throws Exception {
223
+ void matchWithMultipleSegmentsAtTheEnd () {
194
224
UriTemplate template = new UriTemplate ("/account/{accountId}" );
195
225
assertThat (template .matches ("/account/15/alias/5" )).isFalse ();
196
226
}
197
227
198
228
@ Test
199
- public void queryVariables () throws Exception {
229
+ void queryVariables () {
200
230
UriTemplate template = new UriTemplate ("/search?q={query}" );
201
231
assertThat (template .matches ("/search?q=foo" )).isTrue ();
202
232
}
203
233
204
234
@ Test
205
- public void fragments () throws Exception {
235
+ void fragments () {
206
236
UriTemplate template = new UriTemplate ("/search#{fragment}" );
207
237
assertThat (template .matches ("/search#foo" )).isTrue ();
208
238
@@ -211,19 +241,19 @@ public void fragments() throws Exception {
211
241
}
212
242
213
243
@ Test // SPR-13705
214
- public void matchesWithSlashAtTheEnd () throws Exception {
244
+ void matchesWithSlashAtTheEnd () {
215
245
assertThat (new UriTemplate ("/test/" ).matches ("/test/" )).isTrue ();
216
246
}
217
247
218
248
@ Test
219
- public void expandWithDollar () throws Exception {
249
+ void expandWithDollar () {
220
250
UriTemplate template = new UriTemplate ("/{a}" );
221
251
URI uri = template .expand ("$replacement" );
222
252
assertThat (uri .toString ()).isEqualTo ("/$replacement" );
223
253
}
224
254
225
255
@ Test
226
- public void expandWithAtSign () throws Exception {
256
+ void expandWithAtSign () {
227
257
UriTemplate template = new UriTemplate ("http://localhost/query={query}" );
228
258
URI uri = template .expand ("foo@bar" );
229
259
assertThat (uri .toString ()).isEqualTo ("http://localhost/query=foo@bar" );
0 commit comments