Skip to content

Commit

Permalink
Fix issue: Auto-linking files with safe character replacements #9267 (#…
Browse files Browse the repository at this point in the history
…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 <u7321615@anu.edu.au>
  • Loading branch information
Rita-Zhou and xianghao-wang committed Oct 30, 2022
1 parent 122c1e8 commit 46b7339
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public List<Path> findAssociatedFiles(BibEntry entry, List<Path> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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<Path> 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<Path> results = fileFinder.findAssociatedFiles(entryWithUnsafeCitationKey, Collections.singletonList(pdfsDir), Collections.singletonList("pdf"));

assertNotEquals(Collections.singletonList(testFile), results);
}
}

0 comments on commit 46b7339

Please sign in to comment.