Skip to content

Commit 17d4492

Browse files
authored
Add proper closing (#13626)
1 parent bfa37f0 commit 17d4492

File tree

3 files changed

+48
-23
lines changed

3 files changed

+48
-23
lines changed

jablib/src/test/java/org/jabref/logic/git/GitHandlerTest.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010
import org.eclipse.jgit.api.CreateBranchCommand;
1111
import org.eclipse.jgit.api.Git;
1212
import org.eclipse.jgit.api.errors.GitAPIException;
13+
import org.eclipse.jgit.internal.storage.file.WindowCache;
1314
import org.eclipse.jgit.lib.AnyObjectId;
1415
import org.eclipse.jgit.lib.Constants;
16+
import org.eclipse.jgit.lib.RepositoryCache;
1517
import org.eclipse.jgit.revwalk.RevCommit;
18+
import org.eclipse.jgit.storage.file.WindowCacheConfig;
1619
import org.eclipse.jgit.transport.RefSpec;
1720
import org.eclipse.jgit.transport.URIish;
21+
import org.junit.jupiter.api.AfterEach;
1822
import org.junit.jupiter.api.BeforeEach;
1923
import org.junit.jupiter.api.Test;
2024
import org.junit.jupiter.api.io.TempDir;
@@ -35,11 +39,13 @@ class GitHandlerTest {
3539
void setUpGitHandler() throws IOException, GitAPIException, URISyntaxException {
3640
gitHandler = new GitHandler(repositoryPath);
3741

38-
Git remoteGit = Git.init()
42+
try (Git remoteGit = Git.init()
3943
.setBare(true)
4044
.setDirectory(remoteRepoPath.toFile())
4145
.setInitialBranch("main")
42-
.call();
46+
.call()) {
47+
// This ensures the remote repository is initialized and properly closed
48+
}
4349
Path testFile = repositoryPath.resolve("initial.txt");
4450
Files.writeString(testFile, "init");
4551

@@ -65,6 +71,15 @@ void setUpGitHandler() throws IOException, GitAPIException, URISyntaxException {
6571
}
6672
}
6773

74+
@AfterEach
75+
void cleanUp() {
76+
// Required by JGit
77+
// See https://github.com/eclipse-jgit/jgit/issues/155#issuecomment-2765437816 for details
78+
RepositoryCache.clear();
79+
// See https://github.com/eclipse-jgit/jgit/issues/155#issuecomment-3095957214
80+
WindowCache.reconfigure(new WindowCacheConfig());
81+
}
82+
6883
@Test
6984
void checkoutNewBranch() throws IOException, GitAPIException {
7085
gitHandler.checkoutBranch("testBranch");
@@ -96,7 +111,7 @@ void getCurrentlyCheckedOutBranch() throws IOException {
96111
}
97112

98113
@Test
99-
void fetchOnCurrentBranch() throws IOException, GitAPIException, URISyntaxException {
114+
void fetchOnCurrentBranch() throws IOException, GitAPIException {
100115
try (Git cloneGit = Git.cloneRepository()
101116
.setURI(remoteRepoPath.toUri().toString())
102117
.setDirectory(clonePath.toFile())

jablib/src/test/java/org/jabref/logic/git/GitSyncServiceTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121
import org.jabref.model.entry.field.StandardField;
2222

2323
import org.eclipse.jgit.api.Git;
24+
import org.eclipse.jgit.internal.storage.file.WindowCache;
2425
import org.eclipse.jgit.lib.ConfigConstants;
2526
import org.eclipse.jgit.lib.Constants;
2627
import org.eclipse.jgit.lib.PersonIdent;
28+
import org.eclipse.jgit.lib.RepositoryCache;
2729
import org.eclipse.jgit.lib.StoredConfig;
2830
import org.eclipse.jgit.revwalk.RevCommit;
31+
import org.eclipse.jgit.storage.file.WindowCacheConfig;
2932
import org.eclipse.jgit.transport.RefSpec;
3033
import org.eclipse.jgit.transport.URIish;
3134
import org.junit.jupiter.api.AfterEach;
@@ -43,12 +46,16 @@
4346

4447
class GitSyncServiceTest {
4548
private Path library;
46-
private Path remoteDir;
49+
50+
// Class variables for debugging purposes
4751
private Path aliceDir;
4852
private Path bobDir;
53+
private Path remoteDir;
54+
4955
private Git aliceGit;
5056
private Git bobGit;
5157
private Git remoteGit;
58+
5259
private ImportFormatPreferences importFormatPreferences;
5360
private GitConflictResolverStrategy gitConflictResolverStrategy;
5461
private GitSemanticMergeExecutor mergeExecutor;
@@ -189,6 +196,11 @@ void cleanup() {
189196
if (remoteGit != null) {
190197
remoteGit.close();
191198
}
199+
// Required by JGit
200+
// See https://github.com/eclipse-jgit/jgit/issues/155#issuecomment-2765437816 for details
201+
RepositoryCache.clear();
202+
// See https://github.com/eclipse-jgit/jgit/issues/155#issuecomment-3095957214
203+
WindowCache.reconfigure(new WindowCacheConfig());
192204
}
193205

194206
@Test
@@ -240,7 +252,7 @@ void pushTriggersMergeAndPushWhenNoConflicts() throws Exception {
240252
}
241253

242254
@Test
243-
void mergeConflictOnSameFieldTriggersDialogAndUsesUserResolution(@TempDir Path tempDir) throws Exception {
255+
void mergeConflictOnSameFieldTriggersDialogAndUsesUserResolution() throws Exception {
244256
Path bobLibrary = bobDir.resolve("library.bib");
245257
String bobEntry = """
246258
@article{b,

jablib/src/test/java/org/jabref/logic/git/status/GitStatusCheckerTest.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,18 @@ void upToDateStatusAfterInitialSync() {
137137
@Test
138138
void behindStatusWhenRemoteHasNewCommit(@TempDir Path tempDir) throws Exception {
139139
Path remoteWork = tempDir.resolve("remoteWork");
140-
Git remoteClone = Git.cloneRepository()
140+
try (Git remoteClone = Git.cloneRepository()
141141
.setURI(remoteGit.getRepository().getDirectory().toURI().toString())
142142
.setDirectory(remoteWork.toFile())
143143
.setBranchesToClone(List.of("refs/heads/main"))
144144
.setBranch("main")
145-
.call();
146-
Path remoteFile = remoteWork.resolve("library.bib");
147-
commitFile(remoteClone, remoteUpdatedContent, "Remote update");
148-
remoteClone.push()
149-
.setRemote("origin")
150-
.setRefSpecs(new RefSpec("refs/heads/main:refs/heads/main"))
151-
.call();
152-
145+
.call()) {
146+
commitFile(remoteClone, remoteUpdatedContent, "Remote update");
147+
remoteClone.push()
148+
.setRemote("origin")
149+
.setRefSpecs(new RefSpec("refs/heads/main:refs/heads/main"))
150+
.call();
151+
}
153152
localGit.fetch().setRemote("origin").call();
154153
GitStatusSnapshot snapshot = GitStatusChecker.checkStatus(localLibrary);
155154
assertEquals(SyncStatus.BEHIND, snapshot.syncStatus());
@@ -167,19 +166,18 @@ void divergedStatusWhenBothSidesHaveCommits(@TempDir Path tempDir) throws Except
167166
commitFile(localGit, localUpdatedContent, "Local update");
168167

169168
Path remoteWork = tempDir.resolve("remoteWork");
170-
Git remoteClone = Git.cloneRepository()
169+
try (Git remoteClone = Git.cloneRepository()
171170
.setURI(remoteGit.getRepository().getDirectory().toURI().toString())
172171
.setDirectory(remoteWork.toFile())
173172
.setBranchesToClone(List.of("refs/heads/main"))
174173
.setBranch("main")
175-
.call();
176-
Path remoteFile = remoteWork.resolve("library.bib");
177-
commitFile(remoteClone, remoteUpdatedContent, "Remote update");
178-
remoteClone.push()
179-
.setRemote("origin")
180-
.setRefSpecs(new RefSpec("refs/heads/main:refs/heads/main"))
181-
.call();
182-
174+
.call()) {
175+
commitFile(remoteClone, remoteUpdatedContent, "Remote update");
176+
remoteClone.push()
177+
.setRemote("origin")
178+
.setRefSpecs(new RefSpec("refs/heads/main:refs/heads/main"))
179+
.call();
180+
}
183181
localGit.fetch().setRemote("origin").call();
184182
GitStatusSnapshot snapshot = GitStatusChecker.checkStatus(localLibrary);
185183
assertEquals(SyncStatus.DIVERGED, snapshot.syncStatus());

0 commit comments

Comments
 (0)