From 28cd4103b61cca1e4258ef1224a6b07c4f51649f Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Mon, 18 Nov 2019 07:53:27 +0100 Subject: [PATCH] When using the builder of BibEntry, the entry should not be modified - Ensure that `BibEntry#withField` keeps `hasChanged() == false` - Refine comment and add empty lines in BibDatabase.java - Fix semantics of variable in SaveDatabaseActionTest (it really points to a file, not to a directory) --- .../jabref/model/database/BibDatabase.java | 8 +++++- .../java/org/jabref/model/entry/BibEntry.java | 2 ++ .../gui/exporter/SaveDatabaseActionTest.java | 25 +++++++++---------- .../org/jabref/model/entry/BibEntryTest.java | 15 +++++++++++ 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/jabref/model/database/BibDatabase.java b/src/main/java/org/jabref/model/database/BibDatabase.java index 9e3485bc6028..be123c26c4c7 100644 --- a/src/main/java/org/jabref/model/database/BibDatabase.java +++ b/src/main/java/org/jabref/model/database/BibDatabase.java @@ -41,27 +41,33 @@ import org.slf4j.LoggerFactory; /** - * A bibliography database. + * A bibliography database. This is the "bib" file (or the library stored in a shared SQL database) */ public class BibDatabase { private static final Logger LOGGER = LoggerFactory.getLogger(BibDatabase.class); private static final Pattern RESOLVE_CONTENT_PATTERN = Pattern.compile(".*#[^#]+#.*"); + /** * State attributes */ private final ObservableList entries = FXCollections.synchronizedObservableList(FXCollections.observableArrayList(BibEntry::getObservables)); private Map bibtexStrings = new ConcurrentHashMap<>(); + /** * this is kept in sync with the database (upon adding/removing an entry, it is updated as well) */ private final DuplicationChecker duplicationChecker = new DuplicationChecker(); + /** * contains all entry.getID() of the current database */ private final Set internalIDs = new HashSet<>(); + private final EventBus eventBus = new EventBus(); + private String preamble; + // All file contents below the last entry in the file private String epilog = ""; private String sharedDatabaseID; diff --git a/src/main/java/org/jabref/model/entry/BibEntry.java b/src/main/java/org/jabref/model/entry/BibEntry.java index 2d1b29a02c1c..845d482c3d05 100644 --- a/src/main/java/org/jabref/model/entry/BibEntry.java +++ b/src/main/java/org/jabref/model/entry/BibEntry.java @@ -317,6 +317,7 @@ public Optional setCiteKey(String newCiteKey) { public BibEntry withCiteKey(String newCiteKey) { setCiteKey(newCiteKey); + this.setChanged(false); return this; } @@ -797,6 +798,7 @@ public void unregisterListener(Object object) { public BibEntry withField(Field field, String value) { setField(field, value); + this.setChanged(false); return this; } diff --git a/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java b/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java index ecb0717a42fc..31331bfc66a2 100644 --- a/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java +++ b/src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java @@ -27,9 +27,8 @@ class SaveDatabaseActionTest { - private static final String TEST_FILE_PATH = "C:\\Users\\John_Doe\\Jabref"; - private final File file = new File(TEST_FILE_PATH); - private Optional path = Optional.of(file.toPath()); + private static final String TEST_BIBTEX_LIBRARY_LOCATION = "C:\\Users\\John_Doe\\Jabref\\literature.bib"; + private final Path file = Path.of(TEST_BIBTEX_LIBRARY_LOCATION); private DialogService dialogService = mock(DialogService.class); private JabRefPreferences preferences = mock(JabRefPreferences.class); @@ -49,24 +48,24 @@ public void setUp() { @Test public void saveAsShouldSetWorkingDirectory() { - when(preferences.get(JabRefPreferences.WORKING_DIRECTORY)).thenReturn(TEST_FILE_PATH); - when(dialogService.showFileSaveDialog(any(FileDialogConfiguration.class))).thenReturn(path); + when(preferences.get(JabRefPreferences.WORKING_DIRECTORY)).thenReturn(TEST_BIBTEX_LIBRARY_LOCATION); + when(dialogService.showFileSaveDialog(any(FileDialogConfiguration.class))).thenReturn(Optional.of(file)); doNothing().when(saveDatabaseAction).saveAs(any()); saveDatabaseAction.saveAs(); - verify(preferences, times(1)).setWorkingDir(path.get().getParent()); + verify(preferences, times(1)).setWorkingDir(file.getParent()); } @Test public void saveAsShouldNotSetWorkingDirectoryIfNotSelected() { - when(preferences.get(JabRefPreferences.WORKING_DIRECTORY)).thenReturn(TEST_FILE_PATH); + when(preferences.get(JabRefPreferences.WORKING_DIRECTORY)).thenReturn(TEST_BIBTEX_LIBRARY_LOCATION); when(dialogService.showFileSaveDialog(any(FileDialogConfiguration.class))).thenReturn(Optional.empty()); doNothing().when(saveDatabaseAction).saveAs(any()); saveDatabaseAction.saveAs(); - verify(preferences, times(0)).setWorkingDir(path.get().getParent()); + verify(preferences, times(0)).setWorkingDir(file.getParent()); } @Test @@ -75,9 +74,9 @@ public void saveAsShouldSetNewDatabasePathIntoContext() { when(dbContext.getLocation()).thenReturn(DatabaseLocation.LOCAL); when(preferences.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE)).thenReturn(false); - saveDatabaseAction.saveAs(file.toPath()); + saveDatabaseAction.saveAs(file); - verify(dbContext, times(1)).setDatabaseFile(file.toPath()); + verify(dbContext, times(1)).setDatabaseFile(file); } @Test @@ -85,12 +84,12 @@ public void saveShouldShowSaveAsIfDatabaseNotSelected() { when(dbContext.getDatabasePath()).thenReturn(Optional.empty()); when(dbContext.getLocation()).thenReturn(DatabaseLocation.LOCAL); when(preferences.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE)).thenReturn(false); - when(dialogService.showFileSaveDialog(any())).thenReturn(path); - doNothing().when(saveDatabaseAction).saveAs(file.toPath()); + when(dialogService.showFileSaveDialog(any())).thenReturn(Optional.of(file)); + doNothing().when(saveDatabaseAction).saveAs(file); saveDatabaseAction.save(); - verify(saveDatabaseAction, times(1)).saveAs(file.toPath()); + verify(saveDatabaseAction, times(1)).saveAs(file); } @Test diff --git a/src/test/java/org/jabref/model/entry/BibEntryTest.java b/src/test/java/org/jabref/model/entry/BibEntryTest.java index ddfe5dab35c0..02acbfc2863e 100644 --- a/src/test/java/org/jabref/model/entry/BibEntryTest.java +++ b/src/test/java/org/jabref/model/entry/BibEntryTest.java @@ -21,6 +21,7 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.Execution; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -28,7 +29,9 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT; +@Execution(CONCURRENT) class BibEntryTest { private BibEntry entry; @@ -616,4 +619,16 @@ public void testGetResolvedKeywords() { assertEquals(new KeywordList(new Keyword("kw"), new Keyword("kw2"), new Keyword("kw3")), actual); } + + @Test + public void settingTitleFieldsLeadsToChangeFlagged() { + entry.setField(StandardField.AUTHOR, "value"); + assertTrue(entry.hasChanged()); + } + + @Test + public void builderReturnsABibEntryNotChangedFlagged() { + entry = new BibEntry().withField(StandardField.AUTHOR, "value"); + assertFalse(entry.hasChanged()); + } }