public final URI toUri(
      URI fileSystemUri, String root, Iterable<String> names, boolean directory) {
    String path = toUriPath(root, names, directory);
    try {
      // it should not suck this much to create a new URI that's the same except with a path set =(
      // need to do it this way for automatic path escaping
      return new URI(
          fileSystemUri.getScheme(),
          fileSystemUri.getUserInfo(),
          fileSystemUri.getHost(),
          fileSystemUri.getPort(),
          path,
          null,
          null);
    } catch (URISyntaxException e) {
      throw new AssertionError(e);
    }
  }
@Test
  public void testToUri() {
    URI fileUri = type.toUri(fileSystemUri, "$", ImmutableList.of("foo", "bar"), false);
    assertThat(fileUri.toString()).isEqualTo("jimfs://foo/$/foo/bar");
    assertThat(fileUri.getPath()).isEqualTo("/$/foo/bar");

    URI directoryUri = type.toUri(fileSystemUri, "$", ImmutableList.of("foo", "bar"), true);
    assertThat(directoryUri.toString()).isEqualTo("jimfs://foo/$/foo/bar/");
    assertThat(directoryUri.getPath()).isEqualTo("/$/foo/bar/");

    URI rootUri = type.toUri(fileSystemUri, "$", ImmutableList.<String>of(), true);
    assertThat(rootUri.toString()).isEqualTo("jimfs://foo/$/");
    assertThat(rootUri.getPath()).isEqualTo("/$/");
  }