@Test
public void buildUriFromParts() throws URISyntaxException {

    String scheme = "http";
    String host = "host";
    int port = 8080;
    String path = "path/to/somewhere";
    String query = "key1=value1&key2=value2";
    URI u = UriUtils.buildUri(scheme, host, port, path, query);
    assertEquals(scheme, u.getScheme());
    assertEquals(host, u.getHost());
    assertEquals(port, u.getPort());
    assertEquals("/" + path, u.getPath());
    assertEquals(query, u.getQuery());
    String pathEndingWithQueryChar = path + "?";
    u = UriUtils.buildUri(scheme, host, port, pathEndingWithQueryChar, query);
    assertEquals(scheme, u.getScheme());
    assertEquals(host, u.getHost());
    assertEquals(port, u.getPort());
    assertEquals("/" + path, u.getPath());
    assertEquals(query, u.getQuery());
    String queryContainingQueryChar = "key=value&url=/url/with?query=true";
    u = UriUtils.buildUri(scheme, host, port, pathEndingWithQueryChar, queryContainingQueryChar);
    assertEquals(scheme, u.getScheme());
    assertEquals(host, u.getHost());
    assertEquals(port, u.getPort());
    assertEquals("/" + path, u.getPath());
    assertEquals(queryContainingQueryChar, u.getQuery());
}