From 6d32f306c294fae0c99e9fe30bb3a04ef0387bb3 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Sun, 3 Sep 2023 11:15:52 +0200 Subject: [PATCH 1/3] Introduce SelfContainedSaveConfiguration --- .../org/jabref/benchmarks/Benchmarks.java | 3 +- .../org/jabref/cli/ArgumentProcessor.java | 3 +- .../gui/autosaveandbackup/BackupManager.java | 4 +-- .../CreateModifyExporterDialogViewModel.java | 2 +- .../gui/exporter/SaveDatabaseAction.java | 9 ++---- .../jabref/logic/crawler/StudyRepository.java | 3 +- .../logic/exporter/BibDatabaseWriter.java | 6 ++-- .../logic/exporter/BibtexDatabaseWriter.java | 4 +-- .../logic/exporter/ExporterFactory.java | 2 +- .../logic/exporter/SaveConfiguration.java | 9 +++--- .../SelfContainedSaveConfiguration.java | 25 +++++++++++++++ .../metadata/SelfContainedSaveOrder.java | 16 +++++++++- .../jabref/preferences/JabRefPreferences.java | 31 +++++++++++-------- .../preferences/PreferencesService.java | 3 +- .../BackupManagerDiscardedTest.java | 5 +-- .../exporter/ExportToClipboardActionTest.java | 6 ++-- .../exporter/BibtexDatabaseWriterTest.java | 8 ++--- 17 files changed, 92 insertions(+), 47 deletions(-) create mode 100644 src/main/java/org/jabref/logic/exporter/SelfContainedSaveConfiguration.java diff --git a/src/jmh/java/org/jabref/benchmarks/Benchmarks.java b/src/jmh/java/org/jabref/benchmarks/Benchmarks.java index 6b5589ed18e..59aa54b48d3 100644 --- a/src/jmh/java/org/jabref/benchmarks/Benchmarks.java +++ b/src/jmh/java/org/jabref/benchmarks/Benchmarks.java @@ -14,6 +14,7 @@ import org.jabref.logic.exporter.BibWriter; import org.jabref.logic.exporter.BibtexDatabaseWriter; import org.jabref.logic.exporter.SaveConfiguration; +import org.jabref.logic.exporter.SelfContainedSaveConfiguration; import org.jabref.logic.formatter.bibtexfields.HtmlToLatexFormatter; import org.jabref.logic.importer.ParserResult; import org.jabref.logic.importer.fileformat.BibtexParser; @@ -82,7 +83,7 @@ private StringWriter getOutputWriter() throws IOException { BibWriter bibWriter = new BibWriter(outputWriter, OS.NEWLINE); BibtexDatabaseWriter databaseWriter = new BibtexDatabaseWriter( bibWriter, - mock(SaveConfiguration.class), + mock(SelfContainedSaveConfiguration.class), mock(FieldPreferences.class), mock(CitationKeyPatternPreferences.class), new BibEntryTypesManager()); diff --git a/src/main/java/org/jabref/cli/ArgumentProcessor.java b/src/main/java/org/jabref/cli/ArgumentProcessor.java index 9b0d2c1ef59..facab2b4595 100644 --- a/src/main/java/org/jabref/cli/ArgumentProcessor.java +++ b/src/main/java/org/jabref/cli/ArgumentProcessor.java @@ -26,6 +26,7 @@ import org.jabref.logic.exporter.Exporter; import org.jabref.logic.exporter.ExporterFactory; import org.jabref.logic.exporter.SaveConfiguration; +import org.jabref.logic.exporter.SelfContainedSaveConfiguration; import org.jabref.logic.exporter.XmpPdfExporter; import org.jabref.logic.importer.FetcherException; import org.jabref.logic.importer.ImportException; @@ -598,7 +599,7 @@ private void saveDatabase(BibDatabase newBase, String subName) { System.out.println(Localization.lang("Saving") + ": " + subName); try (AtomicFileWriter fileWriter = new AtomicFileWriter(Path.of(subName), StandardCharsets.UTF_8)) { BibWriter bibWriter = new BibWriter(fileWriter, OS.NEWLINE); - SaveConfiguration saveConfiguration = new SaveConfiguration() + SelfContainedSaveConfiguration saveConfiguration = (SelfContainedSaveConfiguration) new SelfContainedSaveConfiguration() .withReformatOnSave(preferencesService.getLibraryPreferences().shouldAlwaysReformatOnSave()); BibDatabaseWriter databaseWriter = new BibtexDatabaseWriter( bibWriter, diff --git a/src/main/java/org/jabref/gui/autosaveandbackup/BackupManager.java b/src/main/java/org/jabref/gui/autosaveandbackup/BackupManager.java index 616c806891a..1b9017f192f 100644 --- a/src/main/java/org/jabref/gui/autosaveandbackup/BackupManager.java +++ b/src/main/java/org/jabref/gui/autosaveandbackup/BackupManager.java @@ -28,7 +28,7 @@ import org.jabref.logic.exporter.AtomicFileWriter; import org.jabref.logic.exporter.BibWriter; import org.jabref.logic.exporter.BibtexDatabaseWriter; -import org.jabref.logic.exporter.SaveConfiguration; +import org.jabref.logic.exporter.SelfContainedSaveConfiguration; import org.jabref.logic.util.BackupFileType; import org.jabref.logic.util.CoarseChangeFilter; import org.jabref.logic.util.io.BackupFileUtil; @@ -250,7 +250,7 @@ void performBackup(Path backupPath) { } }) .orElse(SaveOrder.getDefaultSaveOrder()); - SaveConfiguration saveConfiguration = new SaveConfiguration() + SelfContainedSaveConfiguration saveConfiguration = (SelfContainedSaveConfiguration) new SelfContainedSaveConfiguration() .withMakeBackup(false) .withSaveOrder(saveOrder) .withReformatOnSave(preferences.getLibraryPreferences().shouldAlwaysReformatOnSave()); diff --git a/src/main/java/org/jabref/gui/exporter/CreateModifyExporterDialogViewModel.java b/src/main/java/org/jabref/gui/exporter/CreateModifyExporterDialogViewModel.java index 7d279d9a609..78c808dad3c 100644 --- a/src/main/java/org/jabref/gui/exporter/CreateModifyExporterDialogViewModel.java +++ b/src/main/java/org/jabref/gui/exporter/CreateModifyExporterDialogViewModel.java @@ -67,7 +67,7 @@ public ExporterViewModel saveExporter() { layoutFile.get(), extension.get(), preferences.getLayoutFormatterPreferences(), - preferences.getExportConfiguration().getSaveOrder()); + preferences.getSelfContainedExportConfiguration().getSelfContainedSaveOrder()); format.setCustomExport(true); return new ExporterViewModel(format); } diff --git a/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java b/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java index 096646fd267..3d451540b2e 100644 --- a/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java +++ b/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java @@ -31,8 +31,8 @@ import org.jabref.logic.exporter.BibDatabaseWriter; import org.jabref.logic.exporter.BibWriter; import org.jabref.logic.exporter.BibtexDatabaseWriter; -import org.jabref.logic.exporter.SaveConfiguration; import org.jabref.logic.exporter.SaveException; +import org.jabref.logic.exporter.SelfContainedSaveConfiguration; import org.jabref.logic.l10n.Encodings; import org.jabref.logic.l10n.Localization; import org.jabref.logic.shared.DatabaseLocation; @@ -256,11 +256,8 @@ private boolean save(Path targetPath, SaveDatabaseMode mode) { private boolean saveDatabase(Path file, boolean selectedOnly, Charset encoding, BibDatabaseWriter.SaveType saveType, SelfContainedSaveOrder saveOrder) throws SaveException { // if this code is adapted, please also adapt org.jabref.logic.autosaveandbackup.BackupManager.performBackup - - SaveConfiguration saveConfiguration = new SaveConfiguration() - .withSaveType(saveType) - .withSaveOrder(saveOrder) - .withReformatOnSave(preferences.getLibraryPreferences().shouldAlwaysReformatOnSave()); + SelfContainedSaveConfiguration saveConfiguration + = new SelfContainedSaveConfiguration(saveOrder, false, saveType, preferences.getLibraryPreferences().shouldAlwaysReformatOnSave()); BibDatabaseContext bibDatabaseContext = libraryTab.getBibDatabaseContext(); synchronized (bibDatabaseContext) { try (AtomicFileWriter fileWriter = new AtomicFileWriter(file, encoding, saveConfiguration.shouldMakeBackup())) { diff --git a/src/main/java/org/jabref/logic/crawler/StudyRepository.java b/src/main/java/org/jabref/logic/crawler/StudyRepository.java index 9e3abbe0804..31f7ee2772c 100644 --- a/src/main/java/org/jabref/logic/crawler/StudyRepository.java +++ b/src/main/java/org/jabref/logic/crawler/StudyRepository.java @@ -18,6 +18,7 @@ import org.jabref.logic.exporter.BibtexDatabaseWriter; import org.jabref.logic.exporter.SaveConfiguration; import org.jabref.logic.exporter.SaveException; +import org.jabref.logic.exporter.SelfContainedSaveConfiguration; import org.jabref.logic.git.SlrGitHandler; import org.jabref.logic.importer.OpenDatabase; import org.jabref.logic.importer.SearchBasedFetcher; @@ -427,7 +428,7 @@ private void generateCiteKeys(BibDatabaseContext existingEntries, BibDatabase ta private void writeResultToFile(Path pathToFile, BibDatabaseContext context) throws SaveException { try (AtomicFileWriter fileWriter = new AtomicFileWriter(pathToFile, StandardCharsets.UTF_8)) { - SaveConfiguration saveConfiguration = new SaveConfiguration() + SelfContainedSaveConfiguration saveConfiguration = (SelfContainedSaveConfiguration) new SelfContainedSaveConfiguration() .withSaveOrder(context.getMetaData().getSaveOrder().map(so -> SelfContainedSaveOrder.of(so)).orElse(SaveOrder.getDefaultSaveOrder())) .withReformatOnSave(preferencesService.getLibraryPreferences().shouldAlwaysReformatOnSave()); BibWriter bibWriter = new BibWriter(fileWriter, OS.NEWLINE); diff --git a/src/main/java/org/jabref/logic/exporter/BibDatabaseWriter.java b/src/main/java/org/jabref/logic/exporter/BibDatabaseWriter.java index e8c417b157d..68df8656bf1 100644 --- a/src/main/java/org/jabref/logic/exporter/BibDatabaseWriter.java +++ b/src/main/java/org/jabref/logic/exporter/BibDatabaseWriter.java @@ -58,13 +58,13 @@ public enum SaveType { WITH_JABREF_META_DATA, PLAIN_BIBTEX } private static final Pattern REFERENCE_PATTERN = Pattern.compile("(#[A-Za-z]+#)"); // Used to detect string references in strings protected final BibWriter bibWriter; - protected final SaveConfiguration saveConfiguration; + protected final SelfContainedSaveConfiguration saveConfiguration; protected final CitationKeyPatternPreferences keyPatternPreferences; protected final List saveActionsFieldChanges = new ArrayList<>(); protected final BibEntryTypesManager entryTypesManager; public BibDatabaseWriter(BibWriter bibWriter, - SaveConfiguration saveConfiguration, + SelfContainedSaveConfiguration saveConfiguration, CitationKeyPatternPreferences keyPatternPreferences, BibEntryTypesManager entryTypesManager) { this.bibWriter = Objects.requireNonNull(bibWriter); @@ -181,7 +181,7 @@ public void savePartOfDatabase(BibDatabaseContext bibDatabaseContext, List sortedEntries = getSortedEntries(entries, saveConfiguration.getSaveOrder()); + List sortedEntries = getSortedEntries(entries, saveConfiguration.getSelfContainedSaveOrder()); List saveActionChanges = applySaveActions(sortedEntries, bibDatabaseContext.getMetaData()); saveActionsFieldChanges.addAll(saveActionChanges); if (keyPatternPreferences.shouldGenerateCiteKeysBeforeSaving()) { diff --git a/src/main/java/org/jabref/logic/exporter/BibtexDatabaseWriter.java b/src/main/java/org/jabref/logic/exporter/BibtexDatabaseWriter.java index 39fa27614bf..558687ca21d 100644 --- a/src/main/java/org/jabref/logic/exporter/BibtexDatabaseWriter.java +++ b/src/main/java/org/jabref/logic/exporter/BibtexDatabaseWriter.java @@ -35,7 +35,7 @@ public class BibtexDatabaseWriter extends BibDatabaseWriter { private final FieldPreferences fieldPreferences; public BibtexDatabaseWriter(BibWriter bibWriter, - SaveConfiguration saveConfiguration, + SelfContainedSaveConfiguration saveConfiguration, FieldPreferences fieldPreferences, CitationKeyPatternPreferences citationKeyPatternPreferences, BibEntryTypesManager entryTypesManager) { @@ -49,7 +49,7 @@ public BibtexDatabaseWriter(BibWriter bibWriter, public BibtexDatabaseWriter(Writer writer, String newline, - SaveConfiguration saveConfiguration, + SelfContainedSaveConfiguration saveConfiguration, FieldPreferences fieldPreferences, CitationKeyPatternPreferences citationKeyPatternPreferences, BibEntryTypesManager entryTypesManager) { diff --git a/src/main/java/org/jabref/logic/exporter/ExporterFactory.java b/src/main/java/org/jabref/logic/exporter/ExporterFactory.java index 78373ef98d4..4eaa28e4dea 100644 --- a/src/main/java/org/jabref/logic/exporter/ExporterFactory.java +++ b/src/main/java/org/jabref/logic/exporter/ExporterFactory.java @@ -29,7 +29,7 @@ public static ExporterFactory create(PreferencesService preferencesService, List customFormats = preferencesService.getExportPreferences().getCustomExporters(); LayoutFormatterPreferences layoutPreferences = preferencesService.getLayoutFormatterPreferences(); - SelfContainedSaveOrder saveOrder = SelfContainedSaveOrder.of(preferencesService.getExportConfiguration().getSaveOrder()); + SelfContainedSaveOrder saveOrder = SelfContainedSaveOrder.of(preferencesService.getSelfContainedExportConfiguration().getSaveOrder()); XmpPreferences xmpPreferences = preferencesService.getXmpPreferences(); FieldPreferences fieldPreferences = preferencesService.getFieldPreferences(); BibDatabaseMode bibDatabaseMode = preferencesService.getLibraryPreferences().getDefaultBibDatabaseMode(); diff --git a/src/main/java/org/jabref/logic/exporter/SaveConfiguration.java b/src/main/java/org/jabref/logic/exporter/SaveConfiguration.java index 29742fd4a58..358659113e5 100644 --- a/src/main/java/org/jabref/logic/exporter/SaveConfiguration.java +++ b/src/main/java/org/jabref/logic/exporter/SaveConfiguration.java @@ -2,7 +2,6 @@ import org.jabref.gui.autosaveandbackup.BackupManager; import org.jabref.model.metadata.SaveOrder; -import org.jabref.model.metadata.SelfContainedSaveOrder; public class SaveConfiguration { @@ -10,11 +9,11 @@ public class SaveConfiguration { public static final String ENCODING_PREFIX = "Encoding: "; private boolean reformatFile; - private SelfContainedSaveOrder saveOrder; + private SaveOrder saveOrder; private boolean makeBackup; private BibDatabaseWriter.SaveType saveType; - public SaveConfiguration(SelfContainedSaveOrder saveOrder, + public SaveConfiguration(SaveOrder saveOrder, Boolean makeBackup, BibDatabaseWriter.SaveType saveType, Boolean reformatFile) { @@ -31,11 +30,11 @@ public SaveConfiguration() { false); } - public SelfContainedSaveOrder getSaveOrder() { + public SaveOrder getSaveOrder() { return saveOrder; } - public SaveConfiguration withSaveOrder(SelfContainedSaveOrder newSaveOrder) { + public SaveConfiguration withSaveOrder(SaveOrder newSaveOrder) { this.saveOrder = newSaveOrder; return this; } diff --git a/src/main/java/org/jabref/logic/exporter/SelfContainedSaveConfiguration.java b/src/main/java/org/jabref/logic/exporter/SelfContainedSaveConfiguration.java new file mode 100644 index 00000000000..1e5cb241a1e --- /dev/null +++ b/src/main/java/org/jabref/logic/exporter/SelfContainedSaveConfiguration.java @@ -0,0 +1,25 @@ +package org.jabref.logic.exporter; + +import org.jabref.model.metadata.SaveOrder; +import org.jabref.model.metadata.SelfContainedSaveOrder; + +/** + * This is a {@link SaveOrder} where the contained saveConfiguration is a {@link org.jabref.model.metadata.SelfContainedSaveOrder} + */ +public class SelfContainedSaveConfiguration extends SaveConfiguration { + public SelfContainedSaveConfiguration() { + super(); + } + + public SelfContainedSaveConfiguration( + SelfContainedSaveOrder saveOrder, + Boolean makeBackup, + BibDatabaseWriter.SaveType saveType, + Boolean reformatFile) { + super(saveOrder, makeBackup, saveType, reformatFile); + } + + public SelfContainedSaveOrder getSelfContainedSaveOrder() { + return (SelfContainedSaveOrder) getSaveOrder(); + } +} diff --git a/src/main/java/org/jabref/model/metadata/SelfContainedSaveOrder.java b/src/main/java/org/jabref/model/metadata/SelfContainedSaveOrder.java index 1c40945e9ed..4b5cafc6b9c 100644 --- a/src/main/java/org/jabref/model/metadata/SelfContainedSaveOrder.java +++ b/src/main/java/org/jabref/model/metadata/SelfContainedSaveOrder.java @@ -2,14 +2,23 @@ import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * With this class, the user of an instance can directly sort things. Without looking up anything in the preferences or in the UI. + * + * To avoid confusion at the caller, we offer ORIGINAL and SPECIFIED only. Not TABLE. */ public class SelfContainedSaveOrder extends SaveOrder { + + public static final Logger LOGGER = LoggerFactory.getLogger(SelfContainedSaveOrder.class); + public SelfContainedSaveOrder(OrderType orderType, List sortCriteria) { super(orderType, sortCriteria); if (orderType == OrderType.TABLE) { - throw new IllegalArgumentException("TABLE requires external lookup."); + LOGGER.debug("TABLE with sort criteria {}", sortCriteria); + throw new IllegalArgumentException("TABLE might require external lookup."); } } @@ -22,6 +31,11 @@ public static SelfContainedSaveOrder of(SaveOrder saveOrder) { if (saveOrder instanceof SelfContainedSaveOrder order) { return order; } + if ((saveOrder.getOrderType() == OrderType.TABLE) && (!saveOrder.getSortCriteria().isEmpty())) { + // We map from TABLE to SPECIFIED to have the users of this class just to `switch` between + // ORIGINAL and SPECIFIED + return new SelfContainedSaveOrder(OrderType.SPECIFIED, saveOrder.getSortCriteria()); + } return new SelfContainedSaveOrder(saveOrder.getOrderType(), saveOrder.getSortCriteria()); } } diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 651ecc4ca85..46f2aab1c96 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -65,8 +65,9 @@ import org.jabref.logic.citationstyle.CitationStyle; import org.jabref.logic.citationstyle.CitationStylePreviewLayout; import org.jabref.logic.cleanup.FieldFormatterCleanups; +import org.jabref.logic.exporter.BibDatabaseWriter; import org.jabref.logic.exporter.MetaDataSerializer; -import org.jabref.logic.exporter.SaveConfiguration; +import org.jabref.logic.exporter.SelfContainedSaveConfiguration; import org.jabref.logic.exporter.TemplateExporter; import org.jabref.logic.importer.ImportFormatPreferences; import org.jabref.logic.importer.ImporterPreferences; @@ -2256,29 +2257,33 @@ private void storeExportSaveOrder(SaveOrder saveOrder) { putBoolean(EXPORT_TERTIARY_SORT_DESCENDING, saveOrder.getSortCriteria().get(2).descending); } - public SaveOrder getTableSaveOrder() { + /** + * For the export configuration, generates the SelfContainedSaveOrder having the reference to TABLE resolved. + */ + public SelfContainedSaveOrder getSelfContainedTableSaveOrder() { List sortOrder = mainTableColumnPreferences.getColumnSortOrder(); - return new SaveOrder( - SaveOrder.OrderType.TABLE, + return new SelfContainedSaveOrder( + SaveOrder.OrderType.SPECIFIED, sortOrder.stream().flatMap(model -> model.getSortCriteria().stream()).toList()); } @Override - public SaveConfiguration getExportConfiguration() { - SaveOrder saveOrder = switch (getExportSaveOrder().getOrderType()) { - case TABLE -> this.getTableSaveOrder(); - case SPECIFIED -> this.getExportSaveOrder(); + public SelfContainedSaveConfiguration getSelfContainedExportConfiguration() { + SaveOrder exportSaveOrder = getExportSaveOrder(); + SelfContainedSaveOrder saveOrder = switch (exportSaveOrder.getOrderType()) { + case TABLE -> this.getSelfContainedTableSaveOrder(); + case SPECIFIED -> SelfContainedSaveOrder.of(exportSaveOrder); case ORIGINAL -> SaveOrder.getDefaultSaveOrder(); }; - return new SaveConfiguration() - .withSaveOrder(SelfContainedSaveOrder.of(saveOrder)) - .withReformatOnSave(getLibraryPreferences().shouldAlwaysReformatOnSave()); + return new SelfContainedSaveConfiguration( + saveOrder, false, BibDatabaseWriter.SaveType.WITH_JABREF_META_DATA, getLibraryPreferences() + .shouldAlwaysReformatOnSave()); } private List getCustomExportFormats() { LayoutFormatterPreferences layoutPreferences = getLayoutFormatterPreferences(); - SaveConfiguration saveConfiguration = getExportConfiguration(); + SelfContainedSaveConfiguration saveConfiguration = getSelfContainedExportConfiguration(); List formats = new ArrayList<>(); for (String toImport : getSeries(CUSTOM_EXPORT_FORMAT)) { @@ -2288,7 +2293,7 @@ private List getCustomExportFormats() { formatData.get(EXPORTER_FILENAME_INDEX), formatData.get(EXPORTER_EXTENSION_INDEX), layoutPreferences, - saveConfiguration.getSaveOrder()); + saveConfiguration.getSelfContainedSaveOrder()); format.setCustomExport(true); formats.add(format); } diff --git a/src/main/java/org/jabref/preferences/PreferencesService.java b/src/main/java/org/jabref/preferences/PreferencesService.java index 485e93ded56..890cd723aed 100644 --- a/src/main/java/org/jabref/preferences/PreferencesService.java +++ b/src/main/java/org/jabref/preferences/PreferencesService.java @@ -16,6 +16,7 @@ import org.jabref.logic.bibtex.FieldPreferences; import org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences; import org.jabref.logic.exporter.SaveConfiguration; +import org.jabref.logic.exporter.SelfContainedSaveConfiguration; import org.jabref.logic.importer.ImportFormatPreferences; import org.jabref.logic.importer.ImporterPreferences; import org.jabref.logic.importer.fetcher.GrobidPreferences; @@ -73,7 +74,7 @@ public interface PreferencesService { /** * Returns the export configuration. The contained SaveConfiguration is a {@link org.jabref.model.metadata.SelfContainedSaveOrder} */ - SaveConfiguration getExportConfiguration(); + SelfContainedSaveConfiguration getSelfContainedExportConfiguration(); BibEntryTypesManager getCustomEntryTypesRepository(); diff --git a/src/test/java/org/jabref/gui/autosaveandbackup/BackupManagerDiscardedTest.java b/src/test/java/org/jabref/gui/autosaveandbackup/BackupManagerDiscardedTest.java index e7d868ee862..2221742edef 100644 --- a/src/test/java/org/jabref/gui/autosaveandbackup/BackupManagerDiscardedTest.java +++ b/src/test/java/org/jabref/gui/autosaveandbackup/BackupManagerDiscardedTest.java @@ -11,6 +11,7 @@ import org.jabref.logic.exporter.BibWriter; import org.jabref.logic.exporter.BibtexDatabaseWriter; import org.jabref.logic.exporter.SaveConfiguration; +import org.jabref.logic.exporter.SelfContainedSaveConfiguration; import org.jabref.model.database.BibDatabase; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; @@ -38,7 +39,7 @@ class BackupManagerDiscardedTest { private BibDatabaseContext bibDatabaseContext; private BackupManager backupManager; private Path testBib; - private SaveConfiguration saveConfiguration; + private SelfContainedSaveConfiguration saveConfiguration; private PreferencesService preferencesService; private BibEntryTypesManager bibEntryTypesManager; private Path backupDir; @@ -55,7 +56,7 @@ public void setup(@TempDir Path tempDir) throws Exception { bibEntryTypesManager = new BibEntryTypesManager(); - saveConfiguration = mock(SaveConfiguration.class); + saveConfiguration = mock(SelfContainedSaveConfiguration.class); when(saveConfiguration.shouldMakeBackup()).thenReturn(false); when(saveConfiguration.getSaveOrder()).thenReturn(SaveOrder.getDefaultSaveOrder()); when(saveConfiguration.withMakeBackup(anyBoolean())).thenReturn(saveConfiguration); diff --git a/src/test/java/org/jabref/gui/exporter/ExportToClipboardActionTest.java b/src/test/java/org/jabref/gui/exporter/ExportToClipboardActionTest.java index 9602c84fd59..973247fe8f0 100644 --- a/src/test/java/org/jabref/gui/exporter/ExportToClipboardActionTest.java +++ b/src/test/java/org/jabref/gui/exporter/ExportToClipboardActionTest.java @@ -14,7 +14,7 @@ import org.jabref.gui.util.CurrentThreadTaskExecutor; import org.jabref.gui.util.TaskExecutor; import org.jabref.logic.exporter.Exporter; -import org.jabref.logic.exporter.SaveConfiguration; +import org.jabref.logic.exporter.SelfContainedSaveConfiguration; import org.jabref.logic.l10n.Localization; import org.jabref.logic.util.StandardFileType; import org.jabref.logic.xmp.XmpPreferences; @@ -67,7 +67,7 @@ public void setUp() { taskExecutor = new CurrentThreadTaskExecutor(); when(preferences.getExportPreferences().getCustomExporters()).thenReturn(FXCollections.observableList(List.of())); - when(preferences.getExportConfiguration()).thenReturn(mock(SaveConfiguration.class)); + when(preferences.getSelfContainedExportConfiguration()).thenReturn(mock(SelfContainedSaveConfiguration.class)); when(preferences.getXmpPreferences()).thenReturn(mock(XmpPreferences.class)); exportToClipboardAction = new ExportToClipboardAction(dialogService, stateManager, clipBoardManager, taskExecutor, preferences); } @@ -93,7 +93,7 @@ public void export(BibDatabaseContext databaseContext, Path file, List when(preferences.getFilePreferences()).thenReturn(filePreferences); when(preferences.getLibraryPreferences()).thenReturn(libraryPreferences); when(preferences.getExportPreferences().getLastExportExtension()).thenReturn("HTML"); - when(preferences.getExportConfiguration().getSaveOrder()).thenReturn(SaveOrder.getDefaultSaveOrder()); + when(preferences.getSelfContainedExportConfiguration().getSaveOrder()).thenReturn(SaveOrder.getDefaultSaveOrder()); when(stateManager.getSelectedEntries()).thenReturn(selectedEntries); when(stateManager.getActiveDatabase()).thenReturn(Optional.ofNullable(databaseContext)); // noinspection ConstantConditions since databaseContext is mocked diff --git a/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java b/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java index 5094e60b8ad..be277db4e5b 100644 --- a/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java +++ b/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java @@ -72,7 +72,7 @@ public class BibtexDatabaseWriterTest { private MetaData metaData; private BibDatabaseContext bibtexContext; private ImportFormatPreferences importFormatPreferences; - private SaveConfiguration saveConfiguration; + private SelfContainedSaveConfiguration saveConfiguration; private FieldPreferences fieldPreferences; private CitationKeyPatternPreferences citationKeyPatternPreferences; private BibEntryTypesManager entryTypesManager; @@ -82,7 +82,7 @@ public class BibtexDatabaseWriterTest { @BeforeEach void setUp() { fieldPreferences = new FieldPreferences(true, Collections.emptyList(), Collections.emptyList()); - saveConfiguration = new SaveConfiguration(SaveOrder.getDefaultSaveOrder(), false, BibDatabaseWriter.SaveType.WITH_JABREF_META_DATA, false); + saveConfiguration = new SelfContainedSaveConfiguration(SaveOrder.getDefaultSaveOrder(), false, BibDatabaseWriter.SaveType.WITH_JABREF_META_DATA, false); citationKeyPatternPreferences = mock(CitationKeyPatternPreferences.class, Answers.RETURNS_DEEP_STUBS); entryTypesManager = new BibEntryTypesManager(); stringWriter = new StringWriter(); @@ -680,7 +680,7 @@ void reformatEntryIfAskedToDoSo() throws Exception { entry.setChanged(false); database.insertEntry(entry); - saveConfiguration = new SaveConfiguration(SaveOrder.getDefaultSaveOrder(), false, BibDatabaseWriter.SaveType.WITH_JABREF_META_DATA, true); + saveConfiguration = new SelfContainedSaveConfiguration(SaveOrder.getDefaultSaveOrder(), false, BibDatabaseWriter.SaveType.WITH_JABREF_META_DATA, true); initializeDatabaseWriter(); databaseWriter.savePartOfDatabase(bibtexContext, Collections.singletonList(entry)); @@ -706,7 +706,7 @@ void reformatStringIfAskedToDoSo() throws Exception { string.setParsedSerialization("wrong serialization"); database.addString(string); - saveConfiguration = new SaveConfiguration(SaveOrder.getDefaultSaveOrder(), false, BibDatabaseWriter.SaveType.WITH_JABREF_META_DATA, true); + saveConfiguration = new SelfContainedSaveConfiguration(SaveOrder.getDefaultSaveOrder(), false, BibDatabaseWriter.SaveType.WITH_JABREF_META_DATA, true); initializeDatabaseWriter(); databaseWriter.savePartOfDatabase(bibtexContext, Collections.emptyList()); From e1e244f013fa576f5a6485d380aaf8502160fa06 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Sun, 3 Sep 2023 12:24:51 +0200 Subject: [PATCH 2/3] Fix checkstyle --- src/jmh/java/org/jabref/benchmarks/Benchmarks.java | 1 - src/main/java/org/jabref/cli/ArgumentProcessor.java | 1 - src/main/java/org/jabref/logic/crawler/StudyRepository.java | 1 - src/main/java/org/jabref/preferences/PreferencesService.java | 1 - .../jabref/gui/autosaveandbackup/BackupManagerDiscardedTest.java | 1 - 5 files changed, 5 deletions(-) diff --git a/src/jmh/java/org/jabref/benchmarks/Benchmarks.java b/src/jmh/java/org/jabref/benchmarks/Benchmarks.java index 59aa54b48d3..8eae854642c 100644 --- a/src/jmh/java/org/jabref/benchmarks/Benchmarks.java +++ b/src/jmh/java/org/jabref/benchmarks/Benchmarks.java @@ -13,7 +13,6 @@ import org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences; import org.jabref.logic.exporter.BibWriter; import org.jabref.logic.exporter.BibtexDatabaseWriter; -import org.jabref.logic.exporter.SaveConfiguration; import org.jabref.logic.exporter.SelfContainedSaveConfiguration; import org.jabref.logic.formatter.bibtexfields.HtmlToLatexFormatter; import org.jabref.logic.importer.ParserResult; diff --git a/src/main/java/org/jabref/cli/ArgumentProcessor.java b/src/main/java/org/jabref/cli/ArgumentProcessor.java index facab2b4595..5d5c2c025d6 100644 --- a/src/main/java/org/jabref/cli/ArgumentProcessor.java +++ b/src/main/java/org/jabref/cli/ArgumentProcessor.java @@ -25,7 +25,6 @@ import org.jabref.logic.exporter.EmbeddedBibFilePdfExporter; import org.jabref.logic.exporter.Exporter; import org.jabref.logic.exporter.ExporterFactory; -import org.jabref.logic.exporter.SaveConfiguration; import org.jabref.logic.exporter.SelfContainedSaveConfiguration; import org.jabref.logic.exporter.XmpPdfExporter; import org.jabref.logic.importer.FetcherException; diff --git a/src/main/java/org/jabref/logic/crawler/StudyRepository.java b/src/main/java/org/jabref/logic/crawler/StudyRepository.java index 31f7ee2772c..dc490090339 100644 --- a/src/main/java/org/jabref/logic/crawler/StudyRepository.java +++ b/src/main/java/org/jabref/logic/crawler/StudyRepository.java @@ -16,7 +16,6 @@ import org.jabref.logic.exporter.AtomicFileWriter; import org.jabref.logic.exporter.BibWriter; import org.jabref.logic.exporter.BibtexDatabaseWriter; -import org.jabref.logic.exporter.SaveConfiguration; import org.jabref.logic.exporter.SaveException; import org.jabref.logic.exporter.SelfContainedSaveConfiguration; import org.jabref.logic.git.SlrGitHandler; diff --git a/src/main/java/org/jabref/preferences/PreferencesService.java b/src/main/java/org/jabref/preferences/PreferencesService.java index 890cd723aed..554bcf67d71 100644 --- a/src/main/java/org/jabref/preferences/PreferencesService.java +++ b/src/main/java/org/jabref/preferences/PreferencesService.java @@ -15,7 +15,6 @@ import org.jabref.logic.JabRefException; import org.jabref.logic.bibtex.FieldPreferences; import org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences; -import org.jabref.logic.exporter.SaveConfiguration; import org.jabref.logic.exporter.SelfContainedSaveConfiguration; import org.jabref.logic.importer.ImportFormatPreferences; import org.jabref.logic.importer.ImporterPreferences; diff --git a/src/test/java/org/jabref/gui/autosaveandbackup/BackupManagerDiscardedTest.java b/src/test/java/org/jabref/gui/autosaveandbackup/BackupManagerDiscardedTest.java index 2221742edef..7ef7e6501c8 100644 --- a/src/test/java/org/jabref/gui/autosaveandbackup/BackupManagerDiscardedTest.java +++ b/src/test/java/org/jabref/gui/autosaveandbackup/BackupManagerDiscardedTest.java @@ -10,7 +10,6 @@ import org.jabref.logic.exporter.AtomicFileWriter; import org.jabref.logic.exporter.BibWriter; import org.jabref.logic.exporter.BibtexDatabaseWriter; -import org.jabref.logic.exporter.SaveConfiguration; import org.jabref.logic.exporter.SelfContainedSaveConfiguration; import org.jabref.model.database.BibDatabase; import org.jabref.model.database.BibDatabaseContext; From 801b77e9d79c779377711f9d936d6c639ebe0991 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Sun, 3 Sep 2023 13:25:01 +0200 Subject: [PATCH 3/3] Fix NPE --- .../autosaveandbackup/BackupManagerDiscardedTest.java | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/test/java/org/jabref/gui/autosaveandbackup/BackupManagerDiscardedTest.java b/src/test/java/org/jabref/gui/autosaveandbackup/BackupManagerDiscardedTest.java index 7ef7e6501c8..a1ee0b8031e 100644 --- a/src/test/java/org/jabref/gui/autosaveandbackup/BackupManagerDiscardedTest.java +++ b/src/test/java/org/jabref/gui/autosaveandbackup/BackupManagerDiscardedTest.java @@ -8,6 +8,7 @@ import org.jabref.gui.LibraryTab; import org.jabref.logic.exporter.AtomicFileWriter; +import org.jabref.logic.exporter.BibDatabaseWriter; import org.jabref.logic.exporter.BibWriter; import org.jabref.logic.exporter.BibtexDatabaseWriter; import org.jabref.logic.exporter.SelfContainedSaveConfiguration; @@ -26,9 +27,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; /** * Test for "discarded" flag @@ -54,17 +53,13 @@ public void setup(@TempDir Path tempDir) throws Exception { bibDatabaseContext.setDatabasePath(testBib); bibEntryTypesManager = new BibEntryTypesManager(); - - saveConfiguration = mock(SelfContainedSaveConfiguration.class); - when(saveConfiguration.shouldMakeBackup()).thenReturn(false); - when(saveConfiguration.getSaveOrder()).thenReturn(SaveOrder.getDefaultSaveOrder()); - when(saveConfiguration.withMakeBackup(anyBoolean())).thenReturn(saveConfiguration); - + saveConfiguration = new SelfContainedSaveConfiguration(SaveOrder.getDefaultSaveOrder(), false, BibDatabaseWriter.SaveType.WITH_JABREF_META_DATA, false); preferencesService = mock(PreferencesService.class, Answers.RETURNS_DEEP_STUBS); saveDatabase(); backupManager = new BackupManager(mock(LibraryTab.class), bibDatabaseContext, bibEntryTypesManager, preferencesService); + makeBackup(); }