From 6599111d92216ab2f65d78477a371e29db6cfc9f Mon Sep 17 00:00:00 2001 From: Dave Borowitz Date: Wed, 11 Mar 2015 11:39:59 -0700 Subject: TestRepository: Use try-with-resources where appropriate Change-Id: I06f9534ab84278df37a140700fc2bed5ab667299 --- .../src/org/eclipse/jgit/junit/TestRepository.java | 132 +++++++++------------ 1 file changed, 58 insertions(+), 74 deletions(-) (limited to 'org.eclipse.jgit.junit') diff --git a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java index b30fa59bd8..4f14b52bc3 100644 --- a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java +++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java @@ -217,11 +217,9 @@ public class TestRepository { */ 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 { 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,18 +277,19 @@ public class TestRepository { */ public RevObject get(final RevTree tree, final String path) throws Exception { - final TreeWalk tw = new TreeWalk(pool.getObjectReader()); - tw.setFilter(PathFilterGroup.createFromStrings(Collections - .singleton(path))); - tw.reset(tree); - while (tw.next()) { - if (tw.isSubtree() && !path.equals(tw.getPathString())) { - tw.enterSubtree(); - continue; + try (TreeWalk tw = new TreeWalk(pool.getObjectReader())) { + 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 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()); return null; // never reached. @@ -377,11 +374,9 @@ public class TestRepository { c.setCommitter(new PersonIdent(committer, 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); } @@ -414,11 +409,9 @@ public class TestRepository { t.setTagger(new PersonIdent(committer, 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,34 +574,35 @@ public class TestRepository { */ public void fsck(RevObject... tips) throws MissingObjectException, IncorrectObjectTypeException, IOException { - ObjectWalk ow = new ObjectWalk(db); - if (tips.length != 0) { - for (RevObject o : tips) - ow.markStart(ow.parseAny(o)); - } else { - for (Ref r : db.getAllRefs().values()) - ow.markStart(ow.parseAny(r.getObjectId())); - } + try (ObjectWalk ow = new ObjectWalk(db)) { + if (tips.length != 0) { + for (RevObject o : tips) + ow.markStart(ow.parseAny(o)); + } else { + for (Ref r : db.getAllRefs().values()) + ow.markStart(ow.parseAny(r.getObjectId())); + } - ObjectChecker oc = new ObjectChecker(); - for (;;) { - final RevCommit o = ow.next(); - if (o == null) - break; + ObjectChecker oc = new ObjectChecker(); + for (;;) { + final RevCommit o = ow.next(); + if (o == null) + break; - final byte[] bin = db.open(o, o.getType()).getCachedBytes(); - oc.checkCommit(bin); - assertHash(o, bin); - } + final byte[] bin = db.open(o, o.getType()).getCachedBytes(); + oc.checkCommit(bin); + assertHash(o, bin); + } - for (;;) { - final RevObject o = ow.nextObject(); - if (o == null) - break; + for (;;) { + final RevObject o = ow.nextObject(); + if (o == null) + break; - final byte[] bin = db.open(o, o.getType()).getCachedBytes(); - oc.check(o.getType(), bin); - assertHash(o, bin); + final byte[] bin = db.open(o, o.getType()).getCachedBytes(); + oc.check(o.getType(), bin); + assertHash(o, bin); + } } } @@ -636,35 +630,27 @@ public class TestRepository { NullProgressMonitor m = NullProgressMonitor.INSTANCE; final File pack, idx; - PackWriter pw = new PackWriter(db); - try { + try (PackWriter pw = new PackWriter(db)) { Set all = new HashSet(); for (Ref r : db.getAllRefs().values()) all.add(r.getObjectId()); pw.preparePack(m, all, Collections. 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); @@ -863,15 +849,13 @@ public class TestRepository { 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); -- cgit v1.2.3