From 4a3daa7812813ccdf0752a07ec11a1863b2bbf84 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Thu, 14 Mar 2024 14:20:59 +0100 Subject: [PATCH] Polishing --- .../springframework/web/util/UriTemplate.java | 34 ++++++------- .../web/util/UriTemplateTests.java | 50 +++++++------------ 2 files changed, 35 insertions(+), 49 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java b/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java index 94dfb9de617..828d1591598 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java +++ b/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java @@ -31,7 +31,7 @@ import org.springframework.util.CollectionUtils; /** * Representation of a URI template that can be expanded with URI variables via - * {@link #expand(Map)}, {@link #expand(Object[])}, or matched to a URL via + * {@link #expand(Map)} or {@link #expand(Object[])}, or matched to a URL via * {@link #match(String)}. This class is designed to be thread-safe and * reusable, and allows any number of expand or match calls. * @@ -77,7 +77,7 @@ public class UriTemplate implements Serializable { /** - * Return the names of the variables in the template, in order. + * Return the names of the variables in this template, in order. * @return the template variable names */ public List getVariableNames() { @@ -85,16 +85,16 @@ public class UriTemplate implements Serializable { } /** - * Given the Map of variables, expands this template into a URI. The Map keys represent variable names, - * the Map values variable values. The order of variables is not significant. + * Given the Map of variables, expand this template into a URI. + *

The Map keys represent variable names, and the Map values represent + * variable values. The order of variables is not significant. *

Example: *

 	 * UriTemplate template = new UriTemplate("https://example.com/hotels/{hotel}/bookings/{booking}");
-	 * Map<String, String> uriVariables = new HashMap<String, String>();
-	 * uriVariables.put("booking", "42");
-	 * uriVariables.put("hotel", "Rest & Relax");
-	 * System.out.println(template.expand(uriVariables));
-	 * 
+ * Map<String, String> uriVariables = Map.of( + * "booking", "42", + * "hotel", "Rest & Relax"); + * System.out.println(template.expand(uriVariables)); * will print:
{@code https://example.com/hotels/Rest%20%26%20Relax/bookings/42}
* @param uriVariables the map of URI variables * @return the expanded URI @@ -108,13 +108,13 @@ public class UriTemplate implements Serializable { } /** - * Given an array of variables, expand this template into a full URI. The array represent variable values. - * The order of variables is significant. + * Given the array of variables, expand this template into a full URI. + *

The array represents variable values, and the order of variables is + * significant. *

Example: *

 	 * UriTemplate template = new UriTemplate("https://example.com/hotels/{hotel}/bookings/{booking}");
-	 * System.out.println(template.expand("Rest & Relax", 42));
-	 * 
+ * System.out.println(template.expand("Rest & Relax", 42)); * will print:
{@code https://example.com/hotels/Rest%20%26%20Relax/bookings/42}
* @param uriVariableValues the array of URI variables * @return the expanded URI @@ -141,13 +141,13 @@ public class UriTemplate implements Serializable { } /** - * Match the given URI to a map of variable values. Keys in the returned map are variable names, - * values are variable values, as occurred in the given URI. + * Match the given URI to a map of variable values based on this template. + *

Keys in the returned map are variable names, and the values in the + * returned map are variable values, as present in the given URI. *

Example: *

 	 * UriTemplate template = new UriTemplate("https://example.com/hotels/{hotel}/bookings/{booking}");
-	 * System.out.println(template.match("https://example.com/hotels/1/bookings/42"));
-	 * 
+ * System.out.println(template.match("https://example.com/hotels/1/bookings/42")); * will print:
{@code {hotel=1, booking=42}}
* @param uri the URI to match to * @return a map of variable values diff --git a/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java b/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java index 54c0640bfc0..e95050751ba 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java @@ -17,9 +17,6 @@ package org.springframework.web.util; import java.net.URI; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -50,7 +47,7 @@ class UriTemplateTests { void getVariableNames() { UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); List variableNames = template.getVariableNames(); - assertThat(variableNames).as("Invalid variable names").isEqualTo(Arrays.asList("hotel", "booking")); + assertThat(variableNames).as("Invalid variable names").containsExactly("hotel", "booking"); } @Test @@ -72,6 +69,9 @@ class UriTemplateTests { UriTemplate template = new UriTemplate(""); URI result = template.expand(); assertThat(result).as("Invalid expanded template").isEqualTo(URI.create("")); + + result = template.expand("1", "42"); + assertThat(result).as("Invalid expanded template").isEqualTo(URI.create("")); } @Test // SPR-9712 @@ -89,9 +89,7 @@ class UriTemplateTests { @Test void expandMap() { - Map uriVariables = new HashMap<>(2); - uriVariables.put("booking", "42"); - uriVariables.put("hotel", "1"); + Map uriVariables = Map.of("booking", "42", "hotel", "1"); UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); URI result = template.expand(uriVariables); assertThat(result).as("Invalid expanded template").isEqualTo(URI.create("/hotels/1/bookings/42")); @@ -100,16 +98,14 @@ class UriTemplateTests { @Test void expandMapDuplicateVariables() { UriTemplate template = new UriTemplate("/order/{c}/{c}/{c}"); - assertThat(template.getVariableNames()).isEqualTo(Arrays.asList("c", "c", "c")); - URI result = template.expand(Collections.singletonMap("c", "cheeseburger")); + assertThat(template.getVariableNames()).containsExactly("c", "c", "c"); + URI result = template.expand(Map.of("c", "cheeseburger")); assertThat(result).isEqualTo(URI.create("/order/cheeseburger/cheeseburger/cheeseburger")); } @Test void expandMapNonString() { - Map uriVariables = new HashMap<>(2); - uriVariables.put("booking", 42); - uriVariables.put("hotel", 1); + Map uriVariables = Map.of("booking", 42, "hotel", 1); UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); URI result = template.expand(uriVariables); assertThat(result).as("Invalid expanded template").isEqualTo(URI.create("/hotels/1/bookings/42")); @@ -117,7 +113,7 @@ class UriTemplateTests { @Test void expandMapEncoded() { - Map uriVariables = Collections.singletonMap("hotel", "Z\u00fcrich"); + Map uriVariables = Map.of("hotel", "Z\u00fcrich"); UriTemplate template = new UriTemplate("/hotel list/{hotel}"); URI result = template.expand(uriVariables); assertThat(result).as("Invalid expanded template").isEqualTo(URI.create("/hotel%20list/Z%C3%BCrich")); @@ -125,12 +121,9 @@ class UriTemplateTests { @Test void expandMapUnboundVariables() { - Map uriVariables = new HashMap<>(2); - uriVariables.put("booking", "42"); - uriVariables.put("bar", "1"); + Map uriVariables = Map.of("booking", "42", "bar", "1"); UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); - assertThatIllegalArgumentException().isThrownBy(() -> - template.expand(uriVariables)); + assertThatIllegalArgumentException().isThrownBy(() -> template.expand(uriVariables)); } @Test @@ -167,9 +160,7 @@ class UriTemplateTests { @Test void match() { - Map expected = new HashMap<>(2); - expected.put("booking", "42"); - expected.put("hotel", "1"); + Map expected = Map.of("booking", "42", "hotel", "1"); UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); Map result = template.match("/hotels/1/bookings/42"); @@ -185,9 +176,7 @@ class UriTemplateTests { @Test void matchCustomRegex() { - Map expected = new HashMap<>(2); - expected.put("booking", "42"); - expected.put("hotel", "1"); + Map expected = Map.of("booking", "42", "hotel", "1"); UriTemplate template = new UriTemplate("/hotels/{hotel:\\d}/bookings/{booking:\\d+}"); Map result = template.match("/hotels/1/bookings/42"); @@ -198,24 +187,21 @@ class UriTemplateTests { void matchCustomRegexWithNestedCurlyBraces() { UriTemplate template = new UriTemplate("/site.{domain:co.[a-z]{2}}"); Map result = template.match("/site.co.eu"); - assertThat(result).as("Invalid match").isEqualTo(Collections.singletonMap("domain", "co.eu")); + assertThat(result).as("Invalid match").isEqualTo(Map.of("domain", "co.eu")); } @Test void matchDuplicate() { UriTemplate template = new UriTemplate("/order/{c}/{c}/{c}"); Map result = template.match("/order/cheeseburger/cheeseburger/cheeseburger"); - Map expected = Collections.singletonMap("c", "cheeseburger"); - assertThat(result).as("Invalid match").isEqualTo(expected); + assertThat(result).as("Invalid match").isEqualTo(Map.of("c", "cheeseburger")); } @Test void matchMultipleInOneSegment() { UriTemplate template = new UriTemplate("/{foo}-{bar}"); Map result = template.match("/12-34"); - Map expected = new HashMap<>(2); - expected.put("foo", "12"); - expected.put("bar", "34"); + Map expected = Map.of("foo", "12", "bar", "34"); assertThat(result).as("Invalid match").isEqualTo(expected); } @@ -249,14 +235,14 @@ class UriTemplateTests { void expandWithDollar() { UriTemplate template = new UriTemplate("/{a}"); URI uri = template.expand("$replacement"); - assertThat(uri.toString()).isEqualTo("/$replacement"); + assertThat(uri).hasToString("/$replacement"); } @Test void expandWithAtSign() { UriTemplate template = new UriTemplate("http://localhost/query={query}"); URI uri = template.expand("foo@bar"); - assertThat(uri.toString()).isEqualTo("http://localhost/query=foo@bar"); + assertThat(uri).hasToString("http://localhost/query=foo@bar"); } }