Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue: Auto-linking files with safe character replacements #9267 #9316

Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,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);
}
}