From 1b8da0dcfa52264db36a3613d47a415f00fb10c6 Mon Sep 17 00:00:00 2001 From: Thomas Wolf Date: Mon, 21 Oct 2019 11:16:38 +0200 Subject: [PATCH] Close ObjectInserters in tests Change-Id: I0048a9ae23aee79cfecafb15855c63cd92818c57 Signed-off-by: Thomas Wolf --- .../org/eclipse/jgit/api/AddCommandTest.java | 30 +++---- .../DirCacheCheckoutMaliciousPathTest.java | 80 ++++++++++--------- .../jgit/lib/DirCacheCheckoutTest.java | 6 +- .../org/eclipse/jgit/lib/IndexDiffTest.java | 6 +- 4 files changed, 67 insertions(+), 55 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java index b28e26a639..2f532b4ec4 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java @@ -700,26 +700,26 @@ public class AddCommandTest extends RepositoryTestCase { writer.print("content b"); } - ObjectInserter newObjectInserter = db.newObjectInserter(); DirCache dc = db.lockDirCache(); - DirCacheBuilder builder = dc.builder(); - - addEntryToBuilder("b.txt", file2, newObjectInserter, builder, 0); - addEntryToBuilder("a.txt", file, newObjectInserter, builder, 1); + try (ObjectInserter newObjectInserter = db.newObjectInserter()) { + DirCacheBuilder builder = dc.builder(); - try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) { - writer.print("other content"); - } - addEntryToBuilder("a.txt", file, newObjectInserter, builder, 3); + addEntryToBuilder("b.txt", file2, newObjectInserter, builder, 0); + addEntryToBuilder("a.txt", file, newObjectInserter, builder, 1); - try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) { - writer.print("our content"); - } - addEntryToBuilder("a.txt", file, newObjectInserter, builder, 2) - .getObjectId(); + try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) { + writer.print("other content"); + } + addEntryToBuilder("a.txt", file, newObjectInserter, builder, 3); - builder.commit(); + try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) { + writer.print("our content"); + } + addEntryToBuilder("a.txt", file, newObjectInserter, builder, 2) + .getObjectId(); + builder.commit(); + } assertEquals( "[a.txt, mode:100644, stage:1, content:content]" + "[a.txt, mode:100644, stage:2, content:our content]" + diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutMaliciousPathTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutMaliciousPathTest.java index 057e0c881b..46fd902b6c 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutMaliciousPathTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutMaliciousPathTest.java @@ -341,48 +341,56 @@ public class DirCacheCheckoutMaliciousPathTest extends RepositoryTestCase { String... path) throws GitAPIException, IOException { try (Git git = new Git(db); RevWalk revWalk = new RevWalk(git.getRepository())) { - ObjectInserter newObjectInserter; - newObjectInserter = git.getRepository().newObjectInserter(); - ObjectId blobId = newObjectInserter.insert(Constants.OBJ_BLOB, + ObjectId blobId; + try (ObjectInserter newObjectInserter = git.getRepository() + .newObjectInserter()) { + blobId = newObjectInserter.insert(Constants.OBJ_BLOB, "data".getBytes(UTF_8)); - newObjectInserter = git.getRepository().newObjectInserter(); + } FileMode mode = FileMode.REGULAR_FILE; ObjectId insertId = blobId; - for (int i = path.length - 1; i >= 0; --i) { - TreeFormatter treeFormatter = new TreeFormatter(); - treeFormatter.append("goodpath", mode, insertId); - insertId = newObjectInserter.insert(treeFormatter); - mode = FileMode.TREE; + try (ObjectInserter newObjectInserter = git.getRepository() + .newObjectInserter()) { + for (int i = path.length - 1; i >= 0; --i) { + TreeFormatter treeFormatter = new TreeFormatter(); + treeFormatter.append("goodpath", mode, insertId); + insertId = newObjectInserter.insert(treeFormatter); + mode = FileMode.TREE; + } } - newObjectInserter = git.getRepository().newObjectInserter(); - CommitBuilder commitBuilder = new CommitBuilder(); - commitBuilder.setAuthor(author); - commitBuilder.setCommitter(committer); - commitBuilder.setMessage("foo#1"); - commitBuilder.setTreeId(insertId); - ObjectId firstCommitId = newObjectInserter.insert(commitBuilder); - - newObjectInserter = git.getRepository().newObjectInserter(); - mode = FileMode.REGULAR_FILE; - insertId = blobId; - for (int i = path.length - 1; i >= 0; --i) { - TreeFormatter treeFormatter = new TreeFormatter(); - treeFormatter.append(path[i].getBytes(UTF_8), 0, - path[i].getBytes(UTF_8).length, - mode, insertId, true); - insertId = newObjectInserter.insert(treeFormatter); - mode = FileMode.TREE; + ObjectId firstCommitId; + try (ObjectInserter newObjectInserter = git.getRepository() + .newObjectInserter()) { + CommitBuilder commitBuilder = new CommitBuilder(); + commitBuilder.setAuthor(author); + commitBuilder.setCommitter(committer); + commitBuilder.setMessage("foo#1"); + commitBuilder.setTreeId(insertId); + firstCommitId = newObjectInserter.insert(commitBuilder); } + ObjectId commitId; + try (ObjectInserter newObjectInserter = git.getRepository() + .newObjectInserter()) { + mode = FileMode.REGULAR_FILE; + insertId = blobId; + for (int i = path.length - 1; i >= 0; --i) { + TreeFormatter treeFormatter = new TreeFormatter(); + treeFormatter.append(path[i].getBytes(UTF_8), 0, + path[i].getBytes(UTF_8).length, mode, insertId, + true); + insertId = newObjectInserter.insert(treeFormatter); + mode = FileMode.TREE; + } - // Create another commit - commitBuilder = new CommitBuilder(); - commitBuilder.setAuthor(author); - commitBuilder.setCommitter(committer); - commitBuilder.setMessage("foo#2"); - commitBuilder.setTreeId(insertId); - commitBuilder.setParentId(firstCommitId); - ObjectId commitId = newObjectInserter.insert(commitBuilder); - + // Create another commit + CommitBuilder commitBuilder = new CommitBuilder(); + commitBuilder.setAuthor(author); + commitBuilder.setCommitter(committer); + commitBuilder.setMessage("foo#2"); + commitBuilder.setTreeId(insertId); + commitBuilder.setParentId(firstCommitId); + commitId = newObjectInserter.insert(commitBuilder); + } if (!secondCheckout) git.checkout().setStartPoint(revWalk.parseCommit(firstCommitId)) .setName("refs/heads/master").setCreateBranch(true).call(); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java index 8092c3134b..a272c8f2ee 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java @@ -349,9 +349,11 @@ public class DirCacheCheckoutTest extends RepositoryTestCase { DirCacheEditor editor = dirCache.editor(); for (java.util.Map.Entry e : indexEntries.entrySet()) { writeTrashFile(e.getKey(), e.getValue()); - ObjectInserter inserter = db.newObjectInserter(); - final ObjectId id = inserter.insert(Constants.OBJ_BLOB, + ObjectId id; + try (ObjectInserter inserter = db.newObjectInserter()) { + id = inserter.insert(Constants.OBJ_BLOB, Constants.encode(e.getValue())); + } editor.add(new DirCacheEditor.DeletePath(e.getKey())); editor.add(new DirCacheEditor.PathEdit(e.getKey()) { @Override diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/IndexDiffTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/IndexDiffTest.java index ba5aaf1b18..785daf6058 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/IndexDiffTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/IndexDiffTest.java @@ -81,10 +81,12 @@ public class IndexDiffTest extends RepositoryTestCase { static PathEdit add(final Repository db, final File workdir, final String path) throws FileNotFoundException, IOException { - ObjectInserter inserter = db.newObjectInserter(); final File f = new File(workdir, path); - final ObjectId id = inserter.insert(Constants.OBJ_BLOB, + ObjectId id; + try (ObjectInserter inserter = db.newObjectInserter()) { + id = inserter.insert(Constants.OBJ_BLOB, IO.readFully(f)); + } return new PathEdit(path) { @Override public void apply(DirCacheEntry ent) { -- 2.39.5