]> source.dussan.org Git - jgit.git/commitdiff
Fix resource leak in CancellableDigestOutputStreamTest 29/190329/7
authorFabio Ponciroli <ponch78@gmail.com>
Wed, 2 Feb 2022 17:49:50 +0000 (18:49 +0100)
committerMatthias Sohn <matthias.sohn@sap.com>
Wed, 2 Feb 2022 21:50:57 +0000 (22:50 +0100)
CancellableDigestOutputStream resources are never closed in the tests.
This causes a "Resource leak: 'out' is never closed" warning
at compile time.

Suppress it by using a try with resources.

Bug: 578544
Change-Id: I0dc7de9162b8e3ac6fcaabe3002423f545baddb8

org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/io/CancellableDigestOutputStreamTest.java

index 4d94a40e02a80860f9bfe2d37714c801eb04c379..09a7c0b28ae03d67e6c5a3f7a5e23eda02efee25 100644 (file)
@@ -59,46 +59,47 @@ public class CancellableDigestOutputStreamTest {
        @Test
        public void testCancelInProcess() throws Exception {
                CancelledTestMonitor m = new CancelledTestMonitor();
-               CancellableDigestOutputStream out = new CancellableDigestOutputStream(m,
-                               NullOutputStream.INSTANCE);
-
-               byte[] KB = new byte[1024];
-               int triggerCancelWriteCnt = BYTES_TO_WRITE_BEFORE_CANCEL_CHECK
-                               / KB.length;
-               for (int i = 0; i < triggerCancelWriteCnt + 1; i++) {
-                       out.write(KB);
+               try (CancellableDigestOutputStream out = new CancellableDigestOutputStream(
+                               m, NullOutputStream.INSTANCE)) {
+                       byte[] KB = new byte[1024];
+                       int triggerCancelWriteCnt = BYTES_TO_WRITE_BEFORE_CANCEL_CHECK
+                                       / KB.length;
+                       for (int i = 0; i < triggerCancelWriteCnt + 1; i++) {
+                               out.write(KB);
+                       }
+                       assertTrue(out.length() > BYTES_TO_WRITE_BEFORE_CANCEL_CHECK);
+                       m.setCancelled(true);
+
+                       for (int i = 0; i < triggerCancelWriteCnt - 1; i++) {
+                               out.write(KB);
+                       }
+
+                       long lastLength = out.length();
+                       assertThrows(InterruptedIOException.class, () -> {
+                               out.write(1);
+                       });
+                       assertEquals(lastLength, out.length());
+
+                       assertThrows(InterruptedIOException.class, () -> {
+                               out.write(new byte[1]);
+                       });
+                       assertEquals(lastLength, out.length());
                }
-               assertTrue(out.length() > BYTES_TO_WRITE_BEFORE_CANCEL_CHECK);
-               m.setCancelled(true);
-
-               for (int i = 0; i < triggerCancelWriteCnt - 1; i++) {
-                       out.write(KB);
-               }
-
-               long lastLength = out.length();
-               assertThrows(InterruptedIOException.class, () -> {
-                       out.write(1);
-               });
-               assertEquals(lastLength, out.length());
-
-               assertThrows(InterruptedIOException.class, () -> {
-                       out.write(new byte[1]);
-               });
-               assertEquals(lastLength, out.length());
        }
 
        @Test
        public void testTriggerCheckAfterSingleBytes() throws Exception {
                CancelledTestMonitor m = new CancelledTestMonitor();
-               CancellableDigestOutputStream out = new CancellableDigestOutputStream(m,
-                               NullOutputStream.INSTANCE);
+               try (CancellableDigestOutputStream out = new CancellableDigestOutputStream(
+                               m, NullOutputStream.INSTANCE)) {
 
-               byte[] bytes = new byte[BYTES_TO_WRITE_BEFORE_CANCEL_CHECK + 1];
-               m.setCancelled(true);
+                       byte[] bytes = new byte[BYTES_TO_WRITE_BEFORE_CANCEL_CHECK + 1];
+                       m.setCancelled(true);
 
-               assertThrows(InterruptedIOException.class, () -> {
-                       out.write(bytes);
-               });
-               assertEquals(BYTES_TO_WRITE_BEFORE_CANCEL_CHECK, out.length());
+                       assertThrows(InterruptedIOException.class, () -> {
+                               out.write(bytes);
+                       });
+                       assertEquals(BYTES_TO_WRITE_BEFORE_CANCEL_CHECK, out.length());
+               }
        }
 }