]> source.dussan.org Git - jgit.git/commitdiff
Close ObjectReaders in tests 70/151370/1
authorThomas Wolf <thomas.wolf@paranor.ch>
Mon, 21 Oct 2019 09:38:00 +0000 (11:38 +0200)
committerThomas Wolf <thomas.wolf@paranor.ch>
Mon, 21 Oct 2019 09:39:46 +0000 (11:39 +0200)
Change-Id: Ic839af4f85861fb48091d45679e2a614f001d770
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsInserterTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/CrissCrossMergeTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/EmptyTreeIteratorTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java

index e71ee6d25f74daeff0366d276efb1e83561bf2e9..4d361442126fab3f0f080f8f09490894fdcae178 100644 (file)
@@ -92,18 +92,22 @@ public class DfsInserterTest {
 
        @Test
        public void testReadFromInserterSmallObjects() throws IOException {
-               ObjectInserter ins = db.newObjectInserter();
-               ObjectId id1 = ins.insert(Constants.OBJ_BLOB, Constants.encode("foo"));
-               ObjectId id2 = ins.insert(Constants.OBJ_BLOB, Constants.encode("bar"));
-               assertEquals(0, db.getObjectDatabase().listPacks().size());
+               try (ObjectInserter ins = db.newObjectInserter()) {
+                       ObjectId id1 = ins.insert(Constants.OBJ_BLOB,
+                                       Constants.encode("foo"));
+                       ObjectId id2 = ins.insert(Constants.OBJ_BLOB,
+                                       Constants.encode("bar"));
+                       assertEquals(0, db.getObjectDatabase().listPacks().size());
 
-               ObjectReader reader = ins.newReader();
-               assertSame(ins, reader.getCreatedFromInserter());
-               assertEquals("foo", readString(reader.open(id1)));
-               assertEquals("bar", readString(reader.open(id2)));
-               assertEquals(0, db.getObjectDatabase().listPacks().size());
-               ins.flush();
-               assertEquals(1, db.getObjectDatabase().listPacks().size());
+                       try (ObjectReader reader = ins.newReader()) {
+                               assertSame(ins, reader.getCreatedFromInserter());
+                               assertEquals("foo", readString(reader.open(id1)));
+                               assertEquals("bar", readString(reader.open(id2)));
+                               assertEquals(0, db.getObjectDatabase().listPacks().size());
+                               ins.flush();
+                               assertEquals(1, db.getObjectDatabase().listPacks().size());
+                       }
+               }
        }
 
        @Test
@@ -114,17 +118,19 @@ public class DfsInserterTest {
                        .setBlockLimit(2048));
 
                byte[] data = new TestRng(JGitTestUtil.getName()).nextBytes(8192);
-               DfsInserter ins = (DfsInserter) db.newObjectInserter();
-               ins.setCompressionLevel(Deflater.NO_COMPRESSION);
-               ObjectId id1 = ins.insert(Constants.OBJ_BLOB, data);
-               assertEquals(0, db.getObjectDatabase().listPacks().size());
+               try (DfsInserter ins = (DfsInserter) db.newObjectInserter()) {
+                       ins.setCompressionLevel(Deflater.NO_COMPRESSION);
+                       ObjectId id1 = ins.insert(Constants.OBJ_BLOB, data);
+                       assertEquals(0, db.getObjectDatabase().listPacks().size());
 
-               ObjectReader reader = ins.newReader();
-               assertSame(ins, reader.getCreatedFromInserter());
-               assertTrue(Arrays.equals(data, readStream(reader.open(id1))));
-               assertEquals(0, db.getObjectDatabase().listPacks().size());
-               ins.flush();
+                       try (ObjectReader reader = ins.newReader()) {
+                               assertSame(ins, reader.getCreatedFromInserter());
+                               assertTrue(Arrays.equals(data, readStream(reader.open(id1))));
+                               assertEquals(0, db.getObjectDatabase().listPacks().size());
+                       }
+                       ins.flush();
 
+               }
                List<DfsPackDescription> packs = db.getObjectDatabase().listPacks();
                assertEquals(1, packs.size());
                assertTrue(packs.get(0).getFileSize(PackExt.PACK) > 2048);
@@ -132,48 +138,58 @@ public class DfsInserterTest {
 
        @Test
        public void testReadFromFallback() throws IOException {
-               ObjectInserter ins = db.newObjectInserter();
-               ObjectId id1 = ins.insert(Constants.OBJ_BLOB, Constants.encode("foo"));
-               ins.flush();
-               ObjectId id2 = ins.insert(Constants.OBJ_BLOB, Constants.encode("bar"));
-               assertEquals(1, db.getObjectDatabase().listPacks().size());
-
-               ObjectReader reader = ins.newReader();
-               assertSame(ins, reader.getCreatedFromInserter());
-               assertEquals("foo", readString(reader.open(id1)));
-               assertEquals("bar", readString(reader.open(id2)));
-               assertEquals(1, db.getObjectDatabase().listPacks().size());
-               ins.flush();
-               assertEquals(2, db.getObjectDatabase().listPacks().size());
+               try (ObjectInserter ins = db.newObjectInserter()) {
+                       ObjectId id1 = ins.insert(Constants.OBJ_BLOB,
+                                       Constants.encode("foo"));
+                       ins.flush();
+                       ObjectId id2 = ins.insert(Constants.OBJ_BLOB,
+                                       Constants.encode("bar"));
+                       assertEquals(1, db.getObjectDatabase().listPacks().size());
+
+                       try (ObjectReader reader = ins.newReader()) {
+                               assertSame(ins, reader.getCreatedFromInserter());
+                               assertEquals("foo", readString(reader.open(id1)));
+                               assertEquals("bar", readString(reader.open(id2)));
+                               assertEquals(1, db.getObjectDatabase().listPacks().size());
+                       }
+                       ins.flush();
+                       assertEquals(2, db.getObjectDatabase().listPacks().size());
+               }
        }
 
        @Test
        public void testReaderResolve() throws IOException {
-               ObjectInserter ins = db.newObjectInserter();
-               ObjectId id1 = ins.insert(Constants.OBJ_BLOB, Constants.encode("foo"));
-               ins.flush();
-               ObjectId id2 = ins.insert(Constants.OBJ_BLOB, Constants.encode("bar"));
-               String abbr1 = ObjectId.toString(id1).substring(0, 4);
-               String abbr2 = ObjectId.toString(id2).substring(0, 4);
-               assertFalse(abbr1.equals(abbr2));
-
-               ObjectReader reader = ins.newReader();
-               assertSame(ins, reader.getCreatedFromInserter());
-               Collection<ObjectId> objs;
-               objs = reader.resolve(AbbreviatedObjectId.fromString(abbr1));
-               assertEquals(1, objs.size());
-               assertEquals(id1, objs.iterator().next());
-
-               objs = reader.resolve(AbbreviatedObjectId.fromString(abbr2));
-               assertEquals(1, objs.size());
-               assertEquals(id2, objs.iterator().next());
+               try (ObjectInserter ins = db.newObjectInserter()) {
+                       ObjectId id1 = ins.insert(Constants.OBJ_BLOB,
+                                       Constants.encode("foo"));
+                       ins.flush();
+                       ObjectId id2 = ins.insert(Constants.OBJ_BLOB,
+                                       Constants.encode("bar"));
+                       String abbr1 = ObjectId.toString(id1).substring(0, 4);
+                       String abbr2 = ObjectId.toString(id2).substring(0, 4);
+                       assertFalse(abbr1.equals(abbr2));
+
+                       try (ObjectReader reader = ins.newReader()) {
+                               assertSame(ins, reader.getCreatedFromInserter());
+                               Collection<ObjectId> objs;
+                               objs = reader.resolve(AbbreviatedObjectId.fromString(abbr1));
+                               assertEquals(1, objs.size());
+                               assertEquals(id1, objs.iterator().next());
+
+                               objs = reader.resolve(AbbreviatedObjectId.fromString(abbr2));
+                               assertEquals(1, objs.size());
+                               assertEquals(id2, objs.iterator().next());
+                       }
+               }
        }
 
        @Test
        public void testGarbageSelectivelyVisible() throws IOException {
-               ObjectInserter ins = db.newObjectInserter();
-               ObjectId fooId = ins.insert(Constants.OBJ_BLOB, Constants.encode("foo"));
-               ins.flush();
+               ObjectId fooId;
+               try (ObjectInserter ins = db.newObjectInserter()) {
+                       fooId = ins.insert(Constants.OBJ_BLOB, Constants.encode("foo"));
+                       ins.flush();
+               }
                assertEquals(1, db.getObjectDatabase().listPacks().size());
 
                // Make pack 0 garbage.
@@ -187,36 +203,40 @@ public class DfsInserterTest {
 
        @Test
        public void testInserterIgnoresUnreachable() throws IOException {
-               ObjectInserter ins = db.newObjectInserter();
-               ObjectId fooId = ins.insert(Constants.OBJ_BLOB, Constants.encode("foo"));
-               ins.flush();
-               assertEquals(1, db.getObjectDatabase().listPacks().size());
+               ObjectId fooId;
+               try (ObjectInserter ins = db.newObjectInserter()) {
+                       fooId = ins.insert(Constants.OBJ_BLOB, Constants.encode("foo"));
+                       ins.flush();
+                       assertEquals(1, db.getObjectDatabase().listPacks().size());
 
-               // Make pack 0 garbage.
-               db.getObjectDatabase().listPacks().get(0).setPackSource(PackSource.UNREACHABLE_GARBAGE);
+                       // Make pack 0 garbage.
+                       db.getObjectDatabase().listPacks().get(0)
+                                       .setPackSource(PackSource.UNREACHABLE_GARBAGE);
 
-               // We shouldn't be able to see foo because it's garbage.
-               assertFalse(db.getObjectDatabase().has(fooId, true));
+                       // We shouldn't be able to see foo because it's garbage.
+                       assertFalse(db.getObjectDatabase().has(fooId, true));
 
-               // But if we re-insert foo, it should become visible again.
-               ins.insert(Constants.OBJ_BLOB, Constants.encode("foo"));
-               ins.flush();
+                       // But if we re-insert foo, it should become visible again.
+                       ins.insert(Constants.OBJ_BLOB, Constants.encode("foo"));
+                       ins.flush();
+               }
                assertTrue(db.getObjectDatabase().has(fooId, true));
 
                // Verify that we have a foo in both packs, and 1 of them is garbage.
-               DfsReader reader = new DfsReader(db.getObjectDatabase());
-               DfsPackFile packs[] = db.getObjectDatabase().getPacks();
-               Set<PackSource> pack_sources = new HashSet<>();
+               try (DfsReader reader = new DfsReader(db.getObjectDatabase())) {
+                       DfsPackFile packs[] = db.getObjectDatabase().getPacks();
+                       Set<PackSource> pack_sources = new HashSet<>();
 
-               assertEquals(2, packs.length);
+                       assertEquals(2, packs.length);
 
-               pack_sources.add(packs[0].getPackDescription().getPackSource());
-               pack_sources.add(packs[1].getPackDescription().getPackSource());
+                       pack_sources.add(packs[0].getPackDescription().getPackSource());
+                       pack_sources.add(packs[1].getPackDescription().getPackSource());
 
-               assertTrue(packs[0].hasObject(reader, fooId));
-               assertTrue(packs[1].hasObject(reader, fooId));
-               assertTrue(pack_sources.contains(PackSource.UNREACHABLE_GARBAGE));
-               assertTrue(pack_sources.contains(PackSource.INSERT));
+                       assertTrue(packs[0].hasObject(reader, fooId));
+                       assertTrue(packs[1].hasObject(reader, fooId));
+                       assertTrue(pack_sources.contains(PackSource.UNREACHABLE_GARBAGE));
+                       assertTrue(pack_sources.contains(PackSource.INSERT));
+               }
        }
 
        @Test
@@ -237,17 +257,20 @@ public class DfsInserterTest {
                assertEquals(2, db.getObjectDatabase().listPacks().size());
 
                // Verify that we have a foo in both INSERT packs.
-               DfsReader reader = new DfsReader(db.getObjectDatabase());
-               DfsPackFile packs[] = db.getObjectDatabase().getPacks();
-
-               assertEquals(2, packs.length);
-               DfsPackFile p1 = packs[0];
-               assertEquals(PackSource.INSERT, p1.getPackDescription().getPackSource());
-               assertTrue(p1.hasObject(reader, fooId));
-
-               DfsPackFile p2 = packs[1];
-               assertEquals(PackSource.INSERT, p2.getPackDescription().getPackSource());
-               assertTrue(p2.hasObject(reader, fooId));
+               try (DfsReader reader = new DfsReader(db.getObjectDatabase())) {
+                       DfsPackFile packs[] = db.getObjectDatabase().getPacks();
+
+                       assertEquals(2, packs.length);
+                       DfsPackFile p1 = packs[0];
+                       assertEquals(PackSource.INSERT,
+                                       p1.getPackDescription().getPackSource());
+                       assertTrue(p1.hasObject(reader, fooId));
+
+                       DfsPackFile p2 = packs[1];
+                       assertEquals(PackSource.INSERT,
+                                       p2.getPackDescription().getPackSource());
+                       assertTrue(p2.hasObject(reader, fooId));
+               }
        }
 
        private static String readString(ObjectLoader loader) throws IOException {
index a67c750dbac5eea507a8d1604d312503f12dcb12..5c6636c89e9d1911077ed010bc47985dd5d91f08 100644 (file)
@@ -881,9 +881,9 @@ public class CrissCrossMergeTest extends RepositoryTestCase {
                }
 
                StringBuilder result = new StringBuilder();
-               ObjectReader or = r.newObjectReader();
-               try (BufferedReader br = new BufferedReader(
-                               new InputStreamReader(or.open(blobId).openStream(), UTF_8))) {
+               try (ObjectReader or = r.newObjectReader();
+                               BufferedReader br = new BufferedReader(new InputStreamReader(
+                                               or.open(blobId).openStream(), UTF_8))) {
                        String line;
                        boolean first = true;
                        while ((line = br.readLine()) != null) {
index 58aa608ff5023667ff931e49908382589d4b770b..4deb188784ffc668553770155806f144b14f4699 100644 (file)
@@ -65,12 +65,13 @@ public class EmptyTreeIteratorTest extends RepositoryTestCase {
        @Test
        public void testCreateSubtreeIterator() throws Exception {
                final EmptyTreeIterator etp = new EmptyTreeIterator();
-               final ObjectReader reader = db.newObjectReader();
-               final AbstractTreeIterator sub = etp.createSubtreeIterator(reader);
-               assertNotNull(sub);
-               assertTrue(sub.first());
-               assertTrue(sub.eof());
-               assertTrue(sub instanceof EmptyTreeIterator);
+               try (ObjectReader reader = db.newObjectReader()) {
+                       final AbstractTreeIterator sub = etp.createSubtreeIterator(reader);
+                       assertNotNull(sub);
+                       assertTrue(sub.first());
+                       assertTrue(sub.eof());
+                       assertTrue(sub instanceof EmptyTreeIterator);
+               }
        }
 
        @Test
@@ -121,8 +122,9 @@ public class EmptyTreeIteratorTest extends RepositoryTestCase {
                                called[0] = true;
                        }
                };
-               final ObjectReader reader = db.newObjectReader();
-               parent.createSubtreeIterator(reader).stopWalk();
+               try (ObjectReader reader = db.newObjectReader()) {
+                       parent.createSubtreeIterator(reader).stopWalk();
+               }
                assertTrue(called[0]);
        }
 }
index 3a54ad5b6dcb9ac7e3a2e01462e1e4f697d98255..ffa6e5d252c465e42965a46c120fc1bc86c6dfcf 100644 (file)
@@ -217,29 +217,29 @@ public class FileTreeIteratorTest extends RepositoryTestCase {
                assertFalse(top.eof());
                assertEquals(FileMode.TREE.getBits(), top.mode);
 
-               final ObjectReader reader = db.newObjectReader();
-               final AbstractTreeIterator sub = top.createSubtreeIterator(reader);
-               assertTrue(sub instanceof FileTreeIterator);
-               final FileTreeIterator subfti = (FileTreeIterator) sub;
-               assertTrue(sub.first());
-               assertFalse(sub.eof());
-               assertEquals(paths[2], nameOf(sub));
-               assertEquals(paths[2].length(), subfti.getEntryLength());
-               assertEquals(mtime[2], subfti.getEntryLastModifiedInstant());
-
-               sub.next(1);
-               assertTrue(sub.eof());
-
-               top.next(1);
-               assertFalse(top.first());
-               assertFalse(top.eof());
-               assertEquals(FileMode.REGULAR_FILE.getBits(), top.mode);
-               assertEquals(paths[3], nameOf(top));
-               assertEquals(paths[3].length(), top.getEntryLength());
-               assertEquals(mtime[3], top.getEntryLastModifiedInstant());
-
-               top.next(1);
-               assertTrue(top.eof());
+               try (ObjectReader reader = db.newObjectReader()) {
+                       final AbstractTreeIterator sub = top.createSubtreeIterator(reader);
+                       assertTrue(sub instanceof FileTreeIterator);
+                       final FileTreeIterator subfti = (FileTreeIterator) sub;
+                       assertTrue(sub.first());
+                       assertFalse(sub.eof());
+                       assertEquals(paths[2], nameOf(sub));
+                       assertEquals(paths[2].length(), subfti.getEntryLength());
+                       assertEquals(mtime[2], subfti.getEntryLastModifiedInstant());
+
+                       sub.next(1);
+                       assertTrue(sub.eof());
+                       top.next(1);
+                       assertFalse(top.first());
+                       assertFalse(top.eof());
+                       assertEquals(FileMode.REGULAR_FILE.getBits(), top.mode);
+                       assertEquals(paths[3], nameOf(top));
+                       assertEquals(paths[3].length(), top.getEntryLength());
+                       assertEquals(mtime[3], top.getEntryLastModifiedInstant());
+
+                       top.next(1);
+                       assertTrue(top.eof());
+               }
        }
 
        @Test