From a779428c0ce7c4bfd7cdeb69fc64168588c8d827 Mon Sep 17 00:00:00 2001 From: aloofluo <945517787@qq.com> Date: Mon, 10 May 2021 22:31:27 +0800 Subject: [PATCH 1/9] fix for issue #7719, user should input absolute path --- .../java/org/jabref/gui/groups/GroupDialogViewModel.java | 6 ++++-- .../java/org/jabref/gui/util/DefaultFileUpdateMonitor.java | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java b/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java index 05233f2abdc..0e19bc69f60 100644 --- a/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java +++ b/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java @@ -227,8 +227,9 @@ private void setupValidation() { if (StringUtil.isBlank(input)) { return false; } else { - Path texFilePath = preferencesService.getWorkingDir().resolve(input); - if (!Files.isRegularFile(texFilePath)) { + // we don't need to add the working dir before checking, let user input absolute path + Path inputPath = Path.of(input); + if (!Files.isRegularFile(inputPath)) { return false; } return FileUtil.getFileExtension(input) @@ -335,6 +336,7 @@ public AbstractGroup resultConverter(ButtonType button) { FieldFactory.parseField(autoGroupPersonsFieldProperty.getValue().trim())); } } else if (typeTexProperty.getValue()) { + // issue 7719: if texGroupFilePath is not absolute, path to Jabref will be added, which is wrong resultingGroup = TexGroup.create( groupName, groupHierarchySelectedProperty.getValue(), diff --git a/src/main/java/org/jabref/gui/util/DefaultFileUpdateMonitor.java b/src/main/java/org/jabref/gui/util/DefaultFileUpdateMonitor.java index 588e7616b07..2a4bc417b51 100644 --- a/src/main/java/org/jabref/gui/util/DefaultFileUpdateMonitor.java +++ b/src/main/java/org/jabref/gui/util/DefaultFileUpdateMonitor.java @@ -87,6 +87,7 @@ private void notifyAboutChange(Path path) { public void addListenerForFile(Path file, FileUpdateListener listener) throws IOException { if (isActive()) { // We can't watch files directly, so monitor their parent directory for updates + // toAbsolutePath() will add the path to Jabref as home directory to file, if file is not a absolute path Path directory = file.toAbsolutePath().getParent(); directory.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY); listeners.put(file, listener); From 5f11f2d43454650dd544649eab3a70685a14504c Mon Sep 17 00:00:00 2001 From: aloofluo <945517787@qq.com> Date: Mon, 10 May 2021 23:17:22 +0800 Subject: [PATCH 2/9] fix for issue #7719, v1 --- src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java | 4 +++- .../java/org/jabref/gui/util/DefaultFileUpdateMonitor.java | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java b/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java index 05233f2abdc..49becd539d6 100644 --- a/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java +++ b/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java @@ -335,10 +335,12 @@ public AbstractGroup resultConverter(ButtonType button) { FieldFactory.parseField(autoGroupPersonsFieldProperty.getValue().trim())); } } else if (typeTexProperty.getValue()) { + // issue 7719: add workingDir to filepath if it is relative + Path inputPath = preferencesService.getWorkingDir().resolve(Path.of(texGroupFilePathProperty.getValue().trim())); resultingGroup = TexGroup.create( groupName, groupHierarchySelectedProperty.getValue(), - Path.of(texGroupFilePathProperty.getValue().trim()), + inputPath, new DefaultAuxParser(new BibDatabase()), Globals.getFileUpdateMonitor(), currentDatabase.getMetaData()); diff --git a/src/main/java/org/jabref/gui/util/DefaultFileUpdateMonitor.java b/src/main/java/org/jabref/gui/util/DefaultFileUpdateMonitor.java index 588e7616b07..2a4bc417b51 100644 --- a/src/main/java/org/jabref/gui/util/DefaultFileUpdateMonitor.java +++ b/src/main/java/org/jabref/gui/util/DefaultFileUpdateMonitor.java @@ -87,6 +87,7 @@ private void notifyAboutChange(Path path) { public void addListenerForFile(Path file, FileUpdateListener listener) throws IOException { if (isActive()) { // We can't watch files directly, so monitor their parent directory for updates + // toAbsolutePath() will add the path to Jabref as home directory to file, if file is not a absolute path Path directory = file.toAbsolutePath().getParent(); directory.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY); listeners.put(file, listener); From 57f18615439ce47712b0cdeba07506faee07d9f4 Mon Sep 17 00:00:00 2001 From: aloofluo <945517787@qq.com> Date: Thu, 20 May 2021 23:30:02 +0800 Subject: [PATCH 3/9] fix for issue #7719, v3 --- CHANGELOG.md | 1 + .../jabref/gui/groups/GroupDialogViewModel.java | 16 ++++++++++++---- .../gui/util/DefaultFileUpdateMonitor.java | 1 - 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8606dfa7ed8..a024b2ae4c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -90,6 +90,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve - We fixed an issue where the article title with colon fails to download the arXiv link (pdf file). [#7660](https://github.com/JabRef/issues/7660) - We fixed an issue where the keybinding for delete entry did not work on the main table [7580](https://github.com/JabRef/jabref/pull/7580) - We fixed an issue where the RFC fetcher is not compatible with the draft [7305](https://github.com/JabRef/jabref/issues/7305) +- We fixed an issue where the `Aux file` on `Edit group` doesn't support relative sub-directories path to import. [#7719](https://github.com/JabRef/jabref/issues/7719). ### Removed diff --git a/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java b/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java index 49becd539d6..11fe996adcd 100644 --- a/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java +++ b/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java @@ -227,7 +227,7 @@ private void setupValidation() { if (StringUtil.isBlank(input)) { return false; } else { - Path texFilePath = preferencesService.getWorkingDir().resolve(input); + Path texFilePath = getAbsoluteTexGroupPath(input); if (!Files.isRegularFile(texFilePath)) { return false; } @@ -263,6 +263,16 @@ private void setupValidation() { }); } + /** + * gets the absolute path relative to getLatexFileDirectory, if given a relative path + * @param input the user input path + * @return an absolute path + */ + private Path getAbsoluteTexGroupPath(String input) { + Optional latexFileDirectory = currentDatabase.getMetaData().getLatexFileDirectory(preferencesService.getUser()); + return latexFileDirectory.map(path -> path.resolve(input)).orElse(Path.of(input)); + } + public void validationHandler(Event event) { ValidationStatus validationStatus = validator.getValidationStatus(); if (validationStatus.getHighestMessage().isPresent()) { @@ -335,12 +345,10 @@ public AbstractGroup resultConverter(ButtonType button) { FieldFactory.parseField(autoGroupPersonsFieldProperty.getValue().trim())); } } else if (typeTexProperty.getValue()) { - // issue 7719: add workingDir to filepath if it is relative - Path inputPath = preferencesService.getWorkingDir().resolve(Path.of(texGroupFilePathProperty.getValue().trim())); resultingGroup = TexGroup.create( groupName, groupHierarchySelectedProperty.getValue(), - inputPath, + Path.of(texGroupFilePathProperty.getValue().trim()), new DefaultAuxParser(new BibDatabase()), Globals.getFileUpdateMonitor(), currentDatabase.getMetaData()); diff --git a/src/main/java/org/jabref/gui/util/DefaultFileUpdateMonitor.java b/src/main/java/org/jabref/gui/util/DefaultFileUpdateMonitor.java index 2a4bc417b51..588e7616b07 100644 --- a/src/main/java/org/jabref/gui/util/DefaultFileUpdateMonitor.java +++ b/src/main/java/org/jabref/gui/util/DefaultFileUpdateMonitor.java @@ -87,7 +87,6 @@ private void notifyAboutChange(Path path) { public void addListenerForFile(Path file, FileUpdateListener listener) throws IOException { if (isActive()) { // We can't watch files directly, so monitor their parent directory for updates - // toAbsolutePath() will add the path to Jabref as home directory to file, if file is not a absolute path Path directory = file.toAbsolutePath().getParent(); directory.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY); listeners.put(file, listener); From 89c95fa8b74d165f5e1950860509f63e40160b30 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Fri, 21 May 2021 00:06:30 +0200 Subject: [PATCH 4/9] Update src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java Co-authored-by: Jonatan Asketorp <2598631+k3KAW8Pnf7mkmdSMPHz27@users.noreply.github.com> --- src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java b/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java index 11fe996adcd..47f1d4e254c 100644 --- a/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java +++ b/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java @@ -264,7 +264,7 @@ private void setupValidation() { } /** - * gets the absolute path relative to getLatexFileDirectory, if given a relative path + * Gets the absolute path relative to the LatexFileDirectory, if given a relative path * @param input the user input path * @return an absolute path */ From aae330f0f7d5cef0ff0738d72e216c883703b980 Mon Sep 17 00:00:00 2001 From: aloofluo <945517787@qq.com> Date: Fri, 21 May 2021 10:52:20 +0800 Subject: [PATCH 5/9] fixes for 7719, reject if no LatexGroupPath is set --- .../java/org/jabref/gui/groups/GroupDialogViewModel.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java b/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java index 6661fd1aeef..14534d0912a 100644 --- a/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java +++ b/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java @@ -227,8 +227,8 @@ private void setupValidation() { if (StringUtil.isBlank(input)) { return false; } else { - Path inputPath = Path.of(input); - if (!Files.isRegularFile(inputPath)) { + Path inputPath = getAbsoluteTexGroupPath(input); + if (!inputPath.isAbsolute() || !Files.isRegularFile(inputPath)) { return false; } return FileUtil.getFileExtension(input) @@ -266,7 +266,7 @@ private void setupValidation() { /** * Gets the absolute path relative to the LatexFileDirectory, if given a relative path * @param input the user input path - * @return an absolute path + * @return an absolute path if LatexFileDirectory exists; otherwise, returns input */ private Path getAbsoluteTexGroupPath(String input) { Optional latexFileDirectory = currentDatabase.getMetaData().getLatexFileDirectory(preferencesService.getUser()); From e7c8131996b956063eeba46a473566d83b9ed3ea Mon Sep 17 00:00:00 2001 From: aloofluo <945517787@qq.com> Date: Fri, 21 May 2021 10:55:12 +0800 Subject: [PATCH 6/9] fixes for 7719, delete a comment --- src/main/java/org/jabref/gui/util/DefaultFileUpdateMonitor.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/jabref/gui/util/DefaultFileUpdateMonitor.java b/src/main/java/org/jabref/gui/util/DefaultFileUpdateMonitor.java index 2a4bc417b51..588e7616b07 100644 --- a/src/main/java/org/jabref/gui/util/DefaultFileUpdateMonitor.java +++ b/src/main/java/org/jabref/gui/util/DefaultFileUpdateMonitor.java @@ -87,7 +87,6 @@ private void notifyAboutChange(Path path) { public void addListenerForFile(Path file, FileUpdateListener listener) throws IOException { if (isActive()) { // We can't watch files directly, so monitor their parent directory for updates - // toAbsolutePath() will add the path to Jabref as home directory to file, if file is not a absolute path Path directory = file.toAbsolutePath().getParent(); directory.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY); listeners.put(file, listener); From 7313bb97a10d7446a566770d32ca7ad994e01da7 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Sat, 31 Jul 2021 22:50:44 +0200 Subject: [PATCH 7/9] add test --- .../gui/groups/GroupDialogViewModelTest.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/test/java/org/jabref/gui/groups/GroupDialogViewModelTest.java diff --git a/src/test/java/org/jabref/gui/groups/GroupDialogViewModelTest.java b/src/test/java/org/jabref/gui/groups/GroupDialogViewModelTest.java new file mode 100644 index 00000000000..6fe014e7557 --- /dev/null +++ b/src/test/java/org/jabref/gui/groups/GroupDialogViewModelTest.java @@ -0,0 +1,65 @@ +package org.jabref.gui.groups; + +import static org.junit.jupiter.api.Assertions.*; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Optional; + +import org.jabref.gui.DialogService; +import org.jabref.model.database.BibDatabaseContext; +import org.jabref.model.groups.AbstractGroup; +import org.jabref.model.metadata.MetaData; +import org.jabref.preferences.PreferencesService; + +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.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class GroupDialogViewModelTest { + + private GroupDialogViewModel viewModel; + private Path temporaryFolder; + private BibDatabaseContext bibDatabaseContext; + + @BeforeEach + void setUp(@TempDir Path temporaryFolder) throws Exception { + this.temporaryFolder = temporaryFolder; + bibDatabaseContext = new BibDatabaseContext(); + DialogService dialogService = mock(DialogService.class); + + AbstractGroup group = mock(AbstractGroup.class); + when(group.getName()).thenReturn("Group"); + + PreferencesService preferencesService = mock(PreferencesService.class); + when(preferencesService.getKeywordDelimiter()).thenReturn(','); + when(preferencesService.getUser()).thenReturn("MockedUser"); + + MetaData metaData = mock(MetaData.class); + when(metaData.getLatexFileDirectory(any(String.class))).thenReturn(Optional.empty()); + bibDatabaseContext.setMetaData(metaData); + + viewModel = new GroupDialogViewModel(dialogService, bibDatabaseContext, preferencesService, group, GroupDialogHeader.SUBGROUP); + } + + @Test + void validateExistingAbsolutePath() throws Exception { + var anAuxFile = temporaryFolder.resolve("auxfile.aux").toAbsolutePath(); + Files.createFile(anAuxFile); + viewModel.texGroupFilePathProperty().setValue(anAuxFile.toString()); + assertTrue(viewModel.texGroupFilePathValidatonStatus().isValid()); + } + + @Test + void validateNonExistingAbsolutePath() throws Exception { + var notAnAuxFile = temporaryFolder.resolve("notanauxfile.aux").toAbsolutePath(); + viewModel.texGroupFilePathProperty().setValue(notAnAuxFile.toString()); + assertFalse(viewModel.texGroupFilePathValidatonStatus().isValid()); + } +} \ No newline at end of file From 561b4789c162bf2cb34bb9d1af2163e3f88f1674 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Sat, 31 Jul 2021 23:11:35 +0200 Subject: [PATCH 8/9] Add test for relative dir --- .../gui/groups/GroupDialogViewModelTest.java | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/jabref/gui/groups/GroupDialogViewModelTest.java b/src/test/java/org/jabref/gui/groups/GroupDialogViewModelTest.java index 6fe014e7557..9249f2f8aa8 100644 --- a/src/test/java/org/jabref/gui/groups/GroupDialogViewModelTest.java +++ b/src/test/java/org/jabref/gui/groups/GroupDialogViewModelTest.java @@ -1,7 +1,5 @@ package org.jabref.gui.groups; -import static org.junit.jupiter.api.Assertions.*; - import java.nio.file.Files; import java.nio.file.Path; import java.util.Optional; @@ -27,6 +25,7 @@ class GroupDialogViewModelTest { private GroupDialogViewModel viewModel; private Path temporaryFolder; private BibDatabaseContext bibDatabaseContext; + private final MetaData metaData = mock(MetaData.class); @BeforeEach void setUp(@TempDir Path temporaryFolder) throws Exception { @@ -41,8 +40,6 @@ void setUp(@TempDir Path temporaryFolder) throws Exception { when(preferencesService.getKeywordDelimiter()).thenReturn(','); when(preferencesService.getUser()).thenReturn("MockedUser"); - MetaData metaData = mock(MetaData.class); - when(metaData.getLatexFileDirectory(any(String.class))).thenReturn(Optional.empty()); bibDatabaseContext.setMetaData(metaData); viewModel = new GroupDialogViewModel(dialogService, bibDatabaseContext, preferencesService, group, GroupDialogHeader.SUBGROUP); @@ -51,7 +48,10 @@ void setUp(@TempDir Path temporaryFolder) throws Exception { @Test void validateExistingAbsolutePath() throws Exception { var anAuxFile = temporaryFolder.resolve("auxfile.aux").toAbsolutePath(); + Files.createFile(anAuxFile); + when(metaData.getLatexFileDirectory(any(String.class))).thenReturn(Optional.of(temporaryFolder)); + viewModel.texGroupFilePathProperty().setValue(anAuxFile.toString()); assertTrue(viewModel.texGroupFilePathValidatonStatus().isValid()); } @@ -62,4 +62,16 @@ void validateNonExistingAbsolutePath() throws Exception { viewModel.texGroupFilePathProperty().setValue(notAnAuxFile.toString()); assertFalse(viewModel.texGroupFilePathValidatonStatus().isValid()); } -} \ No newline at end of file + + @Test + void validateExistingRelativePath() throws Exception { + var anAuxFile = Path.of("auxfile.aux"); + + // The file needs to exist + Files.createFile(temporaryFolder.resolve(anAuxFile)); + when(metaData.getLatexFileDirectory(any(String.class))).thenReturn(Optional.of(temporaryFolder)); + + viewModel.texGroupFilePathProperty().setValue(anAuxFile.toString()); + assertTrue(viewModel.texGroupFilePathValidatonStatus().isValid()); + } +} From e066fd26284dc7db87c7b4e9f0b5e0c3eeaa6c16 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Mon, 2 Aug 2021 20:11:24 +0200 Subject: [PATCH 9/9] Fix location in CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index da64dffcde3..25d9f06fc13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve ### Fixed - We fixed an issue when checking for a new version when JabRef is used behind a corporate proxy. [#7884](https://github.com/JabRef/jabref/issues/7884) +- We fixed an issue where the `Aux file` on `Edit group` doesn't support relative sub-directories path to import. [#7719](https://github.com/JabRef/jabref/issues/7719). - We fixed an issue where it was impossible to add or modify groups. [#7912](https://github.com/JabRef/jabref/pull/793://github.com/JabRef/jabref/pull/7921) - We fixed an issue where exported entries from a Citavi bib containing URLs could not be imported [#7892](https://github.com/JabRef/jabref/issues/7882) @@ -228,7 +229,6 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve - We fixed an issue where the article title with colon fails to download the arXiv link (pdf file). [#7660](https://github.com/JabRef/issues/7660) - We fixed an issue where the keybinding for delete entry did not work on the main table [7580](https://github.com/JabRef/jabref/pull/7580) - We fixed an issue where the RFC fetcher is not compatible with the draft [7305](https://github.com/JabRef/jabref/issues/7305) -- We fixed an issue where the `Aux file` on `Edit group` doesn't support relative sub-directories path to import. [#7719](https://github.com/JabRef/jabref/issues/7719). - We fixed an issue where duplicate files (both file names and contents are the same) is downloaded and add to linked files [#6197](https://github.com/JabRef/jabref/issues/6197) - We fixed an issue where changing the appearance of the preview tab did not trigger a restart warning. [#5464](https://github.com/JabRef/jabref/issues/5464) - We fixed an issue where editing "Custom preview style" triggers exception. [#7526](https://github.com/JabRef/jabref/issues/7526)