diff --git a/org.springframework.web/src/main/java/org/springframework/web/util/UriComponents.java b/org.springframework.web/src/main/java/org/springframework/web/util/UriComponents.java index 6123223e792..54cbcef437e 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/util/UriComponents.java +++ b/org.springframework.web/src/main/java/org/springframework/web/util/UriComponents.java @@ -1002,6 +1002,9 @@ public final class UriComponents { } public Object getValue(String name) { + if (!this.uriVariables.containsKey(name)) { + throw new IllegalArgumentException("Map has no value for '" + name + "'"); + } return this.uriVariables.get(name); } } @@ -1010,6 +1013,7 @@ public final class UriComponents { * URI template variables backed by a variable argument array. */ private static class VarArgsTemplateVariables implements UriTemplateVariables { + private final Iterator valueIterator; public VarArgsTemplateVariables(Object... uriVariableValues) { @@ -1018,7 +1022,7 @@ public final class UriComponents { public Object getValue(String name) { if (!valueIterator.hasNext()) { - throw new IllegalArgumentException("Not enough variable values available to expand [" + name + "]"); + throw new IllegalArgumentException("Not enough variable values available to expand '" + name + "'"); } return valueIterator.next(); } diff --git a/org.springframework.web/src/test/java/org/springframework/web/util/UriTemplateTests.java b/org.springframework.web/src/test/java/org/springframework/web/util/UriTemplateTests.java index f0099328cd6..ba2d7a796d2 100644 --- a/org.springframework.web/src/test/java/org/springframework/web/util/UriTemplateTests.java +++ b/org.springframework.web/src/test/java/org/springframework/web/util/UriTemplateTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 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. @@ -89,6 +89,15 @@ public class UriTemplateTests { assertEquals("Invalid expanded template", new URI("http://example.com/hotel%20list/Z%C3%BCrich"), result); } + @Test(expected = IllegalArgumentException.class) + public void expandMapUnboundVariables() throws Exception { + Map uriVariables = new HashMap(2); + uriVariables.put("booking", "42"); + uriVariables.put("bar", "1"); + UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); + template.expand(uriVariables); + } + @Test public void expandEncoded() throws Exception { UriTemplate template = new UriTemplate("http://example.com/hotel list/{hotel}");