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:

committed by
David Pursehouse

parent
d74c5ac077
commit
bb5205f504
@@ -45,8 +45,8 @@ public class GroupTestUtil {
|
||||
String fileName,
|
||||
String contents)
|
||||
throws Exception {
|
||||
try (RevWalk rw = new RevWalk(allUsersRepo)) {
|
||||
TestRepository<Repository> testRepository = new TestRepository<>(allUsersRepo, rw);
|
||||
try (RevWalk rw = new RevWalk(allUsersRepo);
|
||||
TestRepository<Repository> testRepository = new TestRepository<>(allUsersRepo, rw)) {
|
||||
TestRepository<Repository>.CommitBuilder builder =
|
||||
testRepository
|
||||
.branch(refName)
|
||||
|
@@ -106,8 +106,8 @@ public class TestChanges {
|
||||
// Change doesn't exist yet. NoteDb requires that there be a commit for the
|
||||
// first patch set, so create one.
|
||||
GitRepositoryManager repoManager = injector.getInstance(GitRepositoryManager.class);
|
||||
try (Repository repo = repoManager.openRepository(c.getProject())) {
|
||||
TestRepository<Repository> tr = new TestRepository<>(repo);
|
||||
try (Repository repo = repoManager.openRepository(c.getProject());
|
||||
TestRepository<Repository> tr = new TestRepository<>(repo)) {
|
||||
PersonIdent ident =
|
||||
user.asIdentifiedUser().newCommitterIdent(update.getWhen(), TimeZone.getDefault());
|
||||
TestRepository<Repository>.CommitBuilder cb =
|
||||
|
@@ -3952,8 +3952,8 @@ public class ChangeIT extends AbstractDaemonTest {
|
||||
}
|
||||
|
||||
private void modifySubmitRules(String newContent) throws Exception {
|
||||
try (Repository repo = repoManager.openRepository(project)) {
|
||||
TestRepository<?> testRepo = new TestRepository<>((InMemoryRepository) repo);
|
||||
try (Repository repo = repoManager.openRepository(project);
|
||||
TestRepository<Repository> testRepo = new TestRepository<>(repo)) {
|
||||
testRepo
|
||||
.branch(RefNames.REFS_CONFIG)
|
||||
.commit()
|
||||
|
@@ -192,9 +192,9 @@ public class DashboardIT extends AbstractDaemonTest {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
try (Repository r = repoManager.openRepository(project)) {
|
||||
TestRepository<Repository>.CommitBuilder cb =
|
||||
new TestRepository<>(r).branch(canonicalRef).commit();
|
||||
try (Repository r = repoManager.openRepository(project);
|
||||
TestRepository<Repository> tr = new TestRepository<>(r)) {
|
||||
TestRepository<Repository>.CommitBuilder cb = tr.branch(canonicalRef).commit();
|
||||
StringBuilder content = new StringBuilder("[dashboard]\n");
|
||||
if (info.title != null) {
|
||||
content.append("title = ").append(info.title).append("\n");
|
||||
|
@@ -995,8 +995,8 @@ public class RevisionIT extends AbstractDaemonTest {
|
||||
|
||||
// Make the same change in a separate commit and update server HEAD behind Gerrit's back, which
|
||||
// will not reindex any open changes.
|
||||
try (Repository repo = repoManager.openRepository(project)) {
|
||||
TestRepository<?> tr = new TestRepository<>(repo);
|
||||
try (Repository repo = repoManager.openRepository(project);
|
||||
TestRepository<Repository> tr = new TestRepository<>(repo)) {
|
||||
String ref = "refs/heads/master";
|
||||
assertThat(repo.exactRef(ref).getObjectId()).isEqualTo(r1.getCommit());
|
||||
tr.update(ref, tr.getRevWalk().parseCommit(initial));
|
||||
|
@@ -1779,8 +1779,8 @@ public abstract class AbstractPushForReview extends AbstractDaemonTest {
|
||||
Change.Id id2 = r2.getChange().getId();
|
||||
|
||||
// Merge change 1 behind Gerrit's back.
|
||||
try (Repository repo = repoManager.openRepository(project)) {
|
||||
TestRepository<?> tr = new TestRepository<>(repo);
|
||||
try (Repository repo = repoManager.openRepository(project);
|
||||
TestRepository<?> tr = new TestRepository<>(repo)) {
|
||||
tr.branch("refs/heads/master").update(r1.getCommit());
|
||||
}
|
||||
|
||||
@@ -1859,12 +1859,12 @@ public abstract class AbstractPushForReview extends AbstractDaemonTest {
|
||||
Change c = r.getChange().change();
|
||||
|
||||
RevCommit ps2Commit;
|
||||
try (Repository repo = repoManager.openRepository(project)) {
|
||||
try (Repository repo = repoManager.openRepository(project);
|
||||
TestRepository<?> tr = new TestRepository<>(repo)) {
|
||||
// Create a new patch set of the change directly in Gerrit's repository,
|
||||
// without pushing it. In reality it's more likely that the client would
|
||||
// create and push this behind Gerrit's back (e.g. an admin accidentally
|
||||
// using direct ssh access to the repo), but that's harder to do in tests.
|
||||
TestRepository<?> tr = new TestRepository<>(repo);
|
||||
ps2Commit =
|
||||
tr.branch("refs/heads/master")
|
||||
.commit()
|
||||
@@ -1980,8 +1980,8 @@ public abstract class AbstractPushForReview extends AbstractDaemonTest {
|
||||
gApi.changes().id(r.getChangeId()).current().review(ReviewInput.approve());
|
||||
gApi.changes().id(r.getChangeId()).current().submit();
|
||||
|
||||
try (Repository repo = repoManager.openRepository(project)) {
|
||||
TestRepository<?> tr = new TestRepository<>(repo);
|
||||
try (Repository repo = repoManager.openRepository(project);
|
||||
TestRepository<Repository> tr = new TestRepository<>(repo)) {
|
||||
tr.branch("refs/heads/branch").commit().message("Initial commit on branch").create();
|
||||
}
|
||||
|
||||
@@ -2039,8 +2039,8 @@ public abstract class AbstractPushForReview extends AbstractDaemonTest {
|
||||
// expecting the change to be auto-closed, but the change metadata update
|
||||
// fails.
|
||||
ObjectId c2;
|
||||
try (Repository repo = repoManager.openRepository(project)) {
|
||||
TestRepository<?> tr = new TestRepository<>(repo);
|
||||
try (Repository repo = repoManager.openRepository(project);
|
||||
TestRepository<Repository> tr = new TestRepository<>(repo)) {
|
||||
RevCommit commit2 =
|
||||
tr.amend(c1).message("New subject").insertChangeId(r.getChangeId().substring(1)).create();
|
||||
c2 = commit2.copy();
|
||||
|
@@ -71,8 +71,9 @@ public class HttpPushForReviewIT extends AbstractPushForReview {
|
||||
public void uploadPackAuditEventLog() throws Exception {
|
||||
auditService.drainHttpAuditEvents();
|
||||
// testRepo is already a clone. Make a server-side change so we have something to fetch.
|
||||
try (Repository repo = repoManager.openRepository(project)) {
|
||||
new TestRepository<>(repo).branch("master").commit().create();
|
||||
try (Repository repo = repoManager.openRepository(project);
|
||||
TestRepository<Repository> tr = new TestRepository<>(repo)) {
|
||||
tr.branch("master").commit().create();
|
||||
}
|
||||
testRepo.git().fetch().call();
|
||||
|
||||
|
@@ -159,8 +159,8 @@ public class PushPermissionsIT extends AbstractDaemonTest {
|
||||
|
||||
@Test
|
||||
public void groupRefsByMessage() throws Exception {
|
||||
try (Repository repo = repoManager.openRepository(project)) {
|
||||
TestRepository<?> tr = new TestRepository<>(repo);
|
||||
try (Repository repo = repoManager.openRepository(project);
|
||||
TestRepository<Repository> tr = new TestRepository<>(repo)) {
|
||||
tr.branch("foo").commit().create();
|
||||
tr.branch("bar").commit().create();
|
||||
}
|
||||
|
@@ -438,8 +438,8 @@ public class RefAdvertisementIT extends AbstractDaemonTest {
|
||||
@Test
|
||||
public void receivePackOmitsMissingObject() throws Exception {
|
||||
String rev = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
|
||||
try (Repository repo = repoManager.openRepository(project)) {
|
||||
TestRepository<?> tr = new TestRepository<>(repo);
|
||||
try (Repository repo = repoManager.openRepository(project);
|
||||
TestRepository<Repository> tr = new TestRepository<>(repo)) {
|
||||
String subject = "Subject for missing commit";
|
||||
Change c = new Change(cd3.change());
|
||||
PatchSet.Id psId = new PatchSet.Id(cd3.getId(), 2);
|
||||
|
@@ -652,8 +652,9 @@ public class SubmoduleSubscriptionsIT extends AbstractSubmoduleSubscription {
|
||||
}
|
||||
|
||||
private ObjectId directUpdateRef(Project.NameKey project, String ref) throws Exception {
|
||||
try (Repository serverRepo = repoManager.openRepository(project)) {
|
||||
return new TestRepository<>(serverRepo).branch(ref).commit().create().copy();
|
||||
try (Repository serverRepo = repoManager.openRepository(project);
|
||||
TestRepository<Repository> tr = new TestRepository<>(serverRepo)) {
|
||||
return tr.branch(ref).commit().create().copy();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -237,9 +237,9 @@ public class ProjectsRestApiBindingsIT extends AbstractDaemonTest {
|
||||
grant(project, "refs/meta/*", Permission.CREATE);
|
||||
gApi.projects().name(project.get()).branch(dashboardRef).create(new BranchInput());
|
||||
|
||||
try (Repository r = repoManager.openRepository(project)) {
|
||||
TestRepository<Repository>.CommitBuilder cb =
|
||||
new TestRepository<>(r).branch(dashboardRef).commit();
|
||||
try (Repository r = repoManager.openRepository(project);
|
||||
TestRepository<Repository> tr = new TestRepository<>(r)) {
|
||||
TestRepository<Repository>.CommitBuilder cb = tr.branch(dashboardRef).commit();
|
||||
StringBuilder content = new StringBuilder("[dashboard]\n");
|
||||
content.append("title = ").append("Open Changes").append("\n");
|
||||
content.append("[section \"").append("open").append("\"]\n");
|
||||
|
@@ -474,8 +474,8 @@ public class CreateProjectIT extends AbstractDaemonTest {
|
||||
}
|
||||
|
||||
private Optional<String> readProjectConfig(String projectName) throws Exception {
|
||||
try (Repository repo = repoManager.openRepository(new Project.NameKey(projectName))) {
|
||||
TestRepository<?> tr = new TestRepository<>(repo);
|
||||
try (Repository repo = repoManager.openRepository(new Project.NameKey(projectName));
|
||||
TestRepository<Repository> tr = new TestRepository<>(repo)) {
|
||||
RevWalk rw = tr.getRevWalk();
|
||||
Ref ref = repo.exactRef(RefNames.REFS_CONFIG);
|
||||
if (ref == null) {
|
||||
|
@@ -1508,8 +1508,9 @@ public class ChangeNotificationsIT extends AbstractNotificationTest {
|
||||
if (submitType == SubmitType.FAST_FORWARD_ONLY) {
|
||||
continue;
|
||||
}
|
||||
try (Repository repo = repoManager.openRepository(project)) {
|
||||
new TestRepository<>(repo).branch("master").commit().create();
|
||||
try (Repository repo = repoManager.openRepository(project);
|
||||
TestRepository<Repository> tr = new TestRepository<>(repo)) {
|
||||
tr.branch("master").commit().create();
|
||||
}
|
||||
name += " after branch has advanced";
|
||||
}
|
||||
|
@@ -26,7 +26,6 @@ import com.google.gerrit.server.project.SubmitRuleOptions;
|
||||
import com.google.gerrit.server.query.change.ChangeData;
|
||||
import com.google.inject.Inject;
|
||||
import java.util.Collection;
|
||||
import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
|
||||
import org.eclipse.jgit.junit.TestRepository;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.junit.Before;
|
||||
@@ -107,8 +106,8 @@ public class RulesIT extends AbstractDaemonTest {
|
||||
private void modifySubmitRules(String ruleTested) throws Exception {
|
||||
String newContent = String.format(RULE_TEMPLATE, ruleTested);
|
||||
|
||||
try (Repository repo = repoManager.openRepository(project)) {
|
||||
TestRepository<?> testRepo = new TestRepository<>((InMemoryRepository) repo);
|
||||
try (Repository repo = repoManager.openRepository(project);
|
||||
TestRepository<Repository> testRepo = new TestRepository<>(repo)) {
|
||||
testRepo
|
||||
.branch(RefNames.REFS_CONFIG)
|
||||
.commit()
|
||||
|
@@ -110,7 +110,10 @@ public class RefUpdateUtilRepoTest extends GerritBaseTests {
|
||||
@Test
|
||||
public void deleteRef() throws Exception {
|
||||
String ref = "refs/heads/foo";
|
||||
new TestRepository<>(repo).branch(ref).commit().create();
|
||||
try (TestRepository<Repository> tr = new TestRepository<>(repo)) {
|
||||
tr.branch(ref).commit().create();
|
||||
}
|
||||
|
||||
assertThat(repo.exactRef(ref)).isNotNull();
|
||||
RefUpdateUtil.deleteChecked(repo, "refs/heads/foo");
|
||||
assertThat(repo.exactRef(ref)).isNull();
|
||||
|
@@ -82,8 +82,8 @@ public class AllProjectsConfigTest {
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -100,8 +100,8 @@ public class AllProjectsConfigTest {
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
@@ -424,35 +424,36 @@ public class GroupNameNotesTest extends GerritBaseTests {
|
||||
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(new 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<?> tr = new TestRepository<>(repo)) {
|
||||
ObjectId k1 = getNoteKey(g1);
|
||||
ObjectId k2 = getNoteKey(g2);
|
||||
ObjectId k3 = GroupNameNotes.getNoteKey(new 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");
|
||||
|
@@ -175,8 +175,8 @@ public class RepoSequenceTest extends GerritBaseTests {
|
||||
|
||||
@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();
|
||||
try {
|
||||
newSequence("id", 1, 3).next();
|
||||
|
@@ -123,9 +123,9 @@ public class NoteDbSchemaUpdaterTest extends GerritBaseTests {
|
||||
|
||||
@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) {
|
||||
|
@@ -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();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user