From 46b73391c4d7f0d8ffd3f44dd05397648251bc3e Mon Sep 17 00:00:00 2001 From: Rita-Zhou <109927532+Rita-Zhou@users.noreply.github.com> Date: Mon, 31 Oct 2022 06:33:58 +1100 Subject: [PATCH] Fix issue: Auto-linking files with safe character replacements #9267 (#9316) * fix the safe character replacement and auto-linking the files * Test the unsafe citation key attach the file. * Change to citation key filter to the existing FileNameCleaner * Add tests for "exact" and "startsWith" matches of auto-linking files Co-authored-by: Xianghao Wang --- CHANGELOG.md | 1 + .../util/io/CitationKeyBasedFileFinder.java | 2 +- .../io/CitationKeyBasedFileFinderTest.java | 27 +++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2934a1160b6..f6434f3742d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -78,6 +78,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve - We fixed an issue where hitting enter on the search field within the preferences dialog closed the dialog. [koppor#630](https://github.com/koppor/jabref/issues/630) - We fixed a typo within a connection error message. [koppor#625](https://github.com/koppor/jabref/issues/625) - We fixed an issue where the 'close dialog' key binding was not closing the Preferences dialog. [#8888](https://github.com/jabref/jabref/issues/8888) +- We fixed an issue when using an unsafe character in the citation key, the auto-linking feature fails to link files. [#9267](https://github.com/JabRef/jabref/issues/9267) ### Removed diff --git a/src/main/java/org/jabref/logic/util/io/CitationKeyBasedFileFinder.java b/src/main/java/org/jabref/logic/util/io/CitationKeyBasedFileFinder.java index 27f71f6b530..81746101a40 100644 --- a/src/main/java/org/jabref/logic/util/io/CitationKeyBasedFileFinder.java +++ b/src/main/java/org/jabref/logic/util/io/CitationKeyBasedFileFinder.java @@ -66,7 +66,7 @@ public List findAssociatedFiles(BibEntry entry, List directories, Li } private boolean matches(String filename, String citeKey) { - boolean startsWithKey = filename.startsWith(citeKey); + boolean startsWithKey = filename.startsWith(FileNameCleaner.cleanFileName(citeKey)); if (startsWithKey) { // The file name starts with the key, that's already a good start // However, we do not want to match "JabRefa" for "JabRef" since this is probably a file belonging to another entry published in the same time / same name diff --git a/src/test/java/org/jabref/logic/util/io/CitationKeyBasedFileFinderTest.java b/src/test/java/org/jabref/logic/util/io/CitationKeyBasedFileFinderTest.java index edb0bfaed3a..3f29211f4e0 100644 --- a/src/test/java/org/jabref/logic/util/io/CitationKeyBasedFileFinderTest.java +++ b/src/test/java/org/jabref/logic/util/io/CitationKeyBasedFileFinderTest.java @@ -15,6 +15,7 @@ import org.junit.jupiter.api.io.TempDir; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; class CitationKeyBasedFileFinderTest { @@ -99,4 +100,30 @@ void findAssociatedFilesInNonExistingDirectoryFindsNothing() throws Exception { assertEquals(Collections.emptyList(), results); } + + @Test + void findAssociatedFilesWithUnsafeCharactersStartWithSearch() throws Exception { + BibEntry entryWithUnsafeCitationKey = new BibEntry(StandardEntryType.Article); + entryWithUnsafeCitationKey.setCitationKey("?test"); + + Path testFile = Files.createFile(pdfsDir.resolve("_test_file.pdf")); + FileFinder fileFinder = new CitationKeyBasedFileFinder(false); + + List results = fileFinder.findAssociatedFiles(entryWithUnsafeCitationKey, Collections.singletonList(pdfsDir), Collections.singletonList("pdf")); + + assertEquals(Collections.singletonList(testFile), results); + } + + @Test + void findAssociatedFilesWithUnsafeCharactersExactSearch() throws Exception { + BibEntry entryWithUnsafeCitationKey = new BibEntry(StandardEntryType.Article); + entryWithUnsafeCitationKey.setCitationKey("test:test/*test?"); + + Path testFile = Files.createFile(pdfsDir.resolve("test_test__test_.pdf")); + FileFinder fileFinder = new CitationKeyBasedFileFinder(true); + + List results = fileFinder.findAssociatedFiles(entryWithUnsafeCitationKey, Collections.singletonList(pdfsDir), Collections.singletonList("pdf")); + + assertNotEquals(Collections.singletonList(testFile), results); + } }