Abstract out utility functions for creating test commits

These routines create a fairly clean DSL for writing out the
structure of a repository in a test case.  Abstract them into
a helper class that we can reuse in other test environments.

Change-Id: I55cce3d557e1a28afe2fdf37b3a5b67e2651c9f1
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/DateRevQueueTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/DateRevQueueTest.java
index b3a9295..ee9c81c 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/DateRevQueueTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/DateRevQueueTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009, Google Inc.
+ * Copyright (C) 2009-2010, Google Inc.
  * and other copyright owners as documented in the project's IP log.
  *
  * This program and the accompanying materials are made available
@@ -60,10 +60,10 @@ public void testCloneEmpty() throws Exception {
 	}
 
 	public void testInsertOutOfOrder() throws Exception {
-		final RevCommit a = parse(commit());
-		final RevCommit b = parse(commit(10, a));
-		final RevCommit c1 = parse(commit(5, b));
-		final RevCommit c2 = parse(commit(-50, b));
+		final RevCommit a = parseBody(commit());
+		final RevCommit b = parseBody(commit(10, a));
+		final RevCommit c1 = parseBody(commit(5, b));
+		final RevCommit c2 = parseBody(commit(-50, b));
 
 		q.add(c2);
 		q.add(a);
@@ -78,8 +78,8 @@ public void testInsertOutOfOrder() throws Exception {
 	}
 
 	public void testInsertTie() throws Exception {
-		final RevCommit a = parse(commit());
-		final RevCommit b = parse(commit(0, a));
+		final RevCommit a = parseBody(commit());
+		final RevCommit b = parseBody(commit(0, a));
 		{
 			q = create();
 			q.add(a);
@@ -101,9 +101,9 @@ public void testInsertTie() throws Exception {
 	}
 
 	public void testCloneFIFO() throws Exception {
-		final RevCommit a = parse(commit());
-		final RevCommit b = parse(commit(200, a));
-		final RevCommit c = parse(commit(200, b));
+		final RevCommit a = parseBody(commit());
+		final RevCommit b = parseBody(commit(200, a));
+		final RevCommit c = parseBody(commit(200, b));
 
 		final FIFORevQueue src = new FIFORevQueue();
 		src.add(a);
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/ObjectWalkTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/ObjectWalkTest.java
index 7dddeee..582406c 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/ObjectWalkTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/ObjectWalkTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009, Google Inc.
+ * Copyright (C) 2009-2010, Google Inc.
  * and other copyright owners as documented in the project's IP log.
  *
  * This program and the accompanying materials are made available
@@ -46,6 +46,7 @@
 public class ObjectWalkTest extends RevWalkTestCase {
 	protected ObjectWalk objw;
 
+	@Override
 	protected RevWalk createRevWalk() {
 		return objw = new ObjectWalk(db);
 	}
@@ -64,7 +65,7 @@ public void testTwoCommitsEmptyTree() throws Exception {
 		assertCommit(a, objw.next());
 		assertNull(objw.next());
 
-		assertSame(emptyTree, objw.nextObject());
+		assertSame(tree(), objw.nextObject());
 		assertNull(objw.nextObject());
 	}
 
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevObjectTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevObjectTest.java
index 87ecaa8..d4f289a 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevObjectTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevObjectTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009, Google Inc.
+ * Copyright (C) 2009-2010, Google Inc.
  * and other copyright owners as documented in the project's IP log.
  *
  * This program and the accompanying materials are made available
@@ -81,10 +81,10 @@ public void testEqualsIsIdentity() throws Exception {
 	}
 
 	public void testRevObjectTypes() throws Exception {
-		assertEquals(Constants.OBJ_TREE, emptyTree.getType());
+		assertEquals(Constants.OBJ_TREE, tree().getType());
 		assertEquals(Constants.OBJ_COMMIT, commit().getType());
 		assertEquals(Constants.OBJ_BLOB, blob("").getType());
-		assertEquals(Constants.OBJ_TAG, tag("emptyTree", emptyTree).getType());
+		assertEquals(Constants.OBJ_TAG, tag("emptyTree", tree()).getType());
 	}
 
 	public void testHasRevFlag() throws Exception {
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevQueueTestCase.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevQueueTestCase.java
index 24e84b0..c549054 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevQueueTestCase.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevQueueTestCase.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009, Google Inc.
+ * Copyright (C) 2009-2010, Google Inc.
  * and other copyright owners as documented in the project's IP log.
  *
  * This program and the accompanying materials are made available
@@ -61,8 +61,8 @@ public void testEmpty() throws Exception {
 	}
 
 	public void testClear() throws Exception {
-		final RevCommit a = parse(commit());
-		final RevCommit b = parse(commit(a));
+		final RevCommit a = parseBody(commit());
+		final RevCommit b = parseBody(commit(a));
 
 		q.add(a);
 		q.add(b);
@@ -71,8 +71,8 @@ public void testClear() throws Exception {
 	}
 
 	public void testHasFlags() throws Exception {
-		final RevCommit a = parse(commit());
-		final RevCommit b = parse(commit(a));
+		final RevCommit a = parseBody(commit());
+		final RevCommit b = parseBody(commit(a));
 
 		q.add(a);
 		q.add(b);
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkFilterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkFilterTest.java
index db4c38e..a6421c4 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkFilterTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkFilterTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009, Google Inc.
+ * Copyright (C) 2009-2010, Google Inc.
  * and other copyright owners as documented in the project's IP log.
  *
  * This program and the accompanying materials are made available
@@ -231,14 +231,14 @@ public void testCommitTimeRevFilter() throws Exception {
 		final RevCommit b = commit(a);
 		tick(100);
 
-		Date since = new Date(nowTick);
+		Date since = getClock();
 		final RevCommit c1 = commit(b);
 		tick(100);
 
 		final RevCommit c2 = commit(b);
 		tick(100);
 
-		Date until =  new Date(nowTick);
+		Date until = getClock();
 		final RevCommit d = commit(c1, c2);
 		tick(100);
 
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkPathFilter6012Test.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkPathFilter6012Test.java
index 73d41ea..c4bfbf8 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkPathFilter6012Test.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkPathFilter6012Test.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009, Google Inc.
+ * Copyright (C) 2009-2010, Google Inc.
  * and other copyright owners as documented in the project's IP log.
  *
  * This program and the accompanying materials are made available
@@ -80,10 +80,10 @@ public void setUp() throws Exception {
 		b = commit(tree(file(pF, zI)), a);
 		c = commit(tree(file(pF, zI)), a);
 		d = commit(tree(file(pA, zS), file(pF, zI)), c);
-		parse(d);
+		parseBody(d);
 		e = commit(d.getTree(), d, b);
 		f = commit(tree(file(pA, zS), file(pE, zY), file(pF, zI)), e);
-		parse(f);
+		parseBody(f);
 		g = commit(tree(file(pE, zY), file(pF, zI)), b);
 		h = commit(f.getTree(), g, f);
 		i = commit(tree(file(pA, zS), file(pE, zY), file(pF, zF)), h);
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkSortTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkSortTest.java
index 0d3e0cf..65ed873 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkSortTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkSortTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009, Google Inc.
+ * Copyright (C) 2009-2010, Google Inc.
  * and other copyright owners as documented in the project's IP log.
  *
  * This program and the accompanying materials are made available
@@ -96,8 +96,8 @@ public void testSort_COMMIT_TIME_DESC_OutOfOrder1() throws Exception {
 		final RevCommit b = commit(a);
 		final RevCommit c = commit(-5, b);
 		final RevCommit d = commit(10, c);
-		assertTrue(parse(a).getCommitTime() < parse(d).getCommitTime());
-		assertTrue(parse(c).getCommitTime() < parse(b).getCommitTime());
+		assertTrue(parseBody(a).getCommitTime() < parseBody(d).getCommitTime());
+		assertTrue(parseBody(c).getCommitTime() < parseBody(b).getCommitTime());
 
 		rw.sort(RevSort.COMMIT_TIME_DESC);
 		markStart(d);
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkTestCase.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkTestCase.java
index 312fc00..6405232 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkTestCase.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkTestCase.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009, Google Inc.
+ * Copyright (C) 2009-2010, Google Inc.
  * and other copyright owners as documented in the project's IP log.
  *
  * This program and the accompanying materials are made available
@@ -43,130 +43,81 @@
 
 package org.eclipse.jgit.revwalk;
 
-import java.util.Collections;
 import java.util.Date;
 
-import org.eclipse.jgit.dircache.DirCache;
-import org.eclipse.jgit.dircache.DirCacheBuilder;
 import org.eclipse.jgit.dircache.DirCacheEntry;
-import org.eclipse.jgit.lib.Commit;
-import org.eclipse.jgit.lib.Constants;
-import org.eclipse.jgit.lib.FileMode;
-import org.eclipse.jgit.lib.ObjectId;
-import org.eclipse.jgit.lib.ObjectWriter;
-import org.eclipse.jgit.lib.PersonIdent;
+import org.eclipse.jgit.junit.TestRepository;
 import org.eclipse.jgit.lib.RepositoryTestCase;
-import org.eclipse.jgit.lib.Tag;
-import org.eclipse.jgit.lib.Tree;
-import org.eclipse.jgit.treewalk.TreeWalk;
-import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
 
 /** Support for tests of the {@link RevWalk} class. */
 public abstract class RevWalkTestCase extends RepositoryTestCase {
-	protected ObjectWriter ow;
-
-	protected RevTree emptyTree;
-
-	protected long nowTick;
+	private TestRepository util;
 
 	protected RevWalk rw;
 
+	@Override
 	public void setUp() throws Exception {
 		super.setUp();
-		ow = new ObjectWriter(db);
-		rw = createRevWalk();
-		emptyTree = rw.parseTree(ow.writeTree(new Tree(db)));
-		nowTick = 1236977987000L;
+		util = new TestRepository(db, createRevWalk());
+		rw = util.getRevWalk();
 	}
 
 	protected RevWalk createRevWalk() {
 		return new RevWalk(db);
 	}
 
+	protected Date getClock() {
+		return util.getClock();
+	}
+
 	protected void tick(final int secDelta) {
-		nowTick += secDelta * 1000L;
+		util.tick(secDelta);
 	}
 
 	protected RevBlob blob(final String content) throws Exception {
-		return rw.lookupBlob(ow.writeBlob(Constants.encode(content)));
+		return util.blob(content);
 	}
 
 	protected DirCacheEntry file(final String path, final RevBlob blob)
 			throws Exception {
-		final DirCacheEntry e = new DirCacheEntry(path);
-		e.setFileMode(FileMode.REGULAR_FILE);
-		e.setObjectId(blob);
-		return e;
+		return util.file(path, blob);
 	}
 
 	protected RevTree tree(final DirCacheEntry... entries) throws Exception {
-		final DirCache dc = DirCache.newInCore();
-		final DirCacheBuilder b = dc.builder();
-		for (final DirCacheEntry e : entries)
-			b.add(e);
-		b.finish();
-		return rw.lookupTree(dc.writeTree(ow));
+		return util.tree(entries);
 	}
 
 	protected RevObject get(final RevTree tree, final String path)
 			throws Exception {
-		final TreeWalk tw = new TreeWalk(db);
-		tw.setFilter(PathFilterGroup.createFromStrings(Collections
-				.singleton(path)));
-		tw.reset(tree);
-		while (tw.next()) {
-			if (tw.isSubtree() && !path.equals(tw.getPathString())) {
-				tw.enterSubtree();
-				continue;
-			}
-			final ObjectId entid = tw.getObjectId(0);
-			final FileMode entmode = tw.getFileMode(0);
-			return rw.lookupAny(entid, entmode.getObjectType());
-		}
-		fail("Can't find " + path + " in tree " + tree.name());
-		return null; // never reached.
+		return util.get(tree, path);
 	}
 
 	protected RevCommit commit(final RevCommit... parents) throws Exception {
-		return commit(1, emptyTree, parents);
+		return util.commit(parents);
 	}
 
 	protected RevCommit commit(final RevTree tree, final RevCommit... parents)
 			throws Exception {
-		return commit(1, tree, parents);
+		return util.commit(tree, parents);
 	}
 
 	protected RevCommit commit(final int secDelta, final RevCommit... parents)
 			throws Exception {
-		return commit(secDelta, emptyTree, parents);
+		return util.commit(secDelta, parents);
 	}
 
 	protected RevCommit commit(final int secDelta, final RevTree tree,
 			final RevCommit... parents) throws Exception {
-		tick(secDelta);
-		final Commit c = new Commit(db);
-		c.setTreeId(tree);
-		c.setParentIds(parents);
-		c.setAuthor(new PersonIdent(author, new Date(nowTick)));
-		c.setCommitter(new PersonIdent(committer, new Date(nowTick)));
-		c.setMessage("");
-		return rw.lookupCommit(ow.writeCommit(c));
+		return util.commit(secDelta, tree, parents);
 	}
 
 	protected RevTag tag(final String name, final RevObject dst)
 			throws Exception {
-		final Tag t = new Tag(db);
-		t.setType(Constants.typeString(dst.getType()));
-		t.setObjId(dst.toObjectId());
-		t.setTag(name);
-		t.setTagger(new PersonIdent(committer, new Date(nowTick)));
-		t.setMessage("");
-		return (RevTag) rw.lookupAny(ow.writeTag(t), Constants.OBJ_TAG);
+		return util.tag(name, dst);
 	}
 
-	protected <T extends RevObject> T parse(final T t) throws Exception {
-		rw.parseBody(t);
-		return t;
+	protected <T extends RevObject> T parseBody(final T t) throws Exception {
+		return util.parseBody(t);
 	}
 
 	protected void markStart(final RevCommit commit) throws Exception {