Browse Source

Merge topic 'testrepo'

* changes:
  TestRepository: Allow custom author/committer per-commit
  TestRepository: Use try-with-resources where appropriate
stable-4.0
Shawn Pearce 10 years ago committed by Gerrit Code Review @ Eclipse.org
parent
commit
c3b8615ce8
  1. 107
      org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java

107
org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java

@ -106,9 +106,9 @@ import org.eclipse.jgit.util.io.SafeBufferedOutputStream;
* type of Repository the test data is stored on.
*/
public class TestRepository<R extends Repository> {
private static final PersonIdent author;
private static final PersonIdent defaultAuthor;
private static final PersonIdent committer;
private static final PersonIdent defaultCommitter;
static {
final MockSystemReader m = new MockSystemReader();
@ -117,11 +117,11 @@ public class TestRepository<R extends Repository> {
final String an = "J. Author";
final String ae = "jauthor@example.com";
author = new PersonIdent(an, ae, now, tz);
defaultAuthor = new PersonIdent(an, ae, now, tz);
final String cn = "J. Committer";
final String ce = "jcommitter@example.com";
committer = new PersonIdent(cn, ce, now, tz);
defaultCommitter = new PersonIdent(cn, ce, now, tz);
}
private final R db;
@ -191,8 +191,8 @@ public class TestRepository<R extends Repository> {
* the commit builder to store.
*/
public void setAuthorAndCommitter(org.eclipse.jgit.lib.CommitBuilder c) {
c.setAuthor(new PersonIdent(author, new Date(now)));
c.setCommitter(new PersonIdent(committer, new Date(now)));
c.setAuthor(new PersonIdent(defaultAuthor, new Date(now)));
c.setCommitter(new PersonIdent(defaultCommitter, new Date(now)));
}
/**
@ -217,11 +217,9 @@ public class TestRepository<R extends Repository> {
*/
public RevBlob blob(final byte[] content) throws Exception {
ObjectId id;
try {
id = inserter.insert(Constants.OBJ_BLOB, content);
inserter.flush();
} finally {
inserter.release();
try (ObjectInserter ins = inserter) {
id = ins.insert(Constants.OBJ_BLOB, content);
ins.flush();
}
return pool.lookupBlob(id);
}
@ -260,11 +258,9 @@ public class TestRepository<R extends Repository> {
b.add(e);
b.finish();
ObjectId root;
try {
root = dc.writeTree(inserter);
inserter.flush();
} finally {
inserter.release();
try (ObjectInserter ins = inserter) {
root = dc.writeTree(ins);
ins.flush();
}
return pool.lookupTree(root);
}
@ -281,7 +277,7 @@ public class TestRepository<R extends Repository> {
*/
public RevObject get(final RevTree tree, final String path)
throws Exception {
final TreeWalk tw = new TreeWalk(pool.getObjectReader());
try (TreeWalk tw = new TreeWalk(pool.getObjectReader())) {
tw.setFilter(PathFilterGroup.createFromStrings(Collections
.singleton(path)));
tw.reset(tree);
@ -294,6 +290,7 @@ public class TestRepository<R extends Repository> {
final FileMode entmode = tw.getFileMode(0);
return pool.lookupAny(entid, entmode.getObjectType());
}
}
fail("Can't find " + path + " in tree " + tree.name());
return null; // never reached.
}
@ -373,15 +370,13 @@ public class TestRepository<R extends Repository> {
c = new org.eclipse.jgit.lib.CommitBuilder();
c.setTreeId(tree);
c.setParentIds(parents);
c.setAuthor(new PersonIdent(author, new Date(now)));
c.setCommitter(new PersonIdent(committer, new Date(now)));
c.setAuthor(new PersonIdent(defaultAuthor, new Date(now)));
c.setCommitter(new PersonIdent(defaultCommitter, new Date(now)));
c.setMessage("");
ObjectId id;
try {
id = inserter.insert(c);
inserter.flush();
} finally {
inserter.release();
try (ObjectInserter ins = inserter) {
id = ins.insert(c);
ins.flush();
}
return pool.lookupCommit(id);
}
@ -411,14 +406,12 @@ public class TestRepository<R extends Repository> {
final TagBuilder t = new TagBuilder();
t.setObjectId(dst);
t.setTag(name);
t.setTagger(new PersonIdent(committer, new Date(now)));
t.setTagger(new PersonIdent(defaultCommitter, new Date(now)));
t.setMessage("");
ObjectId id;
try {
id = inserter.insert(t);
inserter.flush();
} finally {
inserter.release();
try (ObjectInserter ins = inserter) {
id = ins.insert(t);
ins.flush();
}
return (RevTag) pool.lookupAny(id, Constants.OBJ_TAG);
}
@ -581,7 +574,7 @@ public class TestRepository<R extends Repository> {
*/
public void fsck(RevObject... tips) throws MissingObjectException,
IncorrectObjectTypeException, IOException {
ObjectWalk ow = new ObjectWalk(db);
try (ObjectWalk ow = new ObjectWalk(db)) {
if (tips.length != 0) {
for (RevObject o : tips)
ow.markStart(ow.parseAny(o));
@ -611,6 +604,7 @@ public class TestRepository<R extends Repository> {
assertHash(o, bin);
}
}
}
private static void assertHash(RevObject id, byte[] bin) {
MessageDigest md = Constants.newMessageDigest();
@ -636,35 +630,27 @@ public class TestRepository<R extends Repository> {
NullProgressMonitor m = NullProgressMonitor.INSTANCE;
final File pack, idx;
PackWriter pw = new PackWriter(db);
try {
try (PackWriter pw = new PackWriter(db)) {
Set<ObjectId> all = new HashSet<ObjectId>();
for (Ref r : db.getAllRefs().values())
all.add(r.getObjectId());
pw.preparePack(m, all, Collections.<ObjectId> emptySet());
final ObjectId name = pw.computeName();
OutputStream out;
pack = nameFor(odb, name, ".pack");
out = new SafeBufferedOutputStream(new FileOutputStream(pack));
try {
try (OutputStream out =
new SafeBufferedOutputStream(new FileOutputStream(pack))) {
pw.writePack(m, m, out);
} finally {
out.close();
}
pack.setReadOnly();
idx = nameFor(odb, name, ".idx");
out = new SafeBufferedOutputStream(new FileOutputStream(idx));
try {
try (OutputStream out =
new SafeBufferedOutputStream(new FileOutputStream(idx))) {
pw.writeIndex(out);
} finally {
out.close();
}
idx.setReadOnly();
} finally {
pw.release();
}
odb.openPack(pack);
@ -760,6 +746,9 @@ public class TestRepository<R extends Repository> {
private RevCommit self;
private PersonIdent author;
private PersonIdent committer;
CommitBuilder() {
branch = null;
}
@ -851,6 +840,22 @@ public class TestRepository<R extends Repository> {
return this;
}
public CommitBuilder ident(PersonIdent ident) {
author = ident;
committer = ident;
return this;
}
public CommitBuilder author(PersonIdent a) {
author = a;
return this;
}
public CommitBuilder committer(PersonIdent c) {
committer = c;
return this;
}
public RevCommit create() throws Exception {
if (self == null) {
TestRepository.this.tick(tick);
@ -860,18 +865,20 @@ public class TestRepository<R extends Repository> {
c = new org.eclipse.jgit.lib.CommitBuilder();
c.setParentIds(parents);
setAuthorAndCommitter(c);
if (author != null)
c.setAuthor(author);
if (committer != null)
c.setCommitter(committer);
c.setMessage(message);
ObjectId commitId;
try {
try (ObjectInserter ins = inserter) {
if (topLevelTree != null)
c.setTreeId(topLevelTree);
else
c.setTreeId(tree.writeTree(inserter));
commitId = inserter.insert(c);
inserter.flush();
} finally {
inserter.release();
c.setTreeId(tree.writeTree(ins));
commitId = ins.insert(c);
ins.flush();
}
self = pool.lookupCommit(commitId);

Loading…
Cancel
Save