diff --git a/CHANGELOG.md b/CHANGELOG.md index fd26fa03dce..19306a00169 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve ### Changed +- We slightly changed the layout of the Journal tab in the preferences for ui consistency. [#7937](https://github.com/JabRef/jabref/pull/7937) - The JabRefHost on Windows now writes a temporary file and calls `-importToOpen` instead of passing the bibtex via `-importBibtex`. [#7374](https://github.com/JabRef/jabref/issues/7374), [JabRef Browser Ext #274](https://github.com/JabRef/JabRef-Browser-Extension/issues/274) ### Fixed diff --git a/src/main/java/org/jabref/gui/icon/IconTheme.java b/src/main/java/org/jabref/gui/icon/IconTheme.java index e3e9aab7b23..765571ee1a3 100644 --- a/src/main/java/org/jabref/gui/icon/IconTheme.java +++ b/src/main/java/org/jabref/gui/icon/IconTheme.java @@ -322,9 +322,9 @@ public enum JabRefIcons implements JabRefIcon { LATEX_COMMENT(MaterialDesignC.COMMENT_TEXT_OUTLINE), LATEX_LINE(MaterialDesignF.FORMAT_LINE_SPACING), PASSWORD_REVEALED(MaterialDesignE.EYE), - ADD_ABBREVIATION_LIST(MaterialDesignF.FOLDER_PLUS), + ADD_ABBREVIATION_LIST(MaterialDesignP.PLUS), OPEN_ABBREVIATION_LIST(MaterialDesignF.FOLDER_OUTLINE), - REMOVE_ABBREVIATION_LIST(MaterialDesignF.FOLDER_REMOVE), + REMOVE_ABBREVIATION_LIST(MaterialDesignM.MINUS), ADD_ABBREVIATION(MaterialDesignP.PLAYLIST_PLUS), REMOVE_ABBREVIATION(MaterialDesignP.PLAYLIST_MINUS), NEW_ENTRY_FROM_PLAIN_TEXT(MaterialDesignP.PLUS_BOX), diff --git a/src/main/java/org/jabref/gui/preferences/journals/AbbreviationsFileViewModel.java b/src/main/java/org/jabref/gui/preferences/journals/AbbreviationsFileViewModel.java index b5ad2e62718..4c6961cdf0b 100644 --- a/src/main/java/org/jabref/gui/preferences/journals/AbbreviationsFileViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/journals/AbbreviationsFileViewModel.java @@ -35,7 +35,6 @@ public AbbreviationsFileViewModel(Path filePath) { this.path = Optional.ofNullable(filePath); this.name = path.get().toAbsolutePath().toString(); this.isBuiltInList = new SimpleBooleanProperty(false); - this.abbreviations.add(new AbbreviationViewModel(null)); } /** diff --git a/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.css b/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.css deleted file mode 100644 index 8e11f3af8ff..00000000000 --- a/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.css +++ /dev/null @@ -1,14 +0,0 @@ -.abbreviations-table { - -fx-table-cell-border-color: transparent; -} - -.custom-header { - -fx-margin-top: 10px; - -fx-padding: 10px; - -fx-spacing: 5px; -} - -.icon { - -fx-font-size: 1.5em; - -fx-padding: 0.1em 0.2em 0.1em 0.2em; -} diff --git a/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.fxml b/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.fxml index 0dc1c91d3f0..9ccd806b5dc 100644 --- a/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.fxml +++ b/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.fxml @@ -1,5 +1,6 @@ + @@ -8,67 +9,75 @@ - + - diff --git a/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.java b/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.java index c89d1c0e11c..32733480c77 100644 --- a/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.java +++ b/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.java @@ -28,6 +28,7 @@ import org.jabref.gui.preferences.PreferencesTab; import org.jabref.gui.util.ColorUtil; import org.jabref.gui.util.TaskExecutor; +import org.jabref.gui.util.ValueTableCellFactory; import org.jabref.logic.journals.JournalAbbreviationRepository; import org.jabref.logic.l10n.Localization; @@ -43,16 +44,17 @@ public class JournalAbbreviationsTab extends AbstractPreferenceTabView journalAbbreviationsTable; @FXML private TableColumn journalTableNameColumn; @FXML private TableColumn journalTableAbbreviationColumn; @FXML private TableColumn journalTableShortestUniqueAbbreviationColumn; + @FXML private TableColumn actionsColumn; + private FilteredList filteredAbbreviations; @FXML private ComboBox journalFilesBox; + @FXML private Button addAbbreviationButton; - @FXML private Button removeAbbreviationButton; - @FXML private Button openAbbreviationListButton; - @FXML private Button addAbbreviationListButton; @FXML private Button removeAbbreviationListButton; @FXML private CustomTextField searchBox; @@ -61,8 +63,6 @@ public class JournalAbbreviationsTab extends AbstractPreferenceTabView flashingColor; - private StringProperty flashingColorStringProperty; public JournalAbbreviationsTab() { ViewLoader.view(this) @@ -76,7 +76,6 @@ private void initialize() { filteredAbbreviations = new FilteredList<>(viewModel.abbreviationsProperty()); - setButtonStyles(); setUpTable(); setBindings(); setAnimations(); @@ -85,14 +84,6 @@ private void initialize() { searchBox.setLeft(IconTheme.JabRefIcons.SEARCH.getGraphicNode()); } - private void setButtonStyles() { - addAbbreviationListButton.setGraphic(IconTheme.JabRefIcons.ADD_ABBREVIATION_LIST.getGraphicNode()); - openAbbreviationListButton.setGraphic(IconTheme.JabRefIcons.OPEN_ABBREVIATION_LIST.getGraphicNode()); - removeAbbreviationListButton.setGraphic(IconTheme.JabRefIcons.REMOVE_ABBREVIATION_LIST.getGraphicNode()); - addAbbreviationButton.setGraphic(IconTheme.JabRefIcons.ADD_ABBREVIATION.getGraphicNode()); - removeAbbreviationButton.setGraphic(IconTheme.JabRefIcons.REMOVE_ABBREVIATION.getGraphicNode()); - } - private void setUpTable() { journalTableNameColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty()); journalTableNameColumn.setCellFactory(TextFieldTableCell.forTableColumn()); @@ -102,6 +93,16 @@ private void setUpTable() { journalTableShortestUniqueAbbreviationColumn.setCellValueFactory(cellData -> cellData.getValue().shortestUniqueAbbreviationProperty()); journalTableShortestUniqueAbbreviationColumn.setCellFactory(TextFieldTableCell.forTableColumn()); + + actionsColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty()); + new ValueTableCellFactory() + .withGraphic(name -> IconTheme.JabRefIcons.DELETE_ENTRY.getGraphicNode()) + .withTooltip(name -> Localization.lang("Remove journal '%0'", name)) + .withDisableExpression(item -> viewModel.isEditableAndRemovableProperty().not()) + .withVisibleExpression(item -> viewModel.isEditableAndRemovableProperty()) + .withOnMouseClickedEvent(item -> evt -> + viewModel.removeAbbreviation(journalAbbreviationsTable.getFocusModel().getFocusedItem())) + .install(actionsColumn); } private void setBindings() { @@ -121,19 +122,19 @@ private void setBindings() { journalFilesBox.valueProperty().bindBidirectional(viewModel.currentFileProperty()); addAbbreviationButton.disableProperty().bind(viewModel.isEditableAndRemovableProperty().not()); - removeAbbreviationButton.disableProperty().bind(viewModel.isAbbreviationEditableAndRemovable().not()); loadingLabel.visibleProperty().bind(viewModel.isLoadingProperty()); progressIndicator.visibleProperty().bind(viewModel.isLoadingProperty()); searchBox.textProperty().addListener((observable, previousText, searchTerm) -> { - filteredAbbreviations.setPredicate(abbreviation -> searchTerm.isEmpty() ? true : abbreviation.containsCaseIndependent(searchTerm)); + filteredAbbreviations.setPredicate(abbreviation -> searchTerm.isEmpty() || abbreviation.containsCaseIndependent(searchTerm)); }); } private void setAnimations() { - flashingColor = new SimpleObjectProperty<>(Color.TRANSPARENT); - flashingColorStringProperty = createFlashingColorStringProperty(flashingColor); + ObjectProperty flashingColor = new SimpleObjectProperty<>(Color.TRANSPARENT); + StringProperty flashingColorStringProperty = createFlashingColorStringProperty(flashingColor); + searchBox.styleProperty().bind( new SimpleStringProperty("-fx-control-inner-background: ").concat(flashingColorStringProperty).concat(";") ); @@ -196,11 +197,6 @@ private void editAbbreviation() { journalTableNameColumn); } - @FXML - private void removeAbbreviation() { - viewModel.deleteAbbreviation(); - } - private void selectNewAbbreviation() { int lastRow = viewModel.abbreviationsCountProperty().get() - 1; journalAbbreviationsTable.scrollTo(lastRow); diff --git a/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTabViewModel.java b/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTabViewModel.java index 32d8c826b9a..d83eb917ecb 100644 --- a/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTabViewModel.java @@ -2,7 +2,6 @@ import java.io.IOException; import java.nio.file.Path; -import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; @@ -37,19 +36,24 @@ */ public class JournalAbbreviationsTabViewModel implements PreferenceTabViewModel { - private final Logger logger = LoggerFactory.getLogger(JournalAbbreviationsTabViewModel.class); + private final Logger LOGGER = LoggerFactory.getLogger(JournalAbbreviationsTabViewModel.class); + private final SimpleListProperty journalFiles = new SimpleListProperty<>(FXCollections.observableArrayList()); private final SimpleListProperty abbreviations = new SimpleListProperty<>(FXCollections.observableArrayList()); private final SimpleIntegerProperty abbreviationsCount = new SimpleIntegerProperty(); + private final SimpleObjectProperty currentFile = new SimpleObjectProperty<>(); private final SimpleObjectProperty currentAbbreviation = new SimpleObjectProperty<>(); + private final SimpleBooleanProperty isFileRemovable = new SimpleBooleanProperty(); private final SimpleBooleanProperty isLoading = new SimpleBooleanProperty(false); private final SimpleBooleanProperty isEditableAndRemovable = new SimpleBooleanProperty(false); private final SimpleBooleanProperty isAbbreviationEditableAndRemovable = new SimpleBooleanProperty(false); + private final PreferencesService preferences; private final DialogService dialogService; private final TaskExecutor taskExecutor; + private final JournalAbbreviationPreferences abbreviationsPreferences; private final JournalAbbreviationRepository journalAbbreviationRepository; private boolean shouldWriteLists; @@ -180,7 +184,7 @@ private void openFile(Path filePath) { try { abbreviationsFile.readAbbreviations(); } catch (IOException e) { - logger.debug(e.getLocalizedMessage()); + LOGGER.debug(e.getLocalizedMessage()); } } journalFiles.add(abbreviationsFile); @@ -312,6 +316,17 @@ public void deleteAbbreviation() { } } + public void removeAbbreviation(AbbreviationViewModel abbreviation) { + Objects.requireNonNull(abbreviation); + + if (abbreviation.isPseudoAbbreviation()) { + return; + } + + abbreviations.remove(abbreviation); + shouldWriteLists = true; + } + /** * Calls the {@link AbbreviationsFileViewModel#writeOrCreate()} method for each file in the journalFiles property * which will overwrite the existing files with the content of the abbreviations property of the AbbreviationsFile. @@ -322,43 +337,39 @@ public void saveJournalAbbreviationFiles() { try { file.writeOrCreate(); } catch (IOException e) { - logger.debug(e.getLocalizedMessage()); + LOGGER.debug(e.getLocalizedMessage()); } }); } - /** - * This method stores all file paths of the files in the journalFiles property to the global JabRef preferences. - * Pseudo abbreviation files will not be stored. - */ - private void saveExternalFilesList() { - List extFiles = new ArrayList<>(); - journalFiles.stream() - .filter(file -> !file.isBuiltInListProperty().get()) - .forEach(file -> file.getAbsolutePath().ifPresent(path -> extFiles.add(path.toAbsolutePath().toString()))); - abbreviationsPreferences.setExternalJournalLists(extFiles); - } - /** * This method first saves all external files to its internal list, then writes all abbreviations to their files and - * finally updates the abbreviations auto complete. It basically calls {@link #saveExternalFilesList()}, {@link - * #saveJournalAbbreviationFiles() }}. + * finally updates the abbreviations auto complete. */ @Override public void storeSettings() { - BackgroundTask.wrap(() -> { - saveExternalFilesList(); - - if (shouldWriteLists) { - saveJournalAbbreviationFiles(); - shouldWriteLists = false; - } - - preferences.storeJournalAbbreviationPreferences(abbreviationsPreferences); - }).executeWith(taskExecutor); - - // Update journal abbreviation repository - Globals.journalAbbreviationRepository = JournalAbbreviationLoader.loadRepository(preferences.getJournalAbbreviationPreferences()); + BackgroundTask + .wrap(() -> { + List journalStringList = journalFiles.stream() + .filter(path -> !path.isBuiltInListProperty().get()) + .filter(path -> path.getAbsolutePath().isPresent()) + .map(path -> path.getAbsolutePath().get().toAbsolutePath().toString()) + .collect(Collectors.toList()); + + preferences.storeJournalAbbreviationPreferences(new JournalAbbreviationPreferences( + journalStringList, + abbreviationsPreferences.getDefaultEncoding() + )); + + if (shouldWriteLists) { + saveJournalAbbreviationFiles(); + shouldWriteLists = false; + } + }) + .onSuccess((success) -> Globals.journalAbbreviationRepository = + JournalAbbreviationLoader.loadRepository(preferences.getJournalAbbreviationPreferences())) + .onFailure(exception -> LOGGER.error("Failed to store journal preferences.", exception)) + .executeWith(taskExecutor); } public SimpleBooleanProperty isLoadingProperty() { diff --git a/src/main/java/org/jabref/gui/util/ValueTableCellFactory.java b/src/main/java/org/jabref/gui/util/ValueTableCellFactory.java index 38fa12e1585..3275d57b650 100644 --- a/src/main/java/org/jabref/gui/util/ValueTableCellFactory.java +++ b/src/main/java/org/jabref/gui/util/ValueTableCellFactory.java @@ -3,6 +3,7 @@ import java.util.function.BiFunction; import java.util.function.Function; +import javafx.beans.binding.BooleanExpression; import javafx.event.EventHandler; import javafx.scene.Node; import javafx.scene.control.ContextMenu; @@ -27,6 +28,8 @@ public class ValueTableCellFactory implements Callback, private Function toText; private BiFunction toGraphic; private BiFunction> toOnMouseClickedEvent; + private Function toDisableExpression; + private Function toVisibleExpression; private BiFunction toTooltip; private Function contextMenuFactory; private BiFunction menuFactory; @@ -66,6 +69,16 @@ public ValueTableCellFactory withOnMouseClickedEvent(Function withDisableExpression(Function toDisableBinding) { + this.toDisableExpression = toDisableBinding; + return this; + } + + public ValueTableCellFactory withVisibleExpression(Function toVisibleBinding) { + this.toVisibleExpression = toVisibleBinding; + return this; + } + public ValueTableCellFactory withContextMenu(Function contextMenuFactory) { this.contextMenuFactory = contextMenuFactory; return this; @@ -136,6 +149,14 @@ protected void updateItem(T item, boolean empty) { } } }); + + if (toDisableExpression != null) { + disableProperty().bind(toDisableExpression.apply(item)); + } + + if (toVisibleExpression != null) { + visibleProperty().bind(toVisibleExpression.apply(item)); + } } } }; diff --git a/src/main/resources/l10n/JabRef_en.properties b/src/main/resources/l10n/JabRef_en.properties index 76dd9d53222..4c39b711bb8 100644 --- a/src/main/resources/l10n/JabRef_en.properties +++ b/src/main/resources/l10n/JabRef_en.properties @@ -456,6 +456,9 @@ JabRef\ requests\ recommendations\ from\ Mr.\ DLib,\ which\ is\ an\ external\ se JabRef\ Version\ (Required\ to\ ensure\ backwards\ compatibility\ with\ Mr.\ DLib's\ Web\ Service)=JabRef Version (Required to ensure backwards compatibility with Mr. DLib's Web Service) Journal\ abbreviations=Journal abbreviations +Journal\ lists\:=Journal lists: +Remove\ journal\ '%0'=Remove journal '%0' + Keep\ both=Keep both Keep\ subgroups=Keep subgroups @@ -1585,7 +1588,6 @@ Add\ new\ list=Add new list Open\ existing\ list=Open existing list Remove\ list=Remove list Add\ abbreviation=Add abbreviation -Remove\ abbreviation=Remove abbreviation Full\ journal\ name=Full journal name Abbreviation\ name=Abbreviation name Shortest\ unique\ abbreviation=Shortest unique abbreviation diff --git a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelNoShortestUniqueAbbreviationsTabTest.java b/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelNoShortestUniqueAbbreviationsTabTest.java deleted file mode 100644 index 7c355f55cdc..00000000000 --- a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelNoShortestUniqueAbbreviationsTabTest.java +++ /dev/null @@ -1,465 +0,0 @@ -package org.jabref.gui.journals; - -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Arrays; -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import javafx.collections.FXCollections; -import javafx.collections.ObservableList; - -import org.jabref.gui.DialogService; -import org.jabref.gui.preferences.journals.AbbreviationViewModel; -import org.jabref.gui.preferences.journals.AbbreviationsFileViewModel; -import org.jabref.gui.preferences.journals.JournalAbbreviationsTabViewModel; -import org.jabref.gui.util.CurrentThreadTaskExecutor; -import org.jabref.gui.util.TaskExecutor; -import org.jabref.logic.JabRefException; -import org.jabref.logic.journals.Abbreviation; -import org.jabref.logic.journals.JournalAbbreviationLoader; -import org.jabref.logic.journals.JournalAbbreviationPreferences; -import org.jabref.logic.journals.JournalAbbreviationRepository; -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.jabref.logic.util.OS.NEWLINE; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -class JournalAbbreviationsViewModelNoShortestUniqueAbbreviationsTabTest { - - private JournalAbbreviationsTabViewModel viewModel; - private Path emptyTestFile; - private Path testFile1Entries; - private Path testFile3Entries; - private Path testFile4Entries; - private Path testFile5EntriesWithDuplicate; - private JournalAbbreviationPreferences abbreviationPreferences; - private DialogService dialogService; - private final JournalAbbreviationRepository repository = JournalAbbreviationLoader.loadBuiltInRepository(); - - @BeforeEach - void setUpViewModel(@TempDir Path tempFolder) throws Exception { - abbreviationPreferences = mock(JournalAbbreviationPreferences.class); - PreferencesService preferences = mock(PreferencesService.class); - when(preferences.getJournalAbbreviationPreferences()).thenReturn(abbreviationPreferences); - - dialogService = mock(DialogService.class); - TaskExecutor taskExecutor = new CurrentThreadTaskExecutor(); - viewModel = new JournalAbbreviationsTabViewModel(preferences, dialogService, taskExecutor, repository); - emptyTestFile = createTestFile(tempFolder, "emptyTestFile.csv", ""); - testFile1Entries = createTestFile(tempFolder, "testFile1Entries.csv", "Test Entry;TE" + NEWLINE + ""); - testFile3Entries = createTestFile(tempFolder, "testFile3Entries.csv", "Abbreviations;Abb" + NEWLINE + "Test Entry;TE" + NEWLINE + "MoreEntries;ME" + NEWLINE + ""); - testFile4Entries = createTestFile(tempFolder, "testFile4Entries.csv", "Abbreviations;Abb" + NEWLINE + "Test Entry;TE" + NEWLINE + "MoreEntries;ME" + NEWLINE + "Entry;E" + NEWLINE + ""); - testFile5EntriesWithDuplicate = createTestFile(tempFolder, "testFile5Entries.csv", "Abbreviations;Abb" + NEWLINE + "Test Entry;TE" + NEWLINE + "Test Entry;TE" + NEWLINE + "MoreEntries;ME" + NEWLINE + "EntryEntry;EE" + NEWLINE + ""); - } - - @Test - void testInitialHasNoFilesAndNoAbbreviations() { - assertEquals(0, viewModel.journalFilesProperty().size()); - assertEquals(0, viewModel.abbreviationsProperty().size()); - } - - @Test - void testInitialWithSavedFilesIncrementsFilesCounter() throws Exception { - addFourTestFileToViewModelAndPreferences(); - viewModel.createFileObjects(); - - assertEquals(4, viewModel.journalFilesProperty().size()); - } - - @Test - void testRemoveDuplicatesWhenReadingFiles() throws Exception { - addFourTestFileToViewModelAndPreferences(); - viewModel.createFileObjects(); - viewModel.selectLastJournalFile(); - - // should result in 4 real abbreviations and one pseudo abbreviation - assertEquals(5, viewModel.abbreviationsProperty().size()); - } - - @Test - void addFileIncreasesCounterOfOpenFilesAndHasNoAbbreviations() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); - viewModel.addNewFile(); - - assertEquals(1, viewModel.journalFilesProperty().size()); - assertEquals(1, viewModel.abbreviationsProperty().size()); - } - - @Test - void addDuplicatedFileResultsInErrorDialog() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.addNewFile(); - viewModel.addNewFile(); - verify(dialogService).showErrorDialogAndWait(anyString(), anyString()); - } - - @Test - void testOpenDuplicatedFileResultsInAnException() throws Exception { - when(dialogService.showFileOpenDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.openFile(); - viewModel.openFile(); - verify(dialogService).showErrorDialogAndWait(anyString(), anyString()); - } - - @Test - void testSelectLastJournalFileSwitchesFilesAndTheirAbbreviations() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - assertEquals(1, viewModel.abbreviationsCountProperty().get()); - - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - assertEquals(2, viewModel.abbreviationsCountProperty().get()); - } - - @Test - void testOpenValidFileContainsTheSpecificEntryAndEnoughAbbreviations() throws Exception { - Abbreviation testAbbreviation = new Abbreviation("Test Entry", "TE"); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - - assertEquals(1, viewModel.journalFilesProperty().size()); - // our test file has 3 abbreviations and one pseudo abbreviation - assertEquals(4, viewModel.abbreviationsProperty().size()); - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - } - - @Test - void testRemoveLastListSetsCurrentFileAndCurrentAbbreviationToNull() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.addNewFile(); - viewModel.removeCurrentFile(); - - assertEquals(0, viewModel.journalFilesProperty().size()); - assertEquals(0, viewModel.abbreviationsProperty().size()); - assertNull(viewModel.currentFileProperty().get()); - assertNull(viewModel.currentAbbreviationProperty().get()); - } - - @Test - void testMixedFileUsage() throws Exception { - Abbreviation testAbbreviation = new Abbreviation("Entry", "E"); - Abbreviation testAbbreviation2 = new Abbreviation("EntryEntry", "EE"); - - // simulate open file button twice - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); - viewModel.addNewFile(); - viewModel.currentFileProperty().set(viewModel.journalFilesProperty().get(1)); - - // size of the list of journal files should be incremented by two - assertEquals(2, viewModel.journalFilesProperty().size()); - // our second test file has 4 abbreviations - assertEquals(5, viewModel.abbreviationsProperty().size()); - // check some abbreviation - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - - // simulate add new file button - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); - viewModel.addNewFile(); - viewModel.currentFileProperty().set(viewModel.journalFilesProperty().get(2)); - - // size of the list of journal files should be incremented by one - assertEquals(3, viewModel.journalFilesProperty().size()); - // a new file has zero abbreviations - assertEquals(1, viewModel.abbreviationsProperty().size()); - - // simulate open file button - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); - viewModel.addNewFile(); - viewModel.currentFileProperty().set(viewModel.journalFilesProperty().get(3)); - - // size of the list of journal files should be incremented by one - assertEquals(4, viewModel.journalFilesProperty().size()); - - assertEquals(5, viewModel.abbreviationsProperty().size()); - // check some abbreviation - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation2))); - } - - @Test - void testBuiltInListsIncludeAllBuiltInAbbreviations() { - viewModel.addBuiltInList(); - assertEquals(1, viewModel.journalFilesProperty().getSize()); - viewModel.currentFileProperty().set(viewModel.journalFilesProperty().get(0)); - ObservableList expected = FXCollections - .observableArrayList(repository.getAllLoaded()); - ObservableList actualAbbreviations = FXCollections - .observableArrayList(viewModel.abbreviationsProperty().stream() - .map(AbbreviationViewModel::getAbbreviationObject).collect(Collectors.toList())); - - assertEquals(expected, actualAbbreviations); - } - - @Test - void testcurrentFilePropertyChangeActiveFile() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - AbbreviationsFileViewModel test1 = viewModel.journalFilesProperty().get(0); - AbbreviationsFileViewModel test3 = viewModel.journalFilesProperty().get(1); - AbbreviationsFileViewModel test4 = viewModel.journalFilesProperty().get(2); - AbbreviationsFileViewModel test5 = viewModel.journalFilesProperty().get(3); - - // test if the last opened file is active, but duplicated entry has been removed - assertEquals(5, viewModel.abbreviationsProperty().size()); - - viewModel.currentFileProperty().set(test1); - - // test if the current abbreviations matches with the ones in testFile1Entries - assertEquals(2, viewModel.abbreviationsProperty().size()); - - viewModel.currentFileProperty().set(test3); - assertEquals(4, viewModel.abbreviationsProperty().size()); - viewModel.currentFileProperty().set(test1); - assertEquals(2, viewModel.abbreviationsProperty().size()); - viewModel.currentFileProperty().set(test4); - assertEquals(5, viewModel.abbreviationsProperty().size()); - viewModel.currentFileProperty().set(test5); - assertEquals(5, viewModel.abbreviationsProperty().size()); - } - - @Test - void testAddAbbreviationIncludesAbbreviationsInAbbreviationList() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE"); - addAbbrevaition(testAbbreviation); - - assertEquals(6, viewModel.abbreviationsProperty().size()); - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - } - - @Test - void testAddDuplicatedAbbreviationResultsInException() throws JabRefException { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - viewModel.addAbbreviation("YetAnotherEntry", "YAE"); - viewModel.addAbbreviation("YetAnotherEntry", "YAE"); - verify(dialogService).showErrorDialogAndWait(anyString(), anyString()); - } - - @Test - void testEditSameAbbreviationWithNoChangeDoesNotResultInException() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE"); - addAbbrevaition(testAbbreviation); - editAbbreviation(testAbbreviation); - - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - } - - @Test - void testEditAbbreviationIncludesNewAbbreviationInAbbreviationsList() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - selectLastAbbreviation(); - Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE"); - editAbbreviation(testAbbreviation); - - assertEquals(5, viewModel.abbreviationsProperty().size()); - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - editAbbreviation(testAbbreviation); - - assertEquals(1, viewModel.abbreviationsProperty().size()); - assertFalse(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - } - - @Test - void testEditAbbreviationToExistingOneResultsInException() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - selectLastAbbreviation(); - - assertEquals(4, viewModel.abbreviationsProperty().size()); - - viewModel.editAbbreviation("YetAnotherEntry", "YAE"); - viewModel.currentAbbreviationProperty().set(viewModel.abbreviationsProperty().get(2)); - viewModel.editAbbreviation("YetAnotherEntry", "YAE"); - verify(dialogService).showErrorDialogAndWait(anyString(), anyString()); - } - - @Test - void testEditAbbreviationToEmptyNameResultsInException() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - selectLastAbbreviation(); - - assertEquals(4, viewModel.abbreviationsProperty().size()); - - viewModel.editAbbreviation("", "YAE"); - verify(dialogService).showErrorDialogAndWait(anyString()); - } - - @Test - void testEditAbbreviationToEmptyAbbreviationResultsInException() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - selectLastAbbreviation(); - - assertEquals(4, viewModel.abbreviationsProperty().size()); - - viewModel.editAbbreviation("YetAnotherEntry", ""); - verify(dialogService).showErrorDialogAndWait(anyString()); - } - - @Test - void testDeleteAbbreviationSelectsPreviousOne() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE"); - addAbbrevaition(testAbbreviation); - - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - assertEquals(new AbbreviationViewModel(testAbbreviation), viewModel.currentAbbreviationProperty().get()); - - viewModel.deleteAbbreviation(); - - assertEquals(5, viewModel.abbreviationsProperty().size()); - // check if the previous (the last) element is the current abbreviation - assertEquals(viewModel.currentAbbreviationProperty().get(), viewModel.abbreviationsProperty().get(4)); - } - - @Test - void testDeleteAbbreviationSelectsNextOne() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE"); - addAbbrevaition(testAbbreviation); - viewModel.currentAbbreviationProperty().set(viewModel.abbreviationsProperty().get(1)); - viewModel.deleteAbbreviation(); - - assertEquals(new AbbreviationViewModel(testAbbreviation), viewModel.currentAbbreviationProperty().get()); - } - - @Test - void testSaveAbbreviationsToFilesCreatesNewFilesWithWrittenAbbreviations() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - selectLastAbbreviation(); - Abbreviation testAbbreviation = new Abbreviation("JabRefTestEntry", "JTE"); - editAbbreviation(testAbbreviation); - - assertEquals(5, viewModel.abbreviationsProperty().size()); - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - selectLastAbbreviation(); - viewModel.deleteAbbreviation(); - Abbreviation testAbbreviation1 = new Abbreviation("SomeOtherEntry", "SOE"); - addAbbrevaition(testAbbreviation1); - - assertEquals(5, viewModel.abbreviationsProperty().size()); - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation1))); - - viewModel.saveJournalAbbreviationFiles(); - List expected = Arrays.asList( - "Abbreviations;Abb;Abb", - "Test Entry;TE;TE", - "MoreEntries;ME;ME", - "JabRefTestEntry;JTE;JTE"); - List actual = Files.readAllLines(testFile4Entries, StandardCharsets.UTF_8); - - assertEquals(expected, actual); - - expected = Arrays.asList( - "EntryEntry;EE;EE", - "Abbreviations;Abb;Abb", - "Test Entry;TE;TE", - "SomeOtherEntry;SOE;SOE"); - actual = Files.readAllLines(testFile5EntriesWithDuplicate, StandardCharsets.UTF_8); - - assertEquals(expected, actual); - } - - @Test - void testSaveExternalFilesListToPreferences() throws Exception { - addFourTestFileToViewModelAndPreferences(); - List expected = Stream.of(testFile1Entries, testFile3Entries, testFile4Entries, testFile5EntriesWithDuplicate) - .map(Path::toString).collect(Collectors.toList()); - verify(abbreviationPreferences).setExternalJournalLists(expected); - } - - private Path createTestFile(Path folder, String name, String content) throws Exception { - Path file = folder.resolve(name); - Files.writeString(file, content); - return file; - } - - private void addAbbrevaition(Abbreviation testAbbreviation) throws Exception { - viewModel.addAbbreviation(testAbbreviation.getName(), testAbbreviation.getAbbreviation()); - } - - private void editAbbreviation(Abbreviation testAbbreviation) throws Exception { - viewModel.editAbbreviation(testAbbreviation.getName(), testAbbreviation.getAbbreviation()); - } - - private void addFourTestFileToViewModelAndPreferences() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); - viewModel.addNewFile(); - viewModel.storeSettings(); - } - - /** - * Select the last abbreviation in the list of abbreviations - */ - private void selectLastAbbreviation() { - viewModel.currentAbbreviationProperty() - .set(viewModel.abbreviationsProperty().get(viewModel.abbreviationsCountProperty().get() - 1)); - } -} diff --git a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelWithShortestUniqueAbbreviationsTabTest.java b/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelWithShortestUniqueAbbreviationsTabTest.java deleted file mode 100644 index f0811c06f45..00000000000 --- a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelWithShortestUniqueAbbreviationsTabTest.java +++ /dev/null @@ -1,465 +0,0 @@ -package org.jabref.gui.journals; - -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Arrays; -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import javafx.collections.FXCollections; -import javafx.collections.ObservableList; - -import org.jabref.gui.DialogService; -import org.jabref.gui.preferences.journals.AbbreviationViewModel; -import org.jabref.gui.preferences.journals.AbbreviationsFileViewModel; -import org.jabref.gui.preferences.journals.JournalAbbreviationsTabViewModel; -import org.jabref.gui.util.CurrentThreadTaskExecutor; -import org.jabref.gui.util.TaskExecutor; -import org.jabref.logic.JabRefException; -import org.jabref.logic.journals.Abbreviation; -import org.jabref.logic.journals.JournalAbbreviationLoader; -import org.jabref.logic.journals.JournalAbbreviationPreferences; -import org.jabref.logic.journals.JournalAbbreviationRepository; -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.jabref.logic.util.OS.NEWLINE; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -class JournalAbbreviationsViewModelWithShortestUniqueAbbreviationsTabTest { - - private JournalAbbreviationsTabViewModel viewModel; - private Path emptyTestFile; - private Path testFile1Entries; - private Path testFile3Entries; - private Path testFile4Entries; - private Path testFile5EntriesWithDuplicate; - private JournalAbbreviationPreferences abbreviationPreferences; - private DialogService dialogService; - private final JournalAbbreviationRepository repository = JournalAbbreviationLoader.loadBuiltInRepository(); - - @BeforeEach - void setUpViewModel(@TempDir Path tempFolder) throws Exception { - abbreviationPreferences = mock(JournalAbbreviationPreferences.class); - PreferencesService preferences = mock(PreferencesService.class); - when(preferences.getJournalAbbreviationPreferences()).thenReturn(abbreviationPreferences); - - dialogService = mock(DialogService.class); - TaskExecutor taskExecutor = new CurrentThreadTaskExecutor(); - viewModel = new JournalAbbreviationsTabViewModel(preferences, dialogService, taskExecutor, repository); - emptyTestFile = createTestFile(tempFolder, "emptyTestFile.csv", ""); - testFile1Entries = createTestFile(tempFolder, "testFile1Entries.csv", "Test Entry;TE;T" + NEWLINE + ""); - testFile3Entries = createTestFile(tempFolder, "testFile3Entries.csv", "Abbreviations;Abb;A" + NEWLINE + "Test Entry;TE;T" + NEWLINE + "MoreEntries;ME;M" + NEWLINE + ""); - testFile4Entries = createTestFile(tempFolder, "testFile4Entries.csv", "Abbreviations;Abb;A" + NEWLINE + "Test Entry;TE;T" + NEWLINE + "MoreEntries;ME;M" + NEWLINE + "Entry;En;E" + NEWLINE + ""); - testFile5EntriesWithDuplicate = createTestFile(tempFolder, "testFile5Entries.csv", "Abbreviations;Abb;A" + NEWLINE + "Test Entry;TE;T" + NEWLINE + "Test Entry;TE;T" + NEWLINE + "MoreEntries;ME;M" + NEWLINE + "EntryEntry;EE" + NEWLINE + ""); - } - - @Test - void testInitialHasNoFilesAndNoAbbreviations() { - assertEquals(0, viewModel.journalFilesProperty().size()); - assertEquals(0, viewModel.abbreviationsProperty().size()); - } - - @Test - void testInitialWithSavedFilesIncrementsFilesCounter() throws Exception { - addFourTestFileToViewModelAndPreferences(); - viewModel.createFileObjects(); - - assertEquals(4, viewModel.journalFilesProperty().size()); - } - - @Test - void testRemoveDuplicatesWhenReadingFiles() throws Exception { - addFourTestFileToViewModelAndPreferences(); - viewModel.createFileObjects(); - viewModel.selectLastJournalFile(); - - // should result in 4 real abbreviations and one pseudo abbreviation - assertEquals(5, viewModel.abbreviationsProperty().size()); - } - - @Test - void addFileIncreasesCounterOfOpenFilesAndHasNoAbbreviations() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); - viewModel.addNewFile(); - - assertEquals(1, viewModel.journalFilesProperty().size()); - assertEquals(1, viewModel.abbreviationsProperty().size()); - } - - @Test - void addDuplicatedFileResultsInErrorDialog() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.addNewFile(); - viewModel.addNewFile(); - verify(dialogService).showErrorDialogAndWait(anyString(), anyString()); - } - - @Test - void testOpenDuplicatedFileResultsInAnException() throws Exception { - when(dialogService.showFileOpenDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.openFile(); - viewModel.openFile(); - verify(dialogService).showErrorDialogAndWait(anyString(), anyString()); - } - - @Test - void testSelectLastJournalFileSwitchesFilesAndTheirAbbreviations() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - assertEquals(1, viewModel.abbreviationsCountProperty().get()); - - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - assertEquals(2, viewModel.abbreviationsCountProperty().get()); - } - - @Test - void testOpenValidFileContainsTheSpecificEntryAndEnoughAbbreviations() throws Exception { - Abbreviation testAbbreviation = new Abbreviation("Test Entry", "TE"); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - - assertEquals(1, viewModel.journalFilesProperty().size()); - // our test file has 3 abbreviations and one pseudo abbreviation - assertEquals(4, viewModel.abbreviationsProperty().size()); - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - } - - @Test - void testRemoveLastListSetsCurrentFileAndCurrentAbbreviationToNull() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.addNewFile(); - viewModel.removeCurrentFile(); - - assertEquals(0, viewModel.journalFilesProperty().size()); - assertEquals(0, viewModel.abbreviationsProperty().size()); - assertNull(viewModel.currentFileProperty().get()); - assertNull(viewModel.currentAbbreviationProperty().get()); - } - - @Test - void testMixedFileUsage() throws Exception { - Abbreviation testAbbreviation = new Abbreviation("Entry", "En", "E"); - Abbreviation testAbbreviation2 = new Abbreviation("EntryEntry", "EnEn", "EE"); - - // simulate open file button twice - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); - viewModel.addNewFile(); - viewModel.currentFileProperty().set(viewModel.journalFilesProperty().get(1)); - - // size of the list of journal files should be incremented by two - assertEquals(2, viewModel.journalFilesProperty().size()); - // our second test file has 4 abbreviations - assertEquals(5, viewModel.abbreviationsProperty().size()); - // check some abbreviation - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - - // simulate add new file button - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); - viewModel.addNewFile(); - viewModel.currentFileProperty().set(viewModel.journalFilesProperty().get(2)); - - // size of the list of journal files should be incremented by one - assertEquals(3, viewModel.journalFilesProperty().size()); - // a new file has zero abbreviations - assertEquals(1, viewModel.abbreviationsProperty().size()); - - // simulate open file button - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); - viewModel.addNewFile(); - viewModel.currentFileProperty().set(viewModel.journalFilesProperty().get(3)); - - // size of the list of journal files should be incremented by one - assertEquals(4, viewModel.journalFilesProperty().size()); - - assertEquals(5, viewModel.abbreviationsProperty().size()); - // check some abbreviation - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation2))); - } - - @Test - void testBuiltInListsIncludeAllBuiltInAbbreviations() { - viewModel.addBuiltInList(); - assertEquals(1, viewModel.journalFilesProperty().getSize()); - viewModel.currentFileProperty().set(viewModel.journalFilesProperty().get(0)); - ObservableList expected = FXCollections - .observableArrayList(repository.getAllLoaded()); - ObservableList actualAbbreviations = FXCollections - .observableArrayList(viewModel.abbreviationsProperty().stream() - .map(AbbreviationViewModel::getAbbreviationObject).collect(Collectors.toList())); - - assertEquals(expected, actualAbbreviations); - } - - @Test - void testcurrentFilePropertyChangeActiveFile() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - AbbreviationsFileViewModel test1 = viewModel.journalFilesProperty().get(0); - AbbreviationsFileViewModel test3 = viewModel.journalFilesProperty().get(1); - AbbreviationsFileViewModel test4 = viewModel.journalFilesProperty().get(2); - AbbreviationsFileViewModel test5 = viewModel.journalFilesProperty().get(3); - - // test if the last opened file is active, but duplicated entry has been removed - assertEquals(5, viewModel.abbreviationsProperty().size()); - - viewModel.currentFileProperty().set(test1); - - // test if the current abbreviations matches with the ones in testFile1Entries - assertEquals(2, viewModel.abbreviationsProperty().size()); - - viewModel.currentFileProperty().set(test3); - assertEquals(4, viewModel.abbreviationsProperty().size()); - viewModel.currentFileProperty().set(test1); - assertEquals(2, viewModel.abbreviationsProperty().size()); - viewModel.currentFileProperty().set(test4); - assertEquals(5, viewModel.abbreviationsProperty().size()); - viewModel.currentFileProperty().set(test5); - assertEquals(5, viewModel.abbreviationsProperty().size()); - } - - @Test - void testAddAbbreviationIncludesAbbreviationsInAbbreviationList() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE", "Y"); - addAbbrevaition(testAbbreviation); - - assertEquals(6, viewModel.abbreviationsProperty().size()); - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - } - - @Test - void testAddDuplicatedAbbreviationResultsInException() throws JabRefException { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - viewModel.addAbbreviation("YetAnotherEntry", "YAE", "Y"); - viewModel.addAbbreviation("YetAnotherEntry", "YAE", "Y"); - verify(dialogService).showErrorDialogAndWait(anyString(), anyString()); - } - - @Test - void testEditSameAbbreviationWithNoChangeDoesNotResultInException() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE", "Y"); - addAbbrevaition(testAbbreviation); - editAbbreviation(testAbbreviation); - - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - } - - @Test - void testEditAbbreviationIncludesNewAbbreviationInAbbreviationsList() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - selectLastAbbreviation(); - Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE", "Y"); - editAbbreviation(testAbbreviation); - - assertEquals(5, viewModel.abbreviationsProperty().size()); - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - editAbbreviation(testAbbreviation); - - assertEquals(1, viewModel.abbreviationsProperty().size()); - assertFalse(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - } - - @Test - void testEditAbbreviationToExistingOneResultsInException() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - selectLastAbbreviation(); - - assertEquals(4, viewModel.abbreviationsProperty().size()); - - viewModel.editAbbreviation("YetAnotherEntry", "YAE", "Y"); - viewModel.currentAbbreviationProperty().set(viewModel.abbreviationsProperty().get(2)); - viewModel.editAbbreviation("YetAnotherEntry", "YAE", "Y"); - verify(dialogService).showErrorDialogAndWait(anyString(), anyString()); - } - - @Test - void testEditAbbreviationToEmptyNameResultsInException() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - selectLastAbbreviation(); - - assertEquals(4, viewModel.abbreviationsProperty().size()); - - viewModel.editAbbreviation("", "YAE", "Y"); - verify(dialogService).showErrorDialogAndWait(anyString()); - } - - @Test - void testEditAbbreviationToEmptyAbbreviationResultsInException() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - selectLastAbbreviation(); - - assertEquals(4, viewModel.abbreviationsProperty().size()); - - viewModel.editAbbreviation("YetAnotherEntry", "", "Y"); - verify(dialogService).showErrorDialogAndWait(anyString()); - } - - @Test - void testDeleteAbbreviationSelectsPreviousOne() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE", "Y"); - addAbbrevaition(testAbbreviation); - - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - assertEquals(new AbbreviationViewModel(testAbbreviation), viewModel.currentAbbreviationProperty().get()); - - viewModel.deleteAbbreviation(); - - assertEquals(5, viewModel.abbreviationsProperty().size()); - // check if the previous (the last) element is the current abbreviation - assertEquals(viewModel.currentAbbreviationProperty().get(), viewModel.abbreviationsProperty().get(4)); - } - - @Test - void testDeleteAbbreviationSelectsNextOne() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE", "Y"); - addAbbrevaition(testAbbreviation); - viewModel.currentAbbreviationProperty().set(viewModel.abbreviationsProperty().get(1)); - viewModel.deleteAbbreviation(); - - assertEquals(new AbbreviationViewModel(testAbbreviation), viewModel.currentAbbreviationProperty().get()); - } - - @Test - void testSaveAbbreviationsToFilesCreatesNewFilesWithWrittenAbbreviations() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - selectLastAbbreviation(); - Abbreviation testAbbreviation = new Abbreviation("JabRefTestEntry", "JTE"); - editAbbreviation(testAbbreviation); - - assertEquals(5, viewModel.abbreviationsProperty().size()); - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - selectLastAbbreviation(); - viewModel.deleteAbbreviation(); - Abbreviation testAbbreviation1 = new Abbreviation("SomeOtherEntry", "SOE"); - addAbbrevaition(testAbbreviation1); - - assertEquals(5, viewModel.abbreviationsProperty().size()); - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation1))); - - viewModel.saveJournalAbbreviationFiles(); - List expected = Arrays.asList( - "Abbreviations;Abb;A", - "Test Entry;TE;T", - "MoreEntries;ME;M", - "JabRefTestEntry;JTE;JTE"); - List actual = Files.readAllLines(testFile4Entries, StandardCharsets.UTF_8); - - assertEquals(expected, actual); - - expected = Arrays.asList( - "EntryEntry;EE;EE", - "Abbreviations;Abb;A", - "Test Entry;TE;T", - "SomeOtherEntry;SOE;SOE"); - actual = Files.readAllLines(testFile5EntriesWithDuplicate, StandardCharsets.UTF_8); - - assertEquals(expected, actual); - } - - @Test - void testSaveExternalFilesListToPreferences() throws Exception { - addFourTestFileToViewModelAndPreferences(); - List expected = Stream.of(testFile1Entries, testFile3Entries, testFile4Entries, testFile5EntriesWithDuplicate) - .map(Path::toString).collect(Collectors.toList()); - verify(abbreviationPreferences).setExternalJournalLists(expected); - } - - private Path createTestFile(Path folder, String name, String content) throws Exception { - Path file = folder.resolve(name); - Files.writeString(file, content); - return file; - } - - private void addAbbrevaition(Abbreviation testAbbreviation) throws Exception { - viewModel.addAbbreviation(testAbbreviation.getName(), testAbbreviation.getAbbreviation()); - } - - private void editAbbreviation(Abbreviation testAbbreviation) throws Exception { - viewModel.editAbbreviation(testAbbreviation.getName(), testAbbreviation.getAbbreviation()); - } - - private void addFourTestFileToViewModelAndPreferences() throws Exception { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); - viewModel.addNewFile(); - viewModel.storeSettings(); - } - - /** - * Select the last abbreviation in the list of abbreviations - */ - private void selectLastAbbreviation() { - viewModel.currentAbbreviationProperty() - .set(viewModel.abbreviationsProperty().get(viewModel.abbreviationsCountProperty().get() - 1)); - } -} diff --git a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelMixedAbbreviationsTabTest.java b/src/test/java/org/jabref/gui/preferences/journals/JournalAbbreviationsViewModelTabTest.java similarity index 58% rename from src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelMixedAbbreviationsTabTest.java rename to src/test/java/org/jabref/gui/preferences/journals/JournalAbbreviationsViewModelTabTest.java index 9c906027888..c7db04c42e4 100644 --- a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelMixedAbbreviationsTabTest.java +++ b/src/test/java/org/jabref/gui/preferences/journals/JournalAbbreviationsViewModelTabTest.java @@ -1,9 +1,9 @@ -package org.jabref.gui.journals; +package org.jabref.gui.preferences.journals; +import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -13,9 +13,6 @@ import javafx.collections.ObservableList; import org.jabref.gui.DialogService; -import org.jabref.gui.preferences.journals.AbbreviationViewModel; -import org.jabref.gui.preferences.journals.AbbreviationsFileViewModel; -import org.jabref.gui.preferences.journals.JournalAbbreviationsTabViewModel; import org.jabref.gui.util.CurrentThreadTaskExecutor; import org.jabref.gui.util.TaskExecutor; import org.jabref.logic.journals.Abbreviation; @@ -27,6 +24,9 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import static org.jabref.logic.util.OS.NEWLINE; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -39,32 +39,62 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -class JournalAbbreviationsViewModelMixedAbbreviationsTabTest { +class JournalAbbreviationsViewModelTabTest { private JournalAbbreviationsTabViewModel viewModel; private Path emptyTestFile; - private Path testFile1Entries; - private Path testFile3Entries; - private Path testFile4Entries; - private Path testFile5EntriesWithDuplicate; - private JournalAbbreviationPreferences abbreviationPreferences; + private Path tempFolder; + private PreferencesService preferencesService; private final JournalAbbreviationRepository repository = JournalAbbreviationLoader.loadBuiltInRepository(); private DialogService dialogService; + public static Stream provideTestFiles() { + return Stream.of( + // Mixed abbreviations + Arguments.of( + List.of(List.of("testFile1Entries.csv", "Test Entry;TE" + NEWLINE + ""), + List.of("testFile3Entries.csv", "Abbreviations;Abb;A" + NEWLINE + "Test Entry;TE" + NEWLINE + "MoreEntries;ME;M" + NEWLINE + ""), + List.of("testFile4Entries.csv", "Abbreviations;Abb" + NEWLINE + "Test Entry;TE;T" + NEWLINE + "MoreEntries;ME;M" + NEWLINE + "Entry;E" + NEWLINE + ""), + List.of("testFile5Entries.csv", "Abbreviations;Abb" + NEWLINE + "Test Entry;TE;T" + NEWLINE + "Test Entry;TE" + NEWLINE + "MoreEntries;ME;M" + NEWLINE + "EntryEntry;EE" + NEWLINE + "")), + List.of( + List.of("Abbreviations;Abb;Abb", "Test Entry;TE;T", "MoreEntries;ME;M", "JabRefTestEntry;JTE;JTE"), + List.of("EntryEntry;EE;EE", "Abbreviations;Abb;Abb", "Test Entry;TE;T", "SomeOtherEntry;SOE;SOE"))), + + // No shortest unique abbreviations + Arguments.of( + List.of(List.of("testFile1Entries.csv", "Test Entry;TE" + NEWLINE + ""), + List.of("testFile3Entries.csv", "Abbreviations;Abb" + NEWLINE + "Test Entry;TE" + NEWLINE + "MoreEntries;ME" + NEWLINE + ""), + List.of("testFile4Entries.csv", "Abbreviations;Abb" + NEWLINE + "Test Entry;TE" + NEWLINE + "MoreEntries;ME" + NEWLINE + "Entry;E" + NEWLINE + ""), + List.of("testFile5Entries.csv", "Abbreviations;Abb" + NEWLINE + "Test Entry;TE" + NEWLINE + "Test Entry;TE" + NEWLINE + "MoreEntries;ME" + NEWLINE + "EntryEntry;EE" + NEWLINE + "")), + List.of( + List.of("Abbreviations;Abb;Abb", "Test Entry;TE;TE", "MoreEntries;ME;ME", "JabRefTestEntry;JTE;JTE"), + List.of("EntryEntry;EE;EE", "Abbreviations;Abb;Abb", "Test Entry;TE;TE", "SomeOtherEntry;SOE;SOE"))), + + // Shortest unique abbreviations + Arguments.of( + List.of(List.of("testFile1Entries.csv", "Test Entry;TE;T" + NEWLINE + ""), + List.of("testFile3Entries.csv", "Abbreviations;Abb;A" + NEWLINE + "Test Entry;TE;T" + NEWLINE + "MoreEntries;ME;M" + NEWLINE + ""), + List.of("testFile4Entries.csv", "Abbreviations;Abb;A" + NEWLINE + "Test Entry;TE;T" + NEWLINE + "MoreEntries;ME;M" + NEWLINE + "Entry;En;E" + NEWLINE + ""), + List.of("testFile5Entries.csv", "Abbreviations;Abb;A" + NEWLINE + "Test Entry;TE;T" + NEWLINE + "Test Entry;TE;T" + NEWLINE + "MoreEntries;ME;M" + NEWLINE + "EntryEntry;EE" + NEWLINE + "")), + List.of( + List.of("Abbreviations;Abb;A", "Test Entry;TE;T", "MoreEntries;ME;M", "JabRefTestEntry;JTE;JTE"), + List.of("EntryEntry;EE;EE", "Abbreviations;Abb;A", "Test Entry;TE;T", "SomeOtherEntry;SOE;SOE"))) + ); + } + @BeforeEach void setUpViewModel(@TempDir Path tempFolder) throws Exception { - abbreviationPreferences = mock(JournalAbbreviationPreferences.class); - PreferencesService preferences = mock(PreferencesService.class); - when(preferences.getJournalAbbreviationPreferences()).thenReturn(abbreviationPreferences); + JournalAbbreviationPreferences abbreviationPreferences = mock(JournalAbbreviationPreferences.class); + preferencesService = mock(PreferencesService.class); + when(preferencesService.getJournalAbbreviationPreferences()).thenReturn(abbreviationPreferences); dialogService = mock(DialogService.class); + this.tempFolder = tempFolder; + TaskExecutor taskExecutor = new CurrentThreadTaskExecutor(); - viewModel = new JournalAbbreviationsTabViewModel(preferences, dialogService, taskExecutor, repository); - emptyTestFile = createTestFile(tempFolder, "emptyTestFile.csv", ""); - testFile1Entries = createTestFile(tempFolder, "testFile1Entries.csv", "Test Entry;TE" + NEWLINE + ""); - testFile3Entries = createTestFile(tempFolder, "testFile3Entries.csv", "Abbreviations;Abb;A" + NEWLINE + "Test Entry;TE" + NEWLINE + "MoreEntries;ME;M" + NEWLINE + ""); - testFile4Entries = createTestFile(tempFolder, "testFile4Entries.csv", "Abbreviations;Abb" + NEWLINE + "Test Entry;TE;T" + NEWLINE + "MoreEntries;ME;M" + NEWLINE + "Entry;E" + NEWLINE + ""); - testFile5EntriesWithDuplicate = createTestFile(tempFolder, "testFile5Entries.csv", "Abbreviations;Abb" + NEWLINE + "Test Entry;TE;T" + NEWLINE + "Test Entry;TE" + NEWLINE + "MoreEntries;ME;M" + NEWLINE + "EntryEntry;EE" + NEWLINE + ""); + viewModel = new JournalAbbreviationsTabViewModel(preferencesService, dialogService, taskExecutor, repository); + + emptyTestFile = createTestFile("emptyTestFile.csv", ""); } @Test @@ -73,22 +103,21 @@ void testInitialHasNoFilesAndNoAbbreviations() { assertEquals(0, viewModel.abbreviationsProperty().size()); } - @Test - void testInitialWithSavedFilesIncrementsFilesCounter() { - addFourTestFileToViewModelAndPreferences(); - viewModel.createFileObjects(); - + @ParameterizedTest + @MethodSource("provideTestFiles") + void testInitialWithSavedFilesIncrementsFilesCounter(List> testFiles) throws IOException { + addFourTestFileToViewModelAndPreferences(testFiles); assertEquals(4, viewModel.journalFilesProperty().size()); } - @Test - void testRemoveDuplicatesWhenReadingFiles() { - addFourTestFileToViewModelAndPreferences(); - viewModel.createFileObjects(); + @ParameterizedTest + @MethodSource("provideTestFiles") + void testRemoveDuplicatesWhenReadingFiles(List> testFiles) throws IOException { + addFourTestFileToViewModelAndPreferences(testFiles); viewModel.selectLastJournalFile(); - // should result in 4 real abbreviations and one pseudo abbreviation - assertEquals(5, viewModel.abbreviationsProperty().size()); + assertEquals(4, viewModel.journalFilesProperty().size()); + assertEquals(4, viewModel.abbreviationsProperty().size()); } @Test @@ -97,54 +126,58 @@ void addFileIncreasesCounterOfOpenFilesAndHasNoAbbreviations() { viewModel.addNewFile(); assertEquals(1, viewModel.journalFilesProperty().size()); - assertEquals(1, viewModel.abbreviationsProperty().size()); + assertEquals(0, viewModel.abbreviationsProperty().size()); } - @Test - void addDuplicatedFileResultsInErrorDialog() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); + @ParameterizedTest + @MethodSource("provideTestFiles") + void addDuplicatedFileResultsInErrorDialog(List> testFiles) throws IOException { + when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(0)))); viewModel.addNewFile(); viewModel.addNewFile(); verify(dialogService).showErrorDialogAndWait(anyString(), anyString()); } - @Test - void testOpenDuplicatedFileResultsInAnException() { - when(dialogService.showFileOpenDialog(any())).thenReturn(Optional.of(testFile1Entries)); + @ParameterizedTest + @MethodSource("provideTestFiles") + void testOpenDuplicatedFileResultsInAnException(List> testFiles) throws IOException { + when(dialogService.showFileOpenDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(0)))); viewModel.openFile(); viewModel.openFile(); verify(dialogService).showErrorDialogAndWait(anyString(), anyString()); } - @Test - void testSelectLastJournalFileSwitchesFilesAndTheirAbbreviations() { + @ParameterizedTest + @MethodSource("provideTestFiles") + void testSelectLastJournalFileSwitchesFilesAndTheirAbbreviations(List> testFiles) throws IOException { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); - assertEquals(1, viewModel.abbreviationsCountProperty().get()); + assertEquals(0, viewModel.abbreviationsCountProperty().get()); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); + when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(0)))); viewModel.addNewFile(); viewModel.selectLastJournalFile(); - assertEquals(2, viewModel.abbreviationsCountProperty().get()); + assertEquals(1, viewModel.abbreviationsCountProperty().get()); } - @Test - void testOpenValidFileContainsTheSpecificEntryAndEnoughAbbreviations() { + @ParameterizedTest + @MethodSource("provideTestFiles") + void testOpenValidFileContainsTheSpecificEntryAndEnoughAbbreviations(List> testFiles) throws IOException { Abbreviation testAbbreviation = new Abbreviation("Test Entry", "TE"); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); + when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(1)))); viewModel.addNewFile(); viewModel.selectLastJournalFile(); assertEquals(1, viewModel.journalFilesProperty().size()); - // our test file has 3 abbreviations and one pseudo abbreviation - assertEquals(4, viewModel.abbreviationsProperty().size()); + assertEquals(3, viewModel.abbreviationsProperty().size()); assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); } - @Test - void testRemoveLastListSetsCurrentFileAndCurrentAbbreviationToNull() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); + @ParameterizedTest + @MethodSource("provideTestFiles") + void testRemoveLastListSetsCurrentFileAndCurrentAbbreviationToNull(List> testFiles) throws IOException { + when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(0)))); viewModel.addNewFile(); viewModel.removeCurrentFile(); @@ -154,22 +187,23 @@ void testRemoveLastListSetsCurrentFileAndCurrentAbbreviationToNull() { assertNull(viewModel.currentAbbreviationProperty().get()); } - @Test - void testMixedFileUsage() { + @ParameterizedTest + @MethodSource("provideTestFiles") + void testMixedFileUsage(List> testFiles) throws IOException { Abbreviation testAbbreviation = new Abbreviation("Entry", "E"); Abbreviation testAbbreviation2 = new Abbreviation("EntryEntry", "EE"); // simulate open file button twice - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); + when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(1)))); viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); + when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(2)))); viewModel.addNewFile(); viewModel.currentFileProperty().set(viewModel.journalFilesProperty().get(1)); // size of the list of journal files should be incremented by two assertEquals(2, viewModel.journalFilesProperty().size()); // our second test file has 4 abbreviations - assertEquals(5, viewModel.abbreviationsProperty().size()); + assertEquals(4, viewModel.abbreviationsProperty().size()); // check some abbreviation assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); @@ -181,17 +215,17 @@ void testMixedFileUsage() { // size of the list of journal files should be incremented by one assertEquals(3, viewModel.journalFilesProperty().size()); // a new file has zero abbreviations - assertEquals(1, viewModel.abbreviationsProperty().size()); + assertEquals(0, viewModel.abbreviationsProperty().size()); // simulate open file button - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); + when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(3)))); viewModel.addNewFile(); viewModel.currentFileProperty().set(viewModel.journalFilesProperty().get(3)); // size of the list of journal files should be incremented by one assertEquals(4, viewModel.journalFilesProperty().size()); - assertEquals(5, viewModel.abbreviationsProperty().size()); + assertEquals(4, viewModel.abbreviationsProperty().size()); // check some abbreviation assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation2))); } @@ -210,57 +244,57 @@ void testBuiltInListsIncludeAllBuiltInAbbreviations() { assertEquals(expected, actualAbbreviations); } - @Test - void testCurrentFilePropertyChangeActiveFile() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); - viewModel.addNewFile(); + @ParameterizedTest + @MethodSource("provideTestFiles") + void testCurrentFilePropertyChangeActiveFile(List> testFiles) throws IOException { + for (List testFile : testFiles) { + when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFile))); + viewModel.addNewFile(); + } viewModel.selectLastJournalFile(); + AbbreviationsFileViewModel test1 = viewModel.journalFilesProperty().get(0); AbbreviationsFileViewModel test3 = viewModel.journalFilesProperty().get(1); AbbreviationsFileViewModel test4 = viewModel.journalFilesProperty().get(2); AbbreviationsFileViewModel test5 = viewModel.journalFilesProperty().get(3); // test if the last opened file is active, but duplicated entry has been removed - assertEquals(5, viewModel.abbreviationsProperty().size()); + assertEquals(4, viewModel.abbreviationsProperty().size()); viewModel.currentFileProperty().set(test1); // test if the current abbreviations matches with the ones in testFile1Entries - assertEquals(2, viewModel.abbreviationsProperty().size()); + assertEquals(1, viewModel.abbreviationsProperty().size()); viewModel.currentFileProperty().set(test3); - assertEquals(4, viewModel.abbreviationsProperty().size()); + assertEquals(3, viewModel.abbreviationsProperty().size()); viewModel.currentFileProperty().set(test1); - assertEquals(2, viewModel.abbreviationsProperty().size()); + assertEquals(1, viewModel.abbreviationsProperty().size()); viewModel.currentFileProperty().set(test4); - assertEquals(5, viewModel.abbreviationsProperty().size()); + assertEquals(4, viewModel.abbreviationsProperty().size()); viewModel.currentFileProperty().set(test5); - assertEquals(5, viewModel.abbreviationsProperty().size()); + assertEquals(4, viewModel.abbreviationsProperty().size()); } - @Test - void testAddAbbreviationIncludesAbbreviationsInAbbreviationList() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); + @ParameterizedTest + @MethodSource("provideTestFiles") + void testAddAbbreviationIncludesAbbreviationsInAbbreviationList(List> testFiles) throws IOException { + when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(2)))); viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); + when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(3)))); viewModel.addNewFile(); viewModel.selectLastJournalFile(); Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE"); - addAbbrevaition(testAbbreviation); + addAbbreviation(testAbbreviation); - assertEquals(6, viewModel.abbreviationsProperty().size()); + assertEquals(5, viewModel.abbreviationsProperty().size()); assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); } - @Test - void testAddDuplicatedAbbreviationResultsInException() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); + @ParameterizedTest + @MethodSource("provideTestFiles") + void testAddDuplicatedAbbreviationResultsInException(List> testFiles) throws IOException { + when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(1)))); viewModel.addNewFile(); viewModel.selectLastJournalFile(); viewModel.addAbbreviation("YetAnotherEntry", "YAE"); @@ -274,22 +308,23 @@ void testEditSameAbbreviationWithNoChangeDoesNotResultInException() { viewModel.addNewFile(); viewModel.selectLastJournalFile(); Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE"); - addAbbrevaition(testAbbreviation); + addAbbreviation(testAbbreviation); editAbbreviation(testAbbreviation); assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); } - @Test - void testEditAbbreviationIncludesNewAbbreviationInAbbreviationsList() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); + @ParameterizedTest + @MethodSource("provideTestFiles") + void testEditAbbreviationIncludesNewAbbreviationInAbbreviationsList(List> testFiles) throws IOException { + when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(2)))); viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); + when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(3)))); viewModel.addNewFile(); viewModel.selectLastJournalFile(); selectLastAbbreviation(); Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE"); - editAbbreviation(testAbbreviation); + addAbbreviation(testAbbreviation); assertEquals(5, viewModel.abbreviationsProperty().size()); assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); @@ -297,88 +332,62 @@ void testEditAbbreviationIncludesNewAbbreviationInAbbreviationsList() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); - editAbbreviation(testAbbreviation); + // addAbbreviation(testAbbreviation); - assertEquals(1, viewModel.abbreviationsProperty().size()); + assertEquals(0, viewModel.abbreviationsProperty().size()); assertFalse(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); } - @Test - void testEditAbbreviationToExistingOneResultsInException() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); + @ParameterizedTest + @MethodSource("provideTestFiles") + void testEditAbbreviationToExistingOneResultsInException(List> testFiles) throws IOException { + when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(1)))); viewModel.addNewFile(); viewModel.selectLastJournalFile(); selectLastAbbreviation(); - assertEquals(4, viewModel.abbreviationsProperty().size()); + assertEquals(3, viewModel.abbreviationsProperty().size()); viewModel.editAbbreviation("YetAnotherEntry", "YAE"); - viewModel.currentAbbreviationProperty().set(viewModel.abbreviationsProperty().get(2)); + viewModel.currentAbbreviationProperty().set(viewModel.abbreviationsProperty().get(1)); viewModel.editAbbreviation("YetAnotherEntry", "YAE"); verify(dialogService).showErrorDialogAndWait(anyString(), anyString()); } - @Test - void testEditAbbreviationToEmptyNameResultsInException() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); + @ParameterizedTest + @MethodSource("provideTestFiles") + void testEditAbbreviationToEmptyNameResultsInException(List> testFiles) throws IOException { + when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(1)))); viewModel.addNewFile(); viewModel.selectLastJournalFile(); selectLastAbbreviation(); - assertEquals(4, viewModel.abbreviationsProperty().size()); + assertEquals(3, viewModel.abbreviationsProperty().size()); viewModel.editAbbreviation("", "YAE"); verify(dialogService).showErrorDialogAndWait(anyString()); } - @Test - void testEditAbbreviationToEmptyAbbreviationResultsInException() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); + @ParameterizedTest + @MethodSource("provideTestFiles") + void testEditAbbreviationToEmptyAbbreviationResultsInException(List> testFiles) throws IOException { + when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(1)))); viewModel.addNewFile(); viewModel.selectLastJournalFile(); selectLastAbbreviation(); - assertEquals(4, viewModel.abbreviationsProperty().size()); + assertEquals(3, viewModel.abbreviationsProperty().size()); viewModel.editAbbreviation("YetAnotherEntry", ""); verify(dialogService).showErrorDialogAndWait(anyString()); } - @Test - void testDeleteAbbreviationSelectsPreviousOne() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE"); - addAbbrevaition(testAbbreviation); - - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - assertEquals(new AbbreviationViewModel(testAbbreviation), viewModel.currentAbbreviationProperty().get()); - - viewModel.deleteAbbreviation(); - - assertEquals(5, viewModel.abbreviationsProperty().size()); - // check if the previous (the last) element is the current abbreviation - assertEquals(viewModel.currentAbbreviationProperty().get(), viewModel.abbreviationsProperty().get(4)); - } - - @Test - void testDeleteAbbreviationSelectsNextOne() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE"); - addAbbrevaition(testAbbreviation); - viewModel.currentAbbreviationProperty().set(viewModel.abbreviationsProperty().get(1)); - viewModel.deleteAbbreviation(); + @ParameterizedTest + @MethodSource("provideTestFiles") + void testSaveAbbreviationsToFilesCreatesNewFilesWithWrittenAbbreviations(List> testFiles, List> testEntries) throws Exception { + Path testFile4Entries = createTestFile(testFiles.get(2)); + Path testFile5EntriesWithDuplicate = createTestFile(testFiles.get(3)); - assertEquals(new AbbreviationViewModel(testAbbreviation), viewModel.currentAbbreviationProperty().get()); - } - - @Test - void testSaveAbbreviationsToFilesCreatesNewFilesWithWrittenAbbreviations() throws Exception { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); @@ -386,7 +395,7 @@ void testSaveAbbreviationsToFilesCreatesNewFilesWithWrittenAbbreviations() throw Abbreviation testAbbreviation = new Abbreviation("JabRefTestEntry", "JTE"); editAbbreviation(testAbbreviation); - assertEquals(5, viewModel.abbreviationsProperty().size()); + assertEquals(4, viewModel.abbreviationsProperty().size()); assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); @@ -395,47 +404,28 @@ void testSaveAbbreviationsToFilesCreatesNewFilesWithWrittenAbbreviations() throw selectLastAbbreviation(); viewModel.deleteAbbreviation(); Abbreviation testAbbreviation1 = new Abbreviation("SomeOtherEntry", "SOE"); - addAbbrevaition(testAbbreviation1); + addAbbreviation(testAbbreviation1); - assertEquals(5, viewModel.abbreviationsProperty().size()); + assertEquals(4, viewModel.abbreviationsProperty().size()); assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation1))); viewModel.saveJournalAbbreviationFiles(); - List expected = Arrays.asList( - "Abbreviations;Abb;Abb", - "Test Entry;TE;T", - "MoreEntries;ME;M", - "JabRefTestEntry;JTE;JTE"); - List actual = Files.readAllLines(testFile4Entries, StandardCharsets.UTF_8); - assertEquals(expected, actual); + List actual = Files.readAllLines(testFile4Entries, StandardCharsets.UTF_8); + assertEquals(testEntries.get(0), actual); - expected = Arrays.asList( - "EntryEntry;EE;EE", - "Abbreviations;Abb;Abb", - "Test Entry;TE;T", - "SomeOtherEntry;SOE;SOE"); actual = Files.readAllLines(testFile5EntriesWithDuplicate, StandardCharsets.UTF_8); - - assertEquals(expected, actual); + assertEquals(testEntries.get(1), actual); } - @Test - void testSaveExternalFilesListToPreferences() { - addFourTestFileToViewModelAndPreferences(); - List expected = Stream.of(testFile1Entries, testFile3Entries, testFile4Entries, testFile5EntriesWithDuplicate) - .map(Path::toString) - .collect(Collectors.toList()); - verify(abbreviationPreferences).setExternalJournalLists(expected); + @ParameterizedTest + @MethodSource("provideTestFiles") + void testSaveExternalFilesListToPreferences(List> testFiles) throws IOException { + addFourTestFileToViewModelAndPreferences(testFiles); + verify(preferencesService).storeJournalAbbreviationPreferences(any()); } - private Path createTestFile(Path folder, String name, String content) throws Exception { - Path file = folder.resolve(name); - Files.writeString(file, content); - return file; - } - - private void addAbbrevaition(Abbreviation testAbbreviation) { + private void addAbbreviation(Abbreviation testAbbreviation) { viewModel.addAbbreviation(testAbbreviation.getName(), testAbbreviation.getAbbreviation()); } @@ -443,18 +433,6 @@ private void editAbbreviation(Abbreviation testAbbreviation) { viewModel.editAbbreviation(testAbbreviation.getName(), testAbbreviation.getAbbreviation()); } - private void addFourTestFileToViewModelAndPreferences() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); - viewModel.addNewFile(); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); - viewModel.addNewFile(); - viewModel.storeSettings(); - } - /** * Select the last abbreviation in the list of abbreviations */ @@ -462,4 +440,22 @@ private void selectLastAbbreviation() { viewModel.currentAbbreviationProperty() .set(viewModel.abbreviationsProperty().get(viewModel.abbreviationsCountProperty().get() - 1)); } + + private void addFourTestFileToViewModelAndPreferences(List> testFiles) throws IOException { + for (List testFile : testFiles) { + when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFile.get(0), testFile.get(1)))); + viewModel.addNewFile(); + } + viewModel.storeSettings(); + } + + private Path createTestFile(String name, String content) throws IOException { + Path file = this.tempFolder.resolve(name); + Files.writeString(file, content); + return file; + } + + private Path createTestFile(List testFile) throws IOException { + return createTestFile(testFile.get(0), testFile.get(1)); + } }