From 274fba47f3b15bc12c8d81571c68987989f812e8 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 13 Mar 2024 18:06:17 +0100 Subject: [PATCH] Additional unit tests for operations on empty UriTemplate See gh-32432 (cherry picked from commit 54a6d89da78075b09e6cd5c0e55d4a67885fe24a) --- .../web/util/UriTemplateTests.java | 92 ++++++++++++------- 1 file changed, 61 insertions(+), 31 deletions(-) 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 795fcb147332..54c0640bfc07 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 @@ -34,7 +34,7 @@ * @author Juergen Hoeller * @author Rossen Stoyanchev */ -public class UriTemplateTests { +class UriTemplateTests { @Test void emptyPathDoesNotThrowException() { @@ -47,70 +47,84 @@ void nullPathThrowsException() { } @Test - public void getVariableNames() throws Exception { + 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")); } @Test - public void expandVarArgs() throws Exception { + void getVariableNamesFromEmpty() { + UriTemplate template = new UriTemplate(""); + List variableNames = template.getVariableNames(); + assertThat(variableNames).isEmpty(); + } + + @Test + void expandVarArgs() { UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); URI result = template.expand("1", "42"); - assertThat(result).as("Invalid expanded template").isEqualTo(new URI("/hotels/1/bookings/42")); + assertThat(result).as("Invalid expanded template").isEqualTo(URI.create("/hotels/1/bookings/42")); + } + + @Test + void expandVarArgsFromEmpty() { + UriTemplate template = new UriTemplate(""); + URI result = template.expand(); + assertThat(result).as("Invalid expanded template").isEqualTo(URI.create("")); } @Test // SPR-9712 - public void expandVarArgsWithArrayValue() throws Exception { + void expandVarArgsWithArrayValue() { UriTemplate template = new UriTemplate("/sum?numbers={numbers}"); URI result = template.expand(new int[] {1, 2, 3}); - assertThat(result).isEqualTo(new URI("/sum?numbers=1,2,3")); + assertThat(result).isEqualTo(URI.create("/sum?numbers=1,2,3")); } @Test - public void expandVarArgsNotEnoughVariables() throws Exception { + void expandVarArgsNotEnoughVariables() { UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); assertThatIllegalArgumentException().isThrownBy(() -> template.expand("1")); } @Test - public void expandMap() throws Exception { + void expandMap() { Map uriVariables = new HashMap<>(2); uriVariables.put("booking", "42"); uriVariables.put("hotel", "1"); UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); URI result = template.expand(uriVariables); - assertThat(result).as("Invalid expanded template").isEqualTo(new URI("/hotels/1/bookings/42")); + assertThat(result).as("Invalid expanded template").isEqualTo(URI.create("/hotels/1/bookings/42")); } @Test - public void expandMapDuplicateVariables() throws Exception { + 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(result).isEqualTo(new URI("/order/cheeseburger/cheeseburger/cheeseburger")); + assertThat(result).isEqualTo(URI.create("/order/cheeseburger/cheeseburger/cheeseburger")); } @Test - public void expandMapNonString() throws Exception { + void expandMapNonString() { Map uriVariables = new HashMap<>(2); uriVariables.put("booking", 42); uriVariables.put("hotel", 1); UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); URI result = template.expand(uriVariables); - assertThat(result).as("Invalid expanded template").isEqualTo(new URI("/hotels/1/bookings/42")); + assertThat(result).as("Invalid expanded template").isEqualTo(URI.create("/hotels/1/bookings/42")); } @Test - public void expandMapEncoded() throws Exception { + void expandMapEncoded() { Map uriVariables = Collections.singletonMap("hotel", "Z\u00fcrich"); UriTemplate template = new UriTemplate("/hotel list/{hotel}"); URI result = template.expand(uriVariables); - assertThat(result).as("Invalid expanded template").isEqualTo(new URI("/hotel%20list/Z%C3%BCrich")); + assertThat(result).as("Invalid expanded template").isEqualTo(URI.create("/hotel%20list/Z%C3%BCrich")); } @Test - public void expandMapUnboundVariables() throws Exception { + void expandMapUnboundVariables() { Map uriVariables = new HashMap<>(2); uriVariables.put("booking", "42"); uriVariables.put("bar", "1"); @@ -120,14 +134,14 @@ public void expandMapUnboundVariables() throws Exception { } @Test - public void expandEncoded() throws Exception { + void expandEncoded() { UriTemplate template = new UriTemplate("/hotel list/{hotel}"); URI result = template.expand("Z\u00fcrich"); - assertThat(result).as("Invalid expanded template").isEqualTo(new URI("/hotel%20list/Z%C3%BCrich")); + assertThat(result).as("Invalid expanded template").isEqualTo(URI.create("/hotel%20list/Z%C3%BCrich")); } @Test - public void matches() throws Exception { + void matches() { UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); assertThat(template.matches("/hotels/1/bookings/42")).as("UriTemplate does not match").isTrue(); assertThat(template.matches("/hotels/bookings")).as("UriTemplate matches").isFalse(); @@ -136,14 +150,23 @@ public void matches() throws Exception { } @Test - public void matchesCustomRegex() throws Exception { + void matchesAgainstEmpty() { + UriTemplate template = new UriTemplate(""); + assertThat(template.matches("/hotels/1/bookings/42")).as("UriTemplate matches").isFalse(); + assertThat(template.matches("/hotels/bookings")).as("UriTemplate matches").isFalse(); + assertThat(template.matches("")).as("UriTemplate does not match").isTrue(); + assertThat(template.matches(null)).as("UriTemplate matches").isFalse(); + } + + @Test + void matchesCustomRegex() { UriTemplate template = new UriTemplate("/hotels/{hotel:\\d+}"); assertThat(template.matches("/hotels/42")).as("UriTemplate does not match").isTrue(); assertThat(template.matches("/hotels/foo")).as("UriTemplate matches").isFalse(); } @Test - public void match() throws Exception { + void match() { Map expected = new HashMap<>(2); expected.put("booking", "42"); expected.put("hotel", "1"); @@ -154,7 +177,14 @@ public void match() throws Exception { } @Test - public void matchCustomRegex() throws Exception { + void matchAgainstEmpty() { + UriTemplate template = new UriTemplate(""); + Map result = template.match("/hotels/1/bookings/42"); + assertThat(result).as("Invalid match").isEmpty(); + } + + @Test + void matchCustomRegex() { Map expected = new HashMap<>(2); expected.put("booking", "42"); expected.put("hotel", "1"); @@ -165,14 +195,14 @@ public void matchCustomRegex() throws Exception { } @Test // SPR-13627 - public void matchCustomRegexWithNestedCurlyBraces() throws Exception { + 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")); } @Test - public void matchDuplicate() throws Exception { + void matchDuplicate() { UriTemplate template = new UriTemplate("/order/{c}/{c}/{c}"); Map result = template.match("/order/cheeseburger/cheeseburger/cheeseburger"); Map expected = Collections.singletonMap("c", "cheeseburger"); @@ -180,7 +210,7 @@ public void matchDuplicate() throws Exception { } @Test - public void matchMultipleInOneSegment() throws Exception { + void matchMultipleInOneSegment() { UriTemplate template = new UriTemplate("/{foo}-{bar}"); Map result = template.match("/12-34"); Map expected = new HashMap<>(2); @@ -190,19 +220,19 @@ public void matchMultipleInOneSegment() throws Exception { } @Test // SPR-16169 - public void matchWithMultipleSegmentsAtTheEnd() throws Exception { + void matchWithMultipleSegmentsAtTheEnd() { UriTemplate template = new UriTemplate("/account/{accountId}"); assertThat(template.matches("/account/15/alias/5")).isFalse(); } @Test - public void queryVariables() throws Exception { + void queryVariables() { UriTemplate template = new UriTemplate("/search?q={query}"); assertThat(template.matches("/search?q=foo")).isTrue(); } @Test - public void fragments() throws Exception { + void fragments() { UriTemplate template = new UriTemplate("/search#{fragment}"); assertThat(template.matches("/search#foo")).isTrue(); @@ -211,19 +241,19 @@ public void fragments() throws Exception { } @Test // SPR-13705 - public void matchesWithSlashAtTheEnd() throws Exception { + void matchesWithSlashAtTheEnd() { assertThat(new UriTemplate("/test/").matches("/test/")).isTrue(); } @Test - public void expandWithDollar() throws Exception { + void expandWithDollar() { UriTemplate template = new UriTemplate("/{a}"); URI uri = template.expand("$replacement"); assertThat(uri.toString()).isEqualTo("/$replacement"); } @Test - public void expandWithAtSign() throws Exception { + 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");