diff --git a/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java b/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java index 895ba266a0..1355a991cf 100644 --- a/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java +++ b/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java @@ -664,7 +664,7 @@ final class HierarchicalUriComponents extends UriComponents { private final List pathSegments; public PathSegmentComponent(List pathSegments) { - this.pathSegments = Collections.unmodifiableList(pathSegments); + this.pathSegments = Collections.unmodifiableList(new ArrayList(pathSegments)); } @Override diff --git a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java index 2fb397a1aa..1e1c2ffa78 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java @@ -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; 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 { 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();