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. 173
      org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java

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

Loading…
Cancel
Save