Convert remaining uses of MetaDataUpdate to try-with-resource

These were not causing a "should be managed in try-with-resource"
warning from Eclipse, and were found by manual inspection.

Change-Id: I30c09f329c458117ece5878eeaba9c905eee06b2
This commit is contained in:
David Pursehouse
2015-12-18 14:26:45 +09:00
parent 55a35ca60a
commit eafb9bcd5a
7 changed files with 139 additions and 134 deletions

View File

@@ -519,21 +519,23 @@ public abstract class AbstractDaemonTest {
protected void setUseContributorAgreements(InheritableBoolean value) protected void setUseContributorAgreements(InheritableBoolean value)
throws Exception { throws Exception {
MetaDataUpdate md = metaDataUpdateFactory.create(project); try (MetaDataUpdate md = metaDataUpdateFactory.create(project)) {
ProjectConfig config = ProjectConfig.read(md); ProjectConfig config = ProjectConfig.read(md);
config.getProject().setUseContributorAgreements(value); config.getProject().setUseContributorAgreements(value);
config.commit(md); config.commit(md);
projectCache.evict(config.getProject()); projectCache.evict(config.getProject());
} }
}
protected void setUseSignedOffBy(InheritableBoolean value) protected void setUseSignedOffBy(InheritableBoolean value)
throws Exception { throws Exception {
MetaDataUpdate md = metaDataUpdateFactory.create(project); try (MetaDataUpdate md = metaDataUpdateFactory.create(project)) {
ProjectConfig config = ProjectConfig.read(md); ProjectConfig config = ProjectConfig.read(md);
config.getProject().setUseSignedOffBy(value); config.getProject().setUseSignedOffBy(value);
config.commit(md); config.commit(md);
projectCache.evict(config.getProject()); projectCache.evict(config.getProject());
} }
}
protected void deny(String permission, AccountGroup.UUID id, String ref) protected void deny(String permission, AccountGroup.UUID id, String ref)
throws Exception { throws Exception {
@@ -566,7 +568,7 @@ public abstract class AbstractDaemonTest {
protected void grant(String permission, Project.NameKey project, String ref, protected void grant(String permission, Project.NameKey project, String ref,
boolean force) throws RepositoryNotFoundException, IOException, boolean force) throws RepositoryNotFoundException, IOException,
ConfigInvalidException { ConfigInvalidException {
MetaDataUpdate md = metaDataUpdateFactory.create(project); try (MetaDataUpdate md = metaDataUpdateFactory.create(project)) {
md.setMessage(String.format("Grant %s on %s", permission, ref)); md.setMessage(String.format("Grant %s on %s", permission, ref));
ProjectConfig config = ProjectConfig.read(md); ProjectConfig config = ProjectConfig.read(md);
AccessSection s = config.getAccessSection(ref, true); AccessSection s = config.getAccessSection(ref, true);
@@ -578,6 +580,7 @@ public abstract class AbstractDaemonTest {
config.commit(md); config.commit(md);
projectCache.evict(config.getProject()); projectCache.evict(config.getProject());
} }
}
protected void blockRead(String ref) throws Exception { protected void blockRead(String ref) throws Exception {
block(Permission.READ, REGISTERED_USERS, ref); block(Permission.READ, REGISTERED_USERS, ref);

View File

@@ -30,12 +30,9 @@ import com.google.gerrit.server.git.MetaDataUpdate;
import com.google.gerrit.server.git.ProjectConfig; import com.google.gerrit.server.git.ProjectConfig;
import com.google.gerrit.server.group.SystemGroupBackend; import com.google.gerrit.server.group.SystemGroupBackend;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.IOException;
public class ChangeOwnerIT extends AbstractDaemonTest { public class ChangeOwnerIT extends AbstractDaemonTest {
private TestAccount user2; private TestAccount user2;
@@ -80,9 +77,8 @@ public class ChangeOwnerIT extends AbstractDaemonTest {
approve(a, changeId); approve(a, changeId);
} }
private void grantApproveToChangeOwner() throws IOException, private void grantApproveToChangeOwner() throws Exception {
ConfigInvalidException { try (MetaDataUpdate md = metaDataUpdateFactory.create(project)) {
MetaDataUpdate md = metaDataUpdateFactory.create(project);
md.setMessage(String.format("Grant approve to change owner")); md.setMessage(String.format("Grant approve to change owner"));
ProjectConfig config = ProjectConfig.read(md); ProjectConfig config = ProjectConfig.read(md);
AccessSection s = config.getAccessSection("refs/heads/*", true); AccessSection s = config.getAccessSection("refs/heads/*", true);
@@ -95,6 +91,7 @@ public class ChangeOwnerIT extends AbstractDaemonTest {
config.commit(md); config.commit(md);
projectCache.evict(config.getProject()); projectCache.evict(config.getProject());
} }
}
private String createMyChange() throws Exception { private String createMyChange() throws Exception {
PushOneCommit push = pushFactory.create(db, user.getIdent(), testRepo); PushOneCommit push = pushFactory.create(db, user.getIdent(), testRepo);

View File

@@ -117,13 +117,14 @@ public abstract class AbstractChangeUpdate extends VersionedMetaData {
throws IOException { throws IOException {
if (migration.writeChanges()) { if (migration.writeChanges()) {
load(); load();
MetaDataUpdate md = try (MetaDataUpdate md =
updateFactory.create(getProjectName(), updateFactory.create(getProjectName(),
repoManager.openMetadataRepository(getProjectName()), getUser(), repoManager.openMetadataRepository(getProjectName()), getUser(),
bru); bru)) {
md.setAllowEmpty(true); md.setAllowEmpty(true);
return super.openUpdate(md); return super.openUpdate(md);
} }
}
return new BatchMetaDataUpdate() { return new BatchMetaDataUpdate() {
@Override @Override
public void write(CommitBuilder commit) { public void write(CommitBuilder commit) {

View File

@@ -114,10 +114,10 @@ public class AllProjectsCreator {
private void initAllProjects(Repository git) private void initAllProjects(Repository git)
throws IOException, ConfigInvalidException { throws IOException, ConfigInvalidException {
MetaDataUpdate md = new MetaDataUpdate( try (MetaDataUpdate md = new MetaDataUpdate(
GitReferenceUpdated.DISABLED, GitReferenceUpdated.DISABLED,
allProjectsName, allProjectsName,
git); git)) {
md.getCommitBuilder().setAuthor(serverUser); md.getCommitBuilder().setAuthor(serverUser);
md.getCommitBuilder().setCommitter(serverUser); md.getCommitBuilder().setCommitter(serverUser);
md.setMessage(MoreObjects.firstNonNull( md.setMessage(MoreObjects.firstNonNull(
@@ -177,6 +177,7 @@ public class AllProjectsCreator {
config.commitToNewRef(md, RefNames.REFS_CONFIG); config.commitToNewRef(md, RefNames.REFS_CONFIG);
} }
}
public static LabelType initCodeReviewLabel(ProjectConfig c) { public static LabelType initCodeReviewLabel(ProjectConfig c) {
LabelType type = new LabelType("Code-Review", ImmutableList.of( LabelType type = new LabelType("Code-Review", ImmutableList.of(

View File

@@ -75,10 +75,10 @@ public class AllUsersCreator {
private void initAllUsers(Repository git) private void initAllUsers(Repository git)
throws IOException, ConfigInvalidException { throws IOException, ConfigInvalidException {
MetaDataUpdate md = new MetaDataUpdate( try (MetaDataUpdate md = new MetaDataUpdate(
GitReferenceUpdated.DISABLED, GitReferenceUpdated.DISABLED,
allUsersName, allUsersName,
git); git)) {
md.getCommitBuilder().setAuthor(serverUser); md.getCommitBuilder().setAuthor(serverUser);
md.getCommitBuilder().setCommitter(serverUser); md.getCommitBuilder().setCommitter(serverUser);
md.setMessage("Initialized Gerrit Code Review " + Version.getVersion()); md.setMessage("Initialized Gerrit Code Review " + Version.getVersion());
@@ -101,3 +101,4 @@ public class AllUsersCreator {
config.commit(md); config.commit(md);
} }
} }
}

View File

@@ -196,11 +196,12 @@ public class LabelNormalizerTest {
} }
private void save(ProjectConfig pc) throws Exception { private void save(ProjectConfig pc) throws Exception {
MetaDataUpdate md = try(MetaDataUpdate md =
metaDataUpdateFactory.create(pc.getProject().getNameKey(), user); metaDataUpdateFactory.create(pc.getProject().getNameKey(), user)) {
pc.commit(md); pc.commit(md);
projectCache.evict(pc.getProject().getNameKey()); projectCache.evict(pc.getProject().getNameKey());
} }
}
private PatchSetApproval psa(Account.Id accountId, String label, int value) { private PatchSetApproval psa(Account.Id accountId, String label, int value) {
return new PatchSetApproval( return new PatchSetApproval(

View File

@@ -249,10 +249,10 @@ public class ProjectConfigTest extends LocalDiskRepositoryTestCase {
private RevCommit commit(ProjectConfig cfg) throws IOException, private RevCommit commit(ProjectConfig cfg) throws IOException,
MissingObjectException, IncorrectObjectTypeException { MissingObjectException, IncorrectObjectTypeException {
MetaDataUpdate md = new MetaDataUpdate( try (MetaDataUpdate md = new MetaDataUpdate(
GitReferenceUpdated.DISABLED, GitReferenceUpdated.DISABLED,
cfg.getProject().getNameKey(), cfg.getProject().getNameKey(),
db); db)) {
util.tick(5); util.tick(5);
util.setAuthorAndCommitter(md.getCommitBuilder()); util.setAuthorAndCommitter(md.getCommitBuilder());
md.setMessage("Edit\n"); md.setMessage("Edit\n");
@@ -261,6 +261,7 @@ public class ProjectConfigTest extends LocalDiskRepositoryTestCase {
Ref ref = db.getRef(RefNames.REFS_CONFIG); Ref ref = db.getRef(RefNames.REFS_CONFIG);
return util.getRevWalk().parseCommit(ref.getObjectId()); return util.getRevWalk().parseCommit(ref.getObjectId());
} }
}
private void update(RevCommit rev) throws Exception { private void update(RevCommit rev) throws Exception {
RefUpdate u = db.updateRef(RefNames.REFS_CONFIG); RefUpdate u = db.updateRef(RefNames.REFS_CONFIG);