diff options
author | David Pursehouse <david.pursehouse@gmail.com> | 2018-02-14 10:13:46 +0900 |
---|---|---|
committer | David Pursehouse <david.pursehouse@gmail.com> | 2018-02-14 14:09:40 +0900 |
commit | f527d4548f41436e6e77cd54134f25e1b89ba903 (patch) | |
tree | cc73b24eaa50b183297d08a631112a395bcf54b4 /org.eclipse.jgit.test/tst/org/eclipse/jgit/api/InitCommandTest.java | |
parent | 19355ce12330a85402fae699c2101275b4272874 (diff) | |
download | jgit-f527d4548f41436e6e77cd54134f25e1b89ba903.tar.gz jgit-f527d4548f41436e6e77cd54134f25e1b89ba903.zip |
InitCommand: Don't leave Repository open after Git is closed
The InitCommand returns a Git that is instantiated with the newly
created Repository, but the Repository is not closed with the Git
resulting in resource leaks.
Create the Git with `closeRepo` set to true, such that the Repository
is also closed when the Git is closed.
Adjust the tests to use try-with-resource on the Git instance.
Change-Id: Ib26e7428c7d8840956d1edb09e53b93e23e6fe5a
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
Diffstat (limited to 'org.eclipse.jgit.test/tst/org/eclipse/jgit/api/InitCommandTest.java')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/api/InitCommandTest.java | 77 |
1 files changed, 41 insertions, 36 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/InitCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/InitCommandTest.java index e850223762..9e3ee2c566 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/InitCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/InitCommandTest.java @@ -69,14 +69,14 @@ public class InitCommandTest extends RepositoryTestCase { } @Test - public void testInitRepository() throws IOException, JGitInternalException, - GitAPIException { + public void testInitRepository() + throws IOException, JGitInternalException, GitAPIException { File directory = createTempDirectory("testInitRepository"); InitCommand command = new InitCommand(); command.setDirectory(directory); - Repository repository = command.call().getRepository(); - addRepoToClose(repository); - assertNotNull(repository); + try (Git git = command.call()) { + assertNotNull(git.getRepository()); + } } @Test @@ -89,9 +89,9 @@ public class InitCommandTest extends RepositoryTestCase { assertTrue(directory.listFiles().length > 0); InitCommand command = new InitCommand(); command.setDirectory(directory); - Repository repository = command.call().getRepository(); - addRepoToClose(repository); - assertNotNull(repository); + try (Git git = command.call()) { + assertNotNull(git.getRepository()); + } } @Test @@ -101,10 +101,11 @@ public class InitCommandTest extends RepositoryTestCase { InitCommand command = new InitCommand(); command.setDirectory(directory); command.setBare(true); - Repository repository = command.call().getRepository(); - addRepoToClose(repository); - assertNotNull(repository); - assertTrue(repository.isBare()); + try (Git git = command.call()) { + Repository repository = git.getRepository(); + assertNotNull(repository); + assertTrue(repository.isBare()); + } } // non-bare repos where gitDir and directory is set. Same as @@ -117,11 +118,12 @@ public class InitCommandTest extends RepositoryTestCase { InitCommand command = new InitCommand(); command.setDirectory(wt); command.setGitDir(gitDir); - Repository repository = command.call().getRepository(); - addRepoToClose(repository); - assertNotNull(repository); - assertEqualsFile(wt, repository.getWorkTree()); - assertEqualsFile(gitDir, repository.getDirectory()); + try (Git git = command.call()) { + Repository repository = git.getRepository(); + assertNotNull(repository); + assertEqualsFile(wt, repository.getWorkTree()); + assertEqualsFile(gitDir, repository.getDirectory()); + } } // non-bare repos where only gitDir is set. Same as @@ -135,12 +137,13 @@ public class InitCommandTest extends RepositoryTestCase { File gitDir = createTempDirectory("testInitRepository/.git"); InitCommand command = new InitCommand(); command.setGitDir(gitDir); - Repository repository = command.call().getRepository(); - addRepoToClose(repository); - assertNotNull(repository); - assertEqualsFile(gitDir, repository.getDirectory()); - assertEqualsFile(new File(reader.getProperty("user.dir")), - repository.getWorkTree()); + try (Git git = command.call()) { + Repository repository = git.getRepository(); + assertNotNull(repository); + assertEqualsFile(gitDir, repository.getDirectory()); + assertEqualsFile(new File(reader.getProperty("user.dir")), + repository.getWorkTree()); + } } // Bare repos where gitDir and directory is set will only work if gitDir and @@ -169,13 +172,14 @@ public class InitCommandTest extends RepositoryTestCase { .getAbsolutePath()); InitCommand command = new InitCommand(); command.setBare(false); - Repository repository = command.call().getRepository(); - addRepoToClose(repository); - assertNotNull(repository); - assertEqualsFile(new File(reader.getProperty("user.dir"), ".git"), - repository.getDirectory()); - assertEqualsFile(new File(reader.getProperty("user.dir")), - repository.getWorkTree()); + try (Git git = command.call()) { + Repository repository = git.getRepository(); + assertNotNull(repository); + assertEqualsFile(new File(reader.getProperty("user.dir"), ".git"), + repository.getDirectory()); + assertEqualsFile(new File(reader.getProperty("user.dir")), + repository.getWorkTree()); + } } // If neither directory nor gitDir is set in a bare repo make sure @@ -189,12 +193,13 @@ public class InitCommandTest extends RepositoryTestCase { .getAbsolutePath()); InitCommand command = new InitCommand(); command.setBare(true); - Repository repository = command.call().getRepository(); - addRepoToClose(repository); - assertNotNull(repository); - assertEqualsFile(new File(reader.getProperty("user.dir")), - repository.getDirectory()); - assertNull(repository.getWorkTree()); + try (Git git = command.call()) { + Repository repository = git.getRepository(); + assertNotNull(repository); + assertEqualsFile(new File(reader.getProperty("user.dir")), + repository.getDirectory()); + assertNull(repository.getWorkTree()); + } } // In a non-bare repo when directory and gitDir is set then they shouldn't |