Skip to content

Commit

Permalink
Only disable move to file dir when path equals
Browse files Browse the repository at this point in the history
Fix equals in path method
Fixes #7194
  • Loading branch information
Siedlerchr committed Dec 29, 2020
1 parent 020cc97 commit 9d67aae
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
### Fixed

- We fixed an issue with the style of highlighted check boxes while searching in preferences. [#7226](https://github.com/JabRef/jabref/issues/7226)
- 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)

### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,14 @@ public boolean isGeneratedNameSameAsOriginal() {
}

/**
* Compares suggested filepath of current linkedFile with existing filepath.
* Compares suggested directory of current linkedFile with existing filepath directory.
*
* @return true if suggested filepath is same as existing filepath.
*/
public boolean isGeneratedPathSameAsOriginal() {
Optional<Path> newDir = databaseContext.getFirstExistingFileDir(filePreferences);

Optional<Path> currentDir = linkedFile.findIn(databaseContext, filePreferences);
Optional<Path> currentDir = linkedFile.findIn(databaseContext, filePreferences).map(Path::getParent);

BiPredicate<Path, Path> equality = (fileA, fileB) -> {
try {
Expand Down Expand Up @@ -434,7 +434,7 @@ public void download() {
List<LinkedFile> linkedFiles = entry.getFiles();
int oldFileIndex = -1;
int i = 0;
while (i < linkedFiles.size() && oldFileIndex == -1) {
while ((i < linkedFiles.size()) && (oldFileIndex == -1)) {
LinkedFile file = linkedFiles.get(i);
// The file type changes as part of download process (see prepareDownloadTask), thus we only compare by link
if (file.getLink().equalsIgnoreCase(linkedFile.getLink())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public ContextAction(StandardActions command, LinkedFileViewModel linkedFile, Pr
case MOVE_FILE_TO_FOLDER_AND_RENAME, MOVE_FILE_TO_FOLDER -> Bindings.createBooleanBinding(
() -> !linkedFile.getFile().isOnlineLink()
&& linkedFile.getFile().findIn(databaseContext, preferencesService.getFilePreferences()).isPresent()
&& linkedFile.isGeneratedPathSameAsOriginal(),
&& !linkedFile.isGeneratedPathSameAsOriginal(),
linkedFile.getFile().linkProperty());
case DOWNLOAD_FILE -> Bindings.createBooleanBinding(
() -> linkedFile.getFile().isOnlineLink(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.LinkedFile;
import org.jabref.preferences.FilePreferences;
import org.jabref.testutils.category.FetcherTest;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -152,7 +150,6 @@ void deleteWhenDialogCancelledReturnsFalseAndDoesNotRemoveFile() {
assertTrue(Files.exists(tempFile));
}

@FetcherTest
void downloadDoesNotOverwriteFileTypeExtension() throws MalformedURLException {
linkedFile = new LinkedFile(new URL("http://arxiv.org/pdf/1207.0408v1"), "");

Expand All @@ -170,4 +167,28 @@ void downloadDoesNotOverwriteFileTypeExtension() throws MalformedURLException {
task.onFailure(Assertions::fail);
new CurrentThreadTaskExecutor().execute(task);
}

@Test
void isNotSamePath() {
linkedFile = new LinkedFile("desc", tempFile, "pdf");
databaseContext = mock(BibDatabaseContext.class);
when(filePreferences.getFileNamePattern()).thenReturn("[citationkey]"); // use this variant, as we cannot mock the linkedFileHandler cause it's initialized inside the viewModel
when(databaseContext.getFirstExistingFileDir(filePreferences)).thenReturn(Optional.of(Path.of("/home")));

LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, xmpPreferences, filePreferences, externalFileType);

assertFalse(viewModel.isGeneratedPathSameAsOriginal());
}

@Test
void isSamePath() {
linkedFile = new LinkedFile("desc", tempFile, "pdf");
databaseContext = mock(BibDatabaseContext.class);
when(filePreferences.getFileNamePattern()).thenReturn("[citationkey]"); // use this variant, as we cannot mock the linkedFileHandler cause it's initialized inside the viewModel
when(databaseContext.getFirstExistingFileDir(filePreferences)).thenReturn(Optional.of(tempFile.getParent()));

LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, xmpPreferences, filePreferences, externalFileType);

assertTrue(viewModel.isGeneratedPathSameAsOriginal());
}
}

0 comments on commit 9d67aae

Please sign in to comment.