diff options
author | David Pursehouse <david.pursehouse@gmail.com> | 2018-03-13 11:44:23 +0900 |
---|---|---|
committer | Matthias Sohn <matthias.sohn@sap.com> | 2018-03-13 22:16:06 +0100 |
commit | 5c70be00856d5375485e6f062b6e1e09a606601f (patch) | |
tree | 55726d1e2a1c52d75460a0a4336d487238037ba6 /org.eclipse.jgit.http.test/tst/org/eclipse | |
parent | e23b09ad6efc35f6574cfefd4467ad20e5212ff2 (diff) | |
download | jgit-5c70be00856d5375485e6f062b6e1e09a606601f.tar.gz jgit-5c70be00856d5375485e6f062b6e1e09a606601f.zip |
Open auto-closeable resources in try-with-resource
When an auto-closeable resources is not opened in try-with-resource,
the warning "should be managed by try-with-resource" is emitted by
Eclipse.
Fix the ones that can be silenced simply by moving the declaration of
the variable into a try-with-resource.
In cases where we explicitly call the close() method, for example in
tests where we are testing specific behavior caused by the close(),
suppress the warning.
Leave the ones that will require more significant refcactoring to fix.
They can be done in separate commits that can be reviewed and tested
in isolation.
Change-Id: I9682cd20fb15167d3c7f9027cecdc82bc50b83c4
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
Diffstat (limited to 'org.eclipse.jgit.http.test/tst/org/eclipse')
3 files changed, 7 insertions, 19 deletions
diff --git a/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/ProtocolErrorTest.java b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/ProtocolErrorTest.java index 87d0bad85c..a1baae3b26 100644 --- a/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/ProtocolErrorTest.java +++ b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/ProtocolErrorTest.java @@ -149,19 +149,15 @@ public class ProtocolErrorTest extends HttpTestCase { c.setRequestProperty("Content-Type", GitSmartHttpTools.RECEIVE_PACK_REQUEST_TYPE); c.setFixedLengthStreamingMode(reqbin.length); - OutputStream out = c.getOutputStream(); - try { + try (OutputStream out = c.getOutputStream()) { out.write(reqbin); - } finally { - out.close(); } assertEquals(200, c.getResponseCode()); assertEquals(GitSmartHttpTools.RECEIVE_PACK_RESULT_TYPE, c.getContentType()); - InputStream rawin = c.getInputStream(); - try { + try (InputStream rawin = c.getInputStream()) { PacketLineIn pckin = new PacketLineIn(rawin); assertEquals("unpack error " + JGitText.get().packfileIsTruncatedNoParam, @@ -169,8 +165,6 @@ public class ProtocolErrorTest extends HttpTestCase { assertEquals("ng refs/objects/A n/a (unpacker error)", pckin.readString()); assertSame(PacketLineIn.END, pckin.readString()); - } finally { - rawin.close(); } } finally { c.disconnect(); diff --git a/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/SetAdditionalHeadersTest.java b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/SetAdditionalHeadersTest.java index ef8daec31f..fbc54f3872 100644 --- a/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/SetAdditionalHeadersTest.java +++ b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/SetAdditionalHeadersTest.java @@ -107,8 +107,7 @@ public class SetAdditionalHeadersTest extends HttpTestCase { assertEquals("http", remoteURI.getScheme()); - Transport t = Transport.open(dst, remoteURI); - try { + try (Transport t = Transport.open(dst, remoteURI)) { assertTrue("isa TransportHttp", t instanceof TransportHttp); assertTrue("isa HttpTransport", t instanceof HttpTransport); @@ -117,8 +116,6 @@ public class SetAdditionalHeadersTest extends HttpTestCase { headers.put("AnotherKey", "someValue"); ((TransportHttp) t).setAdditionalHeaders(headers); t.openFetch(); - } finally { - t.close(); } List<AccessEvent> requests = getRequests(); 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 6ded21f8cc..d8541aa13d 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 @@ -267,9 +267,9 @@ public class SmartClientSmartServerTest extends HttpTestCase { final HttpServletResponse r = (HttpServletResponse) response; r.setContentType("text/plain"); r.setCharacterEncoding("UTF-8"); - PrintWriter w = r.getWriter(); - w.print("OK"); - w.close(); + try (PrintWriter w = r.getWriter()) { + w.print("OK"); + } } @Override @@ -397,11 +397,8 @@ public class SmartClientSmartServerTest 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(); } } |