IncludedInResolverTest: Convert to TestRepository

The TestRepository interface is designed for compactly creating specific
commit topologies; in this case it is indeed somewhat shorter than the
Git API commands it's replacing.

Doing this means the test doesn't require an on-disk repository and we
can extend GerritBaseTests instead of RepositoryTestCase.

Change-Id: I6a3b364a497e9203c300b4b6b6576c9542e8427a
diff --git a/javatests/com/google/gerrit/server/change/IncludedInResolverTest.java b/javatests/com/google/gerrit/server/change/IncludedInResolverTest.java
index dca2dcb..0cfe483 100644
--- a/javatests/com/google/gerrit/server/change/IncludedInResolverTest.java
+++ b/javatests/com/google/gerrit/server/change/IncludedInResolverTest.java
@@ -15,22 +15,19 @@
 package com.google.gerrit.server.change;
 
 import static com.google.common.truth.Truth.assertThat;
+import static com.google.gerrit.reviewdb.client.RefNames.REFS_TAGS;
 
-import java.io.IOException;
-import org.eclipse.jgit.api.Git;
-import org.eclipse.jgit.api.MergeCommand.FastForwardMode;
-import org.eclipse.jgit.junit.RepositoryTestCase;
-import org.eclipse.jgit.lib.ObjectId;
-import org.eclipse.jgit.lib.Ref;
+import com.google.gerrit.testing.GerritBaseTests;
+import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
+import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
+import org.eclipse.jgit.junit.TestRepository;
 import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevObject;
 import org.eclipse.jgit.revwalk.RevTag;
