summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.http.test/tst
diff options
context:
space:
mode:
authorShawn Pearce <spearce@spearce.org>2016-01-19 13:08:38 -0800
committerShawn Pearce <spearce@spearce.org>2016-01-19 14:27:11 -0800
commit6b662af76ef66cd75502ace714302d3a36129ed1 (patch)
treed6cf409098b24da1108b569a2dcccfd3ee418199 /org.eclipse.jgit.http.test/tst
parenteadfcd3ec166c55c1ff3f3fe0b5e97dd94ff8d83 (diff)
downloadjgit-6b662af76ef66cd75502ace714302d3a36129ed1.tar.gz
jgit-6b662af76ef66cd75502ace714302d3a36129ed1.zip
Transport: Implement AutoCloseable
After creating a Transport instance callers should always call its close() method. Use AutoCloseable to document this idiom and allow use of try-with-resources. Change-Id: I0c6ff3e39ebecdd7a028dbcae1856a818937b186
Diffstat (limited to 'org.eclipse.jgit.http.test/tst')
-rw-r--r--org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/DumbClientDumbServerTest.java25
-rw-r--r--org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/HttpClientTests.java118
-rw-r--r--org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/SmartClientSmartServerTest.java58
3 files changed, 50 insertions, 151 deletions
diff --git a/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/DumbClientDumbServerTest.java b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/DumbClientDumbServerTest.java
index 362a09d64f..677132d732 100644
--- a/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/DumbClientDumbServerTest.java
+++ b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/DumbClientDumbServerTest.java
@@ -140,8 +140,7 @@ public class DumbClientDumbServerTest extends HttpTestCase {
assertEquals("http", remoteURI.getScheme());
Map<String, Ref> map;
- Transport t = Transport.open(dst, remoteURI);
- try {
+ try (Transport t = Transport.open(dst, remoteURI)) {
// I didn't make up these public interface names, I just
// approved them for inclusion into the code base. Sorry.
// --spearce
@@ -149,14 +148,9 @@ public class DumbClientDumbServerTest extends HttpTestCase {
assertTrue("isa TransportHttp", t instanceof TransportHttp);
assertTrue("isa HttpTransport", t instanceof HttpTransport);
- FetchConnection c = t.openFetch();
- try {
+ try (FetchConnection c = t.openFetch()) {
map = c.getRefsMap();
- } finally {
- c.close();
}
- } finally {
- t.close();
}
assertNotNull("have map of refs", map);
@@ -201,11 +195,8 @@ public class DumbClientDumbServerTest extends HttpTestCase {
Repository dst = createBareRepository();
assertFalse(dst.hasObject(A_txt));
- Transport t = Transport.open(dst, remoteURI);
- try {
+ try (Transport t = Transport.open(dst, remoteURI)) {
t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
- } finally {
- t.close();
}
assertTrue(dst.hasObject(A_txt));
@@ -226,11 +217,8 @@ public class DumbClientDumbServerTest extends HttpTestCase {
Repository dst = createBareRepository();
assertFalse(dst.hasObject(A_txt));
- Transport t = Transport.open(dst, remoteURI);
- try {
+ try (Transport t = Transport.open(dst, remoteURI)) {
t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
- } finally {
- t.close();
}
assertTrue(dst.hasObject(A_txt));
@@ -265,8 +253,7 @@ public class DumbClientDumbServerTest extends HttpTestCase {
final RevCommit Q = src.commit().create();
final Repository db = src.getRepository();
- Transport t = Transport.open(db, remoteURI);
- try {
+ try (Transport t = Transport.open(db, remoteURI)) {
try {
t.push(NullProgressMonitor.INSTANCE, push(src, Q));
fail("push incorrectly completed against a dumb server");
@@ -274,8 +261,6 @@ public class DumbClientDumbServerTest extends HttpTestCase {
String exp = "remote does not support smart HTTP push";
assertEquals(exp, nse.getMessage());
}
- } finally {
- t.close();
}
}
}
diff --git a/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/HttpClientTests.java b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/HttpClientTests.java
index cf20898b37..ce78442785 100644
--- a/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/HttpClientTests.java
+++ b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/HttpClientTests.java
@@ -157,8 +157,7 @@ public class HttpClientTests extends HttpTestCase {
public void testRepositoryNotFound_Dumb() throws Exception {
URIish uri = toURIish("/dumb.none/not-found");
Repository dst = createBareRepository();
- Transport t = Transport.open(dst, uri);
- try {
+ try (Transport t = Transport.open(dst, uri)) {
try {
t.openFetch();
fail("connection opened to not found repository");
@@ -167,8 +166,6 @@ public class HttpClientTests extends HttpTestCase {
+ "/info/refs?service=git-upload-pack not found";
assertEquals(exp, err.getMessage());
}
- } finally {
- t.close();
}
}
@@ -176,8 +173,7 @@ public class HttpClientTests extends HttpTestCase {
public void testRepositoryNotFound_Smart() throws Exception {
URIish uri = toURIish("/smart.none/not-found");
Repository dst = createBareRepository();
- Transport t = Transport.open(dst, uri);
- try {
+ try (Transport t = Transport.open(dst, uri)) {
try {
t.openFetch();
fail("connection opened to not found repository");
@@ -186,8 +182,6 @@ public class HttpClientTests extends HttpTestCase {
+ "/info/refs?service=git-upload-pack not found";
assertEquals(exp, err.getMessage());
}
- } finally {
- t.close();
}
}
@@ -201,16 +195,9 @@ public class HttpClientTests extends HttpTestCase {
Repository dst = createBareRepository();
Ref head;
- Transport t = Transport.open(dst, dumbAuthNoneURI);
- try {
- FetchConnection c = t.openFetch();
- try {
- head = c.getRef(Constants.HEAD);
- } finally {
- c.close();
- }
- } finally {
- t.close();
+ try (Transport t = Transport.open(dst, dumbAuthNoneURI);
+ FetchConnection c = t.openFetch()) {
+ head = c.getRef(Constants.HEAD);
}
assertNotNull("has " + Constants.HEAD, head);
assertEquals(Q, head.getObjectId());
@@ -225,16 +212,9 @@ public class HttpClientTests extends HttpTestCase {
Repository dst = createBareRepository();
Ref head;
- Transport t = Transport.open(dst, dumbAuthNoneURI);
- try {
- FetchConnection c = t.openFetch();
- try {
- head = c.getRef(Constants.HEAD);
- } finally {
- c.close();
- }
- } finally {
- t.close();
+ try (Transport t = Transport.open(dst, dumbAuthNoneURI);
+ FetchConnection c = t.openFetch()) {
+ head = c.getRef(Constants.HEAD);
}
assertNull("has no " + Constants.HEAD, head);
}
@@ -249,16 +229,9 @@ public class HttpClientTests extends HttpTestCase {
Repository dst = createBareRepository();
Ref head;
- Transport t = Transport.open(dst, smartAuthNoneURI);
- try {
- FetchConnection c = t.openFetch();
- try {
- head = c.getRef(Constants.HEAD);
- } finally {
- c.close();
- }
- } finally {
- t.close();
+ try (Transport t = Transport.open(dst, smartAuthNoneURI);
+ FetchConnection c = t.openFetch()) {
+ head = c.getRef(Constants.HEAD);
}
assertNotNull("has " + Constants.HEAD, head);
assertEquals(Q, head.getObjectId());
@@ -268,16 +241,13 @@ public class HttpClientTests extends HttpTestCase {
public void testListRemote_Smart_WithQueryParameters() throws Exception {
URIish myURI = toURIish("/snone/do?r=1&p=test.git");
Repository dst = createBareRepository();
- Transport t = Transport.open(dst, myURI);
- try {
+ try (Transport t = Transport.open(dst, myURI)) {
try {
t.openFetch();
fail("test did not fail to find repository as expected");
} catch (NoRemoteRepositoryException err) {
// expected
}
- } finally {
- t.close();
}
List<AccessEvent> requests = getRequests();
@@ -296,8 +266,7 @@ public class HttpClientTests extends HttpTestCase {
@Test
public void testListRemote_Dumb_NeedsAuth() throws Exception {
Repository dst = createBareRepository();
- Transport t = Transport.open(dst, dumbAuthBasicURI);
- try {
+ try (Transport t = Transport.open(dst, dumbAuthBasicURI)) {
try {
t.openFetch();
fail("connection opened even info/refs needs auth basic");
@@ -306,42 +275,35 @@ public class HttpClientTests extends HttpTestCase {
+ JGitText.get().noCredentialsProvider;
assertEquals(exp, err.getMessage());
}
- } finally {
- t.close();
}
}
@Test
public void testListRemote_Dumb_Auth() throws Exception {
Repository dst = createBareRepository();
- Transport t = Transport.open(dst, dumbAuthBasicURI);
- t.setCredentialsProvider(new UsernamePasswordCredentialsProvider(
- AppServer.username, AppServer.password));
- try {
- t.openFetch();
- } finally {
- t.close();
+ try (Transport t = Transport.open(dst, dumbAuthBasicURI)) {
+ t.setCredentialsProvider(new UsernamePasswordCredentialsProvider(
+ AppServer.username, AppServer.password));
+ t.openFetch().close();
}
- t = Transport.open(dst, dumbAuthBasicURI);
- t.setCredentialsProvider(new UsernamePasswordCredentialsProvider(
- AppServer.username, ""));
- try {
- t.openFetch();
- fail("connection opened even info/refs needs auth basic and we provide wrong password");
- } catch (TransportException err) {
- String exp = dumbAuthBasicURI + ": "
- + JGitText.get().notAuthorized;
- assertEquals(exp, err.getMessage());
- } finally {
- t.close();
+ try (Transport t = Transport.open(dst, dumbAuthBasicURI)) {
+ t.setCredentialsProvider(new UsernamePasswordCredentialsProvider(
+ AppServer.username, ""));
+ try {
+ t.openFetch();
+ fail("connection opened even info/refs needs auth basic and we provide wrong password");
+ } catch (TransportException err) {
+ String exp = dumbAuthBasicURI + ": "
+ + JGitText.get().notAuthorized;
+ assertEquals(exp, err.getMessage());
+ }
}
}
@Test
public void testListRemote_Smart_UploadPackNeedsAuth() throws Exception {
Repository dst = createBareRepository();
- Transport t = Transport.open(dst, smartAuthBasicURI);
- try {
+ try (Transport t = Transport.open(dst, smartAuthBasicURI)) {
try {
t.openFetch();
fail("connection opened even though service disabled");
@@ -350,8 +312,6 @@ public class HttpClientTests extends HttpTestCase {
+ JGitText.get().noCredentialsProvider;
assertEquals(exp, err.getMessage());
}
- } finally {
- t.close();
}
}
@@ -363,8 +323,7 @@ public class HttpClientTests extends HttpTestCase {
cfg.save();
Repository dst = createBareRepository();
- Transport t = Transport.open(dst, smartAuthNoneURI);
- try {
+ try (Transport t = Transport.open(dst, smartAuthNoneURI)) {
try {
t.openFetch();
fail("connection opened even though service disabled");
@@ -373,24 +332,15 @@ public class HttpClientTests extends HttpTestCase {
+ JGitText.get().serviceNotEnabledNoName;
assertEquals(exp, err.getMessage());
}
- } finally {
- t.close();
}
}
@Test
public void testListRemoteWithoutLocalRepository() throws Exception {
- Transport t = Transport.open(smartAuthNoneURI);
- try {
- FetchConnection c = t.openFetch();
- try {
- Ref head = c.getRef(Constants.HEAD);
- assertNotNull(head);
- } finally {
- c.close();
- }
- } finally {
- t.close();
+ try (Transport t = Transport.open(smartAuthNoneURI);
+ FetchConnection c = t.openFetch()) {
+ Ref head = c.getRef(Constants.HEAD);
+ assertNotNull(head);
}
}
}
diff --git a/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/SmartClientSmartServerTest.java b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/SmartClientSmartServerTest.java
index 9ca0789e29..82861ed9b7 100644
--- a/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/SmartClientSmartServerTest.java
+++ b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/SmartClientSmartServerTest.java
@@ -211,8 +211,7 @@ public class SmartClientSmartServerTest extends HttpTestCase {
assertEquals("http", remoteURI.getScheme());
Map<String, Ref> map;
- Transport t = Transport.open(dst, remoteURI);
- try {
+ try (Transport t = Transport.open(dst, remoteURI)) {
// I didn't make up these public interface names, I just
// approved them for inclusion into the code base. Sorry.
// --spearce
@@ -226,8 +225,6 @@ public class SmartClientSmartServerTest extends HttpTestCase {
} finally {
c.close();
}
- } finally {
- t.close();
}
assertNotNull("have map of refs", map);
@@ -257,8 +254,7 @@ public class SmartClientSmartServerTest extends HttpTestCase {
public void testListRemote_BadName() throws IOException, URISyntaxException {
Repository dst = createBareRepository();
URIish uri = new URIish(this.remoteURI.toString() + ".invalid");
- Transport t = Transport.open(dst, uri);
- try {
+ try (Transport t = Transport.open(dst, uri)) {
try {
t.openFetch();
fail("fetch connection opened");
@@ -266,8 +262,6 @@ public class SmartClientSmartServerTest extends HttpTestCase {
assertEquals(uri + ": Git repository not found",
notFound.getMessage());
}
- } finally {
- t.close();
}
List<AccessEvent> requests = getRequests();
@@ -288,11 +282,8 @@ public class SmartClientSmartServerTest extends HttpTestCase {
Repository dst = createBareRepository();
assertFalse(dst.hasObject(A_txt));
- Transport t = Transport.open(dst, remoteURI);
- try {
+ try (Transport t = Transport.open(dst, remoteURI)) {
t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
- } finally {
- t.close();
}
assertTrue(dst.hasObject(A_txt));
@@ -331,11 +322,8 @@ public class SmartClientSmartServerTest extends HttpTestCase {
// Bootstrap by doing the clone.
//
TestRepository dst = createTestRepository();
- Transport t = Transport.open(dst.getRepository(), remoteURI);
- try {
+ try (Transport t = Transport.open(dst.getRepository(), remoteURI)) {
t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
- } finally {
- t.close();
}
assertEquals(B, dst.getRepository().exactRef(master).getObjectId());
List<AccessEvent> cloneRequests = getRequests();
@@ -352,11 +340,8 @@ public class SmartClientSmartServerTest extends HttpTestCase {
// Now incrementally update.
//
- t = Transport.open(dst.getRepository(), remoteURI);
- try {
+ try (Transport t = Transport.open(dst.getRepository(), remoteURI)) {
t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
- } finally {
- t.close();
}
assertEquals(Z, dst.getRepository().exactRef(master).getObjectId());
@@ -394,11 +379,8 @@ public class SmartClientSmartServerTest extends HttpTestCase {
// Bootstrap by doing the clone.
//
TestRepository dst = createTestRepository();
- Transport t = Transport.open(dst.getRepository(), remoteURI);
- try {
+ try (Transport t = Transport.open(dst.getRepository(), remoteURI)) {
t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
- } finally {
- t.close();
}
assertEquals(B, dst.getRepository().exactRef(master).getObjectId());
List<AccessEvent> cloneRequests = getRequests();
@@ -418,11 +400,8 @@ public class SmartClientSmartServerTest extends HttpTestCase {
// Now incrementally update.
//
- t = Transport.open(dst.getRepository(), remoteURI);
- try {
+ try (Transport t = Transport.open(dst.getRepository(), remoteURI)) {
t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
- } finally {
- t.close();
}
assertEquals(Z, dst.getRepository().exactRef(master).getObjectId());
@@ -474,8 +453,7 @@ public class SmartClientSmartServerTest extends HttpTestCase {
Repository dst = createBareRepository();
assertFalse(dst.hasObject(A_txt));
- Transport t = Transport.open(dst, brokenURI);
- try {
+ try (Transport t = Transport.open(dst, brokenURI)) {
try {
t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
fail("fetch completed despite upload-pack being broken");
@@ -485,8 +463,6 @@ public class SmartClientSmartServerTest extends HttpTestCase {
+ " received Content-Type text/plain; charset=UTF-8";
assertEquals(exp, err.getMessage());
}
- } finally {
- t.close();
}
List<AccessEvent> requests = getRequests();
@@ -517,12 +493,10 @@ public class SmartClientSmartServerTest extends HttpTestCase {
final RevCommit Q = src.commit().add("Q", Q_txt).create();
final Repository db = src.getRepository();
final String dstName = Constants.R_HEADS + "new.branch";
- Transport t;
// push anonymous shouldn't be allowed.
//
- t = Transport.open(db, remoteURI);
- try {
+ try (Transport t = Transport.open(db, remoteURI)) {
final String srcExpr = Q.name();
final boolean forceUpdate = false;
final String localName = null;
@@ -538,8 +512,6 @@ public class SmartClientSmartServerTest extends HttpTestCase {
+ JGitText.get().authenticationNotSupported;
assertEquals(exp, e.getMessage());
}
- } finally {
- t.close();
}
List<AccessEvent> requests = getRequests();
@@ -560,12 +532,10 @@ public class SmartClientSmartServerTest extends HttpTestCase {
final RevCommit Q = src.commit().add("Q", Q_txt).create();
final Repository db = src.getRepository();
final String dstName = Constants.R_HEADS + "new.branch";
- Transport t;
enableReceivePack();
- t = Transport.open(db, remoteURI);
- try {
+ try (Transport t = Transport.open(db, remoteURI)) {
final String srcExpr = Q.name();
final boolean forceUpdate = false;
final String localName = null;
@@ -574,8 +544,6 @@ public class SmartClientSmartServerTest extends HttpTestCase {
RemoteRefUpdate u = new RemoteRefUpdate(src.getRepository(),
srcExpr, dstName, forceUpdate, localName, oldId);
t.push(NullProgressMonitor.INSTANCE, Collections.singleton(u));
- } finally {
- t.close();
}
assertTrue(remoteRepository.hasObject(Q_txt));
@@ -633,7 +601,6 @@ public class SmartClientSmartServerTest extends HttpTestCase {
final RevCommit Q = src.commit().add("Q", Q_bin).create();
final Repository db = src.getRepository();
final String dstName = Constants.R_HEADS + "new.branch";
- Transport t;
enableReceivePack();
@@ -642,8 +609,7 @@ public class SmartClientSmartServerTest extends HttpTestCase {
cfg.setInt("http", null, "postbuffer", 8 * 1024);
cfg.save();
- t = Transport.open(db, remoteURI);
- try {
+ try (Transport t = Transport.open(db, remoteURI)) {
final String srcExpr = Q.name();
final boolean forceUpdate = false;
final String localName = null;
@@ -652,8 +618,6 @@ public class SmartClientSmartServerTest extends HttpTestCase {
RemoteRefUpdate u = new RemoteRefUpdate(src.getRepository(),
srcExpr, dstName, forceUpdate, localName, oldId);
t.push(NullProgressMonitor.INSTANCE, Collections.singleton(u));
- } finally {
- t.close();
}
assertTrue(remoteRepository.hasObject(Q_bin));