Browse Source

UriComponentBuilder allows for multiple independent build() calls on same builder instance

Issue: SPR-11885
pull/568/head
Juergen Hoeller 11 years ago
parent
commit
d239016a8c
  1. 2
      spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java
  2. 25
      spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java

2
spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java

@ -664,7 +664,7 @@ final class HierarchicalUriComponents extends UriComponents { @@ -664,7 +664,7 @@ final class HierarchicalUriComponents extends UriComponents {
private final List<String> pathSegments;
public PathSegmentComponent(List<String> pathSegments) {
this.pathSegments = Collections.unmodifiableList(pathSegments);
this.pathSegments = Collections.unmodifiableList(new ArrayList<String>(pathSegments));
}
@Override

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ import java.util.HashMap; @@ -23,6 +23,7 @@ import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@ -50,6 +51,28 @@ public class UriComponentsBuilderTests { @@ -50,6 +51,28 @@ public class UriComponentsBuilderTests {
assertEquals("Invalid result URI", expected, result.toUri());
}
@Test
public void multipleFromSameBuilder() throws URISyntaxException {
UriComponentsBuilder builder = UriComponentsBuilder.newInstance().scheme("http").host("example.com").pathSegment("foo");
UriComponents result1 = builder.build();
builder = builder.pathSegment("foo2").queryParam("bar").fragment("baz");
UriComponents result2 = builder.build();
assertEquals("http", result1.getScheme());
assertEquals("example.com", result1.getHost());
assertEquals("/foo", result1.getPath());
URI expected = new URI("http://example.com/foo");
assertEquals("Invalid result URI", expected, result1.toUri());
assertEquals("http", result2.getScheme());
assertEquals("example.com", result2.getHost());
assertEquals("/foo/foo2", result2.getPath());
assertEquals("bar", result2.getQuery());
assertEquals("baz", result2.getFragment());
expected = new URI("http://example.com/foo/foo2?bar#baz");
assertEquals("Invalid result URI", expected, result2.toUri());
}
@Test
public void fromPath() throws URISyntaxException {
UriComponents result = UriComponentsBuilder.fromPath("foo").queryParam("bar").fragment("baz").build();

Loading…
Cancel
Save