From 5e0eca69432c4a382ab6b0d08728f52b1e305a30 Mon Sep 17 00:00:00 2001 From: Thomas Wolf Date: Tue, 15 Oct 2019 00:58:27 +0200 Subject: [PATCH] Close SubmoduleWalks in tests Change-Id: If1b9d80830b6b136d4ab33f5c7043b140bd92f85 Signed-off-by: Thomas Wolf --- .../eclipse/jgit/api/CloneCommandTest.java | 28 +- .../eclipse/jgit/api/CommitCommandTest.java | 52 ++-- ...hAndPullCommandsRecurseSubmodulesTest.java | 21 +- .../jgit/submodule/SubmoduleAddTest.java | 89 +++---- .../jgit/submodule/SubmoduleDeinitTest.java | 53 ++-- .../jgit/submodule/SubmoduleInitTest.java | 106 ++++---- .../jgit/submodule/SubmoduleSyncTest.java | 62 ++--- .../jgit/submodule/SubmoduleUpdateTest.java | 18 +- .../jgit/submodule/SubmoduleWalkTest.java | 239 +++++++++--------- 9 files changed, 352 insertions(+), 316 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java index 2270a6a8b..b4abb53d8 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java @@ -646,25 +646,27 @@ public class CloneCommandTest extends RepositoryTestCase { assertEquals(sub1Head, pathStatus.getHeadId()); assertEquals(sub1Head, pathStatus.getIndexId()); - SubmoduleWalk walk = SubmoduleWalk.forIndex(git2.getRepository()); - assertTrue(walk.next()); - try (Repository clonedSub1 = walk.getRepository()) { - assertNotNull(clonedSub1); - assertEquals(new File(git2.getRepository().getWorkTree(), - walk.getPath()), clonedSub1.getWorkTree()); - assertEquals( - new File(new File(git2.getRepository().getDirectory(), - "modules"), walk.getPath()), - clonedSub1.getDirectory()); - status = new SubmoduleStatusCommand(clonedSub1); - statuses = status.call(); + try (SubmoduleWalk walk = SubmoduleWalk + .forIndex(git2.getRepository())) { + assertTrue(walk.next()); + try (Repository clonedSub1 = walk.getRepository()) { + assertNotNull(clonedSub1); + assertEquals(new File(git2.getRepository().getWorkTree(), + walk.getPath()), clonedSub1.getWorkTree()); + assertEquals( + new File(new File(git2.getRepository().getDirectory(), + "modules"), walk.getPath()), + clonedSub1.getDirectory()); + status = new SubmoduleStatusCommand(clonedSub1); + statuses = status.call(); + } + assertFalse(walk.next()); } pathStatus = statuses.get(path); assertNotNull(pathStatus); assertEquals(SubmoduleStatusType.INITIALIZED, pathStatus.getType()); assertEquals(sub2Head, pathStatus.getHeadId()); assertEquals(sub2Head, pathStatus.getIndexId()); - assertFalse(walk.next()); } @Test diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java index 3bde0eb33..b5661e844 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java @@ -148,9 +148,10 @@ public class CommitCommandTest extends RepositoryTestCase { writeTrashFile(path, "content"); git.add().addFilepattern(path).call(); RevCommit commit1 = git.commit().setMessage("commit").call(); - TreeWalk walk = TreeWalk.forPath(db, path, commit1.getTree()); - assertNotNull(walk); - assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0)); + try (TreeWalk walk = TreeWalk.forPath(db, path, commit1.getTree())) { + assertNotNull(walk); + assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0)); + } FS nonExecutableFs = new FS() { @@ -204,9 +205,10 @@ public class CommitCommandTest extends RepositoryTestCase { writeTrashFile(path, "content2"); RevCommit commit2 = git2.commit().setOnly(path).setMessage("commit2") .call(); - walk = TreeWalk.forPath(db, path, commit2.getTree()); - assertNotNull(walk); - assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0)); + try (TreeWalk walk = TreeWalk.forPath(db, path, commit2.getTree())) { + assertNotNull(walk); + assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0)); + } } @Test @@ -225,15 +227,16 @@ public class CommitCommandTest extends RepositoryTestCase { 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()); - assertEquals(uri, generator.getConfigUrl()); - try (Repository subModRepo = generator.getRepository()) { - assertNotNull(subModRepo); + try (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()); + try (Repository subModRepo = generator.getRepository()) { + assertNotNull(subModRepo); + } } assertEquals(commit, repo.resolve(Constants.HEAD)); @@ -275,15 +278,16 @@ public class CommitCommandTest extends RepositoryTestCase { assertNotNull(repo); addRepoToClose(repo); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertEquals(path, generator.getPath()); - assertEquals(commit2, generator.getObjectId()); - assertEquals(uri, generator.getModulesUrl()); - assertEquals(path, generator.getModulesPath()); - assertEquals(uri, generator.getConfigUrl()); - try (Repository subModRepo = generator.getRepository()) { - assertNotNull(subModRepo); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertEquals(path, generator.getPath()); + assertEquals(commit2, generator.getObjectId()); + assertEquals(uri, generator.getModulesUrl()); + assertEquals(path, generator.getModulesPath()); + assertEquals(uri, generator.getConfigUrl()); + try (Repository subModRepo = generator.getRepository()) { + assertNotNull(subModRepo); + } } assertEquals(commit2, repo.resolve(Constants.HEAD)); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/FetchAndPullCommandsRecurseSubmodulesTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/FetchAndPullCommandsRecurseSubmodulesTest.java index 73a705b25..08ded559f 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/FetchAndPullCommandsRecurseSubmodulesTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/FetchAndPullCommandsRecurseSubmodulesTest.java @@ -343,19 +343,22 @@ public class FetchAndPullCommandsRecurseSubmodulesTest extends RepositoryTestCas private void assertSubmoduleFetchHeads(ObjectId expectedHead1, ObjectId expectedHead2) throws Exception { + Object newHead1 = null; + ObjectId newHead2 = null; try (SubmoduleWalk walk = SubmoduleWalk .forIndex(git2.getRepository())) { assertTrue(walk.next()); - Repository r = walk.getRepository(); - ObjectId newHead1 = r.resolve(Constants.FETCH_HEAD); - ObjectId newHead2; - try (SubmoduleWalk walk2 = SubmoduleWalk.forIndex(r)) { - assertTrue(walk2.next()); - newHead2 = walk2.getRepository().resolve(Constants.FETCH_HEAD); + try (Repository r = walk.getRepository()) { + newHead1 = r.resolve(Constants.FETCH_HEAD); + try (SubmoduleWalk walk2 = SubmoduleWalk.forIndex(r)) { + assertTrue(walk2.next()); + try (Repository r2 = walk2.getRepository()) { + newHead2 = r2.resolve(Constants.FETCH_HEAD); + } + } } - - assertEquals(expectedHead1, newHead1); - assertEquals(expectedHead2, newHead2); } + assertEquals(expectedHead1, newHead1); + assertEquals(expectedHead2, newHead2); } } 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 1ff64a2e2..80e9e6b14 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 @@ -135,20 +135,20 @@ public class SubmoduleAddTest extends RepositoryTestCase { subCommit = repo.resolve(Constants.HEAD); } - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - generator.loadModulesConfig(); - assertTrue(generator.next()); - assertEquals(path, generator.getModuleName()); - assertEquals(path, generator.getPath()); - assertEquals(commit, generator.getObjectId()); - assertEquals(uri, generator.getModulesUrl()); - assertEquals(path, generator.getModulesPath()); - assertEquals(uri, generator.getConfigUrl()); - try (Repository subModRepo = generator.getRepository()) { - assertNotNull(subModRepo); - assertEquals(subCommit, commit); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + generator.loadModulesConfig(); + assertTrue(generator.next()); + assertEquals(path, generator.getModuleName()); + assertEquals(path, generator.getPath()); + assertEquals(commit, generator.getObjectId()); + assertEquals(uri, generator.getModulesUrl()); + assertEquals(path, generator.getModulesPath()); + assertEquals(uri, generator.getConfigUrl()); + try (Repository subModRepo = generator.getRepository()) { + assertNotNull(subModRepo); + assertEquals(subCommit, commit); + } } - Status status = Git.wrap(db).status().call(); assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES)); assertTrue(status.getAdded().contains(path)); @@ -175,20 +175,20 @@ public class SubmoduleAddTest extends RepositoryTestCase { subCommit = repo.resolve(Constants.HEAD); } - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - generator.loadModulesConfig(); - assertTrue(generator.next()); - assertEquals(name, generator.getModuleName()); - assertEquals(path, generator.getPath()); - assertEquals(commit, generator.getObjectId()); - assertEquals(uri, generator.getModulesUrl()); - assertEquals(path, generator.getModulesPath()); - assertEquals(uri, generator.getConfigUrl()); - try (Repository subModRepo = generator.getRepository()) { - assertNotNull(subModRepo); - assertEquals(subCommit, commit); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + generator.loadModulesConfig(); + assertTrue(generator.next()); + assertEquals(name, generator.getModuleName()); + assertEquals(path, generator.getPath()); + assertEquals(commit, generator.getObjectId()); + assertEquals(uri, generator.getModulesUrl()); + assertEquals(path, generator.getModulesPath()); + assertEquals(uri, generator.getConfigUrl()); + try (Repository subModRepo = generator.getRepository()) { + assertNotNull(subModRepo); + assertEquals(subCommit, commit); + } } - Status status = Git.wrap(db).status().call(); assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES)); assertTrue(status.getAdded().contains(path)); @@ -269,24 +269,25 @@ public class SubmoduleAddTest extends RepositoryTestCase { 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()); - try (Repository subModRepo = generator.getRepository()) { - assertNotNull(subModRepo); - assertEquals(fullUri, - subModRepo.getConfig().getString( - ConfigConstants.CONFIG_REMOTE_SECTION, - Constants.DEFAULT_REMOTE_NAME, - ConfigConstants.CONFIG_KEY_URL)); + try (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()); + try (Repository subModRepo = generator.getRepository()) { + assertNotNull(subModRepo); + assertEquals(fullUri, + subModRepo.getConfig().getString( + ConfigConstants.CONFIG_REMOTE_SECTION, + Constants.DEFAULT_REMOTE_NAME, + ConfigConstants.CONFIG_KEY_URL)); + } } assertEquals(commit, repo.resolve(Constants.HEAD)); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleDeinitTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleDeinitTest.java index 815ce9b35..bb0085106 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleDeinitTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleDeinitTest.java @@ -107,7 +107,6 @@ public class SubmoduleDeinitTest extends RepositoryTestCase { assertEquals(1, updated.size()); File submoduleDir = assertSubmoduleIsInitialized(); - SubmoduleWalk generator; write(new File(submoduleDir, "untracked"), "untracked"); @@ -115,8 +114,9 @@ public class SubmoduleDeinitTest extends RepositoryTestCase { assertEquals(path, result.getPath()); assertEquals(SubmoduleDeinitCommand.SubmoduleDeinitStatus.DIRTY, result.getStatus()); - generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + } assertTrue(submoduleDir.isDirectory()); assertNotEquals(0, submoduleDir.list().length); } @@ -132,33 +132,36 @@ public class SubmoduleDeinitTest extends RepositoryTestCase { assertEquals(1, updated.size()); File submoduleDir = assertSubmoduleIsInitialized(); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - generator.next(); - - //want to create a commit inside the repo... - try (Repository submoduleLocalRepo = generator.getRepository()) { - JGitTestUtil.writeTrashFile(submoduleLocalRepo, "file.txt", - "new data"); - Git.wrap(submoduleLocalRepo).commit().setAll(true) - .setMessage("local commit").call(); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + generator.next(); + + // want to create a commit inside the repo... + try (Repository submoduleLocalRepo = generator.getRepository()) { + JGitTestUtil.writeTrashFile(submoduleLocalRepo, "file.txt", + "new data"); + Git.wrap(submoduleLocalRepo).commit().setAll(true) + .setMessage("local commit").call(); + } } SubmoduleDeinitResult result = runDeinit(new SubmoduleDeinitCommand(db).addPath("sub")); assertEquals(path, result.getPath()); assertEquals(SubmoduleDeinitCommand.SubmoduleDeinitStatus.DIRTY, result.getStatus()); - generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + } assertTrue(submoduleDir.isDirectory()); assertNotEquals(0, submoduleDir.list().length); } private File assertSubmoduleIsInitialized() throws IOException { - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - File submoduleDir = new File(db.getWorkTree(), generator.getPath()); - assertTrue(submoduleDir.isDirectory()); - assertNotEquals(0, submoduleDir.list().length); - return submoduleDir; + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + File submoduleDir = new File(db.getWorkTree(), generator.getPath()); + assertTrue(submoduleDir.isDirectory()); + assertNotEquals(0, submoduleDir.list().length); + return submoduleDir; + } } @Test @@ -180,8 +183,9 @@ public class SubmoduleDeinitTest extends RepositoryTestCase { assertEquals(path, result.getPath()); assertEquals(SubmoduleDeinitCommand.SubmoduleDeinitStatus.FORCED, result.getStatus()); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + } assertTrue(submoduleDir.isDirectory()); assertEquals(0, submoduleDir.list().length); } @@ -202,8 +206,9 @@ public class SubmoduleDeinitTest extends RepositoryTestCase { assertEquals(path, result.getPath()); assertEquals(SubmoduleDeinitCommand.SubmoduleDeinitStatus.SUCCESS, result.getStatus()); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + } assertTrue(submoduleDir.isDirectory()); assertEquals(0, submoduleDir.list().length); } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleInitTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleInitTest.java index c7a009c5b..9fe2fc6fd 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleInitTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleInitTest.java @@ -86,11 +86,11 @@ public class SubmoduleInitTest extends RepositoryTestCase { ConfigInvalidException, GitAPIException { final String path = addSubmoduleToIndex(); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertNull(generator.getConfigUrl()); - assertNull(generator.getConfigUpdate()); - + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertNull(generator.getConfigUrl()); + assertNull(generator.getConfigUpdate()); + } FileBasedConfig modulesConfig = new FileBasedConfig(new File( db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS()); modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, @@ -109,10 +109,11 @@ public class SubmoduleInitTest extends RepositoryTestCase { assertEquals(1, modules.size()); assertEquals(path, modules.iterator().next()); - generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertEquals(url, generator.getConfigUrl()); - assertEquals(update, generator.getConfigUpdate()); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertEquals(url, generator.getConfigUrl()); + assertEquals(update, generator.getConfigUpdate()); + } } @Test @@ -126,11 +127,11 @@ public class SubmoduleInitTest extends RepositoryTestCase { base); config.save(); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertNull(generator.getConfigUrl()); - assertNull(generator.getConfigUpdate()); - + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertNull(generator.getConfigUrl()); + assertNull(generator.getConfigUpdate()); + } FileBasedConfig modulesConfig = new FileBasedConfig(new File( db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS()); modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, @@ -149,10 +150,12 @@ public class SubmoduleInitTest extends RepositoryTestCase { assertEquals(1, modules.size()); assertEquals(path, modules.iterator().next()); - generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertEquals("git://server/repo.git/sub.git", generator.getConfigUrl()); - assertEquals(update, generator.getConfigUpdate()); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertEquals("git://server/repo.git/sub.git", + generator.getConfigUrl()); + assertEquals(update, generator.getConfigUpdate()); + } } @Test @@ -167,11 +170,11 @@ public class SubmoduleInitTest extends RepositoryTestCase { base); config.save(); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertNull(generator.getConfigUrl()); - assertNull(generator.getConfigUpdate()); - + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertNull(generator.getConfigUrl()); + assertNull(generator.getConfigUpdate()); + } FileBasedConfig modulesConfig = new FileBasedConfig(new File( db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS()); modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, @@ -190,10 +193,11 @@ public class SubmoduleInitTest extends RepositoryTestCase { assertEquals(1, modules.size()); assertEquals(path, modules.iterator().next()); - generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertEquals("git://server/sub.git", generator.getConfigUrl()); - assertEquals(update, generator.getConfigUpdate()); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertEquals("git://server/sub.git", generator.getConfigUrl()); + assertEquals(update, generator.getConfigUpdate()); + } } @Test @@ -208,11 +212,11 @@ public class SubmoduleInitTest extends RepositoryTestCase { base); config.save(); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertNull(generator.getConfigUrl()); - assertNull(generator.getConfigUpdate()); - + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertNull(generator.getConfigUrl()); + assertNull(generator.getConfigUpdate()); + } FileBasedConfig modulesConfig = new FileBasedConfig(new File( db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS()); modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, @@ -231,10 +235,11 @@ public class SubmoduleInitTest extends RepositoryTestCase { assertEquals(1, modules.size()); assertEquals(path, modules.iterator().next()); - generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertEquals("git://server2/sub.git", generator.getConfigUrl()); - assertEquals(update, generator.getConfigUpdate()); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertEquals("git://server2/sub.git", generator.getConfigUrl()); + assertEquals(update, generator.getConfigUpdate()); + } } @Test @@ -250,11 +255,11 @@ public class SubmoduleInitTest extends RepositoryTestCase { Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL); config.save(); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertNull(generator.getConfigUrl()); - assertNull(generator.getConfigUpdate()); - + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertNull(generator.getConfigUrl()); + assertNull(generator.getConfigUpdate()); + } FileBasedConfig modulesConfig = new FileBasedConfig(new File( db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS()); modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, @@ -273,10 +278,11 @@ public class SubmoduleInitTest extends RepositoryTestCase { assertEquals(1, modules.size()); assertEquals(path, modules.iterator().next()); - generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertEquals(base + "/sub.git", generator.getConfigUrl()); - assertEquals(update, generator.getConfigUpdate()); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertEquals(base + "/sub.git", generator.getConfigUrl()); + assertEquals(update, generator.getConfigUpdate()); + } } @Test @@ -291,11 +297,11 @@ public class SubmoduleInitTest extends RepositoryTestCase { base); config.save(); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertNull(generator.getConfigUrl()); - assertNull(generator.getConfigUpdate()); - + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertNull(generator.getConfigUrl()); + assertNull(generator.getConfigUpdate()); + } FileBasedConfig modulesConfig = new FileBasedConfig(new File( db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS()); modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleSyncTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleSyncTest.java index 6f3b52f7b..1053e31bc 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleSyncTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleSyncTest.java @@ -119,11 +119,11 @@ public class SubmoduleSyncTest extends RepositoryTestCase { addRepoToClose(subRepo); assertNotNull(subRepo); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertNull(generator.getConfigUrl()); - assertEquals(url, generator.getModulesUrl()); - + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertNull(generator.getConfigUrl()); + assertEquals(url, generator.getModulesUrl()); + } SubmoduleSyncCommand command = new SubmoduleSyncCommand(db); Map synced = command.call(); assertNotNull(synced); @@ -132,16 +132,17 @@ public class SubmoduleSyncTest extends RepositoryTestCase { assertEquals(path, module.getKey()); assertEquals(url, module.getValue()); - generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertEquals(url, generator.getConfigUrl()); - try (Repository subModRepository = generator.getRepository()) { - StoredConfig submoduleConfig = subModRepository.getConfig(); - assertEquals(url, - submoduleConfig.getString( - ConfigConstants.CONFIG_REMOTE_SECTION, - Constants.DEFAULT_REMOTE_NAME, - ConfigConstants.CONFIG_KEY_URL)); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertEquals(url, generator.getConfigUrl()); + try (Repository subModRepository = generator.getRepository()) { + StoredConfig submoduleConfig = subModRepository.getConfig(); + assertEquals(url, + submoduleConfig.getString( + ConfigConstants.CONFIG_REMOTE_SECTION, + Constants.DEFAULT_REMOTE_NAME, + ConfigConstants.CONFIG_KEY_URL)); + } } } @@ -190,11 +191,11 @@ public class SubmoduleSyncTest extends RepositoryTestCase { assertNotNull(subRepo); addRepoToClose(subRepo); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertNull(generator.getConfigUrl()); - assertEquals(current, generator.getModulesUrl()); - + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertNull(generator.getConfigUrl()); + assertEquals(current, generator.getModulesUrl()); + } modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants.CONFIG_KEY_URL, "../sub.git"); modulesConfig.save(); @@ -207,16 +208,17 @@ public class SubmoduleSyncTest extends RepositoryTestCase { assertEquals(path, module.getKey()); assertEquals("git://server/sub.git", module.getValue()); - generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - assertEquals("git://server/sub.git", generator.getConfigUrl()); - try (Repository subModRepository1 = generator.getRepository()) { - StoredConfig submoduleConfig = subModRepository1.getConfig(); - assertEquals("git://server/sub.git", - submoduleConfig.getString( - ConfigConstants.CONFIG_REMOTE_SECTION, - Constants.DEFAULT_REMOTE_NAME, - ConfigConstants.CONFIG_KEY_URL)); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + assertEquals("git://server/sub.git", generator.getConfigUrl()); + try (Repository subModRepository1 = generator.getRepository()) { + StoredConfig submoduleConfig = subModRepository1.getConfig(); + assertEquals("git://server/sub.git", + submoduleConfig.getString( + ConfigConstants.CONFIG_REMOTE_SECTION, + Constants.DEFAULT_REMOTE_NAME, + ConfigConstants.CONFIG_KEY_URL)); + } } } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleUpdateTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleUpdateTest.java index bbce413ef..6f358b47f 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleUpdateTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleUpdateTest.java @@ -119,11 +119,12 @@ public class SubmoduleUpdateTest extends RepositoryTestCase { assertEquals(1, updated.size()); assertEquals(path, updated.iterator().next()); - SubmoduleWalk generator = SubmoduleWalk.forIndex(db); - assertTrue(generator.next()); - try (Repository subRepo = generator.getRepository()) { - assertNotNull(subRepo); - assertEquals(commit, subRepo.resolve(Constants.HEAD)); + try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) { + assertTrue(generator.next()); + try (Repository subRepo = generator.getRepository()) { + assertNotNull(subRepo); + assertEquals(commit, subRepo.resolve(Constants.HEAD)); + } } } @@ -181,10 +182,11 @@ public class SubmoduleUpdateTest extends RepositoryTestCase { }); editor.commit(); - Repository subRepo = Git.init().setBare(false) + try (Repository subRepo = Git.init().setBare(false) .setDirectory(new File(db.getWorkTree(), path)).call() - .getRepository(); - assertNotNull(subRepo); + .getRepository()) { + assertNotNull(subRepo); + } SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db); Collection updated = command.call(); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleWalkTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleWalkTest.java index ea1ace364..0b6047f88 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleWalkTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleWalkTest.java @@ -98,10 +98,11 @@ public class SubmoduleWalkTest extends RepositoryTestCase { @Test public void repositoryWithNoSubmodules() throws IOException { - SubmoduleWalk gen = SubmoduleWalk.forIndex(db); - assertFalse(gen.next()); - assertNull(gen.getPath()); - assertEquals(ObjectId.zeroId(), gen.getObjectId()); + try (SubmoduleWalk gen = SubmoduleWalk.forIndex(db)) { + assertFalse(gen.next()); + assertNull(gen.getPath()); + assertEquals(ObjectId.zeroId(), gen.getObjectId()); + } } @Test @@ -129,20 +130,21 @@ public class SubmoduleWalkTest extends RepositoryTestCase { }); editor.commit(); - SubmoduleWalk gen = SubmoduleWalk.forIndex(db); - assertTrue(gen.next()); - assertEquals(path, gen.getPath()); - assertEquals(id, gen.getObjectId()); - assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); - assertNull(gen.getConfigUpdate()); - assertNull(gen.getConfigUrl()); - assertNull(gen.getModulesPath()); - assertNull(gen.getModulesUpdate()); - assertNull(gen.getModulesUrl()); - assertNull(gen.getRepository()); + try (SubmoduleWalk gen = SubmoduleWalk.forIndex(db)) { + assertTrue(gen.next()); + assertEquals(path, gen.getPath()); + assertEquals(id, gen.getObjectId()); + assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); + assertNull(gen.getConfigUpdate()); + assertNull(gen.getConfigUrl()); + assertNull(gen.getModulesPath()); + assertNull(gen.getModulesUpdate()); + assertNull(gen.getModulesUrl()); + assertNull(gen.getRepository()); + assertFalse(gen.next()); + } Status status = Git.wrap(db).status().call(); - assertTrue(!status.isClean()); - assertFalse(gen.next()); + assertFalse(status.isClean()); } @Test @@ -178,24 +180,25 @@ public class SubmoduleWalkTest extends RepositoryTestCase { }); editor.commit(); - SubmoduleWalk gen = SubmoduleWalk.forIndex(db); - assertTrue(gen.next()); - assertEquals(path, gen.getPath()); - assertEquals(id, gen.getObjectId()); - assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); - assertNull(gen.getConfigUpdate()); - assertNull(gen.getConfigUrl()); - assertNull(gen.getModulesPath()); - assertNull(gen.getModulesUpdate()); - assertNull(gen.getModulesUrl()); - try (Repository subRepo = gen.getRepository()) { - assertNotNull(subRepo); - assertEquals(modulesGitDir.getAbsolutePath(), - subRepo.getDirectory().getAbsolutePath()); - assertEquals(new File(db.getWorkTree(), path).getAbsolutePath(), - subRepo.getWorkTree().getAbsolutePath()); + try (SubmoduleWalk gen = SubmoduleWalk.forIndex(db)) { + assertTrue(gen.next()); + assertEquals(path, gen.getPath()); + assertEquals(id, gen.getObjectId()); + assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); + assertNull(gen.getConfigUpdate()); + assertNull(gen.getConfigUrl()); + assertNull(gen.getModulesPath()); + assertNull(gen.getModulesUpdate()); + assertNull(gen.getModulesUrl()); + try (Repository subRepo = gen.getRepository()) { + assertNotNull(subRepo); + assertEquals(modulesGitDir.getAbsolutePath(), + subRepo.getDirectory().getAbsolutePath()); + assertEquals(new File(db.getWorkTree(), path).getAbsolutePath(), + subRepo.getWorkTree().getAbsolutePath()); + } + assertFalse(gen.next()); } - assertFalse(gen.next()); } @Test @@ -232,23 +235,24 @@ public class SubmoduleWalkTest extends RepositoryTestCase { }); editor.commit(); - SubmoduleWalk gen = SubmoduleWalk.forIndex(db); - assertTrue(gen.next()); - assertEquals(path, gen.getPath()); - assertEquals(id, gen.getObjectId()); - assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); - assertNull(gen.getConfigUpdate()); - assertNull(gen.getConfigUrl()); - assertNull(gen.getModulesPath()); - assertNull(gen.getModulesUpdate()); - assertNull(gen.getModulesUrl()); - try (Repository subRepo = gen.getRepository()) { - assertNotNull(subRepo); - assertEqualsFile(modulesGitDir, subRepo.getDirectory()); - assertEqualsFile(new File(db.getWorkTree(), path), - subRepo.getWorkTree()); - subRepo.close(); - assertFalse(gen.next()); + try (SubmoduleWalk gen = SubmoduleWalk.forIndex(db)) { + assertTrue(gen.next()); + assertEquals(path, gen.getPath()); + assertEquals(id, gen.getObjectId()); + assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); + assertNull(gen.getConfigUpdate()); + assertNull(gen.getConfigUrl()); + assertNull(gen.getModulesPath()); + assertNull(gen.getModulesUpdate()); + assertNull(gen.getModulesUrl()); + try (Repository subRepo = gen.getRepository()) { + assertNotNull(subRepo); + assertEqualsFile(modulesGitDir, subRepo.getDirectory()); + assertEqualsFile(new File(db.getWorkTree(), path), + subRepo.getWorkTree()); + subRepo.close(); + assertFalse(gen.next()); + } } } @@ -270,18 +274,19 @@ public class SubmoduleWalkTest extends RepositoryTestCase { }); editor.commit(); - SubmoduleWalk gen = SubmoduleWalk.forIndex(db); - assertTrue(gen.next()); - assertEquals(path, gen.getPath()); - assertEquals(id, gen.getObjectId()); - assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); - assertNull(gen.getConfigUpdate()); - assertNull(gen.getConfigUrl()); - assertNull(gen.getModulesPath()); - assertNull(gen.getModulesUpdate()); - assertNull(gen.getModulesUrl()); - assertNull(gen.getRepository()); - assertFalse(gen.next()); + try (SubmoduleWalk gen = SubmoduleWalk.forIndex(db)) { + assertTrue(gen.next()); + assertEquals(path, gen.getPath()); + assertEquals(id, gen.getObjectId()); + assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); + assertNull(gen.getConfigUpdate()); + assertNull(gen.getConfigUrl()); + assertNull(gen.getModulesPath()); + assertNull(gen.getModulesUpdate()); + assertNull(gen.getModulesUrl()); + assertNull(gen.getRepository()); + assertFalse(gen.next()); + } } @Test @@ -312,12 +317,13 @@ public class SubmoduleWalkTest extends RepositoryTestCase { }); editor.commit(); - SubmoduleWalk gen = SubmoduleWalk.forIndex(db); - gen.setFilter(PathFilter.create(path1)); - assertTrue(gen.next()); - assertEquals(path1, gen.getPath()); - assertEquals(id1, gen.getObjectId()); - assertFalse(gen.next()); + try (SubmoduleWalk gen = SubmoduleWalk.forIndex(db)) { + gen.setFilter(PathFilter.create(path1)); + assertTrue(gen.next()); + assertEquals(path1, gen.getPath()); + assertEquals(id1, gen.getObjectId()); + assertFalse(gen.next()); + } } @Test @@ -358,18 +364,19 @@ public class SubmoduleWalkTest extends RepositoryTestCase { }); editor.commit(); - SubmoduleWalk gen = SubmoduleWalk.forIndex(db); - assertTrue(gen.next()); - assertEquals(path, gen.getPath()); - assertEquals(subId, gen.getObjectId()); - assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); - assertNull(gen.getConfigUpdate()); - assertNull(gen.getConfigUrl()); - assertEquals("sub", gen.getModulesPath()); - assertNull(gen.getModulesUpdate()); - assertEquals("git://example.com/sub", gen.getModulesUrl()); - assertNull(gen.getRepository()); - assertFalse(gen.next()); + try (SubmoduleWalk gen = SubmoduleWalk.forIndex(db)) { + assertTrue(gen.next()); + assertEquals(path, gen.getPath()); + assertEquals(subId, gen.getObjectId()); + assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); + assertNull(gen.getConfigUpdate()); + assertNull(gen.getConfigUrl()); + assertEquals("sub", gen.getModulesPath()); + assertNull(gen.getModulesUpdate()); + assertEquals("git://example.com/sub", gen.getModulesUrl()); + assertNull(gen.getRepository()); + assertFalse(gen.next()); + } } @Test @@ -397,17 +404,19 @@ public class SubmoduleWalkTest extends RepositoryTestCase { }) .create()); - SubmoduleWalk gen = SubmoduleWalk.forPath(db, commit.getTree(), "sub"); - assertEquals(path, gen.getPath()); - assertEquals(subId, gen.getObjectId()); - assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); - assertNull(gen.getConfigUpdate()); - assertNull(gen.getConfigUrl()); - assertEquals("sub", gen.getModulesPath()); - assertNull(gen.getModulesUpdate()); - assertEquals("git://example.com/sub", gen.getModulesUrl()); - assertNull(gen.getRepository()); - assertFalse(gen.next()); + try (SubmoduleWalk gen = SubmoduleWalk.forPath(db, commit.getTree(), + "sub")) { + assertEquals(path, gen.getPath()); + assertEquals(subId, gen.getObjectId()); + assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); + assertNull(gen.getConfigUpdate()); + assertNull(gen.getConfigUrl()); + assertEquals("sub", gen.getModulesPath()); + assertNull(gen.getModulesUpdate()); + assertEquals("git://example.com/sub", gen.getModulesUrl()); + assertNull(gen.getRepository()); + assertFalse(gen.next()); + } } @Test @@ -437,17 +446,18 @@ public class SubmoduleWalkTest extends RepositoryTestCase { final CanonicalTreeParser p = new CanonicalTreeParser(); p.reset(testDb.getRevWalk().getObjectReader(), commit.getTree()); - SubmoduleWalk gen = SubmoduleWalk.forPath(db, p, "sub"); - assertEquals(path, gen.getPath()); - assertEquals(subId, gen.getObjectId()); - assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); - assertNull(gen.getConfigUpdate()); - assertNull(gen.getConfigUrl()); - assertEquals("sub", gen.getModulesPath()); - assertNull(gen.getModulesUpdate()); - assertEquals("git://example.com/sub", gen.getModulesUrl()); - assertNull(gen.getRepository()); - assertFalse(gen.next()); + try (SubmoduleWalk gen = SubmoduleWalk.forPath(db, p, "sub")) { + assertEquals(path, gen.getPath()); + assertEquals(subId, gen.getObjectId()); + assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); + assertNull(gen.getConfigUpdate()); + assertNull(gen.getConfigUrl()); + assertEquals("sub", gen.getModulesPath()); + assertNull(gen.getModulesUpdate()); + assertEquals("git://example.com/sub", gen.getModulesUrl()); + assertNull(gen.getRepository()); + assertFalse(gen.next()); + } } @Test @@ -477,16 +487,17 @@ public class SubmoduleWalkTest extends RepositoryTestCase { final CanonicalTreeParser p = new CanonicalTreeParser(); p.reset(testDb.getRevWalk().getObjectReader(), commit.getTree()); - SubmoduleWalk gen = SubmoduleWalk.forPath(db, p, "sub"); - assertEquals(path, gen.getPath()); - assertEquals(subId, gen.getObjectId()); - assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); - assertNull(gen.getConfigUpdate()); - assertNull(gen.getConfigUrl()); - assertEquals("sub", gen.getModulesPath()); - assertNull(gen.getModulesUpdate()); - assertEquals("git://example.com/sub", gen.getModulesUrl()); - assertNull(gen.getRepository()); - assertFalse(gen.next()); + try (SubmoduleWalk gen = SubmoduleWalk.forPath(db, p, "sub")) { + assertEquals(path, gen.getPath()); + assertEquals(subId, gen.getObjectId()); + assertEquals(new File(db.getWorkTree(), path), gen.getDirectory()); + assertNull(gen.getConfigUpdate()); + assertNull(gen.getConfigUrl()); + assertEquals("sub", gen.getModulesPath()); + assertNull(gen.getModulesUpdate()); + assertEquals("git://example.com/sub", gen.getModulesUrl()); + assertNull(gen.getRepository()); + assertFalse(gen.next()); + } } }