@Test
 public void port() {
   UriComponents uri1 = fromUriString("http://example.com:8080/bar").build();
   UriComponents uri2 = fromUriString("http://example.com/bar").port(8080).build();
   UriComponents uri3 =
       fromUriString("http://example.com/bar").port("{port}").build().expand(8080);
   UriComponents uri4 =
       fromUriString("http://example.com/bar").port("808{digit}").build().expand(0);
   assertEquals(8080, uri1.getPort());
   assertEquals("http://example.com:8080/bar", uri1.toUriString());
   assertEquals(8080, uri2.getPort());
   assertEquals("http://example.com:8080/bar", uri2.toUriString());
   assertEquals(8080, uri3.getPort());
   assertEquals("http://example.com:8080/bar", uri3.toUriString());
   assertEquals(8080, uri4.getPort());
   assertEquals("http://example.com:8080/bar", uri4.toUriString());
 }
  @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 buildAndExpandOpaque() {
    UriComponents result =
        UriComponentsBuilder.fromUriString("mailto:{user}@{domain}")
            .buildAndExpand("foo", "example.com");
    assertEquals("mailto:[email protected]", result.toUriString());

    Map<String, String> values = new HashMap<>();
    values.put("user", "foo");
    values.put("domain", "example.com");
    UriComponentsBuilder.fromUriString("mailto:{user}@{domain}").buildAndExpand(values);
    assertEquals("mailto:[email protected]", 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 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 queryParamWithoutValueWithEquals() throws Exception {
   UriComponents uriComponents =
       UriComponentsBuilder.fromUriString("http://example.com/foo?bar=").build();
   assertThat(uriComponents.toUriString(), equalTo("http://example.com/foo?bar="));
   assertThat(uriComponents.getQueryParams().get("bar").get(0), equalTo(""));
 }
 @Test
 public void expand() {
   UriComponents uriComponents =
       UriComponentsBuilder.fromUriString("http://example.com").path("/{foo} {bar}").build();
   uriComponents = uriComponents.expand("1 2", "3 4");
   assertEquals("/1 2 3 4", uriComponents.getPath());
   assertEquals("http://example.com/1 2 3 4", uriComponents.toUriString());
 }
  @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());
  }