From c020848dbb2d583824aa682a354236374e4bfe69 Mon Sep 17 00:00:00 2001 From: Thomas Wolf Date: Mon, 21 Oct 2019 11:38:00 +0200 Subject: Close ObjectReaders in tests Change-Id: Ic839af4f85861fb48091d45679e2a614f001d770 Signed-off-by: Thomas Wolf --- .../jgit/internal/storage/dfs/DfsInserterTest.java | 195 ++++++++++++--------- .../eclipse/jgit/merge/CrissCrossMergeTest.java | 6 +- .../jgit/treewalk/EmptyTreeIteratorTest.java | 18 +- .../jgit/treewalk/FileTreeIteratorTest.java | 46 ++--- 4 files changed, 145 insertions(+), 120 deletions(-) (limited to 'org.eclipse.jgit.test') diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsInserterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsInserterTest.java index e71ee6d25f..4d36144212 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsInserterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsInserterTest.java @@ -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 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 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 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 pack_sources = new HashSet<>(); + try (DfsReader reader = new DfsReader(db.getObjectDatabase())) { + DfsPackFile packs[] = db.getObjectDatabase().getPacks(); + Set 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 { diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/CrissCrossMergeTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/CrissCrossMergeTest.java index a67c750dba..5c6636c89e 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/CrissCrossMergeTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/CrissCrossMergeTest.java @@ -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) { diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/EmptyTreeIteratorTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/EmptyTreeIteratorTest.java index 58aa608ff5..4deb188784 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/EmptyTreeIteratorTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/EmptyTreeIteratorTest.java @@ -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]); } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java index 3a54ad5b6d..ffa6e5d252 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java @@ -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 -- cgit v1.2.3