PushCommandTest: Open Git instances in try-with-resource

Change-Id: I3a8e28a4097e868a34ee1b23256c7f28d570cd75
Signed-off-by: David Pursehouse <david.pursehouse@sonymobile.com>
This commit is contained in:
David Pursehouse 2016-02-12 13:55:40 +09:00
parent 83f1f8fc7c
commit f3c250bd08
1 changed files with 175 additions and 178 deletions

View File

@ -90,7 +90,7 @@ public void testPush() throws JGitInternalException, IOException,
remoteConfig.update(config);
config.save();
Git git1 = new Git(db);
try (Git git1 = new Git(db)) {
// create some refs via commits and tag
RevCommit commit = git1.commit().setMessage("initial commit").call();
Ref tagRef = git1.tag().setName("tag").call();
@ -111,6 +111,7 @@ public void testPush() throws JGitInternalException, IOException,
assertEquals(tagRef.getObjectId(),
db2.resolve(tagRef.getObjectId().getName()));
}
}
@Test
public void testPrePushHook() throws JGitInternalException, IOException,
@ -132,7 +133,7 @@ public void testPrePushHook() throws JGitInternalException, IOException,
+ hookOutput.toPath() + "\"\ncat - >>\"" + hookOutput.toPath()
+ "\"\nexit 0");
Git git1 = new Git(db);
try (Git git1 = new Git(db)) {
// create some refs via commits and tag
RevCommit commit = git1.commit().setMessage("initial commit").call();
@ -142,6 +143,7 @@ public void testPrePushHook() throws JGitInternalException, IOException,
+ commit.getName() + " refs/heads/x "
+ ObjectId.zeroId().name(), read(hookOutput));
}
}
private File writeHookFile(final String name, final String data)
throws IOException {
@ -160,8 +162,7 @@ public void testTrackingUpdate() throws Exception {
String branch = "refs/heads/master";
String trackingBranch = "refs/remotes/" + remote + "/master";
Git git = new Git(db);
try (Git git = new Git(db)) {
RevCommit commit1 = git.commit().setMessage("Initial commit")
.call();
@ -200,6 +201,7 @@ public void testTrackingUpdate() throws Exception {
assertEquals(commit2.getId(), db.resolve(trackingBranch));
assertEquals(commit2.getId(), db2.resolve(branch));
}
}
/**
* Check that pushes over file protocol lead to appropriate ref-updates.
@ -208,9 +210,8 @@ public void testTrackingUpdate() throws Exception {
*/
@Test
public void testPushRefUpdate() throws Exception {
Git git = new Git(db);
Git git2 = new Git(createBareRepository());
try (Git git = new Git(db);
Git git2 = new Git(createBareRepository())) {
final StoredConfig config = git.getRepository().getConfig();
RemoteConfig remoteConfig = new RemoteConfig(config, "test");
URIish uri = new URIish(git2.getRepository().getDirectory().toURI()
@ -232,7 +233,6 @@ public void testPushRefUpdate() throws Exception {
git.branchCreate().setName("refs/heads/test").call();
git.checkout().setName("refs/heads/test").call();
for (int i = 0; i < 6; i++) {
writeTrashFile("f" + i, "content of f" + i);
git.add().addFilepattern("f" + i).call();
@ -241,7 +241,7 @@ public void testPushRefUpdate() throws Exception {
git2.getRepository().getAllRefs();
assertEquals("failed to update on attempt " + i, commit.getId(),
git2.getRepository().resolve("refs/heads/test"));
}
}
}
@ -252,9 +252,8 @@ public void testPushRefUpdate() throws Exception {
*/
@Test
public void testPushWithRefSpecFromConfig() throws Exception {
Git git = new Git(db);
Git git2 = new Git(createBareRepository());
try (Git git = new Git(db);
Git git2 = new Git(createBareRepository())) {
final StoredConfig config = git.getRepository().getConfig();
RemoteConfig remoteConfig = new RemoteConfig(config, "test");
URIish uri = new URIish(git2.getRepository().getDirectory().toURI()
@ -272,8 +271,7 @@ public void testPushWithRefSpecFromConfig() throws Exception {
git.push().setRemote("test").call();
assertEquals(commit.getId(),
git2.getRepository().resolve("refs/heads/newbranch"));
}
}
/**
@ -283,9 +281,8 @@ public void testPushWithRefSpecFromConfig() throws Exception {
*/
@Test
public void testPushWithoutPushRefSpec() throws Exception {
Git git = new Git(db);
Git git2 = new Git(createBareRepository());
try (Git git = new Git(db);
Git git2 = new Git(createBareRepository())) {
final StoredConfig config = git.getRepository().getConfig();
RemoteConfig remoteConfig = new RemoteConfig(config, "test");
URIish uri = new URIish(git2.getRepository().getDirectory().toURI()
@ -314,7 +311,7 @@ public void testPushWithoutPushRefSpec() throws Exception {
assertEquals(null, git2.getRepository()
.resolve("refs/heads/not-pushed"));
assertEquals(null, git2.getRepository().resolve("refs/heads/master"));
}
}
/**
@ -335,9 +332,8 @@ public void testPushAfterGC() throws Exception {
remoteConfig.update(config);
config.save();
Git git1 = new Git(db);
Git git2 = new Git(db2);
try (Git git1 = new Git(db);
Git git2 = new Git(db2)) {
// push master (with a new commit) to the remote
git1.commit().setMessage("initial commit").call();
@ -382,4 +378,5 @@ public void testPushAfterGC() throws Exception {
assertEquals(commit3.getId(),
db2.resolve(commit3.getId().getName() + "^{commit}"));
}
}
}