SPR-5973: Using UriComponents in more places, replaced UriBuilder by UriComponentsBuilder, UriComponents is now immutable.

This commit is contained in:
Arjen Poutsma
2011-09-12 14:39:58 +00:00
parent ce8bc8e7e4
commit c8add61a72
12 changed files with 1344 additions and 1644 deletions

View File

@@ -1,131 +0,0 @@
/*
* Copyright 2002-2011 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.util;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
/** @author Arjen Poutsma */
public class UriBuilderTests {
@Test
public void plain() throws URISyntaxException {
UriBuilder builder = UriBuilder.newInstance();
URI result = builder.scheme("http").host("example.com").path("foo").queryParam("bar").fragment("baz").build();
URI expected = new URI("http://example.com/foo?bar#baz");
assertEquals("Invalid result URI", expected, result);
}
@Test
public void fromPath() throws URISyntaxException {
URI result = UriBuilder.fromPath("foo").queryParam("bar").fragment("baz").build();
URI expected = new URI("/foo?bar#baz");
assertEquals("Invalid result URI", expected, result);
}
@Test
public void fromUri() throws URISyntaxException {
URI uri = new URI("http://example.com/foo?bar#baz");
URI result = UriBuilder.fromUri(uri).build();
assertEquals("Invalid result URI", uri, result);
}
@Test
@Ignore("Working on it")
public void templateVarsVarArgs() throws URISyntaxException {
URI result = UriBuilder.fromPath("/{foo}/{bar}").build("baz", "qux");
URI expected = new URI("http://example.com/baz/qux");
assertEquals("Invalid result URI", expected, result);
}
@Test
public void templateVarsEncoded() throws URISyntaxException, UnsupportedEncodingException {
URI result = UriBuilder.fromPath("{foo}").build("bar baz");
URI expected = new URI("/bar%20baz");
assertEquals("Invalid result URI", expected, result);
}
@Test
public void templateVarsNotEncoded() throws URISyntaxException {
UriBuilder builder = UriBuilder.newInstance();
URI result = builder.scheme("http").host("example.com").path("{foo}").buildFromEncoded("bar%20baz");
URI expected = new URI("http://example.com/bar%20baz");
assertEquals("Invalid result URI", expected, result);
}
@Test
public void templateVarsMap() throws URISyntaxException {
Map<String, String> vars = new HashMap<String, String>(2);
vars.put("bar", "qux");
vars.put("foo", "baz");
URI result = UriBuilder.fromPath("/{foo}/{bar}").build(vars);
URI expected = new URI("/baz/qux");
assertEquals("Invalid result URI", expected, result);
}
@Test
public void unusedTemplateVars() throws URISyntaxException {
UriBuilder builder = UriBuilder.newInstance();
URI result = builder.scheme("http").host("example.com").path("{foo}").build();
URI expected = new URI("http://example.com/%7Bfoo%7D");
assertEquals("Invalid result URI", expected, result);
}
@Test
public void pathSegments() throws URISyntaxException {
UriBuilder builder = UriBuilder.newInstance();
URI result = builder.pathSegment("foo").pathSegment("bar").build();
URI expected = new URI("/foo/bar");
assertEquals("Invalid result URI", expected, result);
}
@Test
public void queryParam() throws URISyntaxException {
UriBuilder builder = UriBuilder.newInstance();
URI result = builder.queryParam("baz", "qux", 42).build();
URI expected = new URI("?baz=qux&baz=42");
assertEquals("Invalid result URI", expected, result);
}
@Test
public void emptyQueryParam() throws URISyntaxException {
UriBuilder builder = UriBuilder.newInstance();
URI result = builder.queryParam("baz").build();
URI expected = new URI("?baz");
assertEquals("Invalid result URI", expected, result);
}
}

View File

