@Test
  public void buildAndExpandHierarchical() {
    UriComponents result = UriComponentsBuilder.fromPath("/{foo}").buildAndExpand("fooValue");
    assertEquals("/fooValue", result.toUriString());

    Map<String, String> values = new HashMap<>();
    values.put("foo", "fooValue");
    values.put("bar", "barValue");
    result = UriComponentsBuilder.fromPath("/{foo}/{bar}").buildAndExpand(values);
    assertEquals("/fooValue/barValue", result.toUriString());
  }
  @Test
  public void pathThenPathSegments() {
    UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/foo/bar").pathSegment("ba/z");
    UriComponents result = builder.build().encode();

    assertEquals("/foo/bar/ba%2Fz", result.getPath());
    assertEquals(Arrays.asList("foo", "bar", "ba%2Fz"), result.getPathSegments());
  }
  @Test
  public void path() throws URISyntaxException {
    UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/foo/bar");
    UriComponents result = builder.build();

    assertEquals("/foo/bar", result.getPath());
    assertEquals(Arrays.asList("foo", "bar"), result.getPathSegments());
  }
 @Test
 public void copyToUriComponentsBuilder() {
   UriComponents source = UriComponentsBuilder.fromPath("/foo/bar").pathSegment("ba/z").build();
   UriComponentsBuilder targetBuilder = UriComponentsBuilder.newInstance();
   source.copyToUriComponentsBuilder(targetBuilder);
   UriComponents result = targetBuilder.build().encode();
   assertEquals("/foo/bar/ba%2Fz", result.getPath());
   assertEquals(Arrays.asList("foo", "bar", "ba%2Fz"), result.getPathSegments());
 }
  @Test
  public void fromPath() throws URISyntaxException {
    UriComponents result =
        UriComponentsBuilder.fromPath("foo").queryParam("bar").fragment("baz").build();
    assertEquals("foo", result.getPath());
    assertEquals("bar", result.getQuery());
    assertEquals("baz", result.getFragment());

    assertEquals("Invalid result URI String", "foo?bar#baz", result.toUriString());

    URI expected = new URI("foo?bar#baz");
    assertEquals("Invalid result URI", expected, result.toUri());

    result = UriComponentsBuilder.fromPath("/foo").build();
    assertEquals("/foo", result.getPath());

    expected = new URI("/foo");
    assertEquals("Invalid result URI", expected, result.toUri());
  }
 @Test // SPR-12398
 public void pathWithDuplicateSlashes() throws URISyntaxException {
   UriComponents uriComponents = UriComponentsBuilder.fromPath("/foo/////////bar").build();
   assertEquals("/foo/bar", uriComponents.getPath());
 }
 @Test // SPR-12742
 public void fromHttpRequestWithTrailingSlash() throws Exception {
   UriComponents before = UriComponentsBuilder.fromPath("/foo/").build();
   UriComponents after = UriComponentsBuilder.newInstance().uriComponents(before).build();
   assertEquals("/foo/", after.getPath());
 }
 @Test
 public void encode() {
   UriComponents uriComponents = UriComponentsBuilder.fromPath("/hotel list").build();
   UriComponents encoded = uriComponents.encode();
   assertEquals("/hotel%20list", encoded.getPath());
 }
 @Test(expected = IllegalArgumentException.class)
 public void invalidEncodedSequence() {
   UriComponentsBuilder.fromPath("/fo%2o").build(true);
 }
 @Test(expected = IllegalArgumentException.class)
 public void invalidCharacters() {
   UriComponentsBuilder.fromPath("/{foo}").build(true);
 }
 @Test(expected = IllegalStateException.class)
 public void expandEncoded() {
   UriComponentsBuilder.fromPath("/{foo}").build().encode().expand("bar");
 }