Polish UriTemplateTests
This commit is contained in:
@@ -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<String> 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<String, String> uriVariables = new HashMap<String, String>(2);
|
||||
Map<String, String> 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<String, Integer> uriVariables = new HashMap<String, Integer>(2);
|
||||
Map<String, Integer> 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<String, String> 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<String, String> uriVariables = new HashMap<String, String>(2);
|
||||
Map<String, String> 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<String, String> expected = new HashMap<String, String>(2);
|
||||
Map<String, String> expected = new HashMap<>(2);
|
||||
expected.put("booking", "42");
|
||||
expected.put("hotel", "1");
|
||||
|
||||
UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
|
||||
Map<String, String> result = template.match("http://example.com/hotels/1/bookings/42");
|
||||
UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}");
|
||||
Map<String, String> result = template.match("/hotels/1/bookings/42");
|
||||
assertEquals("Invalid match", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchCustomRegex() throws Exception {
|
||||
Map<String, String> expected = new HashMap<String, String>(2);
|
||||
Map<String, String> 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<String, String> result = template.match("http://example.com/hotels/1/bookings/42");
|
||||
UriTemplate template = new UriTemplate("/hotels/{hotel:\\d}/bookings/{booking:\\d+}");
|
||||
Map<String, String> 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<String, String> result = template.match("/12-34");
|
||||
Map<String, String> expected = new HashMap<String, String>(2);
|
||||
Map<String, String> expected = new HashMap<>(2);
|
||||
expected.put("foo", "12");
|
||||
expected.put("bar", "34");
|
||||
assertEquals("Invalid match", expected, result);
|
||||
|
||||
Reference in New Issue
Block a user