-import org.eclipse.jgit.revwalk.RevWalk;
-import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
-public class IncludedInResolverTest extends RepositoryTestCase {
-
+public class IncludedInResolverTest extends GerritBaseTests {
   // Branch names
   private static final String BRANCH_MASTER = "master";
   private static final String BRANCH_1_0 = "rel-1.0";
@@ -53,12 +50,11 @@
   private RevCommit commit_v1_3;
   private RevCommit commit_v2_5;
 
-  private RevWalk revWalk;
+  private TestRepository<?> tr;
 
-  @Override
   @Before
   public void setUp() throws Exception {
-    super.setUp();
+    tr = new TestRepository<>(new InMemoryRepository(new DfsRepositoryDescription("repo")));
 
     /*- The following graph will be created.
 
@@ -78,55 +74,37 @@
 
     */
 
-    // TODO(dborowitz): Use try/finally when this doesn't double-close the repo.
-    @SuppressWarnings("resource")
-    Git git = new Git(db);
-    revWalk = new RevWalk(db);
     // Version 1.0
-    commit_initial = git.commit().setMessage("c1").call();
-    git.commit().setMessage("c2").call();
-    RevCommit commit_v1_0 = git.commit().setMessage("version 1.0").call();
-    git.tag().setName(TAG_1_0).setObjectId(commit_v1_0).call();
-    RevCommit c3 = git.commit().setMessage("c3").call();
+    commit_initial = tr.branch(BRANCH_MASTER).commit().message("c1").create();
+    tr.branch(BRANCH_MASTER).commit().message("c2").create();
+    RevCommit commit_v1_0 = tr.branch(BRANCH_MASTER).commit().message("version 1.0").create();
+    tag(TAG_1_0, commit_v1_0);
+    RevCommit c3 = tr.branch(BRANCH_MASTER).commit().message("c3").create();
+
     // Version 1.01
-    createAndCheckoutBranch(commit_v1_0, BRANCH_1_0);
-    RevCommit commit_v1_0_1 = git.commit().setMessage("verREFS_HEADS_RELsion 1.0.1").call();
-    git.tag().setName(TAG_1_0_1).setObjectId(commit_v1_0_1).call();
+    tr.branch(BRANCH_1_0).update(commit_v1_0);
+    RevCommit commit_v1_0_1 = tr.branch(BRANCH_1_0).commit().message("version 1.0.1").create();
+    tag(TAG_1_0_1, commit_v1_0_1);
+
     // Version 1.3
-    createAndCheckoutBranch(c3, BRANCH_1_3);
-    commit_v1_3 = git.commit().setMessage("version 1.3").call();
-    git.tag().setName(TAG_1_3).setObjectId(commit_v1_3).call();
+    tr.branch(BRANCH_1_3).update(c3);
+    commit_v1_3 = tr.branch(BRANCH_1_3).commit().message("version 1.3").create();
+    tag(TAG_1_3, commit_v1_3);
+
     // Version 2.0
-    createAndCheckoutBranch(c3, BRANCH_2_0);
-    RevCommit commit_v2_0 = git.commit().setMessage("version 2.0").call();
-    git.tag().setName(TAG_2_0).setObjectId(commit_v2_0).call();
-    RevCommit commit_v2_0_1 = git.commit().setMessage("version 2.0.1").call();
-    git.tag().setName(TAG_2_0_1).setObjectId(commit_v2_0_1).call();
+    tr.branch(BRANCH_2_0).update(c3);
+    RevCommit commit_v2_0 = tr.branch(BRANCH_2_0).commit().message("version 2.0").create();
+    tag(TAG_2_0, commit_v2_0);
+    RevCommit commit_v2_0_1 = tr.branch(BRANCH_2_0).commit().message("version 2.0.1").create();
+    tag(TAG_2_0_1, commit_v2_0_1);
 
     // Version 2.5
-    createAndCheckoutBranch(commit_v1_3, BRANCH_2_5);
-    git.merge()
-        .include(commit_v2_0_1)
-        .setCommit(false)
-        .setFastForward(FastForwardMode.NO_FF)
-        .call();
-    commit_v2_5 = git.commit().setMessage("version 2.5").call();
-    git.tag().setName(TAG_2_5).setObjectId(commit_v2_5).setAnnotated(false).call();
-    Ref ref_tag_2_5_annotated =
-        git.tag().setName(TAG_2_5_ANNOTATED).setObjectId(commit_v2_5).setAnnotated(true).call();
-    RevTag tag_2_5_annotated = revWalk.parseTag(ref_tag_2_5_annotated.getObjectId());
-    git.tag()
-        .setName(TAG_2_5_ANNOTATED_TWICE)
-        .setObjectId(tag_2_5_annotated)
-        .setAnnotated(true)
-        .call();
-  }
-
-  @Override
-  @After
-  public void tearDown() throws Exception {
-    revWalk.close();
-    super.tearDown();
+    tr.branch(BRANCH_2_5).update(commit_v1_3);
+    tr.branch(BRANCH_2_5).commit().parent(commit_v2_0_1).create(); // Merge v2.0.1
+    commit_v2_5 = tr.branch(BRANCH_2_5).commit().message("version 2.5").create();
+    tr.update(REFS_TAGS + TAG_2_5, commit_v2_5);
+    RevTag tag_2_5_annotated = tag(TAG_2_5_ANNOTATED, commit_v2_5);
+    tag(TAG_2_5_ANNOTATED_TWICE, tag_2_5_annotated);
   }
 
   @Test
@@ -171,12 +149,10 @@
   }
 
   private IncludedInResolver.Result resolve(RevCommit commit) throws Exception {
-    return IncludedInResolver.resolve(db, revWalk, commit);
+    return IncludedInResolver.resolve(tr.getRepository(), tr.getRevWalk(), commit);
   }
 
-  private void createAndCheckoutBranch(ObjectId objectId, String branchName) throws IOException {
-    String fullBranchName = "refs/heads/" + branchName;
-    super.createBranch(objectId, fullBranchName);
-    super.checkoutBranch(fullBranchName);
+  private RevTag tag(String name, RevObject dest) throws Exception {
+    return tr.update(REFS_TAGS + name, tr.tag(name, dest));
   }
 }