From 96ff3110498329bd9a0e1850da5d6f9bf6507f51 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 5 Feb 2016 14:11:54 +0900 Subject: [PATCH] SubmoduleAddTest: Open Git in try-with-resource Change-Id: I1276ce206e891729adfa681e00952ca0ba6ffc53 Signed-off-by: David Pursehouse --- .../jgit/submodule/SubmoduleAddTest.java | 178 +++++++++--------- 1 file changed, 91 insertions(+), 87 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java index b13c4cd80..a131e5e63 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java @@ -119,36 +119,37 @@ public void commandWithEmptyUri() throws GitAPIException { @Test public void addSubmodule() throws Exception { - Git git = new Git(db); - writeTrashFile("file.txt", "content"); - git.add().addFilepattern("file.txt").call(); - RevCommit commit = git.commit().setMessage("create file").call(); + try (Git git = new Git(db)) { + writeTrashFile("file.txt", "content"); + git.add().addFilepattern("file.txt").call(); + RevCommit commit = git.commit().setMessage("create file").call(); - SubmoduleAddCommand command = new SubmoduleAddCommand(db); - String path = "sub"; - command.setPath(path); - String uri = db.getDirectory().toURI().toString(); - command.setURI(uri); - Repository repo = command.call(); - assertNotNull(repo); - ObjectId subCommit = repo.resolve(Constants.HEAD); - repo.close(); + SubmoduleAddCommand command = new SubmoduleAddCommand(db); + String path = "sub"; + command.setPath(path); + String uri = db.getDirectory().toURI().toString(); + command.setURI(uri); + Repository repo = command.call(); + assertNotNull(repo); + ObjectId subCommit = repo.resolve(Constants.HEAD); + repo.close(); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertEquals(path, generator.getPath()); - assertEquals(commit, generator.getObjectId()); - assertEquals(uri, generator.getModulesUrl()); - assertEquals(path, generator.getModulesPath()); - assertEquals(uri, generator.getConfigUrl()); - Repository subModRepo = generator.getRepository(); - assertNotNull(subModRepo); - assertEquals(subCommit, commit); - subModRepo.close(); + SubmoduleWalk generator = SubmoduleWalk.forIndex(db); + assertTrue(generator.next()); + assertEquals(path, generator.getPath()); + assertEquals(commit, generator.getObjectId()); + assertEquals(uri, generator.getModulesUrl()); + assertEquals(path, generator.getModulesPath()); + assertEquals(uri, generator.getConfigUrl()); + Repository subModRepo = generator.getRepository(); + assertNotNull(subModRepo); + assertEquals(subCommit, commit); + subModRepo.close(); - Status status = Git.wrap(db).status().call(); - assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES)); - assertTrue(status.getAdded().contains(path)); + Status status = Git.wrap(db).status().call(); + assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES)); + assertTrue(status.getAdded().contains(path)); + } } @Test @@ -182,45 +183,47 @@ public void apply(DirCacheEntry ent) { @Test public void addSubmoduleWithRelativeUri() throws Exception { - Git git = new Git(db); - writeTrashFile("file.txt", "content"); - git.add().addFilepattern("file.txt").call(); - RevCommit commit = git.commit().setMessage("create file").call(); + try (Git git = new Git(db)) { + writeTrashFile("file.txt", "content"); + git.add().addFilepattern("file.txt").call(); + RevCommit commit = git.commit().setMessage("create file").call(); - SubmoduleAddCommand command = new SubmoduleAddCommand(db); - String path = "sub"; - String uri = "./.git"; - command.setPath(path); - command.setURI(uri); - Repository repo = command.call(); - assertNotNull(repo); - addRepoToClose(repo); + SubmoduleAddCommand command = new SubmoduleAddCommand(db); + String path = "sub"; + String uri = "./.git"; + command.setPath(path); + command.setURI(uri); + Repository repo = command.call(); + assertNotNull(repo); + addRepoToClose(repo); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertEquals(path, generator.getPath()); - assertEquals(commit, generator.getObjectId()); - assertEquals(uri, generator.getModulesUrl()); - assertEquals(path, generator.getModulesPath()); - String fullUri = db.getDirectory().getAbsolutePath(); - if (File.separatorChar == '\\') - fullUri = fullUri.replace('\\', '/'); - assertEquals(fullUri, generator.getConfigUrl()); - Repository subModRepo = generator.getRepository(); - assertNotNull(subModRepo); - assertEquals( - fullUri, - subModRepo - .getConfig() - .getString(ConfigConstants.CONFIG_REMOTE_SECTION, - Constants.DEFAULT_REMOTE_NAME, - ConfigConstants.CONFIG_KEY_URL)); - subModRepo.close(); - assertEquals(commit, repo.resolve(Constants.HEAD)); + SubmoduleWalk generator = SubmoduleWalk.forIndex(db); + assertTrue(generator.next()); + assertEquals(path, generator.getPath()); + assertEquals(commit, generator.getObjectId()); + assertEquals(uri, generator.getModulesUrl()); + assertEquals(path, generator.getModulesPath()); + String fullUri = db.getDirectory().getAbsolutePath(); + if (File.separatorChar == '\\') { + fullUri = fullUri.replace('\\', '/'); + } + assertEquals(fullUri, generator.getConfigUrl()); + Repository subModRepo = generator.getRepository(); + assertNotNull(subModRepo); + assertEquals( + fullUri, + subModRepo + .getConfig() + .getString(ConfigConstants.CONFIG_REMOTE_SECTION, + Constants.DEFAULT_REMOTE_NAME, + ConfigConstants.CONFIG_KEY_URL)); + subModRepo.close(); + assertEquals(commit, repo.resolve(Constants.HEAD)); - Status status = Git.wrap(db).status().call(); - assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES)); - assertTrue(status.getAdded().contains(path)); + Status status = Git.wrap(db).status().call(); + assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES)); + assertTrue(status.getAdded().contains(path)); + } } @Test @@ -237,31 +240,32 @@ public void addSubmoduleWithExistingSubmoduleDefined() throws Exception { path1, ConfigConstants.CONFIG_KEY_URL, url1); modulesConfig.save(); - Git git = new Git(db); - writeTrashFile("file.txt", "content"); - git.add().addFilepattern("file.txt").call(); - assertNotNull(git.commit().setMessage("create file").call()); + try (Git git = new Git(db)) { + writeTrashFile("file.txt", "content"); + git.add().addFilepattern("file.txt").call(); + assertNotNull(git.commit().setMessage("create file").call()); - SubmoduleAddCommand command = new SubmoduleAddCommand(db); - command.setPath(path2); - String url2 = db.getDirectory().toURI().toString(); - command.setURI(url2); - Repository r = command.call(); - assertNotNull(r); - addRepoToClose(r); + SubmoduleAddCommand command = new SubmoduleAddCommand(db); + command.setPath(path2); + String url2 = db.getDirectory().toURI().toString(); + command.setURI(url2); + Repository r = command.call(); + assertNotNull(r); + addRepoToClose(r); - modulesConfig.load(); - assertEquals(path1, modulesConfig.getString( - ConfigConstants.CONFIG_SUBMODULE_SECTION, path1, - ConfigConstants.CONFIG_KEY_PATH)); - assertEquals(url1, modulesConfig.getString( - ConfigConstants.CONFIG_SUBMODULE_SECTION, path1, - ConfigConstants.CONFIG_KEY_URL)); - assertEquals(path2, modulesConfig.getString( - ConfigConstants.CONFIG_SUBMODULE_SECTION, path2, - ConfigConstants.CONFIG_KEY_PATH)); - assertEquals(url2, modulesConfig.getString( - ConfigConstants.CONFIG_SUBMODULE_SECTION, path2, - ConfigConstants.CONFIG_KEY_URL)); + modulesConfig.load(); + assertEquals(path1, modulesConfig.getString( + ConfigConstants.CONFIG_SUBMODULE_SECTION, path1, + ConfigConstants.CONFIG_KEY_PATH)); + assertEquals(url1, modulesConfig.getString( + ConfigConstants.CONFIG_SUBMODULE_SECTION, path1, + ConfigConstants.CONFIG_KEY_URL)); + assertEquals(path2, modulesConfig.getString( + ConfigConstants.CONFIG_SUBMODULE_SECTION, path2, + ConfigConstants.CONFIG_KEY_PATH)); + assertEquals(url2, modulesConfig.getString( + ConfigConstants.CONFIG_SUBMODULE_SECTION, path2, + ConfigConstants.CONFIG_KEY_URL)); + } } } \ No newline at end of file