Browse Source

CommitCommandTest: Open Git and TreeWalk in try-with-resource

Change-Id: I65a6fd7028e209c300d992c2756100c09ab4dc19
Signed-off-by: David Pursehouse <david.pursehouse@sonymobile.com>
stable-4.2
David Pursehouse 9 years ago
parent
commit
ff90192a05
  1. 38
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java

38
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java

@ -186,7 +186,7 @@ public class CommitCommandTest extends RepositoryTestCase {
@Test @Test
public void commitNewSubmodule() throws Exception { public void commitNewSubmodule() throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
writeTrashFile("file.txt", "content"); writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call(); git.add().addFilepattern("file.txt").call();
RevCommit commit = git.commit().setMessage("create file").call(); RevCommit commit = git.commit().setMessage("create file").call();
@ -215,7 +215,7 @@ public class CommitCommandTest extends RepositoryTestCase {
RevCommit submoduleCommit = git.commit().setMessage("submodule add") RevCommit submoduleCommit = git.commit().setMessage("submodule add")
.setOnly(path).call(); .setOnly(path).call();
assertNotNull(submoduleCommit); assertNotNull(submoduleCommit);
TreeWalk walk = new TreeWalk(db); try (TreeWalk walk = new TreeWalk(db)) {
walk.addTree(commit.getTree()); walk.addTree(commit.getTree());
walk.addTree(submoduleCommit.getTree()); walk.addTree(submoduleCommit.getTree());
walk.setFilter(TreeFilter.ANY_DIFF); walk.setFilter(TreeFilter.ANY_DIFF);
@ -228,10 +228,12 @@ public class CommitCommandTest extends RepositoryTestCase {
assertEquals(commit, subDiff.getNewId().toObjectId()); assertEquals(commit, subDiff.getNewId().toObjectId());
assertEquals(path, subDiff.getNewPath()); assertEquals(path, subDiff.getNewPath());
} }
}
}
@Test @Test
public void commitSubmoduleUpdate() throws Exception { public void commitSubmoduleUpdate() throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
writeTrashFile("file.txt", "content"); writeTrashFile("file.txt", "content");
git.add().addFilepattern("file.txt").call(); git.add().addFilepattern("file.txt").call();
RevCommit commit = git.commit().setMessage("create file").call(); RevCommit commit = git.commit().setMessage("create file").call();
@ -271,7 +273,7 @@ public class CommitCommandTest extends RepositoryTestCase {
RevCommit submoduleEditCommit = git.commit() RevCommit submoduleEditCommit = git.commit()
.setMessage("submodule add").setOnly(path).call(); .setMessage("submodule add").setOnly(path).call();
assertNotNull(submoduleEditCommit); assertNotNull(submoduleEditCommit);
TreeWalk walk = new TreeWalk(db); try (TreeWalk walk = new TreeWalk(db)) {
walk.addTree(submoduleAddCommit.getTree()); walk.addTree(submoduleAddCommit.getTree());
walk.addTree(submoduleEditCommit.getTree()); walk.addTree(submoduleEditCommit.getTree());
walk.setFilter(TreeFilter.ANY_DIFF); walk.setFilter(TreeFilter.ANY_DIFF);
@ -285,11 +287,12 @@ public class CommitCommandTest extends RepositoryTestCase {
assertEquals(path, subDiff.getNewPath()); assertEquals(path, subDiff.getNewPath());
assertEquals(path, subDiff.getOldPath()); assertEquals(path, subDiff.getOldPath());
} }
}
}
@Test @Test
public void commitUpdatesSmudgedEntries() throws Exception { public void commitUpdatesSmudgedEntries() throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
File file1 = writeTrashFile("file1.txt", "content1"); File file1 = writeTrashFile("file1.txt", "content1");
assertTrue(file1.setLastModified(file1.lastModified() - 5000)); assertTrue(file1.setLastModified(file1.lastModified() - 5000));
File file2 = writeTrashFile("file2.txt", "content2"); File file2 = writeTrashFile("file2.txt", "content2");
@ -341,11 +344,11 @@ public class CommitCommandTest extends RepositoryTestCase {
assertEquals(file2Id, cache.getEntry("file2.txt").getObjectId()); assertEquals(file2Id, cache.getEntry("file2.txt").getObjectId());
assertEquals(file3Id, cache.getEntry("file3.txt").getObjectId()); assertEquals(file3Id, cache.getEntry("file3.txt").getObjectId());
} }
}
@Test @Test
public void commitIgnoresSmudgedEntryWithDifferentId() throws Exception { public void commitIgnoresSmudgedEntryWithDifferentId() throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
File file1 = writeTrashFile("file1.txt", "content1"); File file1 = writeTrashFile("file1.txt", "content1");
assertTrue(file1.setLastModified(file1.lastModified() - 5000)); assertTrue(file1.setLastModified(file1.lastModified() - 5000));
File file2 = writeTrashFile("file2.txt", "content2"); File file2 = writeTrashFile("file2.txt", "content2");
@ -391,11 +394,11 @@ public class CommitCommandTest extends RepositoryTestCase {
assertEquals(file1Size, cache.getEntry("file1.txt").getLength()); assertEquals(file1Size, cache.getEntry("file1.txt").getLength());
assertEquals(0, cache.getEntry("file2.txt").getLength()); assertEquals(0, cache.getEntry("file2.txt").getLength());
} }
}
@Test @Test
public void commitAfterSquashMerge() throws Exception { public void commitAfterSquashMerge() throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
writeTrashFile("file1", "file1"); writeTrashFile("file1", "file1");
git.add().addFilepattern("file1").call(); git.add().addFilepattern("file1").call();
RevCommit first = git.commit().setMessage("initial commit").call(); RevCommit first = git.commit().setMessage("initial commit").call();
@ -431,18 +434,19 @@ public class CommitCommandTest extends RepositoryTestCase {
assertEquals("commit: Squashed commit of the following:", db assertEquals("commit: Squashed commit of the following:", db
.getReflogReader(db.getBranch()).getLastEntry().getComment()); .getReflogReader(db.getBranch()).getLastEntry().getComment());
} }
}
@Test(expected = WrongRepositoryStateException.class) @Test(expected = WrongRepositoryStateException.class)
public void commitAmendOnInitialShouldFail() throws Exception { public void commitAmendOnInitialShouldFail() throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
git.commit().setAmend(true).setMessage("initial commit").call(); git.commit().setAmend(true).setMessage("initial commit").call();
} }
}
@Test @Test
public void commitAmendWithoutAuthorShouldSetOriginalAuthorAndAuthorTime() public void commitAmendWithoutAuthorShouldSetOriginalAuthorAndAuthorTime()
throws Exception { throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
writeTrashFile("file1", "file1"); writeTrashFile("file1", "file1");
git.add().addFilepattern("file1").call(); git.add().addFilepattern("file1").call();
@ -461,11 +465,11 @@ public class CommitCommandTest extends RepositoryTestCase {
assertEquals(authorEmail, amendedAuthor.getEmailAddress()); assertEquals(authorEmail, amendedAuthor.getEmailAddress());
assertEquals(authorDate.getTime(), amendedAuthor.getWhen().getTime()); assertEquals(authorDate.getTime(), amendedAuthor.getWhen().getTime());
} }
}
@Test @Test
public void commitAmendWithAuthorShouldUseIt() throws Exception { public void commitAmendWithAuthorShouldUseIt() throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
writeTrashFile("file1", "file1"); writeTrashFile("file1", "file1");
git.add().addFilepattern("file1").call(); git.add().addFilepattern("file1").call();
git.commit().setMessage("initial commit").call(); git.commit().setMessage("initial commit").call();
@ -478,6 +482,7 @@ public class CommitCommandTest extends RepositoryTestCase {
assertEquals("New Author", amendedAuthor.getName()); assertEquals("New Author", amendedAuthor.getName());
assertEquals("newauthor@example.org", amendedAuthor.getEmailAddress()); assertEquals("newauthor@example.org", amendedAuthor.getEmailAddress());
} }
}
@Test @Test
public void commitEmptyCommits() throws Exception { public void commitEmptyCommits() throws Exception {
@ -532,7 +537,7 @@ public class CommitCommandTest extends RepositoryTestCase {
+ "[unmerged2, mode:100644, stage:3]", + "[unmerged2, mode:100644, stage:3]",
indexState(0)); indexState(0));
Git git = new Git(db); try (Git git = new Git(db)) {
RevCommit commit = git.commit().setOnly("unmerged1") RevCommit commit = git.commit().setOnly("unmerged1")
.setMessage("Only one file").call(); .setMessage("Only one file").call();
@ -546,6 +551,7 @@ public class CommitCommandTest extends RepositoryTestCase {
assertEquals(FileMode.REGULAR_FILE, walk.getFileMode(0)); assertEquals(FileMode.REGULAR_FILE, walk.getFileMode(0));
} }
} }
}
@Test @Test
public void commitOnlyShouldHandleIgnored() throws Exception { public void commitOnlyShouldHandleIgnored() throws Exception {

Loading…
Cancel
Save