@Test
  public void testAddURI() throws Exception {
    readConfig("");

    final URIish uri = new URIish("/some/dir");
    final RemoteConfig rc = new RemoteConfig(config, "backup");
    assertEquals(0, rc.getURIs().size());

    assertTrue(rc.addURI(uri));
    assertEquals(1, rc.getURIs().size());
    assertSame(uri, rc.getURIs().get(0));

    assertFalse(rc.addURI(new URIish(uri.toString())));
    assertEquals(1, rc.getURIs().size());
  }
 @Test
 public void testCreateOrigin() throws Exception {
   final RemoteConfig rc = new RemoteConfig(config, "origin");
   rc.addURI(new URIish("/some/dir"));
   rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/" + rc.getName() + "/*"));
   rc.update(config);
   checkConfig(
       "[remote \"origin\"]\n"
           + "\turl = /some/dir\n"
           + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n");
 }
 @Test
 public void testSaveAllTags() throws Exception {
   final RemoteConfig rc = new RemoteConfig(config, "origin");
   rc.addURI(new URIish("/some/dir"));
   rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/" + rc.getName() + "/*"));
   rc.setTagOpt(TagOpt.FETCH_TAGS);
   rc.update(config);
   checkConfig(
       "[remote \"origin\"]\n"
           + "\turl = /some/dir\n"
           + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
           + "\ttagopt = --tags\n");
 }
  @Test
  public void testRemoveLastURI() throws Exception {
    readConfig("");

    final URIish a = new URIish("/some/dir");
    final URIish b = new URIish("/another/dir");
    final URIish c = new URIish("/more/dirs");
    final RemoteConfig rc = new RemoteConfig(config, "backup");
    assertTrue(rc.addURI(a));
    assertTrue(rc.addURI(b));
    assertTrue(rc.addURI(c));

    assertEquals(3, rc.getURIs().size());
    assertSame(a, rc.getURIs().get(0));
    assertSame(b, rc.getURIs().get(1));
    assertSame(c, rc.getURIs().get(2));

    assertTrue(rc.removeURI(c));
    assertEquals(2, rc.getURIs().size());
    assertSame(a, rc.getURIs().get(0));
    assertSame(b, rc.getURIs().get(1));
  }
  @Test
  public void testRemoveOnlyURI() throws Exception {
    readConfig("");

    final URIish a = new URIish("/some/dir");
    final RemoteConfig rc = new RemoteConfig(config, "backup");
    assertTrue(rc.addURI(a));

    assertEquals(1, rc.getURIs().size());
    assertSame(a, rc.getURIs().get(0));

    assertTrue(rc.removeURI(a));
    assertEquals(0, rc.getURIs().size());
  }
  @Test
  public void testSaveAddURI() throws Exception {
    readConfig(
        "[remote \"spearce\"]\n"
            + "url = http://www.spearce.org/egit.git\n"
            + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");

    final RemoteConfig rc = new RemoteConfig(config, "spearce");
    rc.addURI(new URIish("/some/dir"));
    assertEquals(2, rc.getURIs().size());
    rc.update(config);
    checkConfig(
        "[remote \"spearce\"]\n"
            + "\turl = http://www.spearce.org/egit.git\n"
            + "\turl = /some/dir\n"
            + "\tfetch = +refs/heads/*:refs/remotes/spearce/*\n");
  }
  // add new remote
  private boolean addRemote(HttpServletRequest request, HttpServletResponse response, String path)
      throws IOException, JSONException, ServletException, CoreException, URISyntaxException {
    // expected path: /git/remote/file/{path}
    Path p = new Path(path);
    JSONObject toPut = OrionServlet.readJSONRequest(request);
    String remoteName = toPut.optString(GitConstants.KEY_REMOTE_NAME, null);
    // remoteName is required
    if (remoteName == null || remoteName.isEmpty()) {
      return statusHandler.handleRequest(
          request,
          response,
          new ServerStatus(
              IStatus.ERROR,
              HttpServletResponse.SC_BAD_REQUEST,
              "Remote name must be provided",
              null));
    }
    String remoteURI = toPut.optString(GitConstants.KEY_REMOTE_URI, null);
    // remoteURI is required
    if (remoteURI == null || remoteURI.isEmpty()) {
      return statusHandler.handleRequest(
          request,
          response,
          new ServerStatus(
              IStatus.ERROR,
              HttpServletResponse.SC_BAD_REQUEST,
              "Remote URI must be provided",
              null));
    }
    String fetchRefSpec = toPut.optString(GitConstants.KEY_REMOTE_FETCH_REF, null);
    String remotePushURI = toPut.optString(GitConstants.KEY_REMOTE_PUSH_URI, null);
    String pushRefSpec = toPut.optString(GitConstants.KEY_REMOTE_PUSH_REF, null);

    File gitDir = GitUtils.getGitDir(p);
    Repository db = new FileRepository(gitDir);
    StoredConfig config = db.getConfig();

    RemoteConfig rc = new RemoteConfig(config, remoteName);
    rc.addURI(new URIish(remoteURI));
    // FetchRefSpec is required, but default version can be generated
    // if it isn't provided
    if (fetchRefSpec == null || fetchRefSpec.isEmpty()) {
      fetchRefSpec = String.format("+refs/heads/*:refs/remotes/%s/*", remoteName); // $NON-NLS-1$
    }
    rc.addFetchRefSpec(new RefSpec(fetchRefSpec));
    // pushURI is optional
    if (remotePushURI != null && !remotePushURI.isEmpty()) rc.addPushURI(new URIish(remotePushURI));
    // PushRefSpec is optional
    if (pushRefSpec != null && !pushRefSpec.isEmpty()) rc.addPushRefSpec(new RefSpec(pushRefSpec));

    rc.update(config);
    config.save();

    URI cloneLocation =
        BaseToCloneConverter.getCloneLocation(getURI(request), BaseToCloneConverter.REMOTE_LIST);
    Remote remote = new Remote(cloneLocation, db, remoteName);
    JSONObject result = new JSONObject();
    result.put(ProtocolConstants.KEY_LOCATION, remote.getLocation());
    OrionServlet.writeJSONResponse(request, response, result);
    response.setHeader(
        ProtocolConstants.HEADER_LOCATION, result.getString(ProtocolConstants.KEY_LOCATION));
    response.setStatus(HttpServletResponse.SC_CREATED);
    return true;
  }