TestRepository: Fix resource leak flagged by Eclipse

Since [1], included in JGit 5.3.0, TestRepository is AutoCloseable and
should be managed by try-with-resource.

[1] https://git.eclipse.org/r/#/c/134912
Change-Id: Id5fa8a47e1557ee2a5765aa13fc317f2f73ef5d1
This commit is contained in:
David Ostrovsky
2019-05-26 12:31:31 +02:00
committed by David Pursehouse
parent 9cbb372690
commit 3b2df0674b
23 changed files with 96 additions and 86 deletions

View File

@@ -441,35 +441,36 @@ public class GroupNameNotesTest {
GroupReference g1 = newGroup("a");
GroupReference g2 = newGroup("b");
TestRepository<?> tr = new TestRepository<>(repo);
ObjectId k1 = getNoteKey(g1);
ObjectId k2 = getNoteKey(g2);
ObjectId k3 = GroupNameNotes.getNoteKey(AccountGroup.nameKey("c"));
PersonIdent ident = newPersonIdent();
ObjectId origCommitId =
tr.branch(REFS_GROUPNAMES)
.commit()
.message("Prepopulate group name")
.author(ident)
.committer(ident)
.add(k1.name(), "[group]\n\tuuid = a-1\n\tname = a\nanotherKey = foo\n")
.add(k2.name(), "[group]\n\tuuid = a-1\n\tname = b\n")
.add(k3.name(), "[group]\n\tuuid = c-3\n\tname = c\n")
.create()
.copy();
try (TestRepository<Repository> tr = new TestRepository<>(repo)) {
ObjectId k1 = getNoteKey(g1);
ObjectId k2 = getNoteKey(g2);
ObjectId k3 = GroupNameNotes.getNoteKey(AccountGroup.nameKey("c"));
PersonIdent ident = newPersonIdent();
ObjectId origCommitId =
tr.branch(REFS_GROUPNAMES)
.commit()
.message("Prepopulate group name")
.author(ident)
.committer(ident)
.add(k1.name(), "[group]\n\tuuid = a-1\n\tname = a\nanotherKey = foo\n")
.add(k2.name(), "[group]\n\tuuid = a-1\n\tname = b\n")
.add(k3.name(), "[group]\n\tuuid = c-3\n\tname = c\n")
.create()
.copy();
ident = newPersonIdent();
updateAllGroups(ident, g1, g2);
ident = newPersonIdent();
updateAllGroups(ident, g1, g2);
assertThat(GroupNameNotes.loadAllGroups(repo)).containsExactly(g1, g2);
assertThat(GroupNameNotes.loadAllGroups(repo)).containsExactly(g1, g2);
ImmutableList<CommitInfo> log = log();
assertThat(log).hasSize(2);
assertThat(log.get(0)).commit().isEqualTo(origCommitId.name());
ImmutableList<CommitInfo> log = log();
assertThat(log).hasSize(2);
assertThat(log.get(0)).commit().isEqualTo(origCommitId.name());
assertThat(log.get(1)).message().isEqualTo("Store 2 group names");
assertThat(log.get(1)).author().matches(ident);
assertThat(log.get(1)).committer().matches(ident);
assertThat(log.get(1)).message().isEqualTo("Store 2 group names");
assertThat(log.get(1)).author().matches(ident);
assertThat(log.get(1)).committer().matches(ident);
}
// Old note content was overwritten.
assertThat(readNameNote(g1)).isEqualTo("[group]\n\tuuid = a-1\n\tname = a\n");

View File

@@ -177,8 +177,8 @@ public class RepoSequenceTest {
@Test
public void failOnWrongType() throws Exception {
try (Repository repo = repoManager.openRepository(project)) {
TestRepository<Repository> tr = new TestRepository<>(repo);
try (Repository repo = repoManager.openRepository(project);
TestRepository<Repository> tr = new TestRepository<>(repo)) {
tr.branch(RefNames.REFS_SEQUENCES + "id").commit().create();
StorageException e =
assertThrows(StorageException.class, () -> newSequence("id", 1, 3).next());

View File

@@ -199,8 +199,9 @@ public class RefControlTest {
// Clear out All-Projects and use the lowest-level API possible for project creation, so the
// only ACL entries are exactly what is initialized by this test, and we aren't subject to
// changing defaults in SchemaCreator or ProjectCreator.
try (Repository allProjectsRepo = repoManager.createRepository(allProjectsName)) {
new TestRepository<>(allProjectsRepo).delete(REFS_CONFIG);
try (Repository allProjectsRepo = repoManager.createRepository(allProjectsName);
TestRepository<Repository> tr = new TestRepository<>(allProjectsRepo)) {
tr.delete(REFS_CONFIG);
try (MetaDataUpdate md = metaDataUpdateFactory.create(allProjectsName)) {
ProjectConfig allProjectsConfig = projectConfigFactory.create(allProjectsName);
allProjectsConfig.load(md);

View File

@@ -116,9 +116,9 @@ public class NoteDbSchemaUpdaterTest {
@Override
public void create() throws IOException {
try (Repository repo = repoManager.createRepository(allProjectsName)) {
try (Repository repo = repoManager.createRepository(allProjectsName);
TestRepository<Repository> tr = new TestRepository<>(repo)) {
if (initialVersion.isPresent()) {
TestRepository<?> tr = new TestRepository<>(repo);
tr.update(RefNames.REFS_VERSION, tr.blob(initialVersion.get().toString()));
}
} catch (Exception e) {

View File

@@ -72,8 +72,8 @@ public class ProjectConfigSchemaUpdateTest {
public void noBaseConfig() throws Exception {
assertThat(getConfig().getString("foo", null, "bar")).isNull();
try (Repository repo = new FileRepository(allProjectsRepoFile)) {
TestRepository<?> tr = new TestRepository<>(repo);
try (Repository repo = new FileRepository(allProjectsRepoFile);
TestRepository<Repository> tr = new TestRepository<>(repo)) {
tr.branch("refs/meta/config").commit().add("project.config", "[foo]\nbar = baz").create();
}
@@ -90,8 +90,8 @@ public class ProjectConfigSchemaUpdateTest {
assertThat(getConfig().getString("foo", null, "bar")).isEqualTo("base");
try (Repository repo = new FileRepository(allProjectsRepoFile)) {
TestRepository<?> tr = new TestRepository<>(repo);
try (Repository repo = new FileRepository(allProjectsRepoFile);
TestRepository<Repository> tr = new TestRepository<>(repo)) {
tr.branch("refs/meta/config").commit().add("project.config", "[foo]\nbar = baz").create();
}