Skip to content

Commit

Permalink
When using the builder of BibEntry, the entry should not be modified
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
koppor committed Nov 18, 2019
1 parent eadb1e3 commit 28cd410
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
8 changes: 7 additions & 1 deletion src/main/java/org/jabref/model/database/BibDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<BibEntry> entries = FXCollections.synchronizedObservableList(FXCollections.observableArrayList(BibEntry::getObservables));
private Map<String, BibtexString> 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<String> 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;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/jabref/model/entry/BibEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ public Optional<FieldChange> setCiteKey(String newCiteKey) {

public BibEntry withCiteKey(String newCiteKey) {
setCiteKey(newCiteKey);
this.setChanged(false);
return this;
}

Expand Down Expand Up @@ -797,6 +798,7 @@ public void unregisterListener(Object object) {

public BibEntry withField(Field field, String value) {
setField(field, value);
this.setChanged(false);
return this;
}

Expand Down
25 changes: 12 additions & 13 deletions src/test/java/org/jabref/gui/exporter/SaveDatabaseActionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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> 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);
Expand All @@ -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
Expand All @@ -75,22 +74,22 @@ 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
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
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/jabref/model/entry/BibEntryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@
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;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
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;

Expand Down Expand Up @@ -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());
}
}

0 comments on commit 28cd410

Please sign in to comment.