From 6aa619ec5cf7dc2d4149fa8087d985c801223639 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 5 Feb 2016 17:03:25 +0900 Subject: [PATCH] StatusCommandTest: Open Git in try-with-resource Change-Id: Id13ad9fa3157d0479da12fb8184437be3ca8c948 Signed-off-by: David Pursehouse --- .../eclipse/jgit/api/StatusCommandTest.java | 190 +++++++++--------- 1 file changed, 96 insertions(+), 94 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java index c70604ecb..f3ac65ca0 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java @@ -61,109 +61,111 @@ public class StatusCommandTest extends RepositoryTestCase { @Test public void testEmptyStatus() throws NoWorkTreeException, GitAPIException { - Git git = new Git(db); - - Status stat = git.status().call(); - assertEquals(0, stat.getAdded().size()); - assertEquals(0, stat.getChanged().size()); - assertEquals(0, stat.getMissing().size()); - assertEquals(0, stat.getModified().size()); - assertEquals(0, stat.getRemoved().size()); - assertEquals(0, stat.getUntracked().size()); + try (Git git = new Git(db)) { + Status stat = git.status().call(); + assertEquals(0, stat.getAdded().size()); + assertEquals(0, stat.getChanged().size()); + assertEquals(0, stat.getMissing().size()); + assertEquals(0, stat.getModified().size()); + assertEquals(0, stat.getRemoved().size()); + assertEquals(0, stat.getUntracked().size()); + } } @Test public void testDifferentStates() throws IOException, NoFilepatternException, GitAPIException { - Git git = new Git(db); - writeTrashFile("a", "content of a"); - writeTrashFile("b", "content of b"); - writeTrashFile("c", "content of c"); - git.add().addFilepattern("a").addFilepattern("b").call(); - Status stat = git.status().call(); - assertEquals(Sets.of("a", "b"), stat.getAdded()); - assertEquals(0, stat.getChanged().size()); - assertEquals(0, stat.getMissing().size()); - assertEquals(0, stat.getModified().size()); - assertEquals(0, stat.getRemoved().size()); - assertEquals(Sets.of("c"), stat.getUntracked()); - git.commit().setMessage("initial").call(); - - writeTrashFile("a", "modified content of a"); - writeTrashFile("b", "modified content of b"); - writeTrashFile("d", "content of d"); - git.add().addFilepattern("a").addFilepattern("d").call(); - writeTrashFile("a", "again modified content of a"); - stat = git.status().call(); - assertEquals(Sets.of("d"), stat.getAdded()); - assertEquals(Sets.of("a"), stat.getChanged()); - assertEquals(0, stat.getMissing().size()); - assertEquals(Sets.of("b", "a"), stat.getModified()); - assertEquals(0, stat.getRemoved().size()); - assertEquals(Sets.of("c"), stat.getUntracked()); - git.add().addFilepattern(".").call(); - git.commit().setMessage("second").call(); - - stat = git.status().call(); - assertEquals(0, stat.getAdded().size()); - assertEquals(0, stat.getChanged().size()); - assertEquals(0, stat.getMissing().size()); - assertEquals(0, stat.getModified().size()); - assertEquals(0, stat.getRemoved().size()); - assertEquals(0, stat.getUntracked().size()); - - deleteTrashFile("a"); - assertFalse(new File(git.getRepository().getWorkTree(), "a").exists()); - git.add().addFilepattern("a").setUpdate(true).call(); - writeTrashFile("a", "recreated content of a"); - stat = git.status().call(); - assertEquals(0, stat.getAdded().size()); - assertEquals(0, stat.getChanged().size()); - assertEquals(0, stat.getMissing().size()); - assertEquals(0, stat.getModified().size()); - assertEquals(Sets.of("a"), stat.getRemoved()); - assertEquals(Sets.of("a"), stat.getUntracked()); - git.commit().setMessage("t").call(); - - writeTrashFile("sub/a", "sub-file"); - stat = git.status().call(); - assertEquals(1, stat.getUntrackedFolders().size()); - assertTrue(stat.getUntrackedFolders().contains("sub")); + try (Git git = new Git(db)) { + writeTrashFile("a", "content of a"); + writeTrashFile("b", "content of b"); + writeTrashFile("c", "content of c"); + git.add().addFilepattern("a").addFilepattern("b").call(); + Status stat = git.status().call(); + assertEquals(Sets.of("a", "b"), stat.getAdded()); + assertEquals(0, stat.getChanged().size()); + assertEquals(0, stat.getMissing().size()); + assertEquals(0, stat.getModified().size()); + assertEquals(0, stat.getRemoved().size()); + assertEquals(Sets.of("c"), stat.getUntracked()); + git.commit().setMessage("initial").call(); + + writeTrashFile("a", "modified content of a"); + writeTrashFile("b", "modified content of b"); + writeTrashFile("d", "content of d"); + git.add().addFilepattern("a").addFilepattern("d").call(); + writeTrashFile("a", "again modified content of a"); + stat = git.status().call(); + assertEquals(Sets.of("d"), stat.getAdded()); + assertEquals(Sets.of("a"), stat.getChanged()); + assertEquals(0, stat.getMissing().size()); + assertEquals(Sets.of("b", "a"), stat.getModified()); + assertEquals(0, stat.getRemoved().size()); + assertEquals(Sets.of("c"), stat.getUntracked()); + git.add().addFilepattern(".").call(); + git.commit().setMessage("second").call(); + + stat = git.status().call(); + assertEquals(0, stat.getAdded().size()); + assertEquals(0, stat.getChanged().size()); + assertEquals(0, stat.getMissing().size()); + assertEquals(0, stat.getModified().size()); + assertEquals(0, stat.getRemoved().size()); + assertEquals(0, stat.getUntracked().size()); + + deleteTrashFile("a"); + assertFalse(new File(git.getRepository().getWorkTree(), "a").exists()); + git.add().addFilepattern("a").setUpdate(true).call(); + writeTrashFile("a", "recreated content of a"); + stat = git.status().call(); + assertEquals(0, stat.getAdded().size()); + assertEquals(0, stat.getChanged().size()); + assertEquals(0, stat.getMissing().size()); + assertEquals(0, stat.getModified().size()); + assertEquals(Sets.of("a"), stat.getRemoved()); + assertEquals(Sets.of("a"), stat.getUntracked()); + git.commit().setMessage("t").call(); + + writeTrashFile("sub/a", "sub-file"); + stat = git.status().call(); + assertEquals(1, stat.getUntrackedFolders().size()); + assertTrue(stat.getUntrackedFolders().contains("sub")); + } } @Test public void testDifferentStatesWithPaths() throws IOException, NoFilepatternException, GitAPIException { - Git git = new Git(db); - writeTrashFile("a", "content of a"); - writeTrashFile("D/b", "content of b"); - writeTrashFile("D/c", "content of c"); - writeTrashFile("D/D/d", "content of d"); - git.add().addFilepattern(".").call(); - - writeTrashFile("a", "new content of a"); - writeTrashFile("D/b", "new content of b"); - writeTrashFile("D/D/d", "new content of d"); - - - // filter on an not existing path - Status stat = git.status().addPath("x").call(); - assertEquals(0, stat.getModified().size()); - - // filter on an existing file - stat = git.status().addPath("a").call(); - assertEquals(Sets.of("a"), stat.getModified()); - - // filter on an existing folder - stat = git.status().addPath("D").call(); - assertEquals(Sets.of("D/b", "D/D/d"), stat.getModified()); - - // filter on an existing folder and file - stat = git.status().addPath("D/D").addPath("a").call(); - assertEquals(Sets.of("a", "D/D/d"), stat.getModified()); - - // do not filter at all - stat = git.status().call(); - assertEquals(Sets.of("a", "D/b", "D/D/d"), stat.getModified()); + try (Git git = new Git(db)) { + writeTrashFile("a", "content of a"); + writeTrashFile("D/b", "content of b"); + writeTrashFile("D/c", "content of c"); + writeTrashFile("D/D/d", "content of d"); + git.add().addFilepattern(".").call(); + + writeTrashFile("a", "new content of a"); + writeTrashFile("D/b", "new content of b"); + writeTrashFile("D/D/d", "new content of d"); + + + // filter on an not existing path + Status stat = git.status().addPath("x").call(); + assertEquals(0, stat.getModified().size()); + + // filter on an existing file + stat = git.status().addPath("a").call(); + assertEquals(Sets.of("a"), stat.getModified()); + + // filter on an existing folder + stat = git.status().addPath("D").call(); + assertEquals(Sets.of("D/b", "D/D/d"), stat.getModified()); + + // filter on an existing folder and file + stat = git.status().addPath("D/D").addPath("a").call(); + assertEquals(Sets.of("a", "D/D/d"), stat.getModified()); + + // do not filter at all + stat = git.status().call(); + assertEquals(Sets.of("a", "D/b", "D/D/d"), stat.getModified()); + } } }