From 8ed59c511cf61a23393fabf252a315371a1438f0 Mon Sep 17 00:00:00 2001 From: Jackson Toeniskoetter Date: Thu, 10 Jan 2019 11:51:29 -0800 Subject: [PATCH] Make TestRepository AutoCloseable Currently, unit tests need to either close the Repository underlying a TestRepository manually, or not close it at all. Both are error prone. The TestRepository holds a reference to 4 AutoCloseable objects: Repository, ObjectInserter, Git, and RevWalk. The last two can escape the TestRepository scope, so they are not closed when TestRepository is closed. Change-Id: I4461bb9104d517bd6bef09c38507c7c2ef5c31d4 Signed-off-by: Jackson Toeniskoetter --- .../eclipse/jgit/junit/TestRepository.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java index c9fa2f506..55a7766f6 100644 --- a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java +++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java @@ -112,7 +112,7 @@ import org.eclipse.jgit.util.FileUtils; * @param * type of Repository the test data is stored on. */ -public class TestRepository { +public class TestRepository implements AutoCloseable { /** Constant AUTHOR="J. Author" */ public static final String AUTHOR = "J. Author"; @@ -933,6 +933,23 @@ public class TestRepository { } } + /** + * Closes the underlying {@link Repository} object and any other internal + * resources. + *

+ * {@link AutoCloseable} resources that may escape this object, such as + * those returned by the {@link #git} and {@link #getRevWalk()} methods are + * not closed. + */ + @Override + public void close() { + try { + inserter.close(); + } finally { + db.close(); + } + } + private static void prunePacked(ObjectDirectory odb) throws IOException { for (PackFile p : odb.getPacks()) { for (MutableEntry e : p)