Use try-with-resources statements
- instead of finally blocks - in cases of missing try-finally Change-Id: I94f481a33d8e6a3180c436245d6e95e4d525280c
This commit is contained in:
@@ -99,11 +99,10 @@ public abstract class PrologTestCase {
|
||||
protected void consult(BufferingPrologControl env,
|
||||
Class<?> clazz,
|
||||
String prologResource) throws CompileException, IOException {
|
||||
InputStream in = clazz.getResourceAsStream(prologResource);
|
||||
if (in == null) {
|
||||
throw new FileNotFoundException(prologResource);
|
||||
}
|
||||
try {
|
||||
try (InputStream in = clazz.getResourceAsStream(prologResource)) {
|
||||
if (in == null) {
|
||||
throw new FileNotFoundException(prologResource);
|
||||
}
|
||||
SymbolTerm pathTerm = SymbolTerm.create(prologResource);
|
||||
JavaObjectTerm inTerm =
|
||||
new JavaObjectTerm(new PushbackReader(new BufferedReader(
|
||||
@@ -111,8 +110,6 @@ public abstract class PrologTestCase {
|
||||
if (!env.execute(Prolog.BUILTIN, "consult_stream", pathTerm, inTerm)) {
|
||||
throw new CompileException("Cannot consult " + prologResource);
|
||||
}
|
||||
} finally {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -188,13 +188,10 @@ public class LabelNormalizerTest {
|
||||
}
|
||||
|
||||
private ProjectConfig loadAllProjects() throws Exception {
|
||||
Repository repo = repoManager.openRepository(allProjects);
|
||||
try {
|
||||
try (Repository repo = repoManager.openRepository(allProjects)) {
|
||||
ProjectConfig pc = new ProjectConfig(allProjects);
|
||||
pc.load(repo);
|
||||
return pc;
|
||||
} finally {
|
||||
repo.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -444,23 +444,23 @@ public class ChangeNotesTest extends AbstractChangeNotesTest {
|
||||
RevCommit commitWithComments = commitWithApprovals.getParent(0);
|
||||
assertThat(commitWithComments).isNotNull();
|
||||
|
||||
ChangeNotesParser notesWithComments =
|
||||
new ChangeNotesParser(c, commitWithComments.copy(), rw, repoManager);
|
||||
notesWithComments.parseAll();
|
||||
ImmutableListMultimap<PatchSet.Id, PatchSetApproval> approvals1 =
|
||||
notesWithComments.buildApprovals();
|
||||
assertThat(approvals1).isEmpty();
|
||||
assertThat(notesWithComments.comments).hasSize(1);
|
||||
notesWithComments.close();
|
||||
try (ChangeNotesParser notesWithComments =
|
||||
new ChangeNotesParser(c, commitWithComments.copy(), rw, repoManager)) {
|
||||
notesWithComments.parseAll();
|
||||
ImmutableListMultimap<PatchSet.Id, PatchSetApproval> approvals1 =
|
||||
notesWithComments.buildApprovals();
|
||||
assertThat(approvals1).isEmpty();
|
||||
assertThat(notesWithComments.comments).hasSize(1);
|
||||
}
|
||||
|
||||
ChangeNotesParser notesWithApprovals =
|
||||
new ChangeNotesParser(c, commitWithApprovals.copy(), rw, repoManager);
|
||||
notesWithApprovals.parseAll();
|
||||
ImmutableListMultimap<PatchSet.Id, PatchSetApproval> approvals2 =
|
||||
notesWithApprovals.buildApprovals();
|
||||
assertThat(approvals2).hasSize(1);
|
||||
assertThat(notesWithApprovals.comments).hasSize(1);
|
||||
notesWithApprovals.close();
|
||||
try (ChangeNotesParser notesWithApprovals =
|
||||
new ChangeNotesParser(c, commitWithApprovals.copy(), rw, repoManager)) {
|
||||
notesWithApprovals.parseAll();
|
||||
ImmutableListMultimap<PatchSet.Id, PatchSetApproval> approvals2 =
|
||||
notesWithApprovals.buildApprovals();
|
||||
assertThat(approvals2).hasSize(1);
|
||||
assertThat(notesWithApprovals.comments).hasSize(1);
|
||||
}
|
||||
} finally {
|
||||
batch.close();
|
||||
}
|
||||
|
||||
@@ -102,13 +102,10 @@ public class SchemaCreatorTest {
|
||||
private LabelTypes getLabelTypes() throws Exception {
|
||||
db.create();
|
||||
ProjectConfig c = new ProjectConfig(allProjects);
|
||||
Repository repo = repoManager.openRepository(allProjects);
|
||||
try {
|
||||
try (Repository repo = repoManager.openRepository(allProjects)) {
|
||||
c.load(repo);
|
||||
return new LabelTypes(
|
||||
ImmutableList.copyOf(c.getLabelSections().values()));
|
||||
} finally {
|
||||
repo.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -111,17 +111,10 @@ public class InMemoryDatabase implements SchemaFactory<ReviewDb> {
|
||||
public InMemoryDatabase create() throws OrmException {
|
||||
if (!created) {
|
||||
created = true;
|
||||
final ReviewDb c = open();
|
||||
try {
|
||||
try {
|
||||
schemaCreator.create(c);
|
||||
} catch (IOException e) {
|
||||
throw new OrmException("Cannot create in-memory database", e);
|
||||
} catch (ConfigInvalidException e) {
|
||||
throw new OrmException("Cannot create in-memory database", e);
|
||||
}
|
||||
} finally {
|
||||
c.close();
|
||||
try (ReviewDb c = open()) {
|
||||
schemaCreator.create(c);
|
||||
} catch (IOException | ConfigInvalidException e) {
|
||||
throw new OrmException("Cannot create in-memory database", e);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
@@ -142,20 +135,14 @@ public class InMemoryDatabase implements SchemaFactory<ReviewDb> {
|
||||
}
|
||||
|
||||
public SystemConfig getSystemConfig() throws OrmException {
|
||||
final ReviewDb c = open();
|
||||
try {
|
||||
try (ReviewDb c = open()) {
|
||||
return c.systemConfig().get(new SystemConfig.Key());
|
||||
} finally {
|
||||
c.close();
|
||||
}
|
||||
}
|
||||
|
||||
public CurrentSchemaVersion getSchemaVersion() throws OrmException {
|
||||
final ReviewDb c = open();
|
||||
try {
|
||||
try (ReviewDb c = open()) {
|
||||
return c.schemaVersion().get(new CurrentSchemaVersion.Key());
|
||||
} finally {
|
||||
c.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user