Browse Source

Close ObjectInserters in tests

Change-Id: I0048a9ae23aee79cfecafb15855c63cd92818c57
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
next
Thomas Wolf 5 years ago
parent
commit
1b8da0dcfa
  1. 4
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java
  2. 34
      org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutMaliciousPathTest.java
  3. 6
      org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java
  4. 6
      org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/IndexDiffTest.java

4
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java

@ -700,8 +700,8 @@ public class AddCommandTest extends RepositoryTestCase {
writer.print("content b"); writer.print("content b");
} }
ObjectInserter newObjectInserter = db.newObjectInserter();
DirCache dc = db.lockDirCache(); DirCache dc = db.lockDirCache();
try (ObjectInserter newObjectInserter = db.newObjectInserter()) {
DirCacheBuilder builder = dc.builder(); DirCacheBuilder builder = dc.builder();
addEntryToBuilder("b.txt", file2, newObjectInserter, builder, 0); addEntryToBuilder("b.txt", file2, newObjectInserter, builder, 0);
@ -719,7 +719,7 @@ public class AddCommandTest extends RepositoryTestCase {
.getObjectId(); .getObjectId();
builder.commit(); builder.commit();
}
assertEquals( assertEquals(
"[a.txt, mode:100644, stage:1, content:content]" + "[a.txt, mode:100644, stage:1, content:content]" +
"[a.txt, mode:100644, stage:2, content:our content]" + "[a.txt, mode:100644, stage:2, content:our content]" +

34
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 { String... path) throws GitAPIException, IOException {
try (Git git = new Git(db); try (Git git = new Git(db);
RevWalk revWalk = new RevWalk(git.getRepository())) { RevWalk revWalk = new RevWalk(git.getRepository())) {
ObjectInserter newObjectInserter; ObjectId blobId;
newObjectInserter = git.getRepository().newObjectInserter(); try (ObjectInserter newObjectInserter = git.getRepository()
ObjectId blobId = newObjectInserter.insert(Constants.OBJ_BLOB, .newObjectInserter()) {
blobId = newObjectInserter.insert(Constants.OBJ_BLOB,
"data".getBytes(UTF_8)); "data".getBytes(UTF_8));
newObjectInserter = git.getRepository().newObjectInserter(); }
FileMode mode = FileMode.REGULAR_FILE; FileMode mode = FileMode.REGULAR_FILE;
ObjectId insertId = blobId; ObjectId insertId = blobId;
try (ObjectInserter newObjectInserter = git.getRepository()
.newObjectInserter()) {
for (int i = path.length - 1; i >= 0; --i) { for (int i = path.length - 1; i >= 0; --i) {
TreeFormatter treeFormatter = new TreeFormatter(); TreeFormatter treeFormatter = new TreeFormatter();
treeFormatter.append("goodpath", mode, insertId); treeFormatter.append("goodpath", mode, insertId);
insertId = newObjectInserter.insert(treeFormatter); insertId = newObjectInserter.insert(treeFormatter);
mode = FileMode.TREE; mode = FileMode.TREE;
} }
newObjectInserter = git.getRepository().newObjectInserter(); }
ObjectId firstCommitId;
try (ObjectInserter newObjectInserter = git.getRepository()
.newObjectInserter()) {
CommitBuilder commitBuilder = new CommitBuilder(); CommitBuilder commitBuilder = new CommitBuilder();
commitBuilder.setAuthor(author); commitBuilder.setAuthor(author);
commitBuilder.setCommitter(committer); commitBuilder.setCommitter(committer);
commitBuilder.setMessage("foo#1"); commitBuilder.setMessage("foo#1");
commitBuilder.setTreeId(insertId); commitBuilder.setTreeId(insertId);
ObjectId firstCommitId = newObjectInserter.insert(commitBuilder); firstCommitId = newObjectInserter.insert(commitBuilder);
}
newObjectInserter = git.getRepository().newObjectInserter(); ObjectId commitId;
try (ObjectInserter newObjectInserter = git.getRepository()
.newObjectInserter()) {
mode = FileMode.REGULAR_FILE; mode = FileMode.REGULAR_FILE;
insertId = blobId; insertId = blobId;
for (int i = path.length - 1; i >= 0; --i) { for (int i = path.length - 1; i >= 0; --i) {
TreeFormatter treeFormatter = new TreeFormatter(); TreeFormatter treeFormatter = new TreeFormatter();
treeFormatter.append(path[i].getBytes(UTF_8), 0, treeFormatter.append(path[i].getBytes(UTF_8), 0,
path[i].getBytes(UTF_8).length, path[i].getBytes(UTF_8).length, mode, insertId,
mode, insertId, true); true);
insertId = newObjectInserter.insert(treeFormatter); insertId = newObjectInserter.insert(treeFormatter);
mode = FileMode.TREE; mode = FileMode.TREE;
} }
// Create another commit // Create another commit
commitBuilder = new CommitBuilder(); CommitBuilder commitBuilder = new CommitBuilder();
commitBuilder.setAuthor(author); commitBuilder.setAuthor(author);
commitBuilder.setCommitter(committer); commitBuilder.setCommitter(committer);
commitBuilder.setMessage("foo#2"); commitBuilder.setMessage("foo#2");
commitBuilder.setTreeId(insertId); commitBuilder.setTreeId(insertId);
commitBuilder.setParentId(firstCommitId); commitBuilder.setParentId(firstCommitId);
ObjectId commitId = newObjectInserter.insert(commitBuilder); commitId = newObjectInserter.insert(commitBuilder);
}
if (!secondCheckout) if (!secondCheckout)
git.checkout().setStartPoint(revWalk.parseCommit(firstCommitId)) git.checkout().setStartPoint(revWalk.parseCommit(firstCommitId))
.setName("refs/heads/master").setCreateBranch(true).call(); .setName("refs/heads/master").setCreateBranch(true).call();

6
org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java

@ -349,9 +349,11 @@ public class DirCacheCheckoutTest extends RepositoryTestCase {
DirCacheEditor editor = dirCache.editor(); DirCacheEditor editor = dirCache.editor();
for (java.util.Map.Entry<String,String> e : indexEntries.entrySet()) { for (java.util.Map.Entry<String,String> e : indexEntries.entrySet()) {
writeTrashFile(e.getKey(), e.getValue()); writeTrashFile(e.getKey(), e.getValue());
ObjectInserter inserter = db.newObjectInserter(); ObjectId id;
final ObjectId id = inserter.insert(Constants.OBJ_BLOB, try (ObjectInserter inserter = db.newObjectInserter()) {
id = inserter.insert(Constants.OBJ_BLOB,
Constants.encode(e.getValue())); Constants.encode(e.getValue()));
}
editor.add(new DirCacheEditor.DeletePath(e.getKey())); editor.add(new DirCacheEditor.DeletePath(e.getKey()));
editor.add(new DirCacheEditor.PathEdit(e.getKey()) { editor.add(new DirCacheEditor.PathEdit(e.getKey()) {
@Override @Override

6
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, static PathEdit add(final Repository db, final File workdir,
final String path) throws FileNotFoundException, IOException { final String path) throws FileNotFoundException, IOException {
ObjectInserter inserter = db.newObjectInserter();
final File f = new File(workdir, path); 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)); IO.readFully(f));
}
return new PathEdit(path) { return new PathEdit(path) {
@Override @Override
public void apply(DirCacheEntry ent) { public void apply(DirCacheEntry ent) {

Loading…
Cancel
Save