From 95300c29556b30c7139c8fade43be40604e7015f Mon Sep 17 00:00:00 2001 From: Jonatan Asketorp <2598631+k3KAW8Pnf7mkmdSMPHz27@users.noreply.github.com> Date: Tue, 12 Jan 2021 19:09:06 -0500 Subject: [PATCH 01/17] Add test case --- .../logic/util/io/RegExpBasedFileFinderTests.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java b/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java index 0cc53fdbc37..3cc9101fe7e 100644 --- a/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java +++ b/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java @@ -80,6 +80,19 @@ void testYearAuthFirstPageFindFiles() throws Exception { result); } + @Test + void findAssociatedFilesContainingRegexpFromBracketedExpression() throws Exception { + var bibEntry = new BibEntry().withField(StandardField.TITLE, "Regexp from [A-Z]"); + + var extension = Collections.singletonList("pdf"); + var directory = Collections.singletonList(Path.of(FILES_DIRECTORY)); + RegExpBasedFileFinder fileFinder = new RegExpBasedFileFinder("**/[TITLE]\\\\.[extension]", ','); + + List result = fileFinder.findAssociatedFiles(bibEntry, directory, extension); + assertEquals(Collections.singletonList(Path.of("src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/directory/subdirectory/2003_Hippel_209.pdf")), + result); + } + @Test void testAuthorWithDiacritics() throws Exception { // given From b7a394f55a484aec81bf266ed59be543bc44ac14 Mon Sep 17 00:00:00 2001 From: Jonatan Asketorp <2598631+k3KAW8Pnf7mkmdSMPHz27@users.noreply.github.com> Date: Tue, 12 Jan 2021 19:42:39 -0500 Subject: [PATCH 02/17] Fix test case --- .../logic/util/io/RegExpBasedFileFinderTests.java | 4 ++-- .../unlinkedFilesTestFolder/Regexp from [A-Z].pdf | Bin 2 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/Regexp from [A-Z].pdf diff --git a/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java b/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java index 3cc9101fe7e..10002289c85 100644 --- a/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java +++ b/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java @@ -86,10 +86,10 @@ void findAssociatedFilesContainingRegexpFromBracketedExpression() throws Excepti var extension = Collections.singletonList("pdf"); var directory = Collections.singletonList(Path.of(FILES_DIRECTORY)); - RegExpBasedFileFinder fileFinder = new RegExpBasedFileFinder("**/[TITLE]\\\\.[extension]", ','); + RegExpBasedFileFinder fileFinder = new RegExpBasedFileFinder("[TITLE]\\\\.[extension]", ','); List result = fileFinder.findAssociatedFiles(bibEntry, directory, extension); - assertEquals(Collections.singletonList(Path.of("src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/directory/subdirectory/2003_Hippel_209.pdf")), + assertEquals(Collections.singletonList(Path.of("src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/Regexp from [A-Z].pdf")), result); } diff --git a/src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/Regexp from [A-Z].pdf b/src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/Regexp from [A-Z].pdf new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 From 419c4d0bc360197dfa09c90a1d21cd4662ccae21 Mon Sep 17 00:00:00 2001 From: Jonatan Asketorp <2598631+k3KAW8Pnf7mkmdSMPHz27@users.noreply.github.com> Date: Wed, 13 Jan 2021 08:57:18 -0500 Subject: [PATCH 03/17] Change access modifier This avoid replicating the exact same behaviour in RegExpBasedFileFinder --- .../org/jabref/logic/citationkeypattern/BracketedPattern.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java b/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java index 0e5a7d4a518..b4d4c203be1 100644 --- a/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java +++ b/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java @@ -187,7 +187,7 @@ public static String expandBrackets(String pattern, Character keywordDelimiter, * @param database The {@link BibDatabase} for field resolving. May be null. * @return a function accepting a bracketed expression and returning the result of expanding it */ - private static Function expandBracketContent(Character keywordDelimiter, BibEntry entry, BibDatabase database) { + public static Function expandBracketContent(Character keywordDelimiter, BibEntry entry, BibDatabase database) { return (String bracket) -> { String expandedPattern; List fieldParts = parseFieldAndModifiers(bracket); From 73410f81b0f5a00821ff79504f507516fbe69014 Mon Sep 17 00:00:00 2001 From: Jonatan Asketorp <2598631+k3KAW8Pnf7mkmdSMPHz27@users.noreply.github.com> Date: Wed, 13 Jan 2021 08:57:31 -0500 Subject: [PATCH 04/17] Add regexp safe bracket content expansion For this particular use we must assume that the rest of the text is a regexp, and only put the content of the expanded bracket between quotes. It must return a String as this is only a part of the final regexp that will be compiled later. --- .../logic/util/io/RegExpBasedFileFinder.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java b/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java index cdb2d9a03f9..75df1ed5844 100644 --- a/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java +++ b/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java @@ -11,6 +11,7 @@ import java.util.Collections; import java.util.List; import java.util.function.BiPredicate; +import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; @@ -58,6 +59,16 @@ public static String expandBrackets(String bracketString, BibEntry entry, BibDat return expandedStringBuffer.toString(); } + /** + * Helper function for expanding the content of a bracketed expression to regexp literals. + * + * @param bibEntry the {@link BibEntry} used when expanding the expression + */ + private Function expandBracketContentToRegexpLiterals(BibEntry bibEntry) { + var expandBracketContent = BracketedPattern.expandBracketContent(keywordDelimiter, bibEntry, null); + return bracketContent -> "\\Q" + expandBracketContent.apply(bracketContent) + "\\E"; + } + /** * Method for searching for files using regexp. A list of extensions and directories can be * given. @@ -186,7 +197,9 @@ private List findFile(final BibEntry entry, final Path directory, final St // Last step: check if the given file can be found in this directory String filePart = fileParts[fileParts.length - 1].replace("[extension]", EXT_MARKER); - String filenameToLookFor = expandBrackets(filePart, entry, null, keywordDelimiter).replaceAll(EXT_MARKER, extensionRegExp); + String expandedBracketAsRegexpLiterals = BracketedPattern.expandBrackets(filePart, + expandBracketContentToRegexpLiterals(entry)); + String filenameToLookFor = expandedBracketAsRegexpLiterals.replaceAll(EXT_MARKER, extensionRegExp); try { final Pattern toMatch = Pattern.compile('^' + filenameToLookFor.replaceAll("\\\\\\\\", "\\\\") + '$', From d7ca1a0f3228690225c1c16e6f7bed4481ec6d74 Mon Sep 17 00:00:00 2001 From: Jonatan Asketorp <2598631+k3KAW8Pnf7mkmdSMPHz27@users.noreply.github.com> Date: Thu, 14 Jan 2021 17:19:49 -0500 Subject: [PATCH 05/17] Add matching of file names generated by JabRef --- .../logic/util/io/RegExpBasedFileFinder.java | 69 ++++++++++--------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java b/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java index 75df1ed5844..17ff1646127 100644 --- a/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java +++ b/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java @@ -8,7 +8,6 @@ import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.function.BiPredicate; import java.util.function.Function; @@ -23,8 +22,13 @@ import org.jabref.model.entry.BibEntry; import org.jabref.model.strings.StringUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + class RegExpBasedFileFinder implements FileFinder { + private static final Logger LOGGER = LoggerFactory.getLogger(RegExpBasedFileFinder.class); + private static final String EXT_MARKER = "__EXTENSION__"; private static final Pattern ESCAPE_PATTERN = Pattern.compile("([^\\\\])\\\\([^\\\\])"); @@ -59,14 +63,33 @@ public static String expandBrackets(String bracketString, BibEntry entry, BibDat return expandedStringBuffer.toString(); } - /** - * Helper function for expanding the content of a bracketed expression to regexp literals. - * - * @param bibEntry the {@link BibEntry} used when expanding the expression - */ - private Function expandBracketContentToRegexpLiterals(BibEntry bibEntry) { - var expandBracketContent = BracketedPattern.expandBracketContent(keywordDelimiter, bibEntry, null); - return bracketContent -> "\\Q" + expandBracketContent.apply(bracketContent) + "\\E"; + private Pattern createFileNamePattern(String[] fileParts, String extensionRegExp, BibEntry entry) throws IOException { + // Last step: check if the given file can be found in this directory + // Protect the extension marker so that it isn't treated as a bracketed pattern + String filePart = fileParts[fileParts.length - 1].replace("[extension]", EXT_MARKER); + + // expandBracketContent is the default function for expanding the content of a bracketed expression [field:modifier] + Function expandBracket = BracketedPattern.expandBracketContent(keywordDelimiter, entry, null); + // we want to post-process the content so that it can be used as a regex for finding a file name + Function bracketToFileNameRegex = expandBracket.andThen(RegExpBasedFileFinder::toFileNameRegex); + + String expandedBracketAsRegexpLiterals = BracketedPattern.expandBrackets(filePart, bracketToFileNameRegex); + + String fileNamePattern = expandedBracketAsRegexpLiterals + .replaceAll(EXT_MARKER, extensionRegExp) // Replace the extension marker + .replaceAll("\\\\\\\\", "\\\\"); + try { + return Pattern.compile('^' + fileNamePattern + '$', Pattern.CASE_INSENSITIVE); + } catch (PatternSyntaxException e) { + LOGGER.warn("There is a syntax error in the regular expression \"{}\" used to search for a file", fileNamePattern, e); + throw new IOException("There is a syntax error in the regular expression used to search for files", e); + } + } + + private static String toFileNameRegex(String content) { + var cleanedContent = FileNameCleaner.cleanFileName(content); + return content.equals(cleanedContent) ? Pattern.quote(content) : + "(" + Pattern.quote(content) + ")|(" + Pattern.quote(cleanedContent) + ")"; } /** @@ -195,30 +218,14 @@ private List findFile(final BibEntry entry, final Path directory, final St } // End process directory information } - // Last step: check if the given file can be found in this directory - String filePart = fileParts[fileParts.length - 1].replace("[extension]", EXT_MARKER); - String expandedBracketAsRegexpLiterals = BracketedPattern.expandBrackets(filePart, - expandBracketContentToRegexpLiterals(entry)); - String filenameToLookFor = expandedBracketAsRegexpLiterals.replaceAll(EXT_MARKER, extensionRegExp); - - try { - final Pattern toMatch = Pattern.compile('^' + filenameToLookFor.replaceAll("\\\\\\\\", "\\\\") + '$', - Pattern.CASE_INSENSITIVE); - BiPredicate matcher = (path, attributes) -> toMatch.matcher(path.getFileName().toString()).matches(); - resultFiles.addAll(collectFilesWithMatcher(actualDirectory, matcher)); - } catch (UncheckedIOException | PatternSyntaxException e) { - throw new IOException("Could not look for " + filenameToLookFor, e); - } - - return resultFiles; - } - - private List collectFilesWithMatcher(Path actualDirectory, BiPredicate matcher) { + Pattern toMatch = createFileNamePattern(fileParts, extensionRegExp, entry); + BiPredicate matcher = (path, attributes) -> toMatch.matcher(path.getFileName().toString()).matches(); try (Stream pathStream = Files.find(actualDirectory, 1, matcher, FileVisitOption.FOLLOW_LINKS)) { - return pathStream.collect(Collectors.toList()); - } catch (UncheckedIOException | IOException ioe) { - return Collections.emptyList(); + resultFiles.addAll(pathStream.collect(Collectors.toList())); + } catch (UncheckedIOException uncheckedIOException) { + throw new IOException(uncheckedIOException); } + return resultFiles; } private boolean isSubDirectory(Path rootDirectory, Path path) { From 81547bdddab304f7e9a4f344eb39b489ff5981cc Mon Sep 17 00:00:00 2001 From: Jonatan Asketorp <2598631+k3KAW8Pnf7mkmdSMPHz27@users.noreply.github.com> Date: Thu, 14 Jan 2021 17:44:07 -0500 Subject: [PATCH 06/17] Remove unnecessary logging --- .../org/jabref/logic/util/io/RegExpBasedFileFinder.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java b/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java index 17ff1646127..b6afdbbfcc4 100644 --- a/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java +++ b/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java @@ -22,13 +22,8 @@ import org.jabref.model.entry.BibEntry; import org.jabref.model.strings.StringUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - class RegExpBasedFileFinder implements FileFinder { - private static final Logger LOGGER = LoggerFactory.getLogger(RegExpBasedFileFinder.class); - private static final String EXT_MARKER = "__EXTENSION__"; private static final Pattern ESCAPE_PATTERN = Pattern.compile("([^\\\\])\\\\([^\\\\])"); @@ -81,8 +76,7 @@ private Pattern createFileNamePattern(String[] fileParts, String extensionRegExp try { return Pattern.compile('^' + fileNamePattern + '$', Pattern.CASE_INSENSITIVE); } catch (PatternSyntaxException e) { - LOGGER.warn("There is a syntax error in the regular expression \"{}\" used to search for a file", fileNamePattern, e); - throw new IOException("There is a syntax error in the regular expression used to search for files", e); + throw new IOException(String.format("There is a syntax error in the regular expression %s used to search for files", fileNamePattern), e); } } From 37d08ea2db6bead1c3b0283431de18c54a6f1d7e Mon Sep 17 00:00:00 2001 From: Jonatan Asketorp <2598631+k3KAW8Pnf7mkmdSMPHz27@users.noreply.github.com> Date: Fri, 15 Jan 2021 09:58:17 -0500 Subject: [PATCH 07/17] Remove unused bracketed pattern expanding --- .../logic/util/io/RegExpBasedFileFinder.java | 22 -------------- .../util/io/RegExpBasedFileFinderTests.java | 30 ------------------- 2 files changed, 52 deletions(-) diff --git a/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java b/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java index b6afdbbfcc4..0843d5fb17e 100644 --- a/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java +++ b/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java @@ -18,7 +18,6 @@ import java.util.stream.Stream; import org.jabref.logic.citationkeypattern.BracketedPattern; -import org.jabref.model.database.BibDatabase; import org.jabref.model.entry.BibEntry; import org.jabref.model.strings.StringUtil; @@ -28,7 +27,6 @@ class RegExpBasedFileFinder implements FileFinder { private static final Pattern ESCAPE_PATTERN = Pattern.compile("([^\\\\])\\\\([^\\\\])"); - private static final Pattern SQUARE_BRACKETS_PATTERN = Pattern.compile("\\[.*?\\]"); private final String regExp; private final Character keywordDelimiter; @@ -40,24 +38,6 @@ class RegExpBasedFileFinder implements FileFinder { this.keywordDelimiter = keywordDelimiter; } - /** - * Takes a string that contains bracketed expression and expands each of these using getFieldAndFormat. - *

- * Unknown Bracket expressions are silently dropped. - */ - public static String expandBrackets(String bracketString, BibEntry entry, BibDatabase database, - Character keywordDelimiter) { - Matcher matcher = SQUARE_BRACKETS_PATTERN.matcher(bracketString); - StringBuilder expandedStringBuffer = new StringBuilder(); - while (matcher.find()) { - String replacement = BracketedPattern.expandBrackets(matcher.group(), keywordDelimiter, entry, database); - matcher.appendReplacement(expandedStringBuffer, replacement); - } - matcher.appendTail(expandedStringBuffer); - - return expandedStringBuffer.toString(); - } - private Pattern createFileNamePattern(String[] fileParts, String extensionRegExp, BibEntry entry) throws IOException { // Last step: check if the given file can be found in this directory // Protect the extension marker so that it isn't treated as a bracketed pattern @@ -170,9 +150,7 @@ private List findFile(final BibEntry entry, final Path directory, final St } for (int index = 0; index < (fileParts.length - 1); index++) { - String dirToProcess = fileParts[index]; - dirToProcess = expandBrackets(dirToProcess, entry, null, keywordDelimiter); if (dirToProcess.matches("^.:$")) { // Windows Drive Letter actualDirectory = Path.of(dirToProcess + '/'); diff --git a/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java b/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java index 10002289c85..dc4cedff7b9 100644 --- a/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java +++ b/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java @@ -156,34 +156,4 @@ void testFindFileNonRecursive() throws Exception { // then assertTrue(result.isEmpty()); } - - @Test - void testExpandBrackets() { - - assertEquals("", RegExpBasedFileFinder.expandBrackets("", entry, database, ',')); - - assertEquals("dropped", RegExpBasedFileFinder.expandBrackets("drop[unknownkey]ped", entry, database, - ',')); - - assertEquals("Eric von Hippel and Georg von Krogh", - RegExpBasedFileFinder.expandBrackets("[author]", entry, database, ',')); - - assertEquals("Eric von Hippel and Georg von Krogh are two famous authors.", - RegExpBasedFileFinder.expandBrackets("[author] are two famous authors.", entry, database, - ',')); - - assertEquals("Eric von Hippel and Georg von Krogh are two famous authors.", - RegExpBasedFileFinder.expandBrackets("[author] are two famous authors.", entry, database, - ',')); - - assertEquals( - "Eric von Hippel and Georg von Krogh have published Open Source Software and the \"Private-Collective\" Innovation Model: Issues for Organization Science in Organization Science.", - RegExpBasedFileFinder.expandBrackets("[author] have published [fulltitle] in [journal].", entry, database, - ',')); - - assertEquals( - "Eric von Hippel and Georg von Krogh have published Open Source Software and the \"Private Collective\" Innovation Model: Issues for Organization Science in Organization Science.", - RegExpBasedFileFinder.expandBrackets("[author] have published [title] in [journal].", entry, database, - ',')); - } } From 0a86a99d80b5d2d7d56ed2bb33a481033b606ff4 Mon Sep 17 00:00:00 2001 From: Jonatan Asketorp <2598631+k3KAW8Pnf7mkmdSMPHz27@users.noreply.github.com> Date: Fri, 15 Jan 2021 10:28:25 -0500 Subject: [PATCH 08/17] Improve comments and JavaDoc --- .../logic/util/io/RegExpBasedFileFinder.java | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java b/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java index 0843d5fb17e..657bcba0279 100644 --- a/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java +++ b/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java @@ -38,19 +38,23 @@ class RegExpBasedFileFinder implements FileFinder { this.keywordDelimiter = keywordDelimiter; } + /** + * Creates a Pattern that matches the file name corresponding to the last element of {@code fileParts} with any bracketed patterns expanded. + * + * @throws IOException throws an IOException if a PatternSyntaxException occurs + */ private Pattern createFileNamePattern(String[] fileParts, String extensionRegExp, BibEntry entry) throws IOException { - // Last step: check if the given file can be found in this directory // Protect the extension marker so that it isn't treated as a bracketed pattern String filePart = fileParts[fileParts.length - 1].replace("[extension]", EXT_MARKER); - // expandBracketContent is the default function for expanding the content of a bracketed expression [field:modifier] + // We need to supply a custom function to deal with the content of a bracketed expression and expandBracketContent is the default function Function expandBracket = BracketedPattern.expandBracketContent(keywordDelimiter, entry, null); - // we want to post-process the content so that it can be used as a regex for finding a file name + // but, we want to post-process the expanded content so that it can be used as a regex for finding a file name Function bracketToFileNameRegex = expandBracket.andThen(RegExpBasedFileFinder::toFileNameRegex); - String expandedBracketAsRegexpLiterals = BracketedPattern.expandBrackets(filePart, bracketToFileNameRegex); + String expandedBracketAsFileNameRegex = BracketedPattern.expandBrackets(filePart, bracketToFileNameRegex); - String fileNamePattern = expandedBracketAsRegexpLiterals + String fileNamePattern = expandedBracketAsFileNameRegex .replaceAll(EXT_MARKER, extensionRegExp) // Replace the extension marker .replaceAll("\\\\\\\\", "\\\\"); try { @@ -60,10 +64,16 @@ private Pattern createFileNamePattern(String[] fileParts, String extensionRegExp } } - private static String toFileNameRegex(String content) { - var cleanedContent = FileNameCleaner.cleanFileName(content); - return content.equals(cleanedContent) ? Pattern.quote(content) : - "(" + Pattern.quote(content) + ")|(" + Pattern.quote(cleanedContent) + ")"; + /** + * Helper method for both exact matching (if the file name were not created by JabRef) and cleaned file name matching. + * + * @param expandedContent the expanded content of a bracketed expression + * @return a String representation of a regex matching the expanded content and the expanded content cleaned for file name use + */ + private static String toFileNameRegex(String expandedContent) { + var cleanedContent = FileNameCleaner.cleanFileName(expandedContent); + return expandedContent.equals(cleanedContent) ? Pattern.quote(expandedContent) : + "(" + Pattern.quote(expandedContent) + ")|(" + Pattern.quote(cleanedContent) + ")"; } /** @@ -190,6 +200,7 @@ private List findFile(final BibEntry entry, final Path directory, final St } // End process directory information } + // Last step: check if the given file can be found in this directory Pattern toMatch = createFileNamePattern(fileParts, extensionRegExp, entry); BiPredicate matcher = (path, attributes) -> toMatch.matcher(path.getFileName().toString()).matches(); try (Stream pathStream = Files.find(actualDirectory, 1, matcher, FileVisitOption.FOLLOW_LINKS)) { From 7c084234734084ab63febd476a1cece572668e78 Mon Sep 17 00:00:00 2001 From: Jonatan Asketorp <2598631+k3KAW8Pnf7mkmdSMPHz27@users.noreply.github.com> Date: Fri, 15 Jan 2021 10:35:17 -0500 Subject: [PATCH 09/17] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index faf7f5758a9..736c238775e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve - We fixed an issue where the option "Move file to file directory" was disabled in the entry editor for all files [#7194](https://github.com/JabRef/jabref/issues/7194) - We fixed an issue where application dialogs were opening in the wrong display when using multiple screens [#7273](https://github.com/JabRef/jabref/pull/7273) - We fixed an issue where an exception would be displayed for previewing and preferences when a custom theme has been configured but is missing [#7177](https://github.com/JabRef/jabref/issues/7177) +- We fixed an issue where the regex based file search miss-interpreted specific symbols [#4342](https://github.com/JabRef/jabref/issues/4342) ### Removed From 062a2859443b0a78cfb7f123efa2db0ff88a8911 Mon Sep 17 00:00:00 2001 From: Jonatan Asketorp <2598631+k3KAW8Pnf7mkmdSMPHz27@users.noreply.github.com> Date: Sat, 16 Jan 2021 13:03:03 -0500 Subject: [PATCH 10/17] Fix re-throw of wrapped IOException --- .../java/org/jabref/logic/util/io/RegExpBasedFileFinder.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java b/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java index 657bcba0279..8e6b5a2660f 100644 --- a/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java +++ b/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java @@ -195,7 +195,7 @@ private List findFile(final BibEntry entry, final Path directory, final St resultFiles.addAll(findFile(entry, path, restOfFileString, extensionRegExp)); } } catch (UncheckedIOException ioe) { - throw new IOException(ioe); + throw ioe.getCause(); } } // End process directory information } @@ -206,7 +206,7 @@ private List findFile(final BibEntry entry, final Path directory, final St try (Stream pathStream = Files.find(actualDirectory, 1, matcher, FileVisitOption.FOLLOW_LINKS)) { resultFiles.addAll(pathStream.collect(Collectors.toList())); } catch (UncheckedIOException uncheckedIOException) { - throw new IOException(uncheckedIOException); + throw uncheckedIOException.getCause(); } return resultFiles; } From c6ae6bdb4d143df03b688c8dc7e8469eb7f69f9d Mon Sep 17 00:00:00 2001 From: Jonatan Asketorp <2598631+k3KAW8Pnf7mkmdSMPHz27@users.noreply.github.com> Date: Tue, 19 Jan 2021 14:37:29 -0500 Subject: [PATCH 11/17] Refactor test name --- .../org/jabref/logic/util/io/RegExpBasedFileFinderTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java b/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java index dc4cedff7b9..f695988dc28 100644 --- a/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java +++ b/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java @@ -81,7 +81,7 @@ void testYearAuthFirstPageFindFiles() throws Exception { } @Test - void findAssociatedFilesContainingRegexpFromBracketedExpression() throws Exception { + void findAssociatedFilesContainingBracketsFromBracketedExpression() throws Exception { var bibEntry = new BibEntry().withField(StandardField.TITLE, "Regexp from [A-Z]"); var extension = Collections.singletonList("pdf"); From 6ce3e8acb7dfeb080c0b5cbcbc278a8ff8c8bc25 Mon Sep 17 00:00:00 2001 From: Jonatan Asketorp <2598631+k3KAW8Pnf7mkmdSMPHz27@users.noreply.github.com> Date: Tue, 19 Jan 2021 14:40:47 -0500 Subject: [PATCH 12/17] Refactor test name --- .../org/jabref/logic/util/io/RegExpBasedFileFinderTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java b/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java index f695988dc28..9bd0af9fdc7 100644 --- a/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java +++ b/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java @@ -81,7 +81,7 @@ void testYearAuthFirstPageFindFiles() throws Exception { } @Test - void findAssociatedFilesContainingBracketsFromBracketedExpression() throws Exception { + void findAssociatedFilesFindFileContainingBracketsFromBracketedExpression() throws Exception { var bibEntry = new BibEntry().withField(StandardField.TITLE, "Regexp from [A-Z]"); var extension = Collections.singletonList("pdf"); From beae66c7969af36fcf52fdc5031c1ede34931266 Mon Sep 17 00:00:00 2001 From: Jonatan Asketorp <2598631+k3KAW8Pnf7mkmdSMPHz27@users.noreply.github.com> Date: Wed, 20 Jan 2021 09:18:50 -0500 Subject: [PATCH 13/17] Add user submitted test case --- .../util/io/RegExpBasedFileFinderTests.java | 17 +++++++++++++++++ ...oelectric Metal Organic Framework (MOF).pdf | Bin 2 files changed, 17 insertions(+) create mode 100644 src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/directory/subdirectory/GUO ea - INORG CHEM COMMUN 2010 - Ferroelectric Metal Organic Framework (MOF).pdf diff --git a/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java b/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java index 9bd0af9fdc7..a45edeee6f5 100644 --- a/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java +++ b/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java @@ -93,6 +93,23 @@ void findAssociatedFilesFindFileContainingBracketsFromBracketedExpression() thro result); } + @Test + void findAssociatedFilesFindFileContainingParenthesizesFromBracketedExpression() throws Exception { + var bibEntry = new BibEntry().withCitationKey("Guo_ICC_2010") + .withField(StandardField.TITLE, "Ferroelectric Metal Organic Framework (MOF)") + .withField(StandardField.AUTHOR, "Guo, M. and Cai, H.-L. and Xiong, R.-G.") + .withField(StandardField.JOURNAL, "Inorganic Chemistry Communications") + .withField(StandardField.YEAR, "2010"); + + var extension = Collections.singletonList("pdf"); + var directory = Collections.singletonList(Path.of(FILES_DIRECTORY)); + RegExpBasedFileFinder fileFinder = new RegExpBasedFileFinder("**/.*[TITLE].*\\\\.[extension]", ','); + + List result = fileFinder.findAssociatedFiles(bibEntry, directory, extension); + assertEquals(Collections.singletonList(Path.of("src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/directory/subdirectory/GUO ea - INORG CHEM COMMUN 2010 - Ferroelectric Metal Organic Framework (MOF).pdf")), + result); + } + @Test void testAuthorWithDiacritics() throws Exception { // given diff --git a/src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/directory/subdirectory/GUO ea - INORG CHEM COMMUN 2010 - Ferroelectric Metal Organic Framework (MOF).pdf b/src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/directory/subdirectory/GUO ea - INORG CHEM COMMUN 2010 - Ferroelectric Metal Organic Framework (MOF).pdf new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 From ccafaa275e2ac4dec1930a82bd164ff8d09c0182 Mon Sep 17 00:00:00 2001 From: Jonatan Asketorp <2598631+k3KAW8Pnf7mkmdSMPHz27@users.noreply.github.com> Date: Wed, 20 Jan 2021 09:27:14 -0500 Subject: [PATCH 14/17] Add previously RegExpBasedFileFinder tests --- .../BracketedPatternTest.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java b/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java index bbc0e6eb3ea..16b0ebe59f8 100644 --- a/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java +++ b/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java @@ -324,4 +324,44 @@ void expandBracketsLastNameWithChineseCharacters() { assertEquals("杨秀群", BracketedPattern.expandBrackets("[auth]", null, bibEntry, null)); } + + @Test + void expandBracketsWithTestCasesFromRegExpBasedFileFinder() { + BibEntry entry = new BibEntry(StandardEntryType.Article).withCitationKey("HipKro03"); + entry.setField(StandardField.AUTHOR, "Eric von Hippel and Georg von Krogh"); + entry.setField(StandardField.TITLE, "Open Source Software and the \"Private-Collective\" Innovation Model: Issues for Organization Science"); + entry.setField(StandardField.JOURNAL, "Organization Science"); + entry.setField(StandardField.YEAR, "2003"); + entry.setField(StandardField.VOLUME, "14"); + entry.setField(StandardField.PAGES, "209--223"); + entry.setField(StandardField.NUMBER, "2"); + entry.setField(StandardField.ADDRESS, "Institute for Operations Research and the Management Sciences (INFORMS), Linthicum, Maryland, USA"); + entry.setField(StandardField.DOI, "http://dx.doi.org/10.1287/orsc.14.2.209.14992"); + entry.setField(StandardField.ISSN, "1526-5455"); + entry.setField(StandardField.PUBLISHER, "INFORMS"); + + BibDatabase database = new BibDatabase(); + database.insertEntry(entry); + + assertEquals("", BracketedPattern.expandBrackets("", ',', entry, database)); + + assertEquals("dropped", BracketedPattern.expandBrackets("drop[unknownkey]ped", ',', entry, database)); + + assertEquals("Eric von Hippel and Georg von Krogh", + BracketedPattern.expandBrackets("[author]", ',', entry, database)); + + assertEquals("Eric von Hippel and Georg von Krogh are two famous authors.", + BracketedPattern.expandBrackets("[author] are two famous authors.", ',', entry, database)); + + assertEquals("Eric von Hippel and Georg von Krogh are two famous authors.", + BracketedPattern.expandBrackets("[author] are two famous authors.", ',', entry, database)); + + assertEquals( + "Eric von Hippel and Georg von Krogh have published Open Source Software and the \"Private-Collective\" Innovation Model: Issues for Organization Science in Organization Science.", + BracketedPattern.expandBrackets("[author] have published [fulltitle] in [journal].", ',', entry, database)); + + assertEquals( + "Eric von Hippel and Georg von Krogh have published Open Source Software and the \"Private Collective\" Innovation Model: Issues for Organization Science in Organization Science.", + BracketedPattern.expandBrackets("[author] have published [title] in [journal].", ',', entry, database)); + } } From c43875e2eca36ef0d0a2b121884c950b431ebd76 Mon Sep 17 00:00:00 2001 From: Jonatan Asketorp <2598631+k3KAW8Pnf7mkmdSMPHz27@users.noreply.github.com> Date: Wed, 20 Jan 2021 09:33:07 -0500 Subject: [PATCH 15/17] Add comment about removal of returned empty list --- .../java/org/jabref/logic/util/io/RegExpBasedFileFinder.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java b/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java index 8e6b5a2660f..01bcbbf0ba8 100644 --- a/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java +++ b/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java @@ -206,6 +206,7 @@ private List findFile(final BibEntry entry, final Path directory, final St try (Stream pathStream = Files.find(actualDirectory, 1, matcher, FileVisitOption.FOLLOW_LINKS)) { resultFiles.addAll(pathStream.collect(Collectors.toList())); } catch (UncheckedIOException uncheckedIOException) { + // Previously, an empty list were returned here on both IOException and UncheckedIOException throw uncheckedIOException.getCause(); } return resultFiles; From 7af85e2c9e7a732aa90c0e2e35f83768a6dff3c5 Mon Sep 17 00:00:00 2001 From: Jonatan Asketorp <2598631+k3KAW8Pnf7mkmdSMPHz27@users.noreply.github.com> Date: Wed, 20 Jan 2021 11:16:15 -0500 Subject: [PATCH 16/17] Add test for cleaned file name --- .../logic/util/io/RegExpBasedFileFinderTests.java | 13 +++++++++++++ .../unlinkedFilesTestFolder/ACM_IEEE-CS.pdf | Bin 2 files changed, 13 insertions(+) create mode 100644 src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/ACM_IEEE-CS.pdf diff --git a/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java b/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java index a45edeee6f5..5b729cf452e 100644 --- a/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java +++ b/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java @@ -93,6 +93,19 @@ void findAssociatedFilesFindFileContainingBracketsFromBracketedExpression() thro result); } + @Test + void findAssociatedFilesFindCleamedFileFromBracketedExpression() throws Exception { + var bibEntry = new BibEntry().withField(StandardField.JOURNAL, "ACM/IEEE-CS"); + + var extension = Collections.singletonList("pdf"); + var directory = Collections.singletonList(Path.of(FILES_DIRECTORY)); + RegExpBasedFileFinder fileFinder = new RegExpBasedFileFinder("[JOURNAL]\\\\.[extension]", ','); + + List result = fileFinder.findAssociatedFiles(bibEntry, directory, extension); + assertEquals(Collections.singletonList(Path.of("src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/ACM_IEEE-CS.pdf")), + result); + } + @Test void findAssociatedFilesFindFileContainingParenthesizesFromBracketedExpression() throws Exception { var bibEntry = new BibEntry().withCitationKey("Guo_ICC_2010") diff --git a/src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/ACM_IEEE-CS.pdf b/src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/ACM_IEEE-CS.pdf new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 From 1a6826da0fb38f714b78174772ce1613850066e9 Mon Sep 17 00:00:00 2001 From: Jonatan Asketorp <2598631+k3KAW8Pnf7mkmdSMPHz27@users.noreply.github.com> Date: Fri, 22 Jan 2021 15:22:05 -0500 Subject: [PATCH 17/17] Fix usage of temp files in test cases --- .../util/io/RegExpBasedFileFinderTests.java | 107 ++++++++---------- .../unlinkedFilesTestFolder/ACM_IEEE-CS.pdf | Bin .../Regexp from [A-Z].pdf | Bin .../subdirectory/2003_Hippel_209.pdf | Bin .../2017_Gra\305\276ulis_726.pdf" | Bin ...electric Metal Organic Framework (MOF).pdf | Bin .../subdirectory/pdfInSubdirectory.pdf | Bin 5 -> 0 bytes 7 files changed, 47 insertions(+), 60 deletions(-) delete mode 100644 src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/ACM_IEEE-CS.pdf delete mode 100644 src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/Regexp from [A-Z].pdf delete mode 100644 src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/directory/subdirectory/2003_Hippel_209.pdf delete mode 100644 "src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/directory/subdirectory/2017_Gra\305\276ulis_726.pdf" delete mode 100644 src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/directory/subdirectory/GUO ea - INORG CHEM COMMUN 2010 - Ferroelectric Metal Organic Framework (MOF).pdf delete mode 100644 src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/directory/subdirectory/pdfInSubdirectory.pdf diff --git a/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java b/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java index 5b729cf452e..0c776e52fdd 100644 --- a/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java +++ b/src/test/java/org/jabref/logic/util/io/RegExpBasedFileFinderTests.java @@ -5,26 +5,33 @@ import java.util.Collections; import java.util.List; -import org.jabref.model.database.BibDatabase; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.field.StandardField; import org.jabref.model.entry.types.StandardEntryType; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; class RegExpBasedFileFinderTests { - - private static final String FILES_DIRECTORY = "src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder"; - private BibDatabase database; + private static final List PDF_EXTENSION = Collections.singletonList("pdf"); + private static final List FILE_NAMES = List.of( + "ACM_IEEE-CS.pdf", + "pdfInDatabase.pdf", + "Regexp from [A-Z].pdf", + "directory/subdirectory/2003_Hippel_209.pdf", + "directory/subdirectory/2017_Gražulis_726.pdf", + "directory/subdirectory/pdfInSubdirectory.pdf", + "directory/subdirectory/GUO ea - INORG CHEM COMMUN 2010 - Ferroelectric Metal Organic Framework (MOF).pdf" + ); + private Path directory; private BibEntry entry; @BeforeEach - void setUp() { - + void setUp(@TempDir Path tempDir) throws Exception { entry = new BibEntry(); entry.setType(StandardEntryType.Article); entry.setCitationKey("HipKro03"); @@ -40,70 +47,64 @@ void setUp() { entry.setField(StandardField.ISSN, "1526-5455"); entry.setField(StandardField.PUBLISHER, "INFORMS"); - database = new BibDatabase(); - database.insertEntry(entry); + // Create default directories and files + directory = tempDir; + Files.createDirectories(directory.resolve("directory/subdirectory")); + for (String fileName : FILE_NAMES) { + Files.createFile(directory.resolve(fileName)); + } } @Test void testFindFiles() throws Exception { // given - BibEntry localEntry = new BibEntry(StandardEntryType.Article); - localEntry.setCitationKey("pdfInDatabase"); - localEntry.setField(StandardField.YEAR, "2001"); + BibEntry localEntry = new BibEntry(StandardEntryType.Article).withCitationKey("pdfInDatabase"); - List extensions = Collections.singletonList("pdf"); - - List dirs = Collections.singletonList(Path.of(FILES_DIRECTORY)); RegExpBasedFileFinder fileFinder = new RegExpBasedFileFinder("**/[citationkey].*\\\\.[extension]", ','); // when - List result = fileFinder.findAssociatedFiles(localEntry, dirs, extensions); + List result = fileFinder.findAssociatedFiles(localEntry, List.of(directory), PDF_EXTENSION); + List expected = List.of(directory.resolve("pdfInDatabase.pdf")); // then - assertEquals(Collections.singletonList(Path.of("src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/pdfInDatabase.pdf")), - result); + assertEquals(expected, result); } @Test void testYearAuthFirstPageFindFiles() throws Exception { // given - List extensions = Collections.singletonList("pdf"); - - List dirs = Collections.singletonList(Path.of(FILES_DIRECTORY)); RegExpBasedFileFinder fileFinder = new RegExpBasedFileFinder("**/[year]_[auth]_[firstpage].*\\\\.[extension]", ','); // when - List result = fileFinder.findAssociatedFiles(entry, dirs, extensions); + List result = fileFinder.findAssociatedFiles(entry, List.of(directory), PDF_EXTENSION); + List expected = List.of(directory.resolve("directory/subdirectory/2003_Hippel_209.pdf")); // then - assertEquals(Collections.singletonList(Path.of("src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/directory/subdirectory/2003_Hippel_209.pdf")), - result); + assertEquals(expected, result); } @Test void findAssociatedFilesFindFileContainingBracketsFromBracketedExpression() throws Exception { var bibEntry = new BibEntry().withField(StandardField.TITLE, "Regexp from [A-Z]"); - var extension = Collections.singletonList("pdf"); - var directory = Collections.singletonList(Path.of(FILES_DIRECTORY)); RegExpBasedFileFinder fileFinder = new RegExpBasedFileFinder("[TITLE]\\\\.[extension]", ','); - List result = fileFinder.findAssociatedFiles(bibEntry, directory, extension); - assertEquals(Collections.singletonList(Path.of("src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/Regexp from [A-Z].pdf")), - result); + List result = fileFinder.findAssociatedFiles(bibEntry, List.of(directory), PDF_EXTENSION); + List pdfFile = List.of(directory.resolve("Regexp from [A-Z].pdf")); + + assertEquals(pdfFile, result); } @Test - void findAssociatedFilesFindCleamedFileFromBracketedExpression() throws Exception { + void findAssociatedFilesFindCleanedFileFromBracketedExpression() throws Exception { var bibEntry = new BibEntry().withField(StandardField.JOURNAL, "ACM/IEEE-CS"); - var extension = Collections.singletonList("pdf"); - var directory = Collections.singletonList(Path.of(FILES_DIRECTORY)); RegExpBasedFileFinder fileFinder = new RegExpBasedFileFinder("[JOURNAL]\\\\.[extension]", ','); - List result = fileFinder.findAssociatedFiles(bibEntry, directory, extension); - assertEquals(Collections.singletonList(Path.of("src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/ACM_IEEE-CS.pdf")), - result); + List result = fileFinder.findAssociatedFiles(bibEntry, List.of(directory), PDF_EXTENSION); + List pdfFile = List.of(directory.resolve("ACM_IEEE-CS.pdf")); + + assertEquals(pdfFile, result); } @Test @@ -114,38 +115,30 @@ void findAssociatedFilesFindFileContainingParenthesizesFromBracketedExpression() .withField(StandardField.JOURNAL, "Inorganic Chemistry Communications") .withField(StandardField.YEAR, "2010"); - var extension = Collections.singletonList("pdf"); - var directory = Collections.singletonList(Path.of(FILES_DIRECTORY)); RegExpBasedFileFinder fileFinder = new RegExpBasedFileFinder("**/.*[TITLE].*\\\\.[extension]", ','); - List result = fileFinder.findAssociatedFiles(bibEntry, directory, extension); - assertEquals(Collections.singletonList(Path.of("src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/directory/subdirectory/GUO ea - INORG CHEM COMMUN 2010 - Ferroelectric Metal Organic Framework (MOF).pdf")), - result); + List result = fileFinder.findAssociatedFiles(bibEntry, List.of(directory), PDF_EXTENSION); + List pdfFile = List.of(directory.resolve("directory/subdirectory/GUO ea - INORG CHEM COMMUN 2010 - Ferroelectric Metal Organic Framework (MOF).pdf")); + + assertEquals(pdfFile, result); } @Test void testAuthorWithDiacritics() throws Exception { // given - BibEntry localEntry = new BibEntry(StandardEntryType.Article); - localEntry.setCitationKey("Grazulis2017"); + BibEntry localEntry = new BibEntry(StandardEntryType.Article).withCitationKey("Grazulis2017"); localEntry.setField(StandardField.YEAR, "2017"); localEntry.setField(StandardField.AUTHOR, "Gražulis, Saulius and O. Kitsune"); localEntry.setField(StandardField.PAGES, "726--729"); - List extensions = Collections.singletonList("pdf"); - - List dirs = Collections.singletonList(Path.of(FILES_DIRECTORY)); RegExpBasedFileFinder fileFinder = new RegExpBasedFileFinder("**/[year]_[auth]_[firstpage]\\\\.[extension]", ','); // when - List result = fileFinder.findAssociatedFiles(localEntry, dirs, extensions); - List expected = Collections.singletonList(Path.of("src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/directory/subdirectory/2017_Gražulis_726.pdf")); + List result = fileFinder.findAssociatedFiles(localEntry, List.of(directory), PDF_EXTENSION); + List expected = List.of(directory.resolve("directory/subdirectory/2017_Gražulis_726.pdf")); // then - assertEquals(expected.size(), result.size()); - for (int i = 0; i < expected.size(); i++) { - assertTrue(Files.isSameFile(expected.get(i), result.get(i))); - } + assertEquals(expected, result); } @Test @@ -155,17 +148,14 @@ void testFindFileInSubdirectory() throws Exception { localEntry.setCitationKey("pdfInSubdirectory"); localEntry.setField(StandardField.YEAR, "2017"); - List extensions = Collections.singletonList("pdf"); - - List dirs = Collections.singletonList(Path.of(FILES_DIRECTORY)); RegExpBasedFileFinder fileFinder = new RegExpBasedFileFinder("**/[citationkey].*\\\\.[extension]", ','); // when - List result = fileFinder.findAssociatedFiles(localEntry, dirs, extensions); + List result = fileFinder.findAssociatedFiles(localEntry, List.of(directory), PDF_EXTENSION); + List expected = List.of(directory.resolve("directory/subdirectory/pdfInSubdirectory.pdf")); // then - assertEquals(Collections.singletonList(Path.of("src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/directory/subdirectory/pdfInSubdirectory.pdf")), - result); + assertEquals(expected, result); } @Test @@ -175,13 +165,10 @@ void testFindFileNonRecursive() throws Exception { localEntry.setCitationKey("pdfInSubdirectory"); localEntry.setField(StandardField.YEAR, "2017"); - List extensions = Collections.singletonList("pdf"); - - List dirs = Collections.singletonList(Path.of(FILES_DIRECTORY)); RegExpBasedFileFinder fileFinder = new RegExpBasedFileFinder("*/[citationkey].*\\\\.[extension]", ','); // when - List result = fileFinder.findAssociatedFiles(localEntry, dirs, extensions); + List result = fileFinder.findAssociatedFiles(localEntry, List.of(directory), PDF_EXTENSION); // then assertTrue(result.isEmpty()); diff --git a/src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/ACM_IEEE-CS.pdf b/src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/ACM_IEEE-CS.pdf deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/Regexp from [A-Z].pdf b/src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/Regexp from [A-Z].pdf deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/directory/subdirectory/2003_Hippel_209.pdf b/src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/directory/subdirectory/2003_Hippel_209.pdf deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git "a/src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/directory/subdirectory/2017_Gra\305\276ulis_726.pdf" "b/src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/directory/subdirectory/2017_Gra\305\276ulis_726.pdf" deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/directory/subdirectory/GUO ea - INORG CHEM COMMUN 2010 - Ferroelectric Metal Organic Framework (MOF).pdf b/src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/directory/subdirectory/GUO ea - INORG CHEM COMMUN 2010 - Ferroelectric Metal Organic Framework (MOF).pdf deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/directory/subdirectory/pdfInSubdirectory.pdf b/src/test/resources/org/jabref/logic/importer/unlinkedFilesTestFolder/directory/subdirectory/pdfInSubdirectory.pdf deleted file mode 100644 index 3ac0b7d0dd9994c7ec1c3211b7f4d39211d84753..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 5 McmXp_sw_zb00m9~>Hq)$