Skip to content

Commit

Permalink
Add unit tests, change log entry, and make changes according to the r…
Browse files Browse the repository at this point in the history
…eview
  • Loading branch information
priyanshu16095 committed Jan 25, 2025
1 parent 77ea33b commit 55f40ee
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv

### Added

- We added a 'Copy to' context menu option with features for cross-reference inclusion/exclusion, as well as the ability to save user preferences. [#12374](https://github.com/JabRef/jabref/pull/12374)
- We added a Markdown export layout. [#12220](https://github.com/JabRef/jabref/pull/12220)
- We added a "view as BibTeX" option before importing an entry from the citation relation tab. [#11826](https://github.com/JabRef/jabref/issues/11826)
- We added support finding LaTeX-encoded special characters based on plain Unicode and vice versa. [#11542](https://github.com/JabRef/jabref/pull/11542)
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/jabref/gui/edit/CopyToPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ public class CopyToPreferences {
private final BooleanProperty shouldIncludeCrossReferences = new SimpleBooleanProperty();
private final BooleanProperty shouldAskForIncludingCrossReferences = new SimpleBooleanProperty();

public CopyToPreferences(boolean shouldAskForIncludingCrossReferences,
boolean shouldIncludeCrossReferences
) {
public CopyToPreferences(boolean shouldAskForIncludingCrossReferences, boolean shouldIncludeCrossReferences) {
this.shouldIncludeCrossReferences.set(shouldIncludeCrossReferences);
this.shouldAskForIncludingCrossReferences.set(shouldAskForIncludingCrossReferences);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/maintable/RightClickMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private static Menu createCopyToMenu(ActionFactory factory,
if (!openDatabases.isEmpty()) {
openDatabases.forEach(bibDatabaseContext -> {
Optional<Path> destinationPath = Optional.empty();
String destinationDatabaseName = " ";
String destinationDatabaseName = "";

if (bibDatabaseContext.getDatabasePath().isPresent()) {
destinationPath = bibDatabaseContext.getDatabasePath();
Expand Down
147 changes: 147 additions & 0 deletions src/test/java/org/jabref/gui/edit/CopyToTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package org.jabref.gui.edit;

import java.util.ArrayList;
import java.util.List;

import javafx.collections.ObservableList;

import org.jabref.gui.DialogService;
import org.jabref.gui.StateManager;
import org.jabref.gui.externalfiles.ImportHandler;
import org.jabref.gui.preferences.GuiPreferences;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.types.StandardEntryType;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class CopyToTest {

private final DialogService dialogService = spy(DialogService.class);
private final StateManager stateManager = mock(StateManager.class);
private final GuiPreferences preferences = mock(GuiPreferences.class);
private final ImportHandler importHandler = mock(ImportHandler.class);

private BibEntry entry;
private BibEntry entryWithCrossRef;
private BibEntry referencedEntry;
private List<BibEntry> selectedEntries;

private CopyTo copyTo;
private BibDatabaseContext sourceDatabaseContext;
private BibDatabaseContext targetDatabaseContext;

@BeforeEach
void setUp() {
this.entry = new BibEntry(StandardEntryType.Article)
.withField(StandardField.AUTHOR, "Beck, Aaron T. and Epstein, Norman and Brown, Gary and Steer, Robert A.")
.withField(StandardField.TITLE, "An inventory for measuring clinical anxiety: Psychometric properties.")
.withField(StandardField.YEAR, "1988")
.withField(StandardField.DOI, "10.1037/0022-006x.56.6.893")
.withCitationKey("Beck1988");

this.entryWithCrossRef = new BibEntry(StandardEntryType.InProceedings)
.withField(StandardField.AUTHOR, "Johnson, Emily and Lee, Michael")
.withField(StandardField.TITLE, "Advances in Neural Network Architectures")
.withField(StandardField.CROSSREF, "InternationalConferenceonMachineLearning2023")
.withCitationKey("Johnson2023");

this.referencedEntry = new BibEntry(StandardEntryType.InProceedings)
.withField(StandardField.AUTHOR, "Proceedings of the 40th International Conference on Machine Learning")
.withField(StandardField.TITLE, "Doe, John and Smith, Alice")
.withField(StandardField.BOOKTITLE, "Springer")
.withCitationKey("InternationalConferenceonMachineLearning2023");

this.selectedEntries = stateManager.getSelectedEntries();
this.sourceDatabaseContext = new BibDatabaseContext(new BibDatabase(List.of(entry, entryWithCrossRef, referencedEntry)));
this.targetDatabaseContext = new BibDatabaseContext();
}

@Test
void executeCopyEntriesWithoutCrossRef() {
selectedEntries.add(entry);
when(stateManager.getSelectedEntries()).thenReturn((ObservableList<BibEntry>) selectedEntries);

copyTo = new CopyTo(dialogService, stateManager, preferences.getCopyToPreferences(), importHandler, sourceDatabaseContext, targetDatabaseContext);
copyTo.copyEntriesWithoutCrossRef(selectedEntries, targetDatabaseContext);

verify(importHandler).importEntriesWithDuplicateCheck(targetDatabaseContext, selectedEntries);
}

@Test
void executeCopyEntriesWithCrossRef() {
selectedEntries.add(entryWithCrossRef);
when(stateManager.getSelectedEntries()).thenReturn((ObservableList<BibEntry>) selectedEntries);

copyTo = new CopyTo(dialogService, stateManager, preferences.getCopyToPreferences(), importHandler, sourceDatabaseContext, targetDatabaseContext);
copyTo.copyEntriesWithCrossRef(selectedEntries, targetDatabaseContext);

List<BibEntry> expectedEntries = new ArrayList<>(selectedEntries);
expectedEntries.add(referencedEntry);

verify(importHandler).importEntriesWithDuplicateCheck(targetDatabaseContext, expectedEntries);
}

@Test
void executeGetCrossRefEntry() {
copyTo = new CopyTo(dialogService, stateManager, preferences.getCopyToPreferences(), importHandler, sourceDatabaseContext, targetDatabaseContext);

BibEntry result = copyTo.getCrossRefEntry(entryWithCrossRef, sourceDatabaseContext);

assertNotNull(result);
assertEquals(referencedEntry, result);
}

@Test
void executeExecuteWithoutCrossRef() {
selectedEntries.add(entry);
when(stateManager.getSelectedEntries()).thenReturn((ObservableList<BibEntry>) selectedEntries);
when(preferences.getCopyToPreferences().getShouldAskForIncludingCrossReferences()).thenReturn(false);

copyTo = new CopyTo(dialogService, stateManager, preferences.getCopyToPreferences(), importHandler, sourceDatabaseContext, targetDatabaseContext);
copyTo.execute();

verify(importHandler).importEntriesWithDuplicateCheck(targetDatabaseContext, selectedEntries);
}

@Test
void executeExecuteWithCrossRefAndUserIncludes() {
selectedEntries.add(entryWithCrossRef);
when(stateManager.getSelectedEntries()).thenReturn((ObservableList<BibEntry>) selectedEntries);
when(preferences.getCopyToPreferences().getShouldAskForIncludingCrossReferences()).thenReturn(true);
when(dialogService.showConfirmationDialogWithOptOutAndWait(anyString(), anyString(), anyString(), anyString(), anyString(), any())).thenReturn(true);

copyTo = new CopyTo(dialogService, stateManager, preferences.getCopyToPreferences(), importHandler, sourceDatabaseContext, targetDatabaseContext);
copyTo.execute();

List<BibEntry> expectedEntries = new ArrayList<>(selectedEntries);
expectedEntries.add(referencedEntry);

verify(importHandler).importEntriesWithDuplicateCheck(targetDatabaseContext, expectedEntries);
}

@Test
void executeWithCrossRefAndUserExcludes() {
selectedEntries.add(entryWithCrossRef);
when(stateManager.getSelectedEntries()).thenReturn((ObservableList<BibEntry>) selectedEntries);
when(preferences.getCopyToPreferences().getShouldAskForIncludingCrossReferences()).thenReturn(true);
when(dialogService.showConfirmationDialogWithOptOutAndWait(anyString(), anyString(), anyString(), anyString(), anyString(), any())).thenReturn(false);

copyTo = new CopyTo(dialogService, stateManager, preferences.getCopyToPreferences(), importHandler, sourceDatabaseContext, targetDatabaseContext);
copyTo.execute();

verify(importHandler).importEntriesWithDuplicateCheck(targetDatabaseContext, selectedEntries);
}
}

0 comments on commit 55f40ee

Please sign in to comment.