@Test
  public void testClone() throws URISyntaxException {
    UriComponentsBuilder builder1 = UriComponentsBuilder.newInstance();
    builder1
        .scheme("http")
        .host("e1.com")
        .path("/p1")
        .pathSegment("ps1")
        .queryParam("q1")
        .fragment("f1");

    UriComponentsBuilder builder2 = (UriComponentsBuilder) builder1.clone();
    builder2
        .scheme("https")
        .host("e2.com")
        .path("p2")
        .pathSegment("ps2")
        .queryParam("q2")
        .fragment("f2");

    UriComponents result1 = builder1.build();
    assertEquals("http", result1.getScheme());
    assertEquals("e1.com", result1.getHost());
    assertEquals("/p1/ps1", result1.getPath());
    assertEquals("q1", result1.getQuery());
    assertEquals("f1", result1.getFragment());

    UriComponents result2 = builder2.build();
    assertEquals("https", result2.getScheme());
    assertEquals("e2.com", result2.getHost());
    assertEquals("/p1/ps1/p2/ps2", result2.getPath());
    assertEquals("q1&q2", result2.getQuery());
    assertEquals("f2", result2.getFragment());
  }
  @Test
  public void replaceQueryParam() {
    UriComponentsBuilder builder = UriComponentsBuilder.newInstance().queryParam("baz", "qux", 42);
    builder.replaceQueryParam("baz", "xuq", 24);
    UriComponents result = builder.build();

    assertEquals("baz=xuq&baz=24", result.getQuery());

    builder = UriComponentsBuilder.newInstance().queryParam("baz", "qux", 42);
    builder.replaceQueryParam("baz");
    result = builder.build();

    assertNull("Query param should have been deleted", result.getQuery());
  }
  @Test
  public void replacePath() {
    UriComponentsBuilder builder =
        UriComponentsBuilder.fromUriString("http://www.ietf.org/rfc/rfc2396.txt");
    builder.replacePath("/rfc/rfc3986.txt");
    UriComponents result = builder.build();

    assertEquals("http://www.ietf.org/rfc/rfc3986.txt", result.toUriString());

    builder = UriComponentsBuilder.fromUriString("http://www.ietf.org/rfc/rfc2396.txt");
    builder.replacePath(null);
    result = builder.build();

    assertEquals("http://www.ietf.org", result.toUriString());
  }
  @Test
  public void replaceQuery() {
    UriComponentsBuilder builder =
        UriComponentsBuilder.fromUriString("http://example.com/foo?foo=bar&baz=qux");
    builder.replaceQuery("baz=42");
    UriComponents result = builder.build();

    assertEquals("http://example.com/foo?baz=42", result.toUriString());

    builder = UriComponentsBuilder.fromUriString("http://example.com/foo?foo=bar&baz=qux");
    builder.replaceQuery(null);
    result = builder.build();

    assertEquals("http://example.com/foo", result.toUriString());
  }
  @Test
  public void pathSegmentsThenPath() {
    UriComponentsBuilder builder = UriComponentsBuilder.newInstance().pathSegment("foo").path("/");
    UriComponents result = builder.build();

    assertEquals("/foo/", result.getPath());
    assertEquals(Collections.singletonList("foo"), result.getPathSegments());
  }
  @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 pathSegmentsSomeEmpty() {
    UriComponentsBuilder builder =
        UriComponentsBuilder.newInstance().pathSegment("", "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 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());
  }