|
| 1 | +package org.jabref.gui.fieldeditors; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.Optional; |
| 6 | + |
| 7 | +import javafx.scene.control.Alert.AlertType; |
| 8 | +import javafx.scene.control.ButtonType; |
| 9 | + |
| 10 | +import org.jabref.gui.DialogService; |
| 11 | +import org.jabref.gui.util.TaskExecutor; |
| 12 | +import org.jabref.model.database.BibDatabaseContext; |
| 13 | +import org.jabref.model.entry.BibEntry; |
| 14 | +import org.jabref.model.entry.LinkedFile; |
| 15 | +import org.jabref.model.metadata.FileDirectoryPreferences; |
| 16 | + |
| 17 | +import org.junit.Before; |
| 18 | +import org.junit.Rule; |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.rules.TemporaryFolder; |
| 21 | + |
| 22 | +import static org.junit.Assert.assertFalse; |
| 23 | +import static org.junit.Assert.assertTrue; |
| 24 | +import static org.mockito.ArgumentMatchers.any; |
| 25 | +import static org.mockito.ArgumentMatchers.anyString; |
| 26 | +import static org.mockito.Mockito.doReturn; |
| 27 | +import static org.mockito.Mockito.mock; |
| 28 | +import static org.mockito.Mockito.spy; |
| 29 | +import static org.mockito.Mockito.verify; |
| 30 | +import static org.mockito.Mockito.verifyZeroInteractions; |
| 31 | +import static org.mockito.Mockito.when; |
| 32 | + |
| 33 | +public class LinkedFileViewModelTest { |
| 34 | + |
| 35 | + @Rule public TemporaryFolder tempFolder = new TemporaryFolder(); |
| 36 | + private LinkedFile linkedFile; |
| 37 | + private BibEntry entry; |
| 38 | + private BibDatabaseContext databaseContext; |
| 39 | + private TaskExecutor taskExecutor; |
| 40 | + private DialogService dialogService; |
| 41 | + |
| 42 | + @Before |
| 43 | + public void setUp() { |
| 44 | + entry = new BibEntry(); |
| 45 | + databaseContext = new BibDatabaseContext(); |
| 46 | + taskExecutor = mock(TaskExecutor.class); |
| 47 | + dialogService = mock(DialogService.class); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void deleteWhenFilePathNotPresentReturnsFalse() { |
| 52 | + // Making this a spy, so we can inject an empty optional without digging into the implementation |
| 53 | + linkedFile = spy(new LinkedFile("", "nonexistent file", "")); |
| 54 | + doReturn(Optional.empty()).when(linkedFile).findIn(any(BibDatabaseContext.class), any(FileDirectoryPreferences.class)); |
| 55 | + |
| 56 | + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService); |
| 57 | + boolean removed = viewModel.delete(); |
| 58 | + |
| 59 | + assertFalse(removed); |
| 60 | + verifyZeroInteractions(dialogService); // dialog was never shown |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void deleteWhenRemoveChosenReturnsTrue() throws IOException { |
| 65 | + File tempFile = tempFolder.newFile(); |
| 66 | + linkedFile = new LinkedFile("", tempFile.getAbsolutePath(), ""); |
| 67 | + when(dialogService.showCustomButtonDialogAndWait( |
| 68 | + any(AlertType.class), |
| 69 | + anyString(), |
| 70 | + anyString(), |
| 71 | + any(ButtonType.class), |
| 72 | + any(ButtonType.class), |
| 73 | + any(ButtonType.class) |
| 74 | + )).thenAnswer(invocation -> Optional.of(invocation.getArgument(3))); // first vararg - remove button |
| 75 | + |
| 76 | + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService); |
| 77 | + boolean removed = viewModel.delete(); |
| 78 | + |
| 79 | + assertTrue(removed); |
| 80 | + assertTrue(tempFile.exists()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void deleteWhenDeleteChosenReturnsTrueAndDeletesFile() throws IOException { |
| 85 | + File tempFile = tempFolder.newFile(); |
| 86 | + linkedFile = new LinkedFile("", tempFile.getAbsolutePath(), ""); |
| 87 | + when(dialogService.showCustomButtonDialogAndWait( |
| 88 | + any(AlertType.class), |
| 89 | + anyString(), |
| 90 | + anyString(), |
| 91 | + any(ButtonType.class), |
| 92 | + any(ButtonType.class), |
| 93 | + any(ButtonType.class) |
| 94 | + )).thenAnswer(invocation -> Optional.of(invocation.getArgument(4))); // second vararg - delete button |
| 95 | + |
| 96 | + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService); |
| 97 | + boolean removed = viewModel.delete(); |
| 98 | + |
| 99 | + assertTrue(removed); |
| 100 | + assertFalse(tempFile.exists()); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + @Test |
| 105 | + public void deleteWhenDeleteChosenAndFileMissingReturnsFalse() throws IOException { |
| 106 | + linkedFile = new LinkedFile("", "!!nonexistent file!!", ""); |
| 107 | + when(dialogService.showCustomButtonDialogAndWait( |
| 108 | + any(AlertType.class), |
| 109 | + anyString(), |
| 110 | + anyString(), |
| 111 | + any(ButtonType.class), |
| 112 | + any(ButtonType.class), |
| 113 | + any(ButtonType.class) |
| 114 | + )).thenAnswer(invocation -> Optional.of(invocation.getArgument(4))); // second vararg - delete button |
| 115 | + |
| 116 | + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService); |
| 117 | + boolean removed = viewModel.delete(); |
| 118 | + |
| 119 | + verify(dialogService).showErrorDialogAndWait(anyString(), anyString()); |
| 120 | + assertFalse(removed); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void deleteWhenDialogCancelledReturnsFalse() throws IOException { |
| 125 | + File tempFile = tempFolder.newFile(); |
| 126 | + linkedFile = new LinkedFile("desc", tempFile.getAbsolutePath(), "pdf"); |
| 127 | + when(dialogService.showCustomButtonDialogAndWait( |
| 128 | + any(AlertType.class), |
| 129 | + anyString(), |
| 130 | + anyString(), |
| 131 | + any(ButtonType.class), |
| 132 | + any(ButtonType.class), |
| 133 | + any(ButtonType.class) |
| 134 | + )).thenAnswer(invocation -> Optional.of(invocation.getArgument(5))); // third vararg - cancel button |
| 135 | + |
| 136 | + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService); |
| 137 | + boolean removed = viewModel.delete(); |
| 138 | + |
| 139 | + assertFalse(removed); |
| 140 | + assertTrue(tempFile.exists()); |
| 141 | + } |
| 142 | +} |
0 commit comments