@@ -0,0 +1,110 @@
/*
* Copyright 2002-2011 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.util;
import java.net.URI;
import java.net.URISyntaxException;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/** @author Arjen Poutsma */
public class UriComponentsBuilderTests {
@Test
public void plain() throws URISyntaxException {
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
UriComponents result = builder.scheme("http").host("example.com").path("foo").queryParam("bar").fragment("baz").build();
assertEquals("http", result.getScheme());
assertEquals("example.com", result.getHost());
assertEquals("/foo", result.getPath());
assertEquals("bar", result.getQuery());
assertEquals("baz", result.getFragment());
URI expected = new URI("http://example.com/foo?bar#baz");
assertEquals("Invalid result URI", expected, result.toUri());
}
@Test
public void fromPath() throws URISyntaxException {
UriComponents result = UriComponentsBuilder.fromPath("foo").queryParam("bar").fragment("baz").build();
assertEquals("/foo", result.getPath());
assertEquals("bar", result.getQuery());
assertEquals("baz", result.getFragment());
URI expected = new URI("/foo?bar#baz");
assertEquals("Invalid result URI", expected, result.toUri());
result = UriComponentsBuilder.fromPath("/foo").build();
assertEquals("/foo", result.getPath());
expected = new URI("/foo");
assertEquals("Invalid result URI", expected, result.toUri());
}
@Test
public void fromUri() throws URISyntaxException {
URI uri = new URI("http://example.com/foo?bar#baz");
UriComponents result = UriComponentsBuilder.fromUri(uri).build();
assertEquals("http", result.getScheme());
assertEquals("example.com", result.getHost());
assertEquals("/foo", result.getPath());
assertEquals("bar", result.getQuery());
assertEquals("baz", result.getFragment());
assertEquals("Invalid result URI", uri, result.toUri());
}
@Test
public void pathSegments() throws URISyntaxException {
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
URI result = builder.pathSegment("foo").pathSegment("bar").build().toUri();
URI expected = new URI("/foo/bar");
assertEquals("Invalid result URI", expected, result);
}
@Test
public void queryParam() throws URISyntaxException {
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
URI result = builder.queryParam("baz", "qux", 42).build().toUri();
URI expected = new URI("?baz=qux&baz=42");
assertEquals("Invalid result URI", expected, result);
}
@Test
public void emptyQueryParam() throws URISyntaxException {
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
URI result = builder.queryParam("baz").build().toUri();
URI expected = new URI("?baz");
assertEquals("Invalid result URI", expected, result);
}
@Test
public void combineWithUriTemplate() throws URISyntaxException {
UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/{foo}");
UriComponents components = builder.build();
UriTemplate template = new UriTemplate(components);
URI uri = template.expand("bar baz");
assertEquals(new URI("/bar%20baz"), uri);
}
}

View File

@@ -16,58 +16,105 @@
package org.springframework.web.util;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import java.util.Map;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import static org.junit.Assert.*;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
/** @author Arjen Poutsma */
public class UriComponentsTests {
private UriComponents components;
@Test
public void fromUri() {
Map<UriComponents.Type, String> result = UriComponents.fromUriString("http://www.ietf.org/rfc/rfc3986.txt");
assertEquals("http", result.get(UriComponents.Type.SCHEME));
assertNull(result.get(UriComponents.Type.USER_INFO));
assertEquals("www.ietf.org", result.get(UriComponents.Type.HOST));
assertNull(result.get(UriComponents.Type.PORT));
assertEquals("/rfc/rfc3986.txt", result.get(UriComponents.Type.PATH));
assertNull(result.get(UriComponents.Type.QUERY));
assertNull(result.get(UriComponents.Type.FRAGMENT));
result = UriComponents.fromUriString(
"http://arjen:foobar@java.sun.com:80/javase/6/docs/api/java/util/BitSet.html?foo=bar#and(java.util.BitSet)");
assertEquals("http", result.get(UriComponents.Type.SCHEME));
assertEquals("arjen:foobar", result.get(UriComponents.Type.USER_INFO));
assertEquals("java.sun.com", result.get(UriComponents.Type.HOST));
assertEquals("80", result.get(UriComponents.Type.PORT));
assertEquals("/javase/6/docs/api/java/util/BitSet.html", result.get(UriComponents.Type.PATH));
assertEquals("foo=bar", result.get(UriComponents.Type.QUERY));
assertEquals("and(java.util.BitSet)", result.get(UriComponents.Type.FRAGMENT));
result = UriComponents.fromUriString("mailto:java-net@java.sun.com");
assertEquals("mailto", result.get(UriComponents.Type.SCHEME));
assertNull(result.get(UriComponents.Type.USER_INFO));
assertNull(result.get(UriComponents.Type.HOST));
assertNull(result.get(UriComponents.Type.PORT));
assertEquals("java-net@java.sun.com", result.get(UriComponents.Type.PATH));
assertNull(result.get(UriComponents.Type.QUERY));
assertNull(result.get(UriComponents.Type.FRAGMENT));
result = UriComponents.fromUriString("docs/guide/collections/designfaq.html#28");
assertNull(result.get(UriComponents.Type.SCHEME));
assertNull(result.get(UriComponents.Type.USER_INFO));
assertNull(result.get(UriComponents.Type.HOST));
assertNull(result.get(UriComponents.Type.PORT));
assertEquals("docs/guide/collections/designfaq.html", result.get(UriComponents.Type.PATH));
assertNull(result.get(UriComponents.Type.QUERY));
assertEquals("28", result.get(UriComponents.Type.FRAGMENT));
}
@Before
public void createComponents() {
components = new UriComponents();
}
@Test
public void pathSegments() {
String path = "/foo/bar";
components.setPath(path);
String path = "/foo/bar";
UriComponents components = UriComponents.fromUriComponentMap(Collections.singletonMap(UriComponents.Type.PATH, path));
List<String> expected = Arrays.asList("foo", "bar");
List<String> pathSegments = components.getPathSegments();
assertEquals(expected, pathSegments);
components.setPath(null);
components.setPathSegments(expected);
assertEquals(path, components.getPath());
}
@Test
public void queryParams() {
String query = "foo=bar&foo=baz&qux";
components.setQuery(query);
UriComponents components = UriComponents.fromUriComponentMap(
Collections.singletonMap(UriComponents.Type.QUERY, query));
MultiValueMap<String, String> expected = new LinkedMultiValueMap<String, String>(1);
expected.put("foo", Arrays.asList("bar", "baz"));
expected.set("qux", null);
MultiValueMap<String, String> result = components.getQueryParams();
assertEquals(expected, result);
components.setQuery(null);
components.setQueryParams(expected);
assertEquals(query, components.getQuery());
}
@Test
public void encode() {
UriComponents uriComponents = UriComponents.fromUriString("http://example.com/hotel list");
UriComponents encoded = uriComponents.encode();
assertEquals("/hotel%20list", encoded.getPath());
}
@Test
public void toUriEncoded() throws URISyntaxException {
UriComponents uriComponents = UriComponents.fromUriString("http://example.com/hotel list/Z\u00fcrich");
UriComponents encoded = uriComponents.encode();
assertEquals(new URI("http://example.com/hotel%20list/Z%C3%BCrich"), encoded.toUri());
}
@Test
public void toUriNotEncoded() throws URISyntaxException {
UriComponents uriComponents = UriComponents.fromUriString("http://example.com/hotel list/Z\u00fcrich");
assertEquals(new URI("http://example.com/hotel%20list/Z\u00fcrich"), uriComponents.toUri());
}
}

View File

@@ -53,6 +53,16 @@ public class UriTemplateTests {
template.expand("1");
}
@Test
public void expandMap() throws Exception {
Map<String, String> uriVariables = new HashMap<String, String>(2);
uriVariables.put("booking", "42");
uriVariables.put("hotel", "1");
UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
URI result = template.expand(uriVariables);
assertEquals("Invalid expanded template", new URI("http://example.com/hotels/1/bookings/42"), result);
}
@Test
public void expandMapDuplicateVariables() throws Exception {
UriTemplate template = new UriTemplate("/order/{c}/{c}/{c}");
@@ -61,16 +71,6 @@ public class UriTemplateTests {
assertEquals("Invalid expanded template", new URI("/order/cheeseburger/cheeseburger/cheeseburger"), result);
}
@Test
public void expandMap() throws Exception {
Map<String, String> uriVariables = new HashMap<String, String>(2);
uriVariables.put("booking", "42");
uriVariables.put("hotel", "1");
UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
URI result = template.expand(uriVariables);
assertEquals("Invalid expanded template", new URI("http://example.com/hotels/1/bookings/42"), result);
}
@Test
public void expandMapNonString() throws Exception {
Map<String, Integer> uriVariables = new HashMap<String, Integer>(2);
@@ -80,6 +80,15 @@ public class UriTemplateTests {
URI result = template.expand(uriVariables);
assertEquals("Invalid expanded template", new URI("http://example.com/hotels/1/bookings/42"), result);
}
@Test
public void expandMapEncoded() throws Exception {
Map<String, String> uriVariables = Collections.singletonMap("hotel", "Z\u00fcrich");
UriTemplate template = new UriTemplate("http://example.com/hotel list/{hotel}");
URI result = template.expand(uriVariables);
assertEquals("Invalid expanded template", new URI("http://example.com/hotel%20list/Z%C3%BCrich"), result);
}
@Test(expected = IllegalArgumentException.class)
public void expandMapInvalidAmountVariables() throws Exception {

View File

@@ -17,11 +17,10 @@
package org.springframework.web.util;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
/**
* @author Arjen Poutsma
@@ -31,45 +30,6 @@ public class UriUtilsTests {
private static final String ENC = "UTF-8";
@Test
public void parseUriComponents() {
Map<UriComponents.Type, String> result = UriUtils.parseUriComponents("http://www.ietf.org/rfc/rfc3986.txt");
assertEquals("http", result.get(UriComponents.Type.SCHEME));
assertNull(result.get(UriComponents.Type.USER_INFO));
assertEquals("www.ietf.org", result.get(UriComponents.Type.HOST));
assertNull(result.get(UriComponents.Type.PORT));
assertEquals("/rfc/rfc3986.txt", result.get(UriComponents.Type.PATH));
assertNull(result.get(UriComponents.Type.QUERY));
assertNull(result.get(UriComponents.Type.FRAGMENT));
result = UriUtils.parseUriComponents(
"http://arjen:foobar@java.sun.com:80/javase/6/docs/api/java/util/BitSet.html?foo=bar#and(java.util.BitSet)");
assertEquals("http", result.get(UriComponents.Type.SCHEME));
assertEquals("arjen:foobar", result.get(UriComponents.Type.USER_INFO));
assertEquals("java.sun.com", result.get(UriComponents.Type.HOST));
assertEquals("80", result.get(UriComponents.Type.PORT));
assertEquals("/javase/6/docs/api/java/util/BitSet.html", result.get(UriComponents.Type.PATH));
assertEquals("foo=bar", result.get(UriComponents.Type.QUERY));
assertEquals("and(java.util.BitSet)", result.get(UriComponents.Type.FRAGMENT));
result = UriUtils.parseUriComponents("mailto:java-net@java.sun.com");
assertEquals("mailto", result.get(UriComponents.Type.SCHEME));
assertNull(result.get(UriComponents.Type.USER_INFO));
assertNull(result.get(UriComponents.Type.HOST));
assertNull(result.get(UriComponents.Type.PORT));
assertEquals("java-net@java.sun.com", result.get(UriComponents.Type.PATH));
assertNull(result.get(UriComponents.Type.QUERY));
assertNull(result.get(UriComponents.Type.FRAGMENT));
result = UriUtils.parseUriComponents("docs/guide/collections/designfaq.html#28");
assertNull(result.get(UriComponents.Type.SCHEME));
assertNull(result.get(UriComponents.Type.USER_INFO));
assertNull(result.get(UriComponents.Type.HOST));
assertNull(result.get(UriComponents.Type.PORT));
assertEquals("docs/guide/collections/designfaq.html", result.get(UriComponents.Type.PATH));
assertNull(result.get(UriComponents.Type.QUERY));
assertEquals("28", result.get(UriComponents.Type.FRAGMENT));
}
@Test
public void encodeScheme() throws UnsupportedEncodingException {