Skip to content

Commit e1a0c50

Browse files
committed
Revised UriComponentsBuilder assertions
Issue: SPR-13257
1 parent 291550a commit e1a0c50

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public static UriComponentsBuilder fromUri(URI uri) {
183183
* @return the new {@code UriComponentsBuilder}
184184
*/
185185
public static UriComponentsBuilder fromUriString(String uri) {
186-
Assert.notNull(uri, "'uri' must not be null");
186+
Assert.notNull(uri, "URI must not be null");
187187
Matcher matcher = URI_PATTERN.matcher(uri);
188188
if (matcher.matches()) {
189189
UriComponentsBuilder builder = new UriComponentsBuilder();
@@ -243,7 +243,7 @@ public static UriComponentsBuilder fromUriString(String uri) {
243243
* @return the URI components of the URI
244244
*/
245245
public static UriComponentsBuilder fromHttpUrl(String httpUrl) {
246-
Assert.notNull(httpUrl, "'httpUrl' must not be null");
246+
Assert.notNull(httpUrl, "HTTP URL must not be null");
247247
Matcher matcher = HTTP_URL_PATTERN.matcher(httpUrl);
248248
if (matcher.matches()) {
249249
UriComponentsBuilder builder = new UriComponentsBuilder();
@@ -429,7 +429,7 @@ public String toUriString() {
429429
* @return this UriComponentsBuilder
430430
*/
431431
public UriComponentsBuilder uri(URI uri) {
432-
Assert.notNull(uri, "'uri' must not be null");
432+
Assert.notNull(uri, "URI must not be null");
433433
this.scheme = uri.getScheme();
434434
if (uri.isOpaque()) {
435435
this.ssp = uri.getRawSchemeSpecificPart();
@@ -489,7 +489,7 @@ public UriComponentsBuilder scheme(String scheme) {
489489
* @return this UriComponentsBuilder
490490
*/
491491
public UriComponentsBuilder uriComponents(UriComponents uriComponents) {
492-
Assert.notNull(uriComponents, "'uriComponents' must not be null");
492+
Assert.notNull(uriComponents, "UriComponents must not be null");
493493
uriComponents.copyToUriComponentsBuilder(this);
494494
return this;
495495
}
@@ -538,7 +538,7 @@ public UriComponentsBuilder host(String host) {
538538
* @return this UriComponentsBuilder
539539
*/
540540
public UriComponentsBuilder port(int port) {
541-
Assert.isTrue(port >= -1, "'port' must not be < -1");
541+
Assert.isTrue(port >= -1, "Port must be >= -1");
542542
this.port = String.valueOf(port);
543543
resetSchemeSpecificPart();
544544
return this;
@@ -587,7 +587,6 @@ public UriComponentsBuilder replacePath(String path) {
587587
* @return this UriComponentsBuilder
588588
*/
589589
public UriComponentsBuilder pathSegment(String... pathSegments) throws IllegalArgumentException {
590-
Assert.notNull(pathSegments, "'segments' must not be null");
591590
this.pathBuilder.addPathSegments(pathSegments);
592591
resetSchemeSpecificPart();
593592
return this;
@@ -647,7 +646,7 @@ public UriComponentsBuilder replaceQuery(String query) {
647646
* @return this UriComponentsBuilder
648647
*/
649648
public UriComponentsBuilder queryParam(String name, Object... values) {
650-
Assert.notNull(name, "'name' must not be null");
649+
Assert.notNull(name, "Name must not be null");
651650
if (!ObjectUtils.isEmpty(values)) {
652651
for (Object value : values) {
653652
String valueAsString = (value != null ? value.toString() : null);
@@ -667,8 +666,9 @@ public UriComponentsBuilder queryParam(String name, Object... values) {
667666
* @return this UriComponentsBuilder
668667
*/
669668
public UriComponentsBuilder queryParams(MultiValueMap<String, String> params) {
670-
Assert.notNull(params, "'params' must not be null");
671-
this.queryParams.putAll(params);
669+
if (params != null) {
670+
this.queryParams.putAll(params);
671+
}
672672
return this;
673673
}
674674

@@ -680,7 +680,7 @@ public UriComponentsBuilder queryParams(MultiValueMap<String, String> params) {
680680
* @return this UriComponentsBuilder
681681
*/
682682
public UriComponentsBuilder replaceQueryParam(String name, Object... values) {
683-
Assert.notNull(name, "'name' must not be null");
683+
Assert.notNull(name, "Name must not be null");
684684
this.queryParams.remove(name);
685685
if (!ObjectUtils.isEmpty(values)) {
686686
queryParam(name, values);
@@ -695,9 +695,10 @@ public UriComponentsBuilder replaceQueryParam(String name, Object... values) {
695695
* @return this UriComponentsBuilder
696696
*/
697697
public UriComponentsBuilder replaceQueryParams(MultiValueMap<String, String> params) {
698-
Assert.notNull(params, "'params' must not be null");
699698
this.queryParams.clear();
700-
this.queryParams.putAll(params);
699+
if (params != null) {
700+
this.queryParams.putAll(params);
701+
}
701702
return this;
702703
}
703704

@@ -709,7 +710,7 @@ public UriComponentsBuilder replaceQueryParams(MultiValueMap<String, String> par
709710
*/
710711
public UriComponentsBuilder fragment(String fragment) {
711712
if (fragment != null) {
712-
Assert.hasLength(fragment, "'fragment' must not be empty");
713+
Assert.hasLength(fragment, "Fragment must not be empty");
713714
this.fragment = fragment;
714715
}
715716
else {

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616

1717
package org.springframework.web.util;
1818

19-
import static org.hamcrest.Matchers.*;
20-
import static org.junit.Assert.*;
21-
2219
import java.net.URI;
2320
import java.net.URISyntaxException;
2421
import java.util.Arrays;
@@ -35,12 +32,16 @@
3532
import org.springframework.util.MultiValueMap;
3633
import org.springframework.util.StringUtils;
3734

35+
import static org.hamcrest.Matchers.*;
36+
import static org.junit.Assert.*;
37+
3838
/**
3939
* Unit tests for {@link org.springframework.web.util.UriComponentsBuilder}.
4040
*
4141
* @author Arjen Poutsma
4242
* @author Phillip Webb
4343
* @author Oliver Gierke
44+
* @author David Eckel
4445
*/
4546
public class UriComponentsBuilderTests {
4647

@@ -650,6 +651,12 @@ public void parsesEmptyFragment() {
650651
assertThat(components.toString(), equalTo("/example"));
651652
}
652653

654+
@Test // SPR-13257
655+
public void parsesEmptyUri() {
656+
UriComponents components = UriComponentsBuilder.fromUriString("").build();
657+
assertThat(components.toString(), equalTo(""));
658+
}
659+
653660
@Test
654661
public void testClone() throws URISyntaxException {
655662
UriComponentsBuilder builder1 = UriComponentsBuilder.newInstance();
@@ -740,5 +747,4 @@ public void fromHttpRequestMultipleForwardedHeaderComma() throws Exception {
740747
assertEquals("/rest/mobile/users/1", result.getPath());
741748
}
742749

743-
744750
}

0 commit comments

Comments
 (0)