]> source.dussan.org Git - jgit.git/commitdiff
Close SubmoduleWalks in tests 78/151078/2
authorThomas Wolf <thomas.wolf@paranor.ch>
Mon, 14 Oct 2019 22:58:27 +0000 (00:58 +0200)
committerThomas Wolf <thomas.wolf@paranor.ch>
Tue, 15 Oct 2019 06:54:25 +0000 (08:54 +0200)
Change-Id: If1b9d80830b6b136d4ab33f5c7043b140bd92f85
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/FetchAndPullCommandsRecurseSubmodulesTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleDeinitTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleInitTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleSyncTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleUpdateTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleWalkTest.java

index 2270a6a8be1545b1407bfa8dcd13d07ca802e547..b4abb53d84379330a13913321f4f969c0d822dac 100644 (file)
@@ -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
index 3bde0eb33f8d6ac055f567c1a1973632dbf35836..b5661e8440156580488ae061683642c9761934f9 100644 (file)
@@ -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));
 
index 73a705b252f7a20c98c75689d8f53dbbab4c7a44..08ded559f5c13ab5d4ea4a8151da85675e1657f5 100644 (file)
@@ -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);
        }
 }
index 1ff64a2e288ba9263cef25404b47b17004527ff5..80e9e6b14be27d016616e4c2a499e7c864595914 100644 (file)
@@ -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));
 
index 815ce9b3504b552e8372b368e5284c5b3a2480ee..bb0085106dcca2a3a887f598245c9e51a94f4fc8 100644 (file)
@@ -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);
        }
index c7a009c5bffd1cbed8553147fe011a7cbe9fc188..9fe2fc6fdc0e4853325a96b65cfe958fc9a889ea 100644 (file)
@@ -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,
index 6f3b52f7bbd7e9b7fcbdf0047ab8d37217cd5029..1053e31bc9a26cde2ed6381ec148f69cfd13c60c 100644 (file)
@@ -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<String, String> 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));
+                       }
                }
        }
 }
index bbce413ef366847fdebb79f32cfb5c7a79067ee2..6f358b47fd857aefcc72f8f9fb06a89e569ab33b 100644 (file)
@@ -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<String> updated = command.call();
index ea1ace364e9c258f3445dd87a19371c1ed720bea..0b6047f881e2b5ca7e7d89bdc46ffcda0fb18d80 100644 (file)
@@ -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());
+               }
        }
 }