@Test
 public void toUriAlreadyEncoded() throws URISyntaxException {
   UriComponents uriComponents =
       UriComponentsBuilder.fromUriString("http://example.com/hotel%20list/Z%C3%BCrich")
           .build(true);
   UriComponents encoded = uriComponents.encode();
   assertEquals(new URI("http://example.com/hotel%20list/Z%C3%BCrich"), encoded.toUri());
 }
  @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
  public void fromOpaqueUri() throws URISyntaxException {
    URI uri = new URI("mailto:[email protected]#baz");
    UriComponents result = UriComponentsBuilder.fromUri(uri).build();
    assertEquals("mailto", result.getScheme());
    assertEquals("*****@*****.**", result.getSchemeSpecificPart());
    assertEquals("baz", result.getFragment());

    assertEquals("Invalid result URI", uri, result.toUri());
  }
 @Test
 public void toUriWithIpv6HostAlreadyEncoded() throws URISyntaxException {
   UriComponents uriComponents =
       UriComponentsBuilder.fromUriString(
               "http://[1abc:2abc:3abc::5ABC:6abc]:8080/hotel%20list/Z%C3%BCrich")
           .build(true);
   UriComponents encoded = uriComponents.encode();
   assertEquals(
       new URI("http://[1abc:2abc:3abc::5ABC:6abc]:8080/hotel%20list/Z%C3%BCrich"),
       encoded.toUri());
 }
  @Test
  public void fromHierarchicalUri() throws URISyntaxException {
    URI uri = new URI("http://example.com/foo?bar#baz");
    UriComponents result = UriComponentsBuilder.fromUri(uri).build();
    assertEquals("http", result.getScheme());
    assertEquals("example.com", result.getHost());
    assertEquals("/foo", result.getPath());
    assertEquals("bar", result.getQuery());
    assertEquals("baz", result.getFragment());

    assertEquals("Invalid result URI", uri, result.toUri());
  }
  @Test
  public void multipleFromSameBuilder() throws URISyntaxException {
    UriComponentsBuilder builder =
        UriComponentsBuilder.newInstance().scheme("http").host("example.com").pathSegment("foo");
    UriComponents result1 = builder.build();
    builder = builder.pathSegment("foo2").queryParam("bar").fragment("baz");
    UriComponents result2 = builder.build();

    assertEquals("http", result1.getScheme());
    assertEquals("example.com", result1.getHost());
    assertEquals("/foo", result1.getPath());
    URI expected = new URI("http://example.com/foo");
    assertEquals("Invalid result URI", expected, result1.toUri());

    assertEquals("http", result2.getScheme());
    assertEquals("example.com", result2.getHost());
    assertEquals("/foo/foo2", result2.getPath());
    assertEquals("bar", result2.getQuery());
    assertEquals("baz", result2.getFragment());
    expected = new URI("http://example.com/foo/foo2?bar#baz");
    assertEquals("Invalid result URI", expected, result2.toUri());
  }
  @Test
  public void plain() throws URISyntaxException {
    UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
    UriComponents result =
        builder
            .scheme("http")
            .host("example.com")
            .path("foo")
            .queryParam("bar")
            .fragment("baz")
            .build();
    assertEquals("http", result.getScheme());
    assertEquals("example.com", result.getHost());
    assertEquals("foo", result.getPath());
    assertEquals("bar", result.getQuery());
    assertEquals("baz", result.getFragment());

    URI expected = new URI("http://example.com/foo?bar#baz");
    assertEquals("Invalid result URI", expected, result.toUri());
  }
 @Test
 public void toUriNotEncoded() throws URISyntaxException {
   UriComponents uriComponents =
       UriComponentsBuilder.fromUriString("http://example.com/hotel list/Z\u00fcrich").build();
   assertEquals(new URI("http://example.com/hotel%20list/Z\u00fcrich"), uriComponents.toUri());
 }