diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a8c034a42a..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) diff --git a/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java b/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java index 4117e7b22a0..f57f56cb27c 100644 --- a/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java +++ b/src/main/java/org/jabref/gui/groups/GroupDialogViewModel.java @@ -231,8 +231,8 @@ private void setupValidation() { if (StringUtil.isBlank(input)) { return false; } else { - Path texFilePath = preferencesService.getWorkingDir().resolve(input); - if (!Files.isRegularFile(texFilePath)) { + Path inputPath = getAbsoluteTexGroupPath(input); + if (!inputPath.isAbsolute() || !Files.isRegularFile(inputPath)) { return false; } return FileUtil.getFileExtension(input) @@ -267,6 +267,16 @@ 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 if LatexFileDirectory exists; otherwise, returns input + */ + 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()) { 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..9249f2f8aa8 --- /dev/null +++ b/src/test/java/org/jabref/gui/groups/GroupDialogViewModelTest.java @@ -0,0 +1,77 @@ +package org.jabref.gui.groups; + +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; + private final MetaData metaData = mock(MetaData.class); + + @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"); + + 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); + when(metaData.getLatexFileDirectory(any(String.class))).thenReturn(Optional.of(temporaryFolder)); + + 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()); + } + + @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()); + } +}