diff --git a/spring-web/src/main/java/org/springframework/web/util/DefaultUriTemplateHandler.java b/spring-web/src/main/java/org/springframework/web/util/DefaultUriTemplateHandler.java index e4f7983224..0eb9acf8f4 100644 --- a/spring-web/src/main/java/org/springframework/web/util/DefaultUriTemplateHandler.java +++ b/spring-web/src/main/java/org/springframework/web/util/DefaultUriTemplateHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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. @@ -90,16 +90,16 @@ public class DefaultUriTemplateHandler implements UriTemplateHandler { @Override public URI expand(String uriTemplate, Map uriVariables) { - UriComponentsBuilder uriComponentsBuilder = initUriComponentsBuilder(uriTemplate); - UriComponents uriComponents = uriComponentsBuilder.build().expand(uriVariables).encode(); - return insertBaseUrl(uriComponents); + UriComponentsBuilder builder = initUriComponentsBuilder(uriTemplate); + UriComponents url = builder.build().expand(uriVariables).encode(); + return insertBaseUrl(url); } @Override - public URI expand(String uriTemplate, Object... uriVariableValues) { - UriComponentsBuilder uriComponentsBuilder = initUriComponentsBuilder(uriTemplate); - UriComponents uriComponents = uriComponentsBuilder.build().expand(uriVariableValues).encode(); - return insertBaseUrl(uriComponents); + public URI expand(String uriTemplate, Object... uriVariables) { + UriComponentsBuilder builder = initUriComponentsBuilder(uriTemplate); + UriComponents url = builder.build().expand(uriVariables).encode(); + return insertBaseUrl(url); } protected UriComponentsBuilder initUriComponentsBuilder(String uriTemplate) { 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 7af5550da0..99e1b062b0 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 @@ -36,16 +36,16 @@ public class UriTemplateTests { @Test public void getVariableNames() throws Exception { - UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); + UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); List variableNames = template.getVariableNames(); assertEquals("Invalid variable names", Arrays.asList("hotel", "booking"), variableNames); } @Test public void expandVarArgs() throws Exception { - UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); + UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); URI result = template.expand("1", "42"); - assertEquals("Invalid expanded template", new URI("http://example.com/hotels/1/bookings/42"), result); + assertEquals("Invalid expanded template", new URI("/hotels/1/bookings/42"), result); } // SPR-9712 @@ -59,97 +59,97 @@ public class UriTemplateTests { @Test(expected = IllegalArgumentException.class) public void expandVarArgsNotEnoughVariables() throws Exception { - UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); + UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); template.expand("1"); } @Test public void expandMap() throws Exception { - Map uriVariables = new HashMap(2); + Map uriVariables = new HashMap<>(2); uriVariables.put("booking", "42"); uriVariables.put("hotel", "1"); - UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); + UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); URI result = template.expand(uriVariables); - assertEquals("Invalid expanded template", new URI("http://example.com/hotels/1/bookings/42"), result); + assertEquals("Invalid expanded template", new URI("/hotels/1/bookings/42"), result); } @Test public void expandMapDuplicateVariables() throws Exception { UriTemplate template = new UriTemplate("/order/{c}/{c}/{c}"); - assertEquals("Invalid variable names", Arrays.asList("c", "c", "c"), template.getVariableNames()); + assertEquals(Arrays.asList("c", "c", "c"), template.getVariableNames()); URI result = template.expand(Collections.singletonMap("c", "cheeseburger")); - assertEquals("Invalid expanded template", new URI("/order/cheeseburger/cheeseburger/cheeseburger"), result); + assertEquals(new URI("/order/cheeseburger/cheeseburger/cheeseburger"), result); } @Test public void expandMapNonString() throws Exception { - Map uriVariables = new HashMap(2); + Map uriVariables = new HashMap<>(2); uriVariables.put("booking", 42); uriVariables.put("hotel", 1); - UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); + UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); URI result = template.expand(uriVariables); - assertEquals("Invalid expanded template", new URI("http://example.com/hotels/1/bookings/42"), result); + assertEquals("Invalid expanded template", new URI("/hotels/1/bookings/42"), result); } @Test public void expandMapEncoded() throws Exception { Map uriVariables = Collections.singletonMap("hotel", "Z\u00fcrich"); - UriTemplate template = new UriTemplate("http://example.com/hotel list/{hotel}"); + UriTemplate template = new UriTemplate("/hotel list/{hotel}"); URI result = template.expand(uriVariables); - assertEquals("Invalid expanded template", new URI("http://example.com/hotel%20list/Z%C3%BCrich"), result); + assertEquals("Invalid expanded template", new URI("/hotel%20list/Z%C3%BCrich"), result); } @Test(expected = IllegalArgumentException.class) public void expandMapUnboundVariables() throws Exception { - Map uriVariables = new HashMap(2); + Map uriVariables = new HashMap<>(2); uriVariables.put("booking", "42"); uriVariables.put("bar", "1"); - UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); + UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); template.expand(uriVariables); } @Test public void expandEncoded() throws Exception { - UriTemplate template = new UriTemplate("http://example.com/hotel list/{hotel}"); + UriTemplate template = new UriTemplate("/hotel list/{hotel}"); URI result = template.expand("Z\u00fcrich"); - assertEquals("Invalid expanded template", new URI("http://example.com/hotel%20list/Z%C3%BCrich"), result); + assertEquals("Invalid expanded template", new URI("/hotel%20list/Z%C3%BCrich"), result); } @Test public void matches() throws Exception { - UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); - assertTrue("UriTemplate does not match", template.matches("http://example.com/hotels/1/bookings/42")); - assertFalse("UriTemplate matches", template.matches("http://example.com/hotels/bookings")); + UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); + assertTrue("UriTemplate does not match", template.matches("/hotels/1/bookings/42")); + assertFalse("UriTemplate matches", template.matches("/hotels/bookings")); assertFalse("UriTemplate matches", template.matches("")); assertFalse("UriTemplate matches", template.matches(null)); } @Test public void matchesCustomRegex() throws Exception { - UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel:\\d+}"); - assertTrue("UriTemplate does not match", template.matches("http://example.com/hotels/42")); - assertFalse("UriTemplate matches", template.matches("http://example.com/hotels/foo")); + UriTemplate template = new UriTemplate("/hotels/{hotel:\\d+}"); + assertTrue("UriTemplate does not match", template.matches("/hotels/42")); + assertFalse("UriTemplate matches", template.matches("/hotels/foo")); } @Test public void match() throws Exception { - Map expected = new HashMap(2); + Map expected = new HashMap<>(2); expected.put("booking", "42"); expected.put("hotel", "1"); - UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); - Map result = template.match("http://example.com/hotels/1/bookings/42"); + UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); + Map result = template.match("/hotels/1/bookings/42"); assertEquals("Invalid match", expected, result); } @Test public void matchCustomRegex() throws Exception { - Map expected = new HashMap(2); + Map expected = new HashMap<>(2); expected.put("booking", "42"); expected.put("hotel", "1"); - UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel:\\d}/bookings/{booking:\\d+}"); - Map result = template.match("http://example.com/hotels/1/bookings/42"); + UriTemplate template = new UriTemplate("/hotels/{hotel:\\d}/bookings/{booking:\\d+}"); + Map result = template.match("/hotels/1/bookings/42"); assertEquals("Invalid match", expected, result); } @@ -174,7 +174,7 @@ public class UriTemplateTests { public void matchMultipleInOneSegment() throws Exception { UriTemplate template = new UriTemplate("/{foo}-{bar}"); Map result = template.match("/12-34"); - Map expected = new HashMap(2); + Map expected = new HashMap<>(2); expected.put("foo", "12"); expected.put("bar", "34"); assertEquals("Invalid match", expected, result);