From ba9446d7104c21662a2da71273a68a4d2436e1a0 Mon Sep 17 00:00:00 2001 From: Hrishi-Baskaran <144738300+Hrishi-Baskaran@users.noreply.github.com> Date: Fri, 14 Nov 2025 18:32:38 -0700 Subject: [PATCH 1/2] Modifies BibliographyConsistencyCheck.check to Allow Custom Entry Types Also modifies BibliographyConsistencyCheckTest to mock BibEntryTypes manager. --- .../consistency/ConsistencyCheckAction.java | 4 ++- .../java/org/jabref/cli/CheckConsistency.java | 3 +- .../BibliographyConsistencyCheck.java | 14 +++------- ...hyConsistencyCheckResultCsvWriterTest.java | 11 ++++---- ...hyConsistencyCheckResultTxtWriterTest.java | 16 +++++------ .../BibliographyConsistencyCheckTest.java | 28 +++++++++++++------ .../util/LspConsistencyCheck.java | 4 +-- .../util/LspDiagnosticHandler.java | 3 +- 8 files changed, 46 insertions(+), 37 deletions(-) diff --git a/jabgui/src/main/java/org/jabref/gui/consistency/ConsistencyCheckAction.java b/jabgui/src/main/java/org/jabref/gui/consistency/ConsistencyCheckAction.java index 3cb1d9adaab..eaa4d80adf1 100644 --- a/jabgui/src/main/java/org/jabref/gui/consistency/ConsistencyCheckAction.java +++ b/jabgui/src/main/java/org/jabref/gui/consistency/ConsistencyCheckAction.java @@ -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; @@ -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; public ConsistencyCheckAction(Supplier tabSupplier, DialogService dialogService, @@ -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)); diff --git a/jabkit/src/main/java/org/jabref/cli/CheckConsistency.java b/jabkit/src/main/java/org/jabref/cli/CheckConsistency.java index 9af8ecce73f..7407c1c018a 100644 --- a/jabkit/src/main/java/org/jabref/cli/CheckConsistency.java +++ b/jabkit/src/main/java/org/jabref/cli/CheckConsistency.java @@ -15,6 +15,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.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -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) -> { if (!sharedOptions.porcelain) { System.out.println(Localization.lang("Checking consistency for entry type %0 of %1", count + 1, total)); } diff --git a/jablib/src/main/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheck.java b/jablib/src/main/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheck.java index 64915003c35..bed79c6856d 100644 --- a/jablib/src/main/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheck.java +++ b/jablib/src/main/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheck.java @@ -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; @@ -102,22 +101,17 @@ public record EntryTypeResult(Collection fields, SequencedCollection entriesGroupingProgress) { + public Result check(BibDatabaseContext bibContext, BibEntryTypesManager bibEntryTypesManager, BiConsumer entriesGroupingProgress) { // collects fields existing in any entry, scoped by entry type Map> entryTypeToFieldsInAnyEntryMap = new HashMap<>(); - // collects fields existing in all entries, scoped by entry type + // collects fields existing in all entries, scoped by entry typed Map> entryTypeToFieldsInAllEntriesMap = new HashMap<>(); // collects entries of the same type Map> entryTypeToEntriesMap = new HashMap<>(); collectEntriesIntoMaps(bibContext, entryTypeToFieldsInAnyEntryMap, entryTypeToFieldsInAllEntriesMap, entryTypeToEntriesMap); - List entryTypeDefinitions; - if (bibContext.getMode() == BibDatabaseMode.BIBLATEX) { - entryTypeDefinitions = BiblatexEntryTypeDefinitions.ALL; - } else { - entryTypeDefinitions = BibtexEntryTypeDefinitions.ALL; - } + List entryTypeDefinitions = bibEntryTypesManager.getAllTypes(bibContext.getMode()).stream().toList(); // Use LinkedHashMap to preserve the order of Bib(tex|latex)EntryTypeDefinitions.ALL Map resultMap = new LinkedHashMap<>(); diff --git a/jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckResultCsvWriterTest.java b/jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckResultCsvWriterTest.java index 6bcb7ba90a6..6f0ff16dd62 100644 --- a/jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckResultCsvWriterTest.java +++ b/jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckResultCsvWriterTest.java @@ -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; @@ -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) -> { }); Path csvFile = tempDir.resolve("checkSimpleLibrary-result.csv"); @@ -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"); @@ -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"); @@ -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"); @@ -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)) { diff --git a/jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckResultTxtWriterTest.java b/jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckResultTxtWriterTest.java index 176ee0fc7c3..7c0e03786dd 100644 --- a/jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckResultTxtWriterTest.java +++ b/jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckResultTxtWriterTest.java @@ -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"); @@ -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"); @@ -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"); @@ -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"); @@ -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"); @@ -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"); @@ -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"); @@ -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)) { diff --git a/jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckTest.java b/jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckTest.java index 12987b2cf40..208409ce1b8 100644 --- a/jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckTest.java +++ b/jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckTest.java @@ -9,6 +9,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.Field; import org.jabref.model.entry.field.SpecialField; import org.jabref.model.entry.field.StandardField; @@ -16,6 +17,7 @@ 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; @@ -23,6 +25,14 @@ class BibliographyConsistencyCheckTest { + private BibEntryTypesManager entryTypesManager; + + @BeforeEach + void setUp() { + // TODO: add some custom entry types for this manager and test with it + entryTypesManager = new BibEntryTypesManager(); + } + @Test void checkSimpleLibrary(@TempDir Path tempDir) { BibEntry first = new BibEntry(StandardEntryType.Article, "first") @@ -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)); @@ -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)); @@ -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)); @@ -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()); @@ -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(), @@ -150,7 +160,7 @@ void nonFilteredFieldDifferenceIsReported() { BibDatabaseContext bibContext = new BibDatabaseContext(bibDatabase); BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck() - .check(bibContext, (_, _) -> { + .check(bibContext, entryTypesManager, (_, _) -> { }); BibliographyConsistencyCheck.EntryTypeResult typeResult = @@ -174,7 +184,7 @@ void unsetRequriedFieldsReported() { bibContext.setMode(BibDatabaseMode.BIBLATEX); BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck() - .check(bibContext, (_, _) -> { + .check(bibContext, entryTypesManager, (_, _) -> { }); BibliographyConsistencyCheck.EntryTypeResult typeResult = @@ -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); @@ -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)); diff --git a/jabls/src/main/java/org/jabref/languageserver/util/LspConsistencyCheck.java b/jabls/src/main/java/org/jabref/languageserver/util/LspConsistencyCheck.java index 0e4635e70ff..1bdbed27e25 100644 --- a/jabls/src/main/java/org/jabref/languageserver/util/LspConsistencyCheck.java +++ b/jabls/src/main/java/org/jabref/languageserver/util/LspConsistencyCheck.java @@ -28,10 +28,10 @@ public LspConsistencyCheck(ExtensionSettings settings) { this.settings = settings; } - public List check(ParserResult parserResult) { + public List check(ParserResult parserResult, BibEntryTypesManager bibEntryTypesManager) { List diagnostics = new ArrayList<>(); BibliographyConsistencyCheck consistencyCheck = new BibliographyConsistencyCheck(); - BibliographyConsistencyCheck.Result result = consistencyCheck.check(parserResult.getDatabaseContext(), (_, _) -> { + BibliographyConsistencyCheck.Result result = consistencyCheck.check(parserResult.getDatabaseContext(), bibEntryTypesManager, (_, _) -> { }); Set allReportedFields = result.entryTypeToResultMap().values().stream().flatMap(entryTypeResult -> entryTypeResult.fields().stream()).collect(Collectors.toUnmodifiableSet()); diff --git a/jabls/src/main/java/org/jabref/languageserver/util/LspDiagnosticHandler.java b/jabls/src/main/java/org/jabref/languageserver/util/LspDiagnosticHandler.java index fe08f777144..5603c322625 100644 --- a/jabls/src/main/java/org/jabref/languageserver/util/LspDiagnosticHandler.java +++ b/jabls/src/main/java/org/jabref/languageserver/util/LspDiagnosticHandler.java @@ -16,6 +16,7 @@ import org.jabref.logic.journals.JournalAbbreviationRepository; import org.jabref.logic.l10n.Localization; import org.jabref.logic.preferences.CliPreferences; +import org.jabref.model.entry.BibEntryTypesManager; import org.eclipse.lsp4j.Diagnostic; import org.eclipse.lsp4j.DiagnosticSeverity; @@ -88,7 +89,7 @@ private List computeDiagnostics(String content, String uri) { } if (clientHandler.getSettings().isConsistencyCheck()) { - consistencyDiagnosticsCache.put(uri, lspConsistencyCheck.check(parserResult)); + consistencyDiagnosticsCache.put(uri, lspConsistencyCheck.check(parserResult, new BibEntryTypesManager())); LOGGER.debug("Cached consistency diagnostics for {}", uri); } From 794123888805563e1612eb2e317ccf41272ebb99 Mon Sep 17 00:00:00 2001 From: Hrishi-Baskaran <144738300+Hrishi-Baskaran@users.noreply.github.com> Date: Sat, 22 Nov 2025 16:21:50 -0700 Subject: [PATCH 2/2] Adds Tests with Custom Types for Consistency Check Also changes style and refactors some code to adhere to JabRef standards. --- .../consistency/ConsistencyCheckAction.java | 5 +- .../jabref/toolkit/cli/CheckConsistency.java | 3 +- .../BibliographyConsistencyCheck.java | 2 +- ...hyConsistencyCheckResultCsvWriterTest.java | 40 ++++++- .../BibliographyConsistencyCheckTest.java | 102 +++++++++++++++++- 5 files changed, 140 insertions(+), 12 deletions(-) diff --git a/jabgui/src/main/java/org/jabref/gui/consistency/ConsistencyCheckAction.java b/jabgui/src/main/java/org/jabref/gui/consistency/ConsistencyCheckAction.java index eaa4d80adf1..f2d34b1b155 100644 --- a/jabgui/src/main/java/org/jabref/gui/consistency/ConsistencyCheckAction.java +++ b/jabgui/src/main/java/org/jabref/gui/consistency/ConsistencyCheckAction.java @@ -17,7 +17,6 @@ import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntryTypesManager; -import jakarta.inject.Inject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,7 +31,6 @@ public class ConsistencyCheckAction extends SimpleCommand { private final GuiPreferences preferences; private final BibEntryTypesManager entryTypesManager; private final UiTaskExecutor taskExecutor; - @Inject private BibEntryTypesManager bibEntryTypesManager; public ConsistencyCheckAction(Supplier tabSupplier, DialogService dialogService, @@ -46,7 +44,6 @@ public ConsistencyCheckAction(Supplier tabSupplier, this.preferences = preferences; this.entryTypesManager = entryTypesManager; this.taskExecutor = taskExecutor; - this.executable.bind(needsDatabase(stateManager)); } @@ -65,7 +62,7 @@ public BibliographyConsistencyCheck.Result call() { BibDatabaseContext bibContext = databaseContext.get(); BibliographyConsistencyCheck consistencyCheck = new BibliographyConsistencyCheck(); - return consistencyCheck.check(bibContext, bibEntryTypesManager, (count, total) -> + return consistencyCheck.check(bibContext, entryTypesManager, (count, total) -> UiTaskExecutor.runInJavaFXThread(() -> { updateProgress(count, total); updateMessage(Localization.lang("%0/%1 entry types", count + 1, total)); diff --git a/jabkit/src/main/java/org/jabref/toolkit/cli/CheckConsistency.java b/jabkit/src/main/java/org/jabref/toolkit/cli/CheckConsistency.java index f1e5996fa30..e92fe0e2f05 100644 --- a/jabkit/src/main/java/org/jabref/toolkit/cli/CheckConsistency.java +++ b/jabkit/src/main/java/org/jabref/toolkit/cli/CheckConsistency.java @@ -66,7 +66,8 @@ public Integer call() { BibDatabaseContext databaseContext = parserResult.get().getDatabaseContext(); BibliographyConsistencyCheck consistencyCheck = new BibliographyConsistencyCheck(); - BibliographyConsistencyCheck.Result result = consistencyCheck.check(databaseContext, new BibEntryTypesManager(), (count, total) -> { + BibEntryTypesManager bibEntryTypesManager = new BibEntryTypesManager(); + BibliographyConsistencyCheck.Result result = consistencyCheck.check(databaseContext, bibEntryTypesManager, (count, total) -> { if (!sharedOptions.porcelain) { System.out.println(Localization.lang("Checking consistency for entry type %0 of %1", count + 1, total)); } diff --git a/jablib/src/main/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheck.java b/jablib/src/main/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheck.java index bed79c6856d..c1f17954a31 100644 --- a/jablib/src/main/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheck.java +++ b/jablib/src/main/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheck.java @@ -104,7 +104,7 @@ public record EntryTypeResult(Collection fields, SequencedCollection entriesGroupingProgress) { // collects fields existing in any entry, scoped by entry type Map> entryTypeToFieldsInAnyEntryMap = new HashMap<>(); - // collects fields existing in all entries, scoped by entry typed + // collects fields existing in all entries, scoped by entry type Map> entryTypeToFieldsInAllEntriesMap = new HashMap<>(); // collects entries of the same type Map> entryTypeToEntriesMap = new HashMap<>(); diff --git a/jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckResultCsvWriterTest.java b/jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckResultCsvWriterTest.java index 6f0ff16dd62..0bb03627eee 100644 --- a/jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckResultCsvWriterTest.java +++ b/jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckResultCsvWriterTest.java @@ -5,6 +5,8 @@ import java.io.Writer; import java.nio.file.Files; import java.nio.file.Path; +import java.util.List; +import java.util.Set; import org.jabref.logic.importer.ImportFormatPreferences; import org.jabref.logic.importer.fileformat.BibtexImporter; @@ -12,12 +14,18 @@ import org.jabref.model.database.BibDatabaseContext; 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.BibField; +import org.jabref.model.entry.field.FieldPriority; import org.jabref.model.entry.field.StandardField; import org.jabref.model.entry.field.UnknownField; +import org.jabref.model.entry.types.EntryType; import org.jabref.model.entry.types.StandardEntryType; +import org.jabref.model.entry.types.UnknownEntryType; import org.jabref.model.util.DummyFileUpdateMonitor; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -28,8 +36,30 @@ class BibliographyConsistencyCheckResultCsvWriterTest { + private static final EntryType UNKNOWN_TYPE = new UnknownEntryType("unknownType"); + private static final EntryType CUSTOM_TYPE = new UnknownEntryType("customType"); + private final BibtexImporter importer = new BibtexImporter(mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS), new DummyFileUpdateMonitor()); + private BibEntryType newCustomType; + private BibEntryType overwrittenStandardType; + private BibEntryTypesManager entryTypesManager; + + @BeforeEach + void setUp() { + newCustomType = new BibEntryType( + CUSTOM_TYPE, + List.of(new BibField(StandardField.AUTHOR, FieldPriority.IMPORTANT)), + Set.of()); + + overwrittenStandardType = new BibEntryType( + StandardEntryType.Article, + List.of(new BibField(StandardField.TITLE, FieldPriority.IMPORTANT)), + Set.of()); + + entryTypesManager = new BibEntryTypesManager(); + } + @Test void checkSimpleLibrary(@TempDir Path tempDir) throws IOException { BibEntry first = new BibEntry(StandardEntryType.Article, "first") @@ -43,7 +73,7 @@ void checkSimpleLibrary(@TempDir Path tempDir) throws IOException { database.insertEntry(second); BibDatabaseContext bibContext = new BibDatabaseContext(database); - BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, new BibEntryTypesManager(), (count, total) -> { + BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, entryTypesManager, (count, total) -> { }); Path csvFile = tempDir.resolve("checkSimpleLibrary-result.csv"); @@ -74,7 +104,7 @@ void checkDifferentOutputSymbols(@TempDir Path tempDir) throws IOException { BibDatabaseContext bibContext = new BibDatabaseContext(database); bibContext.setMode(BibDatabaseMode.BIBTEX); - BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, new BibEntryTypesManager(), (count, total) -> { + BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, entryTypesManager, (count, total) -> { }); Path csvFile = tempDir.resolve("checkDifferentOutputSymbols-result.csv"); @@ -120,7 +150,7 @@ void checkComplexLibrary(@TempDir Path tempDir) throws IOException { database.insertEntry(fifth); BibDatabaseContext bibContext = new BibDatabaseContext(database); - BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, new BibEntryTypesManager(), (count, total) -> { + BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, entryTypesManager, (count, total) -> { }); Path csvFile = tempDir.resolve("checkSimpleLibrary-result.csv"); @@ -152,7 +182,7 @@ void checkLibraryWithoutIssues(@TempDir Path tempDir) throws IOException { BibDatabaseContext bibContext = new BibDatabaseContext(database); bibContext.setMode(BibDatabaseMode.BIBTEX); - BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, new BibEntryTypesManager(), (count, total) -> { + BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, entryTypesManager, (count, total) -> { }); Path csvFile = tempDir.resolve("checkLibraryWithoutIssues-result.csv"); @@ -171,7 +201,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, new BibEntryTypesManager(), (_, _) -> { + BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(databaseContext, entryTypesManager, (_, _) -> { }); try (Writer writer = new OutputStreamWriter(Files.newOutputStream(csvFile)); BibliographyConsistencyCheckResultCsvWriter paperConsistencyCheckResultCsvWriter = new BibliographyConsistencyCheckResultCsvWriter(result, writer, true)) { diff --git a/jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckTest.java b/jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckTest.java index 208409ce1b8..ecb8f043e96 100644 --- a/jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckTest.java +++ b/jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckTest.java @@ -9,13 +9,18 @@ import org.jabref.model.database.BibDatabaseContext; 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.BibField; import org.jabref.model.entry.field.Field; +import org.jabref.model.entry.field.FieldPriority; 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.EntryType; import org.jabref.model.entry.types.StandardEntryType; +import org.jabref.model.entry.types.UnknownEntryType; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -25,14 +30,109 @@ class BibliographyConsistencyCheckTest { + private static final EntryType UNKNOWN_TYPE = new UnknownEntryType("unknownType"); + private static final EntryType CUSTOM_TYPE = new UnknownEntryType("customType"); + + private BibEntryType newCustomType; + private BibEntryType overwrittenStandardType; private BibEntryTypesManager entryTypesManager; @BeforeEach void setUp() { - // TODO: add some custom entry types for this manager and test with it + newCustomType = new BibEntryType( + CUSTOM_TYPE, + List.of(new BibField(StandardField.AUTHOR, FieldPriority.IMPORTANT)), + Set.of()); + + overwrittenStandardType = new BibEntryType( + StandardEntryType.Article, + List.of(new BibField(StandardField.TITLE, FieldPriority.IMPORTANT)), + Set.of()); + entryTypesManager = new BibEntryTypesManager(); } + @Test + void checkComplexLibraryWithCustomEntryTypes(@TempDir Path tempDir) { + BibEntry first = new BibEntry(StandardEntryType.Article, "first") + .withField(StandardField.AUTHOR, "Author One") + .withField(StandardField.PAGES, "some pages"); + BibEntry second = new BibEntry(StandardEntryType.Article, "second") + .withField(StandardField.AUTHOR, "Author One") + .withField(StandardField.PUBLISHER, "publisher"); + BibEntry third = new BibEntry(StandardEntryType.InProceedings, "third") + .withField(StandardField.AUTHOR, "Author One") + .withField(StandardField.LOCATION, "location") + .withField(StandardField.YEAR, "2024") + .withField(StandardField.PAGES, "some pages"); + BibEntry fourth = new BibEntry(StandardEntryType.InProceedings, "fourth") + .withField(StandardField.AUTHOR, "Author One") + .withField(StandardField.YEAR, "2024") + .withField(StandardField.PUBLISHER, "publisher"); + BibEntry fifth = new BibEntry(StandardEntryType.InProceedings, "fifth") + .withField(StandardField.AUTHOR, "Author One") + .withField(StandardField.YEAR, "2024"); + BibEntry sixth = new BibEntry(newCustomType.getType(), "sixth") + .withField(StandardField.AUTHOR, "Author One") + .withField(StandardField.PAGES, "some pages") + .withField(StandardField.ABSTRACT, "some abstract"); + BibEntry seventh = new BibEntry(newCustomType.getType(), "seventh") + .withField(StandardField.AUTHOR, "Author One") + .withField(StandardField.PAGES, "some pages"); + BibEntry eighth = new BibEntry(newCustomType.getType(), "eighth") + .withField(StandardField.AUTHOR, "Author One") + .withField(StandardField.PAGES, "some pages") + .withField(StandardField.ABSTRACT, "some abstract") + .withField(StandardField.YEAR, "2025"); + BibEntry ninth = new BibEntry(newCustomType.getType(), "ninth") + .withField(StandardField.AUTHOR, "Author One") + .withField(StandardField.PAGES, "some pages") + .withField(StandardField.YEAR, "2025"); + + BibDatabase bibDatabase = new BibDatabase(List.of(first, second, third, fourth, fifth, sixth, seventh, eighth, ninth)); + BibDatabaseContext bibContext = new BibDatabaseContext(bibDatabase); + + BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, entryTypesManager, (_, _) -> { + }); + + BibliographyConsistencyCheck.EntryTypeResult articleResult = new BibliographyConsistencyCheck.EntryTypeResult(Set.of(StandardField.PAGES, StandardField.PUBLISHER), List.of(first, second)); + BibliographyConsistencyCheck.EntryTypeResult inProceedingsResult = new BibliographyConsistencyCheck.EntryTypeResult(Set.of(StandardField.PAGES, StandardField.PUBLISHER, StandardField.LOCATION), List.of(fifth, fourth, third)); + BibliographyConsistencyCheck.EntryTypeResult customResult = new BibliographyConsistencyCheck.EntryTypeResult(Set.of(StandardField.ABSTRACT, StandardField.YEAR), List.of(eighth, ninth, sixth)); + BibliographyConsistencyCheck.Result expected = new BibliographyConsistencyCheck.Result(Map.of( + StandardEntryType.Article, articleResult, + StandardEntryType.InProceedings, inProceedingsResult, + CUSTOM_TYPE, customResult + )); + assertEquals(expected, result); + } + + @Test + void checkSimpleLibraryWithCustomTypes() { + BibEntry first = new BibEntry(newCustomType.getType(), "first") + .withField(StandardField.AUTHOR, "Author One") + .withField(StandardField.PAGES, "some pages"); + BibEntry second = new BibEntry(newCustomType.getType(), "second") + .withField(StandardField.AUTHOR, "Author Two") + .withField(StandardField.PAGES, "some pages"); + BibEntry third = new BibEntry(newCustomType.getType(), "third") + .withField(StandardField.AUTHOR, "Author Three") + .withField(StandardField.PAGES, "some pages") + .withField(StandardField.ABSTRACT, "some abstract"); + + BibDatabase database = new BibDatabase(List.of(first, second, third)); + BibDatabaseContext bibContext = new BibDatabaseContext(database); + bibContext.setMode(BibDatabaseMode.BIBTEX); + + entryTypesManager.addCustomOrModifiedType(newCustomType, bibContext.getMode()); + + BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, entryTypesManager, (count, total) -> { + }); + + BibliographyConsistencyCheck.EntryTypeResult entryTypeResult = new BibliographyConsistencyCheck.EntryTypeResult(Set.of(StandardField.ABSTRACT), List.of(third)); + BibliographyConsistencyCheck.Result expected = new BibliographyConsistencyCheck.Result(Map.of(CUSTOM_TYPE, entryTypeResult)); + assertEquals(expected, result); + } + @Test void checkSimpleLibrary(@TempDir Path tempDir) { BibEntry first = new BibEntry(StandardEntryType.Article, "first")