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. 30
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java
  2. 80
      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

30
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]" +

80
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();

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();
for (java.util.Map.Entry<String,String> 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

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,
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) {

Loading…
Cancel
Save