Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntryTypesManager;

import jakarta.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -31,6 +32,7 @@ public class ConsistencyCheckAction extends SimpleCommand {
private final GuiPreferences preferences;
private final BibEntryTypesManager entryTypesManager;
private final UiTaskExecutor taskExecutor;
@Inject private BibEntryTypesManager bibEntryTypesManager;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't inject - we pass by constructor

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


public ConsistencyCheckAction(Supplier<LibraryTab> tabSupplier,
DialogService dialogService,
Expand Down Expand Up @@ -63,7 +65,7 @@ public BibliographyConsistencyCheck.Result call() {
BibDatabaseContext bibContext = databaseContext.get();

BibliographyConsistencyCheck consistencyCheck = new BibliographyConsistencyCheck();
return consistencyCheck.check(bibContext, (count, total) ->
return consistencyCheck.check(bibContext, bibEntryTypesManager, (count, total) ->
UiTaskExecutor.runInJavaFXThread(() -> {
updateProgress(count, total);
updateMessage(Localization.lang("%0/%1 entry types", count + 1, total));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.jabref.logic.quality.consistency.BibliographyConsistencyCheckResultTxtWriter;
import org.jabref.logic.quality.consistency.BibliographyConsistencyCheckResultWriter;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.toolkit.cli.converter.CygWinPathConverter;

import org.slf4j.Logger;
Expand Down Expand Up @@ -65,7 +66,7 @@ public Integer call() {
BibDatabaseContext databaseContext = parserResult.get().getDatabaseContext();

BibliographyConsistencyCheck consistencyCheck = new BibliographyConsistencyCheck();
BibliographyConsistencyCheck.Result result = consistencyCheck.check(databaseContext, (count, total) -> {
BibliographyConsistencyCheck.Result result = consistencyCheck.check(databaseContext, new BibEntryTypesManager(), (count, total) -> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor this as variable to follow the style of Jabef

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

view update:
https://github.com/JabRef/jabref/pull/14257/files#diff-d3050a72d09ad6b3c585cb60f5d8e92bb6b9d7539ff23103b86c8c01391e247cR69-R70

Also just curious: should I be constructing a new BibEntryTypesManager here like I've done? This basically prevents the user from using custom types when using the cli consistency check. How might we let users specify a entry types manager from the cli? Or maybe I'm missing something and this isn't what we want to do.

if (!sharedOptions.porcelain) {
System.out.println(Localization.lang("Checking consistency for entry type %0 of %1", count + 1, total));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibEntryType;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.InternalField;
import org.jabref.model.entry.field.SpecialField;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.field.UserSpecificCommentField;
import org.jabref.model.entry.types.BiblatexEntryTypeDefinitions;
import org.jabref.model.entry.types.BibtexEntryTypeDefinitions;
import org.jabref.model.entry.types.EntryType;

import com.google.common.annotations.VisibleForTesting;
Expand Down Expand Up @@ -102,22 +101,17 @@ public record EntryTypeResult(Collection<Field> fields, SequencedCollection<BibE
*
* @implNote This class does not implement {@link org.jabref.logic.integrity.DatabaseChecker}, because it returns a list of {@link org.jabref.logic.integrity.IntegrityMessage}, which are too fine-grained.
*/
public Result check(BibDatabaseContext bibContext, BiConsumer<Integer, Integer> entriesGroupingProgress) {
public Result check(BibDatabaseContext bibContext, BibEntryTypesManager bibEntryTypesManager, BiConsumer<Integer, Integer> entriesGroupingProgress) {
// collects fields existing in any entry, scoped by entry type
Map<EntryType, Set<Field>> entryTypeToFieldsInAnyEntryMap = new HashMap<>();
// collects fields existing in all entries, scoped by entry type
// collects fields existing in all entries, scoped by entry typed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo - please undo

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Map<EntryType, Set<Field>> entryTypeToFieldsInAllEntriesMap = new HashMap<>();
// collects entries of the same type
Map<EntryType, Set<BibEntry>> entryTypeToEntriesMap = new HashMap<>();

collectEntriesIntoMaps(bibContext, entryTypeToFieldsInAnyEntryMap, entryTypeToFieldsInAllEntriesMap, entryTypeToEntriesMap);

List<BibEntryType> entryTypeDefinitions;
if (bibContext.getMode() == BibDatabaseMode.BIBLATEX) {
entryTypeDefinitions = BiblatexEntryTypeDefinitions.ALL;
} else {
entryTypeDefinitions = BibtexEntryTypeDefinitions.ALL;
}
List<BibEntryType> entryTypeDefinitions = bibEntryTypesManager.getAllTypes(bibContext.getMode()).stream().toList();

// Use LinkedHashMap to preserve the order of Bib(tex|latex)EntryTypeDefinitions.ALL
Map<EntryType, EntryTypeResult> resultMap = new LinkedHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.field.UnknownField;
import org.jabref.model.entry.types.StandardEntryType;
Expand Down Expand Up @@ -42,7 +43,7 @@ void checkSimpleLibrary(@TempDir Path tempDir) throws IOException {
database.insertEntry(second);

BibDatabaseContext bibContext = new BibDatabaseContext(database);
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (count, total) -> {
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, new BibEntryTypesManager(), (count, total) -> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a variable in the test - and reuse it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update:
https://github.com/JabRef/jabref/pull/14257/files#diff-641c831afa366f64368ec5c237c128d3fd3506f92ecc2c4d4a8125e23219849dR39-R62

I also made the mocked BibEntryTypesManager for this test file have some custom and unknown types (similar to what I did for BibliographyConsistencyCheckTest.java). But for this file, I don't use the custom and unknown types. Should I add some unit tests that use these, or remove these types for the mocked BibEntryTypesManager of the CSV test file?

});

Path csvFile = tempDir.resolve("checkSimpleLibrary-result.csv");
Expand Down Expand Up @@ -73,7 +74,7 @@ void checkDifferentOutputSymbols(@TempDir Path tempDir) throws IOException {

BibDatabaseContext bibContext = new BibDatabaseContext(database);
bibContext.setMode(BibDatabaseMode.BIBTEX);
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (count, total) -> {
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, new BibEntryTypesManager(), (count, total) -> {
});

Path csvFile = tempDir.resolve("checkDifferentOutputSymbols-result.csv");
Expand Down Expand Up @@ -119,7 +120,7 @@ void checkComplexLibrary(@TempDir Path tempDir) throws IOException {
database.insertEntry(fifth);

BibDatabaseContext bibContext = new BibDatabaseContext(database);
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (count, total) -> {
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, new BibEntryTypesManager(), (count, total) -> {
});

Path csvFile = tempDir.resolve("checkSimpleLibrary-result.csv");
Expand Down Expand Up @@ -151,7 +152,7 @@ void checkLibraryWithoutIssues(@TempDir Path tempDir) throws IOException {

BibDatabaseContext bibContext = new BibDatabaseContext(database);
bibContext.setMode(BibDatabaseMode.BIBTEX);
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (count, total) -> {
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, new BibEntryTypesManager(), (count, total) -> {
});

Path csvFile = tempDir.resolve("checkLibraryWithoutIssues-result.csv");
Expand All @@ -170,7 +171,7 @@ void checkManualInput() throws IOException {
Path file = Path.of("C:\\TEMP\\JabRef\\biblio-anon.bib");
Path csvFile = file.resolveSibling("biblio-cited.csv");
BibDatabaseContext databaseContext = importer.importDatabase(file).getDatabaseContext();
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(databaseContext, (_, _) -> {
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(databaseContext, new BibEntryTypesManager(), (_, _) -> {
});
try (Writer writer = new OutputStreamWriter(Files.newOutputStream(csvFile));
BibliographyConsistencyCheckResultCsvWriter paperConsistencyCheckResultCsvWriter = new BibliographyConsistencyCheckResultCsvWriter(result, writer, true)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void checkSimpleLibrary(@TempDir Path tempDir) throws IOException {

BibDatabaseContext bibContext = new BibDatabaseContext(database);
bibContext.setMode(BibDatabaseMode.BIBTEX);
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (count, total) -> {
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, new BibEntryTypesManager(), (count, total) -> {
});

Path txtFile = tempDir.resolve("checkSimpleLibrary-result.txt");
Expand Down Expand Up @@ -89,7 +89,7 @@ void entriesMissingRequiredFieldsAreReported(@TempDir Path tempDir) throws Excep
bibContext.setMode(BibDatabaseMode.BIBLATEX);

BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck()
.check(bibContext, (_, _) -> {
.check(bibContext, new BibEntryTypesManager(), (_, _) -> {
});

Path txtFile = tempDir.resolve("checkSimpleLibrary-result.txt");
Expand Down Expand Up @@ -131,7 +131,7 @@ void checkDifferentOutputSymbols(@TempDir Path tempDir) throws IOException {
BibDatabaseContext bibContext = new BibDatabaseContext(bibDatabase);
bibContext.setMode(BibDatabaseMode.BIBTEX);

BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (_, _) -> {
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, new BibEntryTypesManager(), (_, _) -> {
});

Path txtFile = tempDir.resolve("checkDifferentOutputSymbols-result.txt");
Expand Down Expand Up @@ -171,7 +171,7 @@ void checkVeryLongCitationKey(@TempDir Path tempDir) throws IOException {
bibDatabase.insertEntries(bibEntriesList);
BibDatabaseContext bibContext = new BibDatabaseContext(bibDatabase);
bibContext.setMode(BibDatabaseMode.BIBTEX);
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (_, _) -> {
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, new BibEntryTypesManager(), (_, _) -> {
});

Path txtFile = tempDir.resolve("checkDifferentOutputSymbols-result.txt");
Expand Down Expand Up @@ -228,7 +228,7 @@ void checkComplexLibrary(@TempDir Path tempDir) throws IOException {
bibDatabase.insertEntries(bibEntriesList);
BibDatabaseContext bibContext = new BibDatabaseContext(bibDatabase);

BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (_, _) -> {
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, new BibEntryTypesManager(), (_, _) -> {
});

Path txtFile = tempDir.resolve("checkSimpleLibrary-result.txt");
Expand Down Expand Up @@ -270,7 +270,7 @@ void checkLibraryWithoutIssuesWithOutPorcelain(@TempDir Path tempDir) throws IOE
bibDatabase.insertEntries(bibEntriesList);
BibDatabaseContext bibContext = new BibDatabaseContext(bibDatabase);

BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (_, _) -> {
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, new BibEntryTypesManager(), (_, _) -> {
});

Path txtFile = tempDir.resolve("checkLibraryWithoutIssues-result.txt");
Expand Down Expand Up @@ -298,7 +298,7 @@ void checkLibraryWithoutIssuesWithPorcelain(@TempDir Path tempDir) throws IOExce
bibDatabase.insertEntries(bibEntriesList);
BibDatabaseContext bibContext = new BibDatabaseContext(bibDatabase);

BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (_, _) -> {
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, new BibEntryTypesManager(), (_, _) -> {
});

Path txtFile = tempDir.resolve("checkLibraryWithoutIssues-result.txt");
Expand All @@ -315,7 +315,7 @@ void checkManualInput() throws IOException {
Path file = Path.of("C:\\TEMP\\JabRef\\biblio-anon.bib");
Path txtFile = file.resolveSibling("biblio-cited.txt");
BibDatabaseContext databaseContext = importer.importDatabase(file).getDatabaseContext();
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(databaseContext, (_, _) -> {
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(databaseContext, new BibEntryTypesManager(), (_, _) -> {
});
try (Writer writer = new OutputStreamWriter(Files.newOutputStream(txtFile));
BibliographyConsistencyCheckResultTxtWriter txtWriter = new BibliographyConsistencyCheckResultTxtWriter(result, writer, true)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,30 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.SpecialField;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.field.UnknownField;
import org.jabref.model.entry.field.UserSpecificCommentField;
import org.jabref.model.entry.types.StandardEntryType;

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

import static org.junit.jupiter.api.Assertions.assertEquals;

class BibliographyConsistencyCheckTest {

private BibEntryTypesManager entryTypesManager;

@BeforeEach
void setUp() {
// TODO: add some custom entry types for this manager and test with it
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please :) - The issue description at #13794 should provide you with one.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

entryTypesManager = new BibEntryTypesManager();
}

@Test
void checkSimpleLibrary(@TempDir Path tempDir) {
BibEntry first = new BibEntry(StandardEntryType.Article, "first")
Expand All @@ -34,7 +44,7 @@ void checkSimpleLibrary(@TempDir Path tempDir) {
BibDatabase database = new BibDatabase(List.of(first, second));
BibDatabaseContext bibContext = new BibDatabaseContext(database);
bibContext.setMode(BibDatabaseMode.BIBTEX);
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (count, total) -> {
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, entryTypesManager, (count, total) -> {
});

BibliographyConsistencyCheck.EntryTypeResult entryTypeResult = new BibliographyConsistencyCheck.EntryTypeResult(Set.of(StandardField.PAGES, StandardField.PUBLISHER), List.of(first, second));
Expand All @@ -55,7 +65,7 @@ void checkDifferentOutputSymbols(@TempDir Path tempDir) {
BibDatabase bibDatabase = new BibDatabase(List.of(first, second));
BibDatabaseContext bibContext = new BibDatabaseContext(bibDatabase);
bibContext.setMode(BibDatabaseMode.BIBTEX);
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (_, _) -> {
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, entryTypesManager, (_, _) -> {
});

BibliographyConsistencyCheck.EntryTypeResult entryTypeResult = new BibliographyConsistencyCheck.EntryTypeResult(Set.of(StandardField.PAGES, StandardField.TITLE, customField), List.of(first, second));
Expand Down Expand Up @@ -88,7 +98,7 @@ void checkComplexLibrary(@TempDir Path tempDir) {
BibDatabase bibDatabase = new BibDatabase(List.of(first, second, third, fourth, fifth));
BibDatabaseContext bibContext = new BibDatabaseContext(bibDatabase);

BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (_, _) -> {
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, entryTypesManager, (_, _) -> {
});

BibliographyConsistencyCheck.EntryTypeResult articleResult = new BibliographyConsistencyCheck.EntryTypeResult(Set.of(StandardField.PAGES, StandardField.PUBLISHER), List.of(first, second));
Expand All @@ -111,7 +121,7 @@ void checkLibraryWithoutIssues(@TempDir Path tempDir) {
BibDatabase bibDatabase = new BibDatabase(List.of(first, second));
BibDatabaseContext bibContext = new BibDatabaseContext(bibDatabase);

BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (_, _) -> {
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, entryTypesManager, (_, _) -> {
});

BibliographyConsistencyCheck.Result expected = new BibliographyConsistencyCheck.Result(Map.of());
Expand All @@ -133,7 +143,7 @@ void filteredFieldsAreIgnored() {
BibDatabaseContext bibContext = new BibDatabaseContext(bibDatabase);

BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck()
.check(bibContext, (_, _) -> {
.check(bibContext, entryTypesManager, (_, _) -> {
});

assertEquals(Map.of(), result.entryTypeToResultMap(),
Expand All @@ -150,7 +160,7 @@ void nonFilteredFieldDifferenceIsReported() {
BibDatabaseContext bibContext = new BibDatabaseContext(bibDatabase);

BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck()
.check(bibContext, (_, _) -> {
.check(bibContext, entryTypesManager, (_, _) -> {
});

BibliographyConsistencyCheck.EntryTypeResult typeResult =
Expand All @@ -174,7 +184,7 @@ void unsetRequriedFieldsReported() {
bibContext.setMode(BibDatabaseMode.BIBLATEX);

BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck()
.check(bibContext, (_, _) -> {
.check(bibContext, entryTypesManager, (_, _) -> {
});

BibliographyConsistencyCheck.EntryTypeResult typeResult =
Expand All @@ -198,7 +208,7 @@ void unsetFieldsReportedInBibtexMode() {
BibDatabaseContext bibContext = new BibDatabaseContext(bibDatabase);
bibContext.setMode(BibDatabaseMode.BIBTEX);
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck()
.check(bibContext, (_, _) -> {
.check(bibContext, entryTypesManager, (_, _) -> {
});
BibliographyConsistencyCheck.EntryTypeResult typeResult =
result.entryTypeToResultMap().get(StandardEntryType.Online);
Expand Down Expand Up @@ -268,7 +278,7 @@ void checkComplexLibraryWithAdditionalEntry(@TempDir Path tempDir) {
BibDatabase bibDatabase = new BibDatabase(List.of(first, second, third, fourth, fifth, sixth));
BibDatabaseContext bibContext = new BibDatabaseContext(bibDatabase);

BibliographyConsistencyCheck.Result actualResult = new BibliographyConsistencyCheck().check(bibContext, (_, _) -> {
BibliographyConsistencyCheck.Result actualResult = new BibliographyConsistencyCheck().check(bibContext, entryTypesManager, (_, _) -> {
});

BibliographyConsistencyCheck.EntryTypeResult articleResult = new BibliographyConsistencyCheck.EntryTypeResult(Set.of(StandardField.PAGES, StandardField.PUBLISHER), List.of(first, second));
Expand Down
Loading