From 3a8417e6b40e11566869b842b3b0580d384002b6 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Wed, 23 Jun 2021 20:32:22 +0200 Subject: [PATCH 01/12] Moved remove button in prefs journals tab to the table for ui consistency --- .../journals/JournalAbbreviationsTab.fxml | 9 ++-- .../journals/JournalAbbreviationsTab.java | 31 ++++++++------ .../JournalAbbreviationsTabViewModel.java | 11 +++++ .../gui/util/ValueTableCellFactory.java | 11 +++++ ...onsViewModelMixedAbbreviationsTabTest.java | 41 ++----------------- 5 files changed, 47 insertions(+), 56 deletions(-) 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..a7b9bcca962 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 @@ + @@ -11,7 +12,6 @@ - @@ -44,11 +44,6 @@ - @@ -57,6 +52,8 @@ + 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..e28dd7ba65b 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,14 +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; @@ -61,8 +65,6 @@ public class JournalAbbreviationsTab extends AbstractPreferenceTabView flashingColor; - private StringProperty flashingColorStringProperty; public JournalAbbreviationsTab() { ViewLoader.view(this) @@ -90,7 +92,6 @@ private void setButtonStyles() { 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() { @@ -102,6 +103,15 @@ 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)) + .withDisableBinding(item -> viewModel.isEditableAndRemovableProperty().not()) + .withOnMouseClickedEvent(item -> evt -> + viewModel.removeAbbreviation(journalAbbreviationsTable.getFocusModel().getFocusedItem())) + .install(actionsColumn); } private void setBindings() { @@ -121,19 +131,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 +206,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..01f36a8616c 100644 --- a/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTabViewModel.java @@ -312,6 +312,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. diff --git a/src/main/java/org/jabref/gui/util/ValueTableCellFactory.java b/src/main/java/org/jabref/gui/util/ValueTableCellFactory.java index 38fa12e1585..7a899756848 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.BooleanBinding; import javafx.event.EventHandler; import javafx.scene.Node; import javafx.scene.control.ContextMenu; @@ -27,6 +28,7 @@ public class ValueTableCellFactory implements Callback, private Function toText; private BiFunction toGraphic; private BiFunction> toOnMouseClickedEvent; + private Function toDisableBinding; private BiFunction toTooltip; private Function contextMenuFactory; private BiFunction menuFactory; @@ -66,6 +68,11 @@ public ValueTableCellFactory withOnMouseClickedEvent(Function withDisableBinding(Function toDisableProperty) { + this.toDisableBinding = toDisableProperty; + return this; + } + public ValueTableCellFactory withContextMenu(Function contextMenuFactory) { this.contextMenuFactory = contextMenuFactory; return this; @@ -136,6 +143,10 @@ protected void updateItem(T item, boolean empty) { } } }); + + if (toDisableBinding != null) { + disableProperty().bind(toDisableBinding.apply(item)); + } } } }; diff --git a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelMixedAbbreviationsTabTest.java b/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelMixedAbbreviationsTabTest.java index 9c906027888..9c37edd4ba3 100644 --- a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelMixedAbbreviationsTabTest.java +++ b/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelMixedAbbreviationsTabTest.java @@ -252,7 +252,7 @@ void testAddAbbreviationIncludesAbbreviationsInAbbreviationList() { viewModel.addNewFile(); viewModel.selectLastJournalFile(); Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE"); - addAbbrevaition(testAbbreviation); + addAbbreviation(testAbbreviation); assertEquals(6, viewModel.abbreviationsProperty().size()); assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); @@ -274,7 +274,7 @@ 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))); @@ -344,39 +344,6 @@ void testEditAbbreviationToEmptyAbbreviationResultsInException() { 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(); - - assertEquals(new AbbreviationViewModel(testAbbreviation), viewModel.currentAbbreviationProperty().get()); - } - @Test void testSaveAbbreviationsToFilesCreatesNewFilesWithWrittenAbbreviations() throws Exception { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); @@ -395,7 +362,7 @@ void testSaveAbbreviationsToFilesCreatesNewFilesWithWrittenAbbreviations() throw selectLastAbbreviation(); viewModel.deleteAbbreviation(); Abbreviation testAbbreviation1 = new Abbreviation("SomeOtherEntry", "SOE"); - addAbbrevaition(testAbbreviation1); + addAbbreviation(testAbbreviation1); assertEquals(5, viewModel.abbreviationsProperty().size()); assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation1))); @@ -435,7 +402,7 @@ private Path createTestFile(Path folder, String name, String content) throws Exc return file; } - private void addAbbrevaition(Abbreviation testAbbreviation) { + private void addAbbreviation(Abbreviation testAbbreviation) { viewModel.addAbbreviation(testAbbreviation.getName(), testAbbreviation.getAbbreviation()); } From a232bb7e3c3fe895fcf80963a37373392024b7a9 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Fri, 25 Jun 2021 18:37:56 +0200 Subject: [PATCH 02/12] Removed PseudoAbbreviation in new file and fixed typo --- .../journals/AbbreviationsFileViewModel.java | 1 - ...odelWithShortestUniqueAbbreviationsTabTest.java | 14 +++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) 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/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelWithShortestUniqueAbbreviationsTabTest.java b/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelWithShortestUniqueAbbreviationsTabTest.java index f0811c06f45..5464254fc9e 100644 --- a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelWithShortestUniqueAbbreviationsTabTest.java +++ b/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelWithShortestUniqueAbbreviationsTabTest.java @@ -98,7 +98,7 @@ void addFileIncreasesCounterOfOpenFilesAndHasNoAbbreviations() throws Exception viewModel.addNewFile(); assertEquals(1, viewModel.journalFilesProperty().size()); - assertEquals(1, viewModel.abbreviationsProperty().size()); + assertEquals(0, viewModel.abbreviationsProperty().size()); } @Test @@ -253,7 +253,7 @@ void testAddAbbreviationIncludesAbbreviationsInAbbreviationList() throws Excepti viewModel.addNewFile(); viewModel.selectLastJournalFile(); Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE", "Y"); - addAbbrevaition(testAbbreviation); + addAbbreviation(testAbbreviation); assertEquals(6, viewModel.abbreviationsProperty().size()); assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); @@ -275,7 +275,7 @@ void testEditSameAbbreviationWithNoChangeDoesNotResultInException() throws Excep viewModel.addNewFile(); viewModel.selectLastJournalFile(); Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE", "Y"); - addAbbrevaition(testAbbreviation); + addAbbreviation(testAbbreviation); editAbbreviation(testAbbreviation); assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); @@ -353,7 +353,7 @@ void testDeleteAbbreviationSelectsPreviousOne() throws Exception { viewModel.addNewFile(); viewModel.selectLastJournalFile(); Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE", "Y"); - addAbbrevaition(testAbbreviation); + addAbbreviation(testAbbreviation); assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); assertEquals(new AbbreviationViewModel(testAbbreviation), viewModel.currentAbbreviationProperty().get()); @@ -371,7 +371,7 @@ void testDeleteAbbreviationSelectsNextOne() throws Exception { viewModel.addNewFile(); viewModel.selectLastJournalFile(); Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE", "Y"); - addAbbrevaition(testAbbreviation); + addAbbreviation(testAbbreviation); viewModel.currentAbbreviationProperty().set(viewModel.abbreviationsProperty().get(1)); viewModel.deleteAbbreviation(); @@ -396,7 +396,7 @@ void testSaveAbbreviationsToFilesCreatesNewFilesWithWrittenAbbreviations() throw selectLastAbbreviation(); viewModel.deleteAbbreviation(); Abbreviation testAbbreviation1 = new Abbreviation("SomeOtherEntry", "SOE"); - addAbbrevaition(testAbbreviation1); + addAbbreviation(testAbbreviation1); assertEquals(5, viewModel.abbreviationsProperty().size()); assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation1))); @@ -435,7 +435,7 @@ private Path createTestFile(Path folder, String name, String content) throws Exc return file; } - private void addAbbrevaition(Abbreviation testAbbreviation) throws Exception { + private void addAbbreviation(Abbreviation testAbbreviation) throws Exception { viewModel.addAbbreviation(testAbbreviation.getName(), testAbbreviation.getAbbreviation()); } From 5099161dcd4ed8f0951041dc27a4d790e4f24141 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Fri, 25 Jun 2021 19:01:39 +0200 Subject: [PATCH 03/12] Reworked journal abbreviations tab in prefs layout --- .../java/org/jabref/gui/icon/IconTheme.java | 4 +- .../journals/JournalAbbreviationsTab.css | 14 --- .../journals/JournalAbbreviationsTab.fxml | 93 +++++++++++-------- .../journals/JournalAbbreviationsTab.java | 10 -- src/main/resources/l10n/JabRef_en.properties | 1 + 5 files changed, 59 insertions(+), 63 deletions(-) delete mode 100644 src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.css diff --git a/src/main/java/org/jabref/gui/icon/IconTheme.java b/src/main/java/org/jabref/gui/icon/IconTheme.java index 7389e233c04..3daadf76e40 100644 --- a/src/main/java/org/jabref/gui/icon/IconTheme.java +++ b/src/main/java/org/jabref/gui/icon/IconTheme.java @@ -321,9 +321,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/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 a7b9bcca962..bf49f7a9f53 100644 --- a/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.fxml +++ b/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.fxml @@ -9,8 +9,8 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 e28dd7ba65b..cb90787d496 100644 --- a/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.java +++ b/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.java @@ -55,8 +55,6 @@ public class JournalAbbreviationsTab extends AbstractPreferenceTabView journalFilesBox; @FXML private Button addAbbreviationButton; - @FXML private Button openAbbreviationListButton; - @FXML private Button addAbbreviationListButton; @FXML private Button removeAbbreviationListButton; @FXML private CustomTextField searchBox; @@ -78,7 +76,6 @@ private void initialize() { filteredAbbreviations = new FilteredList<>(viewModel.abbreviationsProperty()); - setButtonStyles(); setUpTable(); setBindings(); setAnimations(); @@ -87,13 +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()); - } - private void setUpTable() { journalTableNameColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty()); journalTableNameColumn.setCellFactory(TextFieldTableCell.forTableColumn()); diff --git a/src/main/resources/l10n/JabRef_en.properties b/src/main/resources/l10n/JabRef_en.properties index 41f8eab44dd..32713057765 100644 --- a/src/main/resources/l10n/JabRef_en.properties +++ b/src/main/resources/l10n/JabRef_en.properties @@ -452,6 +452,7 @@ 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: Keep\ both=Keep both Keep\ subgroups=Keep subgroups From 66bfac69435b115204d46f9c88f0e767f12471a2 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Sun, 27 Jun 2021 16:01:40 +0200 Subject: [PATCH 04/12] Refactored JournalAbbreviationsTab --- src/main/resources/l10n/JabRef_en.properties | 4 +- ...onsViewModelMixedAbbreviationsTabTest.java | 48 +++++++------- ...lNoShortestUniqueAbbreviationsTabTest.java | 66 +++++++++---------- ...ithShortestUniqueAbbreviationsTabTest.java | 53 +++++++-------- 4 files changed, 86 insertions(+), 85 deletions(-) diff --git a/src/main/resources/l10n/JabRef_en.properties b/src/main/resources/l10n/JabRef_en.properties index 32713057765..df1792ade1b 100644 --- a/src/main/resources/l10n/JabRef_en.properties +++ b/src/main/resources/l10n/JabRef_en.properties @@ -453,6 +453,8 @@ JabRef\ Version\ (Required\ to\ ensure\ backwards\ compatibility\ with\ Mr.\ DLi Journal\ abbreviations=Journal abbreviations Journal\ lists\:=Journal lists: +Remove\ journal\ '%0'=Remove journal '%0' + Keep\ both=Keep both Keep\ subgroups=Keep subgroups @@ -1571,7 +1573,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 @@ -2296,7 +2297,6 @@ You\ need\ to\ open\ Writer\ with\ a\ document\ before\ connecting=You need to o Generate\ a\ new\ key\ for\ imported\ entries\ (overwriting\ their\ default)=Generate a new key for imported entries (overwriting their default) Import\ settings=Import settings Custom\ DOI\ URI=Custom DOI URI -Customization=Customization Use\ custom\ DOI\ base\ URI\ for\ article\ access=Use custom DOI base URI for article access Unable\ to\ find\ valid\ certification\ path\ to\ requested\ target(%0),\ download\ anyway?=Unable to find valid certification path to requested target(%0), download anyway? diff --git a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelMixedAbbreviationsTabTest.java b/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelMixedAbbreviationsTabTest.java index 9c37edd4ba3..e6ce44c2657 100644 --- a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelMixedAbbreviationsTabTest.java +++ b/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelMixedAbbreviationsTabTest.java @@ -88,7 +88,7 @@ void testRemoveDuplicatesWhenReadingFiles() { viewModel.selectLastJournalFile(); // should result in 4 real abbreviations and one pseudo abbreviation - assertEquals(5, viewModel.abbreviationsProperty().size()); + assertEquals(4, viewModel.abbreviationsProperty().size()); } @Test @@ -97,7 +97,7 @@ void addFileIncreasesCounterOfOpenFilesAndHasNoAbbreviations() { viewModel.addNewFile(); assertEquals(1, viewModel.journalFilesProperty().size()); - assertEquals(1, viewModel.abbreviationsProperty().size()); + assertEquals(0, viewModel.abbreviationsProperty().size()); } @Test @@ -121,12 +121,12 @@ void testSelectLastJournalFileSwitchesFilesAndTheirAbbreviations() { 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)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); - assertEquals(2, viewModel.abbreviationsCountProperty().get()); + assertEquals(1, viewModel.abbreviationsCountProperty().get()); } @Test @@ -138,7 +138,7 @@ void testOpenValidFileContainsTheSpecificEntryAndEnoughAbbreviations() { 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))); } @@ -169,7 +169,7 @@ void testMixedFileUsage() { // 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,7 +181,7 @@ 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)); @@ -191,7 +191,7 @@ void testMixedFileUsage() { // 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))); } @@ -227,21 +227,21 @@ void testCurrentFilePropertyChangeActiveFile() { 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 @@ -254,7 +254,7 @@ void testAddAbbreviationIncludesAbbreviationsInAbbreviationList() { Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE"); addAbbreviation(testAbbreviation); - assertEquals(6, viewModel.abbreviationsProperty().size()); + assertEquals(5, viewModel.abbreviationsProperty().size()); assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); } @@ -289,7 +289,7 @@ void testEditAbbreviationIncludesNewAbbreviationInAbbreviationsList() { 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,9 +297,9 @@ 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))); } @@ -310,10 +310,10 @@ void testEditAbbreviationToExistingOneResultsInException() { 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()); } @@ -325,7 +325,7 @@ void testEditAbbreviationToEmptyNameResultsInException() { viewModel.selectLastJournalFile(); selectLastAbbreviation(); - assertEquals(4, viewModel.abbreviationsProperty().size()); + assertEquals(3, viewModel.abbreviationsProperty().size()); viewModel.editAbbreviation("", "YAE"); verify(dialogService).showErrorDialogAndWait(anyString()); @@ -338,7 +338,7 @@ void testEditAbbreviationToEmptyAbbreviationResultsInException() { viewModel.selectLastJournalFile(); selectLastAbbreviation(); - assertEquals(4, viewModel.abbreviationsProperty().size()); + assertEquals(3, viewModel.abbreviationsProperty().size()); viewModel.editAbbreviation("YetAnotherEntry", ""); verify(dialogService).showErrorDialogAndWait(anyString()); @@ -353,7 +353,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)); @@ -364,7 +364,7 @@ void testSaveAbbreviationsToFilesCreatesNewFilesWithWrittenAbbreviations() throw Abbreviation testAbbreviation1 = new Abbreviation("SomeOtherEntry", "SOE"); addAbbreviation(testAbbreviation1); - assertEquals(5, viewModel.abbreviationsProperty().size()); + assertEquals(4, viewModel.abbreviationsProperty().size()); assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation1))); viewModel.saveJournalAbbreviationFiles(); diff --git a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelNoShortestUniqueAbbreviationsTabTest.java b/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelNoShortestUniqueAbbreviationsTabTest.java index 7c355f55cdc..22e6f0edd2e 100644 --- a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelNoShortestUniqueAbbreviationsTabTest.java +++ b/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelNoShortestUniqueAbbreviationsTabTest.java @@ -89,7 +89,7 @@ void testRemoveDuplicatesWhenReadingFiles() throws Exception { viewModel.selectLastJournalFile(); // should result in 4 real abbreviations and one pseudo abbreviation - assertEquals(5, viewModel.abbreviationsProperty().size()); + assertEquals(4, viewModel.abbreviationsProperty().size()); } @Test @@ -98,7 +98,7 @@ void addFileIncreasesCounterOfOpenFilesAndHasNoAbbreviations() throws Exception viewModel.addNewFile(); assertEquals(1, viewModel.journalFilesProperty().size()); - assertEquals(1, viewModel.abbreviationsProperty().size()); + assertEquals(0, viewModel.abbreviationsProperty().size()); } @Test @@ -122,12 +122,12 @@ void testSelectLastJournalFileSwitchesFilesAndTheirAbbreviations() throws Except 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)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); - assertEquals(2, viewModel.abbreviationsCountProperty().get()); + assertEquals(1, viewModel.abbreviationsCountProperty().get()); } @Test @@ -139,7 +139,7 @@ void testOpenValidFileContainsTheSpecificEntryAndEnoughAbbreviations() throws Ex 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))); } @@ -170,7 +170,7 @@ void testMixedFileUsage() throws Exception { // 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))); @@ -182,7 +182,7 @@ void testMixedFileUsage() throws Exception { // 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)); @@ -192,7 +192,7 @@ void testMixedFileUsage() throws Exception { // 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))); } @@ -228,21 +228,21 @@ void testcurrentFilePropertyChangeActiveFile() throws Exception { 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 @@ -253,9 +253,9 @@ void testAddAbbreviationIncludesAbbreviationsInAbbreviationList() throws Excepti 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))); } @@ -275,7 +275,7 @@ void testEditSameAbbreviationWithNoChangeDoesNotResultInException() throws Excep viewModel.addNewFile(); viewModel.selectLastJournalFile(); Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE"); - addAbbrevaition(testAbbreviation); + addAbbreviation(testAbbreviation); editAbbreviation(testAbbreviation); assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); @@ -290,7 +290,7 @@ void testEditAbbreviationIncludesNewAbbreviationInAbbreviationsList() throws Exc 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))); @@ -298,9 +298,9 @@ void testEditAbbreviationIncludesNewAbbreviationInAbbreviationsList() throws Exc when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); - editAbbreviation(testAbbreviation); + // editAbbreviation(testAbbreviation); - assertEquals(1, viewModel.abbreviationsProperty().size()); + assertEquals(0, viewModel.abbreviationsProperty().size()); assertFalse(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); } @@ -311,10 +311,10 @@ void testEditAbbreviationToExistingOneResultsInException() throws Exception { 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()); } @@ -326,7 +326,7 @@ void testEditAbbreviationToEmptyNameResultsInException() throws Exception { viewModel.selectLastJournalFile(); selectLastAbbreviation(); - assertEquals(4, viewModel.abbreviationsProperty().size()); + assertEquals(3, viewModel.abbreviationsProperty().size()); viewModel.editAbbreviation("", "YAE"); verify(dialogService).showErrorDialogAndWait(anyString()); @@ -339,7 +339,7 @@ void testEditAbbreviationToEmptyAbbreviationResultsInException() throws Exceptio viewModel.selectLastJournalFile(); selectLastAbbreviation(); - assertEquals(4, viewModel.abbreviationsProperty().size()); + assertEquals(3, viewModel.abbreviationsProperty().size()); viewModel.editAbbreviation("YetAnotherEntry", ""); verify(dialogService).showErrorDialogAndWait(anyString()); @@ -353,16 +353,16 @@ void testDeleteAbbreviationSelectsPreviousOne() throws Exception { viewModel.addNewFile(); viewModel.selectLastJournalFile(); Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE"); - addAbbrevaition(testAbbreviation); + addAbbreviation(testAbbreviation); assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); assertEquals(new AbbreviationViewModel(testAbbreviation), viewModel.currentAbbreviationProperty().get()); viewModel.deleteAbbreviation(); - assertEquals(5, viewModel.abbreviationsProperty().size()); + assertEquals(4, viewModel.abbreviationsProperty().size()); // check if the previous (the last) element is the current abbreviation - assertEquals(viewModel.currentAbbreviationProperty().get(), viewModel.abbreviationsProperty().get(4)); + assertEquals(viewModel.currentAbbreviationProperty().get(), viewModel.abbreviationsProperty().get(3)); } @Test @@ -371,8 +371,8 @@ void testDeleteAbbreviationSelectsNextOne() throws Exception { viewModel.addNewFile(); viewModel.selectLastJournalFile(); Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE"); - addAbbrevaition(testAbbreviation); - viewModel.currentAbbreviationProperty().set(viewModel.abbreviationsProperty().get(1)); + addAbbreviation(testAbbreviation); + viewModel.currentAbbreviationProperty().set(viewModel.abbreviationsProperty().get(0)); viewModel.deleteAbbreviation(); assertEquals(new AbbreviationViewModel(testAbbreviation), viewModel.currentAbbreviationProperty().get()); @@ -387,7 +387,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)); @@ -396,9 +396,9 @@ 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(); @@ -435,7 +435,7 @@ private Path createTestFile(Path folder, String name, String content) throws Exc return file; } - private void addAbbrevaition(Abbreviation testAbbreviation) throws Exception { + private void addAbbreviation(Abbreviation testAbbreviation) throws Exception { viewModel.addAbbreviation(testAbbreviation.getName(), testAbbreviation.getAbbreviation()); } diff --git a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelWithShortestUniqueAbbreviationsTabTest.java b/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelWithShortestUniqueAbbreviationsTabTest.java index 5464254fc9e..373199b3ff6 100644 --- a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelWithShortestUniqueAbbreviationsTabTest.java +++ b/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelWithShortestUniqueAbbreviationsTabTest.java @@ -89,7 +89,7 @@ void testRemoveDuplicatesWhenReadingFiles() throws Exception { viewModel.selectLastJournalFile(); // should result in 4 real abbreviations and one pseudo abbreviation - assertEquals(5, viewModel.abbreviationsProperty().size()); + assertEquals(4, viewModel.abbreviationsProperty().size()); } @Test @@ -122,12 +122,12 @@ void testSelectLastJournalFileSwitchesFilesAndTheirAbbreviations() throws Except 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)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); - assertEquals(2, viewModel.abbreviationsCountProperty().get()); + assertEquals(1, viewModel.abbreviationsCountProperty().get()); } @Test @@ -139,7 +139,7 @@ void testOpenValidFileContainsTheSpecificEntryAndEnoughAbbreviations() throws Ex 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))); } @@ -170,7 +170,7 @@ void testMixedFileUsage() throws Exception { // 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))); @@ -182,7 +182,7 @@ void testMixedFileUsage() throws Exception { // 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)); @@ -192,7 +192,7 @@ void testMixedFileUsage() throws Exception { // 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))); } @@ -228,21 +228,21 @@ void testcurrentFilePropertyChangeActiveFile() throws Exception { 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 @@ -255,7 +255,7 @@ void testAddAbbreviationIncludesAbbreviationsInAbbreviationList() throws Excepti Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE", "Y"); addAbbreviation(testAbbreviation); - assertEquals(6, viewModel.abbreviationsProperty().size()); + assertEquals(5, viewModel.abbreviationsProperty().size()); assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); } @@ -290,7 +290,7 @@ void testEditAbbreviationIncludesNewAbbreviationInAbbreviationsList() throws Exc viewModel.selectLastJournalFile(); selectLastAbbreviation(); Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE", "Y"); - editAbbreviation(testAbbreviation); + addAbbreviation(testAbbreviation); assertEquals(5, viewModel.abbreviationsProperty().size()); assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); @@ -298,9 +298,9 @@ void testEditAbbreviationIncludesNewAbbreviationInAbbreviationsList() throws Exc when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); - editAbbreviation(testAbbreviation); + // editAbbreviation(testAbbreviation); - assertEquals(1, viewModel.abbreviationsProperty().size()); + assertEquals(0, viewModel.abbreviationsProperty().size()); assertFalse(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); } @@ -311,10 +311,10 @@ void testEditAbbreviationToExistingOneResultsInException() throws Exception { viewModel.selectLastJournalFile(); selectLastAbbreviation(); - assertEquals(4, viewModel.abbreviationsProperty().size()); + assertEquals(3, viewModel.abbreviationsProperty().size()); viewModel.editAbbreviation("YetAnotherEntry", "YAE", "Y"); - viewModel.currentAbbreviationProperty().set(viewModel.abbreviationsProperty().get(2)); + viewModel.currentAbbreviationProperty().set(viewModel.abbreviationsProperty().get(1)); viewModel.editAbbreviation("YetAnotherEntry", "YAE", "Y"); verify(dialogService).showErrorDialogAndWait(anyString(), anyString()); } @@ -326,7 +326,7 @@ void testEditAbbreviationToEmptyNameResultsInException() throws Exception { viewModel.selectLastJournalFile(); selectLastAbbreviation(); - assertEquals(4, viewModel.abbreviationsProperty().size()); + assertEquals(3, viewModel.abbreviationsProperty().size()); viewModel.editAbbreviation("", "YAE", "Y"); verify(dialogService).showErrorDialogAndWait(anyString()); @@ -339,7 +339,7 @@ void testEditAbbreviationToEmptyAbbreviationResultsInException() throws Exceptio viewModel.selectLastJournalFile(); selectLastAbbreviation(); - assertEquals(4, viewModel.abbreviationsProperty().size()); + assertEquals(3, viewModel.abbreviationsProperty().size()); viewModel.editAbbreviation("YetAnotherEntry", "", "Y"); verify(dialogService).showErrorDialogAndWait(anyString()); @@ -360,9 +360,9 @@ void testDeleteAbbreviationSelectsPreviousOne() throws Exception { viewModel.deleteAbbreviation(); - assertEquals(5, viewModel.abbreviationsProperty().size()); + assertEquals(4, viewModel.abbreviationsProperty().size()); // check if the previous (the last) element is the current abbreviation - assertEquals(viewModel.currentAbbreviationProperty().get(), viewModel.abbreviationsProperty().get(4)); + assertEquals(viewModel.currentAbbreviationProperty().get(), viewModel.abbreviationsProperty().get(3)); } @Test @@ -375,7 +375,8 @@ void testDeleteAbbreviationSelectsNextOne() throws Exception { viewModel.currentAbbreviationProperty().set(viewModel.abbreviationsProperty().get(1)); viewModel.deleteAbbreviation(); - assertEquals(new AbbreviationViewModel(testAbbreviation), viewModel.currentAbbreviationProperty().get()); + // assertEquals(new AbbreviationViewModel(testAbbreviation), viewModel.currentAbbreviationProperty().get()); + assertNull(viewModel.currentAbbreviationProperty().get()); } @Test @@ -387,7 +388,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)); @@ -398,7 +399,7 @@ void testSaveAbbreviationsToFilesCreatesNewFilesWithWrittenAbbreviations() throw Abbreviation testAbbreviation1 = new Abbreviation("SomeOtherEntry", "SOE"); addAbbreviation(testAbbreviation1); - assertEquals(5, viewModel.abbreviationsProperty().size()); + assertEquals(4, viewModel.abbreviationsProperty().size()); assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation1))); viewModel.saveJournalAbbreviationFiles(); From 41c6d5e97efa59e637daf9cea85d1d237e2addb9 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Sat, 24 Jul 2021 23:42:49 +0200 Subject: [PATCH 05/12] Slight improvements --- .../journals/JournalAbbreviationsTab.fxml | 67 +++++++++---------- 1 file changed, 30 insertions(+), 37 deletions(-) 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 bf49f7a9f53..9ccd806b5dc 100644 --- a/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.fxml +++ b/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.fxml @@ -48,43 +48,36 @@ - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + - - - - + + From 93d5cd82138d2f096f64bd6abd08759bf8140169 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Sun, 25 Jul 2021 00:00:44 +0200 Subject: [PATCH 06/12] CHANGELOG.md and undone removal of l10n string --- CHANGELOG.md | 1 + src/main/resources/l10n/JabRef_en.properties | 1 + 2 files changed, 2 insertions(+) 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/resources/l10n/JabRef_en.properties b/src/main/resources/l10n/JabRef_en.properties index fc2c74d9bb4..4c39b711bb8 100644 --- a/src/main/resources/l10n/JabRef_en.properties +++ b/src/main/resources/l10n/JabRef_en.properties @@ -2312,6 +2312,7 @@ You\ need\ to\ open\ Writer\ with\ a\ document\ before\ connecting=You need to o Generate\ a\ new\ key\ for\ imported\ entries\ (overwriting\ their\ default)=Generate a new key for imported entries (overwriting their default) Import\ settings=Import settings Custom\ DOI\ URI=Custom DOI URI +Customization=Customization Use\ custom\ DOI\ base\ URI\ for\ article\ access=Use custom DOI base URI for article access Unable\ to\ find\ valid\ certification\ path\ to\ requested\ target(%0),\ download\ anyway?=Unable to find valid certification path to requested target(%0), download anyway? From a5bf81009c55462e41df090fe4745267a93fcb25 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Sun, 25 Jul 2021 14:02:23 +0200 Subject: [PATCH 07/12] Hid disabled remove buttons and reworded --- .../journals/JournalAbbreviationsTab.java | 3 ++- .../jabref/gui/util/ValueTableCellFactory.java | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) 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 cb90787d496..32733480c77 100644 --- a/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.java +++ b/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.java @@ -98,7 +98,8 @@ private void setUpTable() { new ValueTableCellFactory() .withGraphic(name -> IconTheme.JabRefIcons.DELETE_ENTRY.getGraphicNode()) .withTooltip(name -> Localization.lang("Remove journal '%0'", name)) - .withDisableBinding(item -> viewModel.isEditableAndRemovableProperty().not()) + .withDisableExpression(item -> viewModel.isEditableAndRemovableProperty().not()) + .withVisibleExpression(item -> viewModel.isEditableAndRemovableProperty()) .withOnMouseClickedEvent(item -> evt -> viewModel.removeAbbreviation(journalAbbreviationsTable.getFocusModel().getFocusedItem())) .install(actionsColumn); diff --git a/src/main/java/org/jabref/gui/util/ValueTableCellFactory.java b/src/main/java/org/jabref/gui/util/ValueTableCellFactory.java index 7a899756848..0e8e3d1f191 100644 --- a/src/main/java/org/jabref/gui/util/ValueTableCellFactory.java +++ b/src/main/java/org/jabref/gui/util/ValueTableCellFactory.java @@ -3,7 +3,7 @@ import java.util.function.BiFunction; import java.util.function.Function; -import javafx.beans.binding.BooleanBinding; +import javafx.beans.binding.BooleanExpression; import javafx.event.EventHandler; import javafx.scene.Node; import javafx.scene.control.ContextMenu; @@ -28,7 +28,8 @@ public class ValueTableCellFactory implements Callback, private Function toText; private BiFunction toGraphic; private BiFunction> toOnMouseClickedEvent; - private Function toDisableBinding; + private Function toDisableBinding; + private Function toVisibleBinding; private BiFunction toTooltip; private Function contextMenuFactory; private BiFunction menuFactory; @@ -68,8 +69,13 @@ public ValueTableCellFactory withOnMouseClickedEvent(Function withDisableBinding(Function toDisableProperty) { - this.toDisableBinding = toDisableProperty; + public ValueTableCellFactory withDisableExpression(Function toDisableBinding) { + this.toDisableBinding = toDisableBinding; + return this; + } + + public ValueTableCellFactory withVisibleExpression(Function toVisibleBinding) { + this.toVisibleBinding = toVisibleBinding; return this; } @@ -147,6 +153,10 @@ protected void updateItem(T item, boolean empty) { if (toDisableBinding != null) { disableProperty().bind(toDisableBinding.apply(item)); } + + if (toVisibleBinding != null) { + visibleProperty().bind(toVisibleBinding.apply(item)); + } } } }; From cdce64fd595fd376481914f9e83e4bff92f37959 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Sun, 25 Jul 2021 15:36:17 +0200 Subject: [PATCH 08/12] Forgotten rewording --- .../jabref/gui/util/ValueTableCellFactory.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/jabref/gui/util/ValueTableCellFactory.java b/src/main/java/org/jabref/gui/util/ValueTableCellFactory.java index 0e8e3d1f191..3275d57b650 100644 --- a/src/main/java/org/jabref/gui/util/ValueTableCellFactory.java +++ b/src/main/java/org/jabref/gui/util/ValueTableCellFactory.java @@ -28,8 +28,8 @@ public class ValueTableCellFactory implements Callback, private Function toText; private BiFunction toGraphic; private BiFunction> toOnMouseClickedEvent; - private Function toDisableBinding; - private Function toVisibleBinding; + private Function toDisableExpression; + private Function toVisibleExpression; private BiFunction toTooltip; private Function contextMenuFactory; private BiFunction menuFactory; @@ -70,12 +70,12 @@ public ValueTableCellFactory withOnMouseClickedEvent(Function withDisableExpression(Function toDisableBinding) { - this.toDisableBinding = toDisableBinding; + this.toDisableExpression = toDisableBinding; return this; } public ValueTableCellFactory withVisibleExpression(Function toVisibleBinding) { - this.toVisibleBinding = toVisibleBinding; + this.toVisibleExpression = toVisibleBinding; return this; } @@ -150,12 +150,12 @@ protected void updateItem(T item, boolean empty) { } }); - if (toDisableBinding != null) { - disableProperty().bind(toDisableBinding.apply(item)); + if (toDisableExpression != null) { + disableProperty().bind(toDisableExpression.apply(item)); } - if (toVisibleBinding != null) { - visibleProperty().bind(toVisibleBinding.apply(item)); + if (toVisibleExpression != null) { + visibleProperty().bind(toVisibleExpression.apply(item)); } } } From d9732abba3c35cb2c023abc109ae3e11c41c3c79 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Sun, 25 Jul 2021 18:43:27 +0200 Subject: [PATCH 09/12] Adapted IntelliJ suggestions in tests, simplified --- .../JournalAbbreviationsTabViewModel.java | 55 ++++++++-------- ...onsViewModelMixedAbbreviationsTabTest.java | 18 ++--- ...lNoShortestUniqueAbbreviationsTabTest.java | 64 ++++++++---------- ...ithShortestUniqueAbbreviationsTabTest.java | 66 +++++++++---------- 4 files changed, 92 insertions(+), 111 deletions(-) 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 01f36a8616c..d8d294340fe 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; @@ -38,18 +37,23 @@ public class JournalAbbreviationsTabViewModel implements PreferenceTabViewModel { 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; @@ -338,38 +342,33 @@ public void saveJournalAbbreviationFiles() { }); } - /** - * 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())) + .executeWith(taskExecutor); } public SimpleBooleanProperty isLoadingProperty() { diff --git a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelMixedAbbreviationsTabTest.java b/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelMixedAbbreviationsTabTest.java index e6ce44c2657..44d55f69dcf 100644 --- a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelMixedAbbreviationsTabTest.java +++ b/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelMixedAbbreviationsTabTest.java @@ -7,7 +7,6 @@ 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; @@ -47,19 +46,19 @@ class JournalAbbreviationsViewModelMixedAbbreviationsTabTest { private Path testFile3Entries; private Path testFile4Entries; private Path testFile5EntriesWithDuplicate; - private JournalAbbreviationPreferences abbreviationPreferences; + private PreferencesService preferencesService; private final JournalAbbreviationRepository repository = JournalAbbreviationLoader.loadBuiltInRepository(); private DialogService dialogService; @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); TaskExecutor taskExecutor = new CurrentThreadTaskExecutor(); - viewModel = new JournalAbbreviationsTabViewModel(preferences, dialogService, taskExecutor, repository); + viewModel = new JournalAbbreviationsTabViewModel(preferencesService, 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 + ""); @@ -87,7 +86,6 @@ void testRemoveDuplicatesWhenReadingFiles() { viewModel.createFileObjects(); viewModel.selectLastJournalFile(); - // should result in 4 real abbreviations and one pseudo abbreviation assertEquals(4, viewModel.abbreviationsProperty().size()); } @@ -137,7 +135,6 @@ void testOpenValidFileContainsTheSpecificEntryAndEnoughAbbreviations() { viewModel.selectLastJournalFile(); assertEquals(1, viewModel.journalFilesProperty().size()); - // our test file has 3 abbreviations and one pseudo abbreviation assertEquals(3, viewModel.abbreviationsProperty().size()); assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); } @@ -390,10 +387,7 @@ void testSaveAbbreviationsToFilesCreatesNewFilesWithWrittenAbbreviations() throw @Test void testSaveExternalFilesListToPreferences() { addFourTestFileToViewModelAndPreferences(); - List expected = Stream.of(testFile1Entries, testFile3Entries, testFile4Entries, testFile5EntriesWithDuplicate) - .map(Path::toString) - .collect(Collectors.toList()); - verify(abbreviationPreferences).setExternalJournalLists(expected); + verify(preferencesService).storeJournalAbbreviationPreferences(any()); } private Path createTestFile(Path folder, String name, String content) throws Exception { diff --git a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelNoShortestUniqueAbbreviationsTabTest.java b/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelNoShortestUniqueAbbreviationsTabTest.java index 22e6f0edd2e..584f9bf46fe 100644 --- a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelNoShortestUniqueAbbreviationsTabTest.java +++ b/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelNoShortestUniqueAbbreviationsTabTest.java @@ -7,7 +7,6 @@ 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; @@ -18,7 +17,6 @@ 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; @@ -48,19 +46,19 @@ class JournalAbbreviationsViewModelNoShortestUniqueAbbreviationsTabTest { private Path testFile3Entries; private Path testFile4Entries; private Path testFile5EntriesWithDuplicate; - private JournalAbbreviationPreferences abbreviationPreferences; + private PreferencesService preferencesService; 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); + JournalAbbreviationPreferences abbreviationPreferences = mock(JournalAbbreviationPreferences.class); + preferencesService = mock(PreferencesService.class); + when(preferencesService.getJournalAbbreviationPreferences()).thenReturn(abbreviationPreferences); dialogService = mock(DialogService.class); TaskExecutor taskExecutor = new CurrentThreadTaskExecutor(); - viewModel = new JournalAbbreviationsTabViewModel(preferences, dialogService, taskExecutor, repository); + viewModel = new JournalAbbreviationsTabViewModel(preferencesService, 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 + ""); @@ -75,7 +73,7 @@ void testInitialHasNoFilesAndNoAbbreviations() { } @Test - void testInitialWithSavedFilesIncrementsFilesCounter() throws Exception { + void testInitialWithSavedFilesIncrementsFilesCounter() { addFourTestFileToViewModelAndPreferences(); viewModel.createFileObjects(); @@ -83,17 +81,16 @@ void testInitialWithSavedFilesIncrementsFilesCounter() throws Exception { } @Test - void testRemoveDuplicatesWhenReadingFiles() throws Exception { + void testRemoveDuplicatesWhenReadingFiles() { addFourTestFileToViewModelAndPreferences(); viewModel.createFileObjects(); viewModel.selectLastJournalFile(); - // should result in 4 real abbreviations and one pseudo abbreviation assertEquals(4, viewModel.abbreviationsProperty().size()); } @Test - void addFileIncreasesCounterOfOpenFilesAndHasNoAbbreviations() throws Exception { + void addFileIncreasesCounterOfOpenFilesAndHasNoAbbreviations() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); viewModel.addNewFile(); @@ -102,7 +99,7 @@ void addFileIncreasesCounterOfOpenFilesAndHasNoAbbreviations() throws Exception } @Test - void addDuplicatedFileResultsInErrorDialog() throws Exception { + void addDuplicatedFileResultsInErrorDialog() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); viewModel.addNewFile(); viewModel.addNewFile(); @@ -110,7 +107,7 @@ void addDuplicatedFileResultsInErrorDialog() throws Exception { } @Test - void testOpenDuplicatedFileResultsInAnException() throws Exception { + void testOpenDuplicatedFileResultsInAnException() { when(dialogService.showFileOpenDialog(any())).thenReturn(Optional.of(testFile1Entries)); viewModel.openFile(); viewModel.openFile(); @@ -118,7 +115,7 @@ void testOpenDuplicatedFileResultsInAnException() throws Exception { } @Test - void testSelectLastJournalFileSwitchesFilesAndTheirAbbreviations() throws Exception { + void testSelectLastJournalFileSwitchesFilesAndTheirAbbreviations() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); @@ -131,20 +128,19 @@ void testSelectLastJournalFileSwitchesFilesAndTheirAbbreviations() throws Except } @Test - void testOpenValidFileContainsTheSpecificEntryAndEnoughAbbreviations() throws Exception { + void testOpenValidFileContainsTheSpecificEntryAndEnoughAbbreviations() { 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(3, viewModel.abbreviationsProperty().size()); assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); } @Test - void testRemoveLastListSetsCurrentFileAndCurrentAbbreviationToNull() throws Exception { + void testRemoveLastListSetsCurrentFileAndCurrentAbbreviationToNull() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); viewModel.addNewFile(); viewModel.removeCurrentFile(); @@ -156,7 +152,7 @@ void testRemoveLastListSetsCurrentFileAndCurrentAbbreviationToNull() throws Exce } @Test - void testMixedFileUsage() throws Exception { + void testMixedFileUsage() { Abbreviation testAbbreviation = new Abbreviation("Entry", "E"); Abbreviation testAbbreviation2 = new Abbreviation("EntryEntry", "EE"); @@ -212,7 +208,7 @@ void testBuiltInListsIncludeAllBuiltInAbbreviations() { } @Test - void testcurrentFilePropertyChangeActiveFile() throws Exception { + void testcurrentFilePropertyChangeActiveFile() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); viewModel.addNewFile(); when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); @@ -246,7 +242,7 @@ void testcurrentFilePropertyChangeActiveFile() throws Exception { } @Test - void testAddAbbreviationIncludesAbbreviationsInAbbreviationList() throws Exception { + void testAddAbbreviationIncludesAbbreviationsInAbbreviationList() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); viewModel.addNewFile(); when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); @@ -260,7 +256,7 @@ void testAddAbbreviationIncludesAbbreviationsInAbbreviationList() throws Excepti } @Test - void testAddDuplicatedAbbreviationResultsInException() throws JabRefException { + void testAddDuplicatedAbbreviationResultsInException() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); @@ -270,7 +266,7 @@ void testAddDuplicatedAbbreviationResultsInException() throws JabRefException { } @Test - void testEditSameAbbreviationWithNoChangeDoesNotResultInException() throws Exception { + void testEditSameAbbreviationWithNoChangeDoesNotResultInException() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); @@ -282,7 +278,7 @@ void testEditSameAbbreviationWithNoChangeDoesNotResultInException() throws Excep } @Test - void testEditAbbreviationIncludesNewAbbreviationInAbbreviationsList() throws Exception { + void testEditAbbreviationIncludesNewAbbreviationInAbbreviationsList() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); viewModel.addNewFile(); when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); @@ -305,7 +301,7 @@ void testEditAbbreviationIncludesNewAbbreviationInAbbreviationsList() throws Exc } @Test - void testEditAbbreviationToExistingOneResultsInException() throws Exception { + void testEditAbbreviationToExistingOneResultsInException() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); @@ -320,7 +316,7 @@ void testEditAbbreviationToExistingOneResultsInException() throws Exception { } @Test - void testEditAbbreviationToEmptyNameResultsInException() throws Exception { + void testEditAbbreviationToEmptyNameResultsInException() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); @@ -333,7 +329,7 @@ void testEditAbbreviationToEmptyNameResultsInException() throws Exception { } @Test - void testEditAbbreviationToEmptyAbbreviationResultsInException() throws Exception { + void testEditAbbreviationToEmptyAbbreviationResultsInException() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); @@ -346,7 +342,7 @@ void testEditAbbreviationToEmptyAbbreviationResultsInException() throws Exceptio } @Test - void testDeleteAbbreviationSelectsPreviousOne() throws Exception { + void testDeleteAbbreviationSelectsPreviousOne() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); viewModel.addNewFile(); when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); @@ -366,7 +362,7 @@ void testDeleteAbbreviationSelectsPreviousOne() throws Exception { } @Test - void testDeleteAbbreviationSelectsNextOne() throws Exception { + void testDeleteAbbreviationSelectsNextOne() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); @@ -422,11 +418,9 @@ void testSaveAbbreviationsToFilesCreatesNewFilesWithWrittenAbbreviations() throw } @Test - void testSaveExternalFilesListToPreferences() throws Exception { + void testSaveExternalFilesListToPreferences() { addFourTestFileToViewModelAndPreferences(); - List expected = Stream.of(testFile1Entries, testFile3Entries, testFile4Entries, testFile5EntriesWithDuplicate) - .map(Path::toString).collect(Collectors.toList()); - verify(abbreviationPreferences).setExternalJournalLists(expected); + verify(preferencesService).storeJournalAbbreviationPreferences(any()); } private Path createTestFile(Path folder, String name, String content) throws Exception { @@ -435,15 +429,15 @@ private Path createTestFile(Path folder, String name, String content) throws Exc return file; } - private void addAbbreviation(Abbreviation testAbbreviation) throws Exception { + private void addAbbreviation(Abbreviation testAbbreviation) { viewModel.addAbbreviation(testAbbreviation.getName(), testAbbreviation.getAbbreviation()); } - private void editAbbreviation(Abbreviation testAbbreviation) throws Exception { + private void editAbbreviation(Abbreviation testAbbreviation) { viewModel.editAbbreviation(testAbbreviation.getName(), testAbbreviation.getAbbreviation()); } - private void addFourTestFileToViewModelAndPreferences() throws Exception { + private void addFourTestFileToViewModelAndPreferences() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); viewModel.addNewFile(); when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); diff --git a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelWithShortestUniqueAbbreviationsTabTest.java b/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelWithShortestUniqueAbbreviationsTabTest.java index 373199b3ff6..feaca86dae9 100644 --- a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelWithShortestUniqueAbbreviationsTabTest.java +++ b/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelWithShortestUniqueAbbreviationsTabTest.java @@ -7,7 +7,6 @@ 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; @@ -18,7 +17,6 @@ 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; @@ -48,19 +46,19 @@ class JournalAbbreviationsViewModelWithShortestUniqueAbbreviationsTabTest { private Path testFile3Entries; private Path testFile4Entries; private Path testFile5EntriesWithDuplicate; - private JournalAbbreviationPreferences abbreviationPreferences; + private PreferencesService preferencesService; 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); + JournalAbbreviationPreferences abbreviationPreferences = mock(JournalAbbreviationPreferences.class); + preferencesService = mock(PreferencesService.class); + when(preferencesService.getJournalAbbreviationPreferences()).thenReturn(abbreviationPreferences); dialogService = mock(DialogService.class); TaskExecutor taskExecutor = new CurrentThreadTaskExecutor(); - viewModel = new JournalAbbreviationsTabViewModel(preferences, dialogService, taskExecutor, repository); + viewModel = new JournalAbbreviationsTabViewModel(preferencesService, 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 + ""); @@ -75,7 +73,7 @@ void testInitialHasNoFilesAndNoAbbreviations() { } @Test - void testInitialWithSavedFilesIncrementsFilesCounter() throws Exception { + void testInitialWithSavedFilesIncrementsFilesCounter() { addFourTestFileToViewModelAndPreferences(); viewModel.createFileObjects(); @@ -83,17 +81,16 @@ void testInitialWithSavedFilesIncrementsFilesCounter() throws Exception { } @Test - void testRemoveDuplicatesWhenReadingFiles() throws Exception { + void testRemoveDuplicatesWhenReadingFiles() { addFourTestFileToViewModelAndPreferences(); viewModel.createFileObjects(); viewModel.selectLastJournalFile(); - // should result in 4 real abbreviations and one pseudo abbreviation assertEquals(4, viewModel.abbreviationsProperty().size()); } @Test - void addFileIncreasesCounterOfOpenFilesAndHasNoAbbreviations() throws Exception { + void addFileIncreasesCounterOfOpenFilesAndHasNoAbbreviations() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); viewModel.addNewFile(); @@ -102,7 +99,7 @@ void addFileIncreasesCounterOfOpenFilesAndHasNoAbbreviations() throws Exception } @Test - void addDuplicatedFileResultsInErrorDialog() throws Exception { + void addDuplicatedFileResultsInErrorDialog() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); viewModel.addNewFile(); viewModel.addNewFile(); @@ -110,7 +107,7 @@ void addDuplicatedFileResultsInErrorDialog() throws Exception { } @Test - void testOpenDuplicatedFileResultsInAnException() throws Exception { + void testOpenDuplicatedFileResultsInAnException() { when(dialogService.showFileOpenDialog(any())).thenReturn(Optional.of(testFile1Entries)); viewModel.openFile(); viewModel.openFile(); @@ -118,7 +115,7 @@ void testOpenDuplicatedFileResultsInAnException() throws Exception { } @Test - void testSelectLastJournalFileSwitchesFilesAndTheirAbbreviations() throws Exception { + void testSelectLastJournalFileSwitchesFilesAndTheirAbbreviations() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); @@ -131,20 +128,19 @@ void testSelectLastJournalFileSwitchesFilesAndTheirAbbreviations() throws Except } @Test - void testOpenValidFileContainsTheSpecificEntryAndEnoughAbbreviations() throws Exception { + void testOpenValidFileContainsTheSpecificEntryAndEnoughAbbreviations() { 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(3, viewModel.abbreviationsProperty().size()); assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); } @Test - void testRemoveLastListSetsCurrentFileAndCurrentAbbreviationToNull() throws Exception { + void testRemoveLastListSetsCurrentFileAndCurrentAbbreviationToNull() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); viewModel.addNewFile(); viewModel.removeCurrentFile(); @@ -156,7 +152,7 @@ void testRemoveLastListSetsCurrentFileAndCurrentAbbreviationToNull() throws Exce } @Test - void testMixedFileUsage() throws Exception { + void testMixedFileUsage() { Abbreviation testAbbreviation = new Abbreviation("Entry", "En", "E"); Abbreviation testAbbreviation2 = new Abbreviation("EntryEntry", "EnEn", "EE"); @@ -212,7 +208,7 @@ void testBuiltInListsIncludeAllBuiltInAbbreviations() { } @Test - void testcurrentFilePropertyChangeActiveFile() throws Exception { + void testcurrentFilePropertyChangeActiveFile() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); viewModel.addNewFile(); when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); @@ -246,7 +242,7 @@ void testcurrentFilePropertyChangeActiveFile() throws Exception { } @Test - void testAddAbbreviationIncludesAbbreviationsInAbbreviationList() throws Exception { + void testAddAbbreviationIncludesAbbreviationsInAbbreviationList() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); viewModel.addNewFile(); when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); @@ -260,7 +256,7 @@ void testAddAbbreviationIncludesAbbreviationsInAbbreviationList() throws Excepti } @Test - void testAddDuplicatedAbbreviationResultsInException() throws JabRefException { + void testAddDuplicatedAbbreviationResultsInException() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); @@ -270,7 +266,7 @@ void testAddDuplicatedAbbreviationResultsInException() throws JabRefException { } @Test - void testEditSameAbbreviationWithNoChangeDoesNotResultInException() throws Exception { + void testEditSameAbbreviationWithNoChangeDoesNotResultInException() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); @@ -282,7 +278,7 @@ void testEditSameAbbreviationWithNoChangeDoesNotResultInException() throws Excep } @Test - void testEditAbbreviationIncludesNewAbbreviationInAbbreviationsList() throws Exception { + void testEditAbbreviationIncludesNewAbbreviationInAbbreviationsList() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); viewModel.addNewFile(); when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); @@ -298,14 +294,14 @@ void testEditAbbreviationIncludesNewAbbreviationInAbbreviationsList() throws Exc when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); - // editAbbreviation(testAbbreviation); + editAbbreviation(testAbbreviation); assertEquals(0, viewModel.abbreviationsProperty().size()); assertFalse(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); } @Test - void testEditAbbreviationToExistingOneResultsInException() throws Exception { + void testEditAbbreviationToExistingOneResultsInException() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); @@ -320,7 +316,7 @@ void testEditAbbreviationToExistingOneResultsInException() throws Exception { } @Test - void testEditAbbreviationToEmptyNameResultsInException() throws Exception { + void testEditAbbreviationToEmptyNameResultsInException() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); @@ -333,7 +329,7 @@ void testEditAbbreviationToEmptyNameResultsInException() throws Exception { } @Test - void testEditAbbreviationToEmptyAbbreviationResultsInException() throws Exception { + void testEditAbbreviationToEmptyAbbreviationResultsInException() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); @@ -346,7 +342,7 @@ void testEditAbbreviationToEmptyAbbreviationResultsInException() throws Exceptio } @Test - void testDeleteAbbreviationSelectsPreviousOne() throws Exception { + void testDeleteAbbreviationSelectsPreviousOne() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); viewModel.addNewFile(); when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile5EntriesWithDuplicate)); @@ -366,7 +362,7 @@ void testDeleteAbbreviationSelectsPreviousOne() throws Exception { } @Test - void testDeleteAbbreviationSelectsNextOne() throws Exception { + void testDeleteAbbreviationSelectsNextOne() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); @@ -423,11 +419,9 @@ void testSaveAbbreviationsToFilesCreatesNewFilesWithWrittenAbbreviations() throw } @Test - void testSaveExternalFilesListToPreferences() throws Exception { + void testSaveExternalFilesListToPreferences() { addFourTestFileToViewModelAndPreferences(); - List expected = Stream.of(testFile1Entries, testFile3Entries, testFile4Entries, testFile5EntriesWithDuplicate) - .map(Path::toString).collect(Collectors.toList()); - verify(abbreviationPreferences).setExternalJournalLists(expected); + verify(preferencesService).storeJournalAbbreviationPreferences(any()); } private Path createTestFile(Path folder, String name, String content) throws Exception { @@ -436,15 +430,15 @@ private Path createTestFile(Path folder, String name, String content) throws Exc return file; } - private void addAbbreviation(Abbreviation testAbbreviation) throws Exception { + private void addAbbreviation(Abbreviation testAbbreviation) { viewModel.addAbbreviation(testAbbreviation.getName(), testAbbreviation.getAbbreviation()); } - private void editAbbreviation(Abbreviation testAbbreviation) throws Exception { + private void editAbbreviation(Abbreviation testAbbreviation) { viewModel.editAbbreviation(testAbbreviation.getName(), testAbbreviation.getAbbreviation()); } - private void addFourTestFileToViewModelAndPreferences() throws Exception { + private void addFourTestFileToViewModelAndPreferences() { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); viewModel.addNewFile(); when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); From af29c4107ac9c65df1bdaea8b697479f15a909e4 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Wed, 28 Jul 2021 12:31:29 +0200 Subject: [PATCH 10/12] Fixed test, added logger message on store failure --- .../journals/JournalAbbreviationsTabViewModel.java | 7 ++++--- ...nalAbbreviationsViewModelMixedAbbreviationsTabTest.java | 5 ++--- 2 files changed, 6 insertions(+), 6 deletions(-) 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 d8d294340fe..d83eb917ecb 100644 --- a/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTabViewModel.java @@ -36,7 +36,7 @@ */ 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()); @@ -184,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); @@ -337,7 +337,7 @@ public void saveJournalAbbreviationFiles() { try { file.writeOrCreate(); } catch (IOException e) { - logger.debug(e.getLocalizedMessage()); + LOGGER.debug(e.getLocalizedMessage()); } }); } @@ -368,6 +368,7 @@ public void storeSettings() { }) .onSuccess((success) -> Globals.journalAbbreviationRepository = JournalAbbreviationLoader.loadRepository(preferences.getJournalAbbreviationPreferences())) + .onFailure(exception -> LOGGER.error("Failed to store journal preferences.", exception)) .executeWith(taskExecutor); } diff --git a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelMixedAbbreviationsTabTest.java b/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelMixedAbbreviationsTabTest.java index 44d55f69dcf..019e6b3c8f4 100644 --- a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelMixedAbbreviationsTabTest.java +++ b/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelMixedAbbreviationsTabTest.java @@ -75,17 +75,16 @@ void testInitialHasNoFilesAndNoAbbreviations() { @Test void testInitialWithSavedFilesIncrementsFilesCounter() { addFourTestFileToViewModelAndPreferences(); - viewModel.createFileObjects(); - assertEquals(4, viewModel.journalFilesProperty().size()); } @Test void testRemoveDuplicatesWhenReadingFiles() { addFourTestFileToViewModelAndPreferences(); - viewModel.createFileObjects(); + addFourTestFileToViewModelAndPreferences(); viewModel.selectLastJournalFile(); + assertEquals(4, viewModel.journalFilesProperty().size()); assertEquals(4, viewModel.abbreviationsProperty().size()); } From cdcb8fc944deb6068a844d8ae708ddf8e864fcbe Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Wed, 28 Jul 2021 14:39:51 +0200 Subject: [PATCH 11/12] Parameterized JournalAbbreviationsViewModelTabTest --- ...lNoShortestUniqueAbbreviationsTabTest.java | 459 ----------------- ...ithShortestUniqueAbbreviationsTabTest.java | 460 ------------------ ...JournalAbbreviationsViewModelTabTest.java} | 254 +++++----- 3 files changed, 145 insertions(+), 1028 deletions(-) delete mode 100644 src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelNoShortestUniqueAbbreviationsTabTest.java delete mode 100644 src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelWithShortestUniqueAbbreviationsTabTest.java rename src/test/java/org/jabref/gui/{journals/JournalAbbreviationsViewModelMixedAbbreviationsTabTest.java => preferences/journals/JournalAbbreviationsViewModelTabTest.java} (64%) 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 584f9bf46fe..00000000000 --- a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelNoShortestUniqueAbbreviationsTabTest.java +++ /dev/null @@ -1,459 +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 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.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 PreferencesService preferencesService; - private DialogService dialogService; - private final JournalAbbreviationRepository repository = JournalAbbreviationLoader.loadBuiltInRepository(); - - @BeforeEach - void setUpViewModel(@TempDir Path tempFolder) throws Exception { - JournalAbbreviationPreferences abbreviationPreferences = mock(JournalAbbreviationPreferences.class); - preferencesService = mock(PreferencesService.class); - when(preferencesService.getJournalAbbreviationPreferences()).thenReturn(abbreviationPreferences); - - dialogService = mock(DialogService.class); - TaskExecutor taskExecutor = new CurrentThreadTaskExecutor(); - viewModel = new JournalAbbreviationsTabViewModel(preferencesService, 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() { - addFourTestFileToViewModelAndPreferences(); - viewModel.createFileObjects(); - - assertEquals(4, viewModel.journalFilesProperty().size()); - } - - @Test - void testRemoveDuplicatesWhenReadingFiles() { - addFourTestFileToViewModelAndPreferences(); - viewModel.createFileObjects(); - viewModel.selectLastJournalFile(); - - assertEquals(4, viewModel.abbreviationsProperty().size()); - } - - @Test - void addFileIncreasesCounterOfOpenFilesAndHasNoAbbreviations() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); - viewModel.addNewFile(); - - assertEquals(1, viewModel.journalFilesProperty().size()); - assertEquals(0, viewModel.abbreviationsProperty().size()); - } - - @Test - void addDuplicatedFileResultsInErrorDialog() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.addNewFile(); - viewModel.addNewFile(); - verify(dialogService).showErrorDialogAndWait(anyString(), anyString()); - } - - @Test - void testOpenDuplicatedFileResultsInAnException() { - when(dialogService.showFileOpenDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.openFile(); - viewModel.openFile(); - verify(dialogService).showErrorDialogAndWait(anyString(), anyString()); - } - - @Test - void testSelectLastJournalFileSwitchesFilesAndTheirAbbreviations() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - assertEquals(0, viewModel.abbreviationsCountProperty().get()); - - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - assertEquals(1, viewModel.abbreviationsCountProperty().get()); - } - - @Test - void testOpenValidFileContainsTheSpecificEntryAndEnoughAbbreviations() { - Abbreviation testAbbreviation = new Abbreviation("Test Entry", "TE"); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - - assertEquals(1, viewModel.journalFilesProperty().size()); - assertEquals(3, viewModel.abbreviationsProperty().size()); - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - } - - @Test - void testRemoveLastListSetsCurrentFileAndCurrentAbbreviationToNull() { - 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() { - 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(4, 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(0, 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(4, 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() { - 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(4, viewModel.abbreviationsProperty().size()); - - viewModel.currentFileProperty().set(test1); - - // test if the current abbreviations matches with the ones in testFile1Entries - assertEquals(1, viewModel.abbreviationsProperty().size()); - - viewModel.currentFileProperty().set(test3); - assertEquals(3, viewModel.abbreviationsProperty().size()); - viewModel.currentFileProperty().set(test1); - assertEquals(1, viewModel.abbreviationsProperty().size()); - viewModel.currentFileProperty().set(test4); - assertEquals(4, viewModel.abbreviationsProperty().size()); - viewModel.currentFileProperty().set(test5); - assertEquals(4, viewModel.abbreviationsProperty().size()); - } - - @Test - void testAddAbbreviationIncludesAbbreviationsInAbbreviationList() { - 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"); - addAbbreviation(testAbbreviation); - - assertEquals(5, viewModel.abbreviationsProperty().size()); - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - } - - @Test - void testAddDuplicatedAbbreviationResultsInException() { - 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() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE"); - addAbbreviation(testAbbreviation); - editAbbreviation(testAbbreviation); - - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - } - - @Test - void testEditAbbreviationIncludesNewAbbreviationInAbbreviationsList() { - 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"); - addAbbreviation(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(0, viewModel.abbreviationsProperty().size()); - assertFalse(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - } - - @Test - void testEditAbbreviationToExistingOneResultsInException() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - selectLastAbbreviation(); - - assertEquals(3, viewModel.abbreviationsProperty().size()); - - viewModel.editAbbreviation("YetAnotherEntry", "YAE"); - 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)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - selectLastAbbreviation(); - - assertEquals(3, viewModel.abbreviationsProperty().size()); - - viewModel.editAbbreviation("", "YAE"); - verify(dialogService).showErrorDialogAndWait(anyString()); - } - - @Test - void testEditAbbreviationToEmptyAbbreviationResultsInException() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - selectLastAbbreviation(); - - 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"); - addAbbreviation(testAbbreviation); - - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - assertEquals(new AbbreviationViewModel(testAbbreviation), viewModel.currentAbbreviationProperty().get()); - - viewModel.deleteAbbreviation(); - - assertEquals(4, viewModel.abbreviationsProperty().size()); - // check if the previous (the last) element is the current abbreviation - assertEquals(viewModel.currentAbbreviationProperty().get(), viewModel.abbreviationsProperty().get(3)); - } - - @Test - void testDeleteAbbreviationSelectsNextOne() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE"); - addAbbreviation(testAbbreviation); - viewModel.currentAbbreviationProperty().set(viewModel.abbreviationsProperty().get(0)); - 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(4, 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"); - addAbbreviation(testAbbreviation1); - - assertEquals(4, 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() { - addFourTestFileToViewModelAndPreferences(); - 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 addAbbreviation(Abbreviation testAbbreviation) { - viewModel.addAbbreviation(testAbbreviation.getName(), testAbbreviation.getAbbreviation()); - } - - 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 - */ - 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 feaca86dae9..00000000000 --- a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelWithShortestUniqueAbbreviationsTabTest.java +++ /dev/null @@ -1,460 +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 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.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 PreferencesService preferencesService; - private DialogService dialogService; - private final JournalAbbreviationRepository repository = JournalAbbreviationLoader.loadBuiltInRepository(); - - @BeforeEach - void setUpViewModel(@TempDir Path tempFolder) throws Exception { - JournalAbbreviationPreferences abbreviationPreferences = mock(JournalAbbreviationPreferences.class); - preferencesService = mock(PreferencesService.class); - when(preferencesService.getJournalAbbreviationPreferences()).thenReturn(abbreviationPreferences); - - dialogService = mock(DialogService.class); - TaskExecutor taskExecutor = new CurrentThreadTaskExecutor(); - viewModel = new JournalAbbreviationsTabViewModel(preferencesService, 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() { - addFourTestFileToViewModelAndPreferences(); - viewModel.createFileObjects(); - - assertEquals(4, viewModel.journalFilesProperty().size()); - } - - @Test - void testRemoveDuplicatesWhenReadingFiles() { - addFourTestFileToViewModelAndPreferences(); - viewModel.createFileObjects(); - viewModel.selectLastJournalFile(); - - assertEquals(4, viewModel.abbreviationsProperty().size()); - } - - @Test - void addFileIncreasesCounterOfOpenFilesAndHasNoAbbreviations() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); - viewModel.addNewFile(); - - assertEquals(1, viewModel.journalFilesProperty().size()); - assertEquals(0, viewModel.abbreviationsProperty().size()); - } - - @Test - void addDuplicatedFileResultsInErrorDialog() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.addNewFile(); - viewModel.addNewFile(); - verify(dialogService).showErrorDialogAndWait(anyString(), anyString()); - } - - @Test - void testOpenDuplicatedFileResultsInAnException() { - when(dialogService.showFileOpenDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.openFile(); - viewModel.openFile(); - verify(dialogService).showErrorDialogAndWait(anyString(), anyString()); - } - - @Test - void testSelectLastJournalFileSwitchesFilesAndTheirAbbreviations() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - assertEquals(0, viewModel.abbreviationsCountProperty().get()); - - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - assertEquals(1, viewModel.abbreviationsCountProperty().get()); - } - - @Test - void testOpenValidFileContainsTheSpecificEntryAndEnoughAbbreviations() { - Abbreviation testAbbreviation = new Abbreviation("Test Entry", "TE"); - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - - assertEquals(1, viewModel.journalFilesProperty().size()); - assertEquals(3, viewModel.abbreviationsProperty().size()); - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - } - - @Test - void testRemoveLastListSetsCurrentFileAndCurrentAbbreviationToNull() { - 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() { - 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(4, 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(0, 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(4, 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() { - 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(4, viewModel.abbreviationsProperty().size()); - - viewModel.currentFileProperty().set(test1); - - // test if the current abbreviations matches with the ones in testFile1Entries - assertEquals(1, viewModel.abbreviationsProperty().size()); - - viewModel.currentFileProperty().set(test3); - assertEquals(3, viewModel.abbreviationsProperty().size()); - viewModel.currentFileProperty().set(test1); - assertEquals(1, viewModel.abbreviationsProperty().size()); - viewModel.currentFileProperty().set(test4); - assertEquals(4, viewModel.abbreviationsProperty().size()); - viewModel.currentFileProperty().set(test5); - assertEquals(4, viewModel.abbreviationsProperty().size()); - } - - @Test - void testAddAbbreviationIncludesAbbreviationsInAbbreviationList() { - 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"); - addAbbreviation(testAbbreviation); - - assertEquals(5, viewModel.abbreviationsProperty().size()); - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - } - - @Test - void testAddDuplicatedAbbreviationResultsInException() { - 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() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE", "Y"); - addAbbreviation(testAbbreviation); - editAbbreviation(testAbbreviation); - - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - } - - @Test - void testEditAbbreviationIncludesNewAbbreviationInAbbreviationsList() { - 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"); - addAbbreviation(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(0, viewModel.abbreviationsProperty().size()); - assertFalse(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - } - - @Test - void testEditAbbreviationToExistingOneResultsInException() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - selectLastAbbreviation(); - - assertEquals(3, viewModel.abbreviationsProperty().size()); - - viewModel.editAbbreviation("YetAnotherEntry", "YAE", "Y"); - viewModel.currentAbbreviationProperty().set(viewModel.abbreviationsProperty().get(1)); - viewModel.editAbbreviation("YetAnotherEntry", "YAE", "Y"); - verify(dialogService).showErrorDialogAndWait(anyString(), anyString()); - } - - @Test - void testEditAbbreviationToEmptyNameResultsInException() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - selectLastAbbreviation(); - - assertEquals(3, viewModel.abbreviationsProperty().size()); - - viewModel.editAbbreviation("", "YAE", "Y"); - verify(dialogService).showErrorDialogAndWait(anyString()); - } - - @Test - void testEditAbbreviationToEmptyAbbreviationResultsInException() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile3Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - selectLastAbbreviation(); - - assertEquals(3, viewModel.abbreviationsProperty().size()); - - viewModel.editAbbreviation("YetAnotherEntry", "", "Y"); - 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", "Y"); - addAbbreviation(testAbbreviation); - - assertTrue(viewModel.abbreviationsProperty().contains(new AbbreviationViewModel(testAbbreviation))); - assertEquals(new AbbreviationViewModel(testAbbreviation), viewModel.currentAbbreviationProperty().get()); - - viewModel.deleteAbbreviation(); - - assertEquals(4, viewModel.abbreviationsProperty().size()); - // check if the previous (the last) element is the current abbreviation - assertEquals(viewModel.currentAbbreviationProperty().get(), viewModel.abbreviationsProperty().get(3)); - } - - @Test - void testDeleteAbbreviationSelectsNextOne() { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile1Entries)); - viewModel.addNewFile(); - viewModel.selectLastJournalFile(); - Abbreviation testAbbreviation = new Abbreviation("YetAnotherEntry", "YAE", "Y"); - addAbbreviation(testAbbreviation); - viewModel.currentAbbreviationProperty().set(viewModel.abbreviationsProperty().get(1)); - viewModel.deleteAbbreviation(); - - // assertEquals(new AbbreviationViewModel(testAbbreviation), viewModel.currentAbbreviationProperty().get()); - assertNull(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(4, 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"); - addAbbreviation(testAbbreviation1); - - assertEquals(4, 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() { - addFourTestFileToViewModelAndPreferences(); - 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 addAbbreviation(Abbreviation testAbbreviation) { - viewModel.addAbbreviation(testAbbreviation.getName(), testAbbreviation.getAbbreviation()); - } - - 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 - */ - 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 64% rename from src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelMixedAbbreviationsTabTest.java rename to src/test/java/org/jabref/gui/preferences/journals/JournalAbbreviationsViewModelTabTest.java index 019e6b3c8f4..8057a09e26e 100644 --- a/src/test/java/org/jabref/gui/journals/JournalAbbreviationsViewModelMixedAbbreviationsTabTest.java +++ b/src/test/java/org/jabref/gui/preferences/journals/JournalAbbreviationsViewModelTabTest.java @@ -1,20 +1,18 @@ -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; +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.journals.Abbreviation; @@ -26,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; @@ -38,18 +39,49 @@ 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 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(new String[]{"testFile1Entries.csv", "Test Entry;TE" + NEWLINE + ""}, + new String[]{"testFile3Entries.csv", "Abbreviations;Abb;A" + NEWLINE + "Test Entry;TE" + NEWLINE + "MoreEntries;ME;M" + NEWLINE + ""}, + new String[]{"testFile4Entries.csv", "Abbreviations;Abb" + NEWLINE + "Test Entry;TE;T" + NEWLINE + "MoreEntries;ME;M" + NEWLINE + "Entry;E" + NEWLINE + ""}, + new String[]{"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(new String[]{"testFile1Entries.csv", "Test Entry;TE" + NEWLINE + ""}, + new String[]{"testFile3Entries.csv", "Abbreviations;Abb" + NEWLINE + "Test Entry;TE" + NEWLINE + "MoreEntries;ME" + NEWLINE + ""}, + new String[]{"testFile4Entries.csv", "Abbreviations;Abb" + NEWLINE + "Test Entry;TE" + NEWLINE + "MoreEntries;ME" + NEWLINE + "Entry;E" + NEWLINE + ""}, + new String[]{"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(new String[]{"testFile1Entries.csv", "Test Entry;TE;T" + NEWLINE + ""}, + new String[]{"testFile3Entries.csv", "Abbreviations;Abb;A" + NEWLINE + "Test Entry;TE;T" + NEWLINE + "MoreEntries;ME;M" + NEWLINE + ""}, + new String[]{"testFile4Entries.csv", "Abbreviations;Abb;A" + NEWLINE + "Test Entry;TE;T" + NEWLINE + "MoreEntries;ME;M" + NEWLINE + "Entry;En;E" + NEWLINE + ""}, + new String[]{"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 { JournalAbbreviationPreferences abbreviationPreferences = mock(JournalAbbreviationPreferences.class); @@ -57,13 +89,12 @@ void setUpViewModel(@TempDir Path tempFolder) throws Exception { when(preferencesService.getJournalAbbreviationPreferences()).thenReturn(abbreviationPreferences); dialogService = mock(DialogService.class); + this.tempFolder = tempFolder; + TaskExecutor taskExecutor = new CurrentThreadTaskExecutor(); viewModel = new JournalAbbreviationsTabViewModel(preferencesService, 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 + ""); + + emptyTestFile = createTestFile("emptyTestFile.csv", ""); } @Test @@ -72,16 +103,17 @@ void testInitialHasNoFilesAndNoAbbreviations() { assertEquals(0, viewModel.abbreviationsProperty().size()); } - @Test - void testInitialWithSavedFilesIncrementsFilesCounter() { - addFourTestFileToViewModelAndPreferences(); + @ParameterizedTest + @MethodSource("provideTestFiles") + void testInitialWithSavedFilesIncrementsFilesCounter(List testFiles) throws IOException { + addFourTestFileToViewModelAndPreferences(testFiles); assertEquals(4, viewModel.journalFilesProperty().size()); } - @Test - void testRemoveDuplicatesWhenReadingFiles() { - addFourTestFileToViewModelAndPreferences(); - addFourTestFileToViewModelAndPreferences(); + @ParameterizedTest + @MethodSource("provideTestFiles") + void testRemoveDuplicatesWhenReadingFiles(List testFiles) throws IOException { + addFourTestFileToViewModelAndPreferences(testFiles); viewModel.selectLastJournalFile(); assertEquals(4, viewModel.journalFilesProperty().size()); @@ -97,39 +129,43 @@ void addFileIncreasesCounterOfOpenFilesAndHasNoAbbreviations() { 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(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(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(); @@ -138,9 +174,10 @@ void testOpenValidFileContainsTheSpecificEntryAndEnoughAbbreviations() { 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(); @@ -150,15 +187,16 @@ 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)); @@ -180,7 +218,7 @@ void testMixedFileUsage() { 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)); @@ -206,17 +244,15 @@ 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 (String[] 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); @@ -240,11 +276,12 @@ void testCurrentFilePropertyChangeActiveFile() { 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"); @@ -254,9 +291,10 @@ void testAddAbbreviationIncludesAbbreviationsInAbbreviationList() { 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"); @@ -276,11 +314,12 @@ void testEditSameAbbreviationWithNoChangeDoesNotResultInException() { 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(); @@ -299,9 +338,10 @@ void testEditAbbreviationIncludesNewAbbreviationInAbbreviationsList() { 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(); @@ -314,9 +354,10 @@ void testEditAbbreviationToExistingOneResultsInException() { 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(); @@ -327,9 +368,10 @@ void testEditAbbreviationToEmptyNameResultsInException() { 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(); @@ -340,8 +382,12 @@ void testEditAbbreviationToEmptyAbbreviationResultsInException() { verify(dialogService).showErrorDialogAndWait(anyString()); } - @Test - void testSaveAbbreviationsToFilesCreatesNewFilesWithWrittenAbbreviations() throws Exception { + @ParameterizedTest + @MethodSource("provideTestFiles") + void testSaveAbbreviationsToFilesCreatesNewFilesWithWrittenAbbreviations(List testFiles, List> testEntries) throws Exception { + Path testFile4Entries = createTestFile(testFiles.get(2)); + Path testFile5EntriesWithDuplicate = createTestFile(testFiles.get(3)); + when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(testFile4Entries)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); @@ -364,37 +410,21 @@ void testSaveAbbreviationsToFilesCreatesNewFilesWithWrittenAbbreviations() throw 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(); + @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 addAbbreviation(Abbreviation testAbbreviation) { viewModel.addAbbreviation(testAbbreviation.getName(), testAbbreviation.getAbbreviation()); } @@ -403,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 */ @@ -422,4 +440,22 @@ private void selectLastAbbreviation() { viewModel.currentAbbreviationProperty() .set(viewModel.abbreviationsProperty().get(viewModel.abbreviationsCountProperty().get() - 1)); } + + private void addFourTestFileToViewModelAndPreferences(List testFiles) throws IOException { + for (String[] testFile : testFiles) { + when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFile[0], testFile[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(String[] testFile) throws IOException { + return createTestFile(testFile[0], testFile[1]); + } } From af85f2c4dbbd679d92ebf6967ea317be221050f8 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Fri, 30 Jul 2021 23:15:28 +0200 Subject: [PATCH 12/12] use list in test --- .../JournalAbbreviationsViewModelTabTest.java | 72 +++++++++---------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/src/test/java/org/jabref/gui/preferences/journals/JournalAbbreviationsViewModelTabTest.java b/src/test/java/org/jabref/gui/preferences/journals/JournalAbbreviationsViewModelTabTest.java index 8057a09e26e..c7db04c42e4 100644 --- a/src/test/java/org/jabref/gui/preferences/journals/JournalAbbreviationsViewModelTabTest.java +++ b/src/test/java/org/jabref/gui/preferences/journals/JournalAbbreviationsViewModelTabTest.java @@ -52,30 +52,30 @@ public static Stream provideTestFiles() { return Stream.of( // Mixed abbreviations Arguments.of( - List.of(new String[]{"testFile1Entries.csv", "Test Entry;TE" + NEWLINE + ""}, - new String[]{"testFile3Entries.csv", "Abbreviations;Abb;A" + NEWLINE + "Test Entry;TE" + NEWLINE + "MoreEntries;ME;M" + NEWLINE + ""}, - new String[]{"testFile4Entries.csv", "Abbreviations;Abb" + NEWLINE + "Test Entry;TE;T" + NEWLINE + "MoreEntries;ME;M" + NEWLINE + "Entry;E" + NEWLINE + ""}, - new String[]{"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("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(new String[]{"testFile1Entries.csv", "Test Entry;TE" + NEWLINE + ""}, - new String[]{"testFile3Entries.csv", "Abbreviations;Abb" + NEWLINE + "Test Entry;TE" + NEWLINE + "MoreEntries;ME" + NEWLINE + ""}, - new String[]{"testFile4Entries.csv", "Abbreviations;Abb" + NEWLINE + "Test Entry;TE" + NEWLINE + "MoreEntries;ME" + NEWLINE + "Entry;E" + NEWLINE + ""}, - new String[]{"testFile5Entries.csv", "Abbreviations;Abb" + NEWLINE + "Test Entry;TE" + NEWLINE + "Test Entry;TE" + NEWLINE + "MoreEntries;ME" + NEWLINE + "EntryEntry;EE" + NEWLINE + ""}), + 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"))), + List.of("EntryEntry;EE;EE", "Abbreviations;Abb;Abb", "Test Entry;TE;TE", "SomeOtherEntry;SOE;SOE"))), // Shortest unique abbreviations Arguments.of( - List.of(new String[]{"testFile1Entries.csv", "Test Entry;TE;T" + NEWLINE + ""}, - new String[]{"testFile3Entries.csv", "Abbreviations;Abb;A" + NEWLINE + "Test Entry;TE;T" + NEWLINE + "MoreEntries;ME;M" + NEWLINE + ""}, - new String[]{"testFile4Entries.csv", "Abbreviations;Abb;A" + NEWLINE + "Test Entry;TE;T" + NEWLINE + "MoreEntries;ME;M" + NEWLINE + "Entry;En;E" + NEWLINE + ""}, - new String[]{"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("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"))) @@ -105,14 +105,14 @@ void testInitialHasNoFilesAndNoAbbreviations() { @ParameterizedTest @MethodSource("provideTestFiles") - void testInitialWithSavedFilesIncrementsFilesCounter(List testFiles) throws IOException { + void testInitialWithSavedFilesIncrementsFilesCounter(List> testFiles) throws IOException { addFourTestFileToViewModelAndPreferences(testFiles); assertEquals(4, viewModel.journalFilesProperty().size()); } @ParameterizedTest @MethodSource("provideTestFiles") - void testRemoveDuplicatesWhenReadingFiles(List testFiles) throws IOException { + void testRemoveDuplicatesWhenReadingFiles(List> testFiles) throws IOException { addFourTestFileToViewModelAndPreferences(testFiles); viewModel.selectLastJournalFile(); @@ -131,7 +131,7 @@ void addFileIncreasesCounterOfOpenFilesAndHasNoAbbreviations() { @ParameterizedTest @MethodSource("provideTestFiles") - void addDuplicatedFileResultsInErrorDialog(List testFiles) throws IOException { + void addDuplicatedFileResultsInErrorDialog(List> testFiles) throws IOException { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(0)))); viewModel.addNewFile(); viewModel.addNewFile(); @@ -140,7 +140,7 @@ void addDuplicatedFileResultsInErrorDialog(List testFiles) throws IOEx @ParameterizedTest @MethodSource("provideTestFiles") - void testOpenDuplicatedFileResultsInAnException(List testFiles) throws IOException { + void testOpenDuplicatedFileResultsInAnException(List> testFiles) throws IOException { when(dialogService.showFileOpenDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(0)))); viewModel.openFile(); viewModel.openFile(); @@ -149,7 +149,7 @@ void testOpenDuplicatedFileResultsInAnException(List testFiles) throws @ParameterizedTest @MethodSource("provideTestFiles") - void testSelectLastJournalFileSwitchesFilesAndTheirAbbreviations(List testFiles) throws IOException { + void testSelectLastJournalFileSwitchesFilesAndTheirAbbreviations(List> testFiles) throws IOException { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(emptyTestFile)); viewModel.addNewFile(); viewModel.selectLastJournalFile(); @@ -163,7 +163,7 @@ void testSelectLastJournalFileSwitchesFilesAndTheirAbbreviations(List @ParameterizedTest @MethodSource("provideTestFiles") - void testOpenValidFileContainsTheSpecificEntryAndEnoughAbbreviations(List testFiles) throws IOException { + void testOpenValidFileContainsTheSpecificEntryAndEnoughAbbreviations(List> testFiles) throws IOException { Abbreviation testAbbreviation = new Abbreviation("Test Entry", "TE"); when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(1)))); viewModel.addNewFile(); @@ -176,7 +176,7 @@ void testOpenValidFileContainsTheSpecificEntryAndEnoughAbbreviations(List testFiles) throws IOException { + void testRemoveLastListSetsCurrentFileAndCurrentAbbreviationToNull(List> testFiles) throws IOException { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(0)))); viewModel.addNewFile(); viewModel.removeCurrentFile(); @@ -189,7 +189,7 @@ void testRemoveLastListSetsCurrentFileAndCurrentAbbreviationToNull(List testFiles) throws IOException { + void testMixedFileUsage(List> testFiles) throws IOException { Abbreviation testAbbreviation = new Abbreviation("Entry", "E"); Abbreviation testAbbreviation2 = new Abbreviation("EntryEntry", "EE"); @@ -246,8 +246,8 @@ void testBuiltInListsIncludeAllBuiltInAbbreviations() { @ParameterizedTest @MethodSource("provideTestFiles") - void testCurrentFilePropertyChangeActiveFile(List testFiles) throws IOException { - for (String[] testFile : testFiles) { + void testCurrentFilePropertyChangeActiveFile(List> testFiles) throws IOException { + for (List testFile : testFiles) { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFile))); viewModel.addNewFile(); } @@ -278,7 +278,7 @@ void testCurrentFilePropertyChangeActiveFile(List testFiles) throws IO @ParameterizedTest @MethodSource("provideTestFiles") - void testAddAbbreviationIncludesAbbreviationsInAbbreviationList(List testFiles) throws IOException { + 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(createTestFile(testFiles.get(3)))); @@ -293,7 +293,7 @@ void testAddAbbreviationIncludesAbbreviationsInAbbreviationList(List t @ParameterizedTest @MethodSource("provideTestFiles") - void testAddDuplicatedAbbreviationResultsInException(List testFiles) throws IOException { + void testAddDuplicatedAbbreviationResultsInException(List> testFiles) throws IOException { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(1)))); viewModel.addNewFile(); viewModel.selectLastJournalFile(); @@ -316,7 +316,7 @@ void testEditSameAbbreviationWithNoChangeDoesNotResultInException() { @ParameterizedTest @MethodSource("provideTestFiles") - void testEditAbbreviationIncludesNewAbbreviationInAbbreviationsList(List testFiles) throws IOException { + 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(createTestFile(testFiles.get(3)))); @@ -340,7 +340,7 @@ void testEditAbbreviationIncludesNewAbbreviationInAbbreviationsList(List testFiles) throws IOException { + void testEditAbbreviationToExistingOneResultsInException(List> testFiles) throws IOException { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(1)))); viewModel.addNewFile(); viewModel.selectLastJournalFile(); @@ -356,7 +356,7 @@ void testEditAbbreviationToExistingOneResultsInException(List testFile @ParameterizedTest @MethodSource("provideTestFiles") - void testEditAbbreviationToEmptyNameResultsInException(List testFiles) throws IOException { + void testEditAbbreviationToEmptyNameResultsInException(List> testFiles) throws IOException { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(1)))); viewModel.addNewFile(); viewModel.selectLastJournalFile(); @@ -370,7 +370,7 @@ void testEditAbbreviationToEmptyNameResultsInException(List testFiles) @ParameterizedTest @MethodSource("provideTestFiles") - void testEditAbbreviationToEmptyAbbreviationResultsInException(List testFiles) throws IOException { + void testEditAbbreviationToEmptyAbbreviationResultsInException(List> testFiles) throws IOException { when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFiles.get(1)))); viewModel.addNewFile(); viewModel.selectLastJournalFile(); @@ -384,7 +384,7 @@ void testEditAbbreviationToEmptyAbbreviationResultsInException(List te @ParameterizedTest @MethodSource("provideTestFiles") - void testSaveAbbreviationsToFilesCreatesNewFilesWithWrittenAbbreviations(List testFiles, List> testEntries) throws Exception { + void testSaveAbbreviationsToFilesCreatesNewFilesWithWrittenAbbreviations(List> testFiles, List> testEntries) throws Exception { Path testFile4Entries = createTestFile(testFiles.get(2)); Path testFile5EntriesWithDuplicate = createTestFile(testFiles.get(3)); @@ -420,7 +420,7 @@ void testSaveAbbreviationsToFilesCreatesNewFilesWithWrittenAbbreviations(List testFiles) throws IOException { + void testSaveExternalFilesListToPreferences(List> testFiles) throws IOException { addFourTestFileToViewModelAndPreferences(testFiles); verify(preferencesService).storeJournalAbbreviationPreferences(any()); } @@ -441,9 +441,9 @@ private void selectLastAbbreviation() { .set(viewModel.abbreviationsProperty().get(viewModel.abbreviationsCountProperty().get() - 1)); } - private void addFourTestFileToViewModelAndPreferences(List testFiles) throws IOException { - for (String[] testFile : testFiles) { - when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(createTestFile(testFile[0], testFile[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(); @@ -455,7 +455,7 @@ private Path createTestFile(String name, String content) throws IOException { return file; } - private Path createTestFile(String[] testFile) throws IOException { - return createTestFile(testFile[0], testFile[1]); + private Path createTestFile(List testFile) throws IOException { + return createTestFile(testFile.get(0), testFile.get(1)); } }