Skip to content

Commit ba9446d

Browse files
Modifies BibliographyConsistencyCheck.check to Allow Custom Entry Types
Also modifies BibliographyConsistencyCheckTest to mock BibEntryTypes manager.
1 parent a985422 commit ba9446d

File tree

8 files changed

+46
-37
lines changed

8 files changed

+46
-37
lines changed

jabgui/src/main/java/org/jabref/gui/consistency/ConsistencyCheckAction.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.jabref.model.database.BibDatabaseContext;
1818
import org.jabref.model.entry.BibEntryTypesManager;
1919

20+
import jakarta.inject.Inject;
2021
import org.slf4j.Logger;
2122
import org.slf4j.LoggerFactory;
2223

@@ -31,6 +32,7 @@ public class ConsistencyCheckAction extends SimpleCommand {
3132
private final GuiPreferences preferences;
3233
private final BibEntryTypesManager entryTypesManager;
3334
private final UiTaskExecutor taskExecutor;
35+
@Inject private BibEntryTypesManager bibEntryTypesManager;
3436

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

6567
BibliographyConsistencyCheck consistencyCheck = new BibliographyConsistencyCheck();
66-
return consistencyCheck.check(bibContext, (count, total) ->
68+
return consistencyCheck.check(bibContext, bibEntryTypesManager, (count, total) ->
6769
UiTaskExecutor.runInJavaFXThread(() -> {
6870
updateProgress(count, total);
6971
updateMessage(Localization.lang("%0/%1 entry types", count + 1, total));

jabkit/src/main/java/org/jabref/cli/CheckConsistency.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.jabref.logic.quality.consistency.BibliographyConsistencyCheckResultTxtWriter;
1616
import org.jabref.logic.quality.consistency.BibliographyConsistencyCheckResultWriter;
1717
import org.jabref.model.database.BibDatabaseContext;
18+
import org.jabref.model.entry.BibEntryTypesManager;
1819

1920
import org.slf4j.Logger;
2021
import org.slf4j.LoggerFactory;
@@ -65,7 +66,7 @@ public Integer call() {
6566
BibDatabaseContext databaseContext = parserResult.get().getDatabaseContext();
6667

6768
BibliographyConsistencyCheck consistencyCheck = new BibliographyConsistencyCheck();
68-
BibliographyConsistencyCheck.Result result = consistencyCheck.check(databaseContext, (count, total) -> {
69+
BibliographyConsistencyCheck.Result result = consistencyCheck.check(databaseContext, new BibEntryTypesManager(), (count, total) -> {
6970
if (!sharedOptions.porcelain) {
7071
System.out.println(Localization.lang("Checking consistency for entry type %0 of %1", count + 1, total));
7172
}

jablib/src/main/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheck.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@
2020
import org.jabref.model.database.BibDatabaseMode;
2121
import org.jabref.model.entry.BibEntry;
2222
import org.jabref.model.entry.BibEntryType;
23+
import org.jabref.model.entry.BibEntryTypesManager;
2324
import org.jabref.model.entry.field.Field;
2425
import org.jabref.model.entry.field.InternalField;
2526
import org.jabref.model.entry.field.SpecialField;
2627
import org.jabref.model.entry.field.StandardField;
2728
import org.jabref.model.entry.field.UserSpecificCommentField;
28-
import org.jabref.model.entry.types.BiblatexEntryTypeDefinitions;
29-
import org.jabref.model.entry.types.BibtexEntryTypeDefinitions;
3029
import org.jabref.model.entry.types.EntryType;
3130

3231
import com.google.common.annotations.VisibleForTesting;
@@ -102,22 +101,17 @@ public record EntryTypeResult(Collection<Field> fields, SequencedCollection<BibE
102101
*
103102
* @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.
104103
*/
105-
public Result check(BibDatabaseContext bibContext, BiConsumer<Integer, Integer> entriesGroupingProgress) {
104+
public Result check(BibDatabaseContext bibContext, BibEntryTypesManager bibEntryTypesManager, BiConsumer<Integer, Integer> entriesGroupingProgress) {
106105
// collects fields existing in any entry, scoped by entry type
107106
Map<EntryType, Set<Field>> entryTypeToFieldsInAnyEntryMap = new HashMap<>();
108-
// collects fields existing in all entries, scoped by entry type
107+
// collects fields existing in all entries, scoped by entry typed
109108
Map<EntryType, Set<Field>> entryTypeToFieldsInAllEntriesMap = new HashMap<>();
110109
// collects entries of the same type
111110
Map<EntryType, Set<BibEntry>> entryTypeToEntriesMap = new HashMap<>();
112111

113112
collectEntriesIntoMaps(bibContext, entryTypeToFieldsInAnyEntryMap, entryTypeToFieldsInAllEntriesMap, entryTypeToEntriesMap);
114113

115-
List<BibEntryType> entryTypeDefinitions;
116-
if (bibContext.getMode() == BibDatabaseMode.BIBLATEX) {
117-
entryTypeDefinitions = BiblatexEntryTypeDefinitions.ALL;
118-
} else {
119-
entryTypeDefinitions = BibtexEntryTypeDefinitions.ALL;
120-
}
114+
List<BibEntryType> entryTypeDefinitions = bibEntryTypesManager.getAllTypes(bibContext.getMode()).stream().toList();
121115

122116
// Use LinkedHashMap to preserve the order of Bib(tex|latex)EntryTypeDefinitions.ALL
123117
Map<EntryType, EntryTypeResult> resultMap = new LinkedHashMap<>();

jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckResultCsvWriterTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.jabref.model.database.BibDatabaseContext;
1313
import org.jabref.model.database.BibDatabaseMode;
1414
import org.jabref.model.entry.BibEntry;
15+
import org.jabref.model.entry.BibEntryTypesManager;
1516
import org.jabref.model.entry.field.StandardField;
1617
import org.jabref.model.entry.field.UnknownField;
1718
import org.jabref.model.entry.types.StandardEntryType;
@@ -42,7 +43,7 @@ void checkSimpleLibrary(@TempDir Path tempDir) throws IOException {
4243
database.insertEntry(second);
4344

4445
BibDatabaseContext bibContext = new BibDatabaseContext(database);
45-
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (count, total) -> {
46+
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, new BibEntryTypesManager(), (count, total) -> {
4647
});
4748

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

7475
BibDatabaseContext bibContext = new BibDatabaseContext(database);
7576
bibContext.setMode(BibDatabaseMode.BIBTEX);
76-
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (count, total) -> {
77+
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, new BibEntryTypesManager(), (count, total) -> {
7778
});
7879

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

121122
BibDatabaseContext bibContext = new BibDatabaseContext(database);
122-
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (count, total) -> {
123+
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, new BibEntryTypesManager(), (count, total) -> {
123124
});
124125

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

152153
BibDatabaseContext bibContext = new BibDatabaseContext(database);
153154
bibContext.setMode(BibDatabaseMode.BIBTEX);
154-
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (count, total) -> {
155+
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, new BibEntryTypesManager(), (count, total) -> {
155156
});
156157

157158
Path csvFile = tempDir.resolve("checkLibraryWithoutIssues-result.csv");
@@ -170,7 +171,7 @@ void checkManualInput() throws IOException {
170171
Path file = Path.of("C:\\TEMP\\JabRef\\biblio-anon.bib");
171172
Path csvFile = file.resolveSibling("biblio-cited.csv");
172173
BibDatabaseContext databaseContext = importer.importDatabase(file).getDatabaseContext();
173-
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(databaseContext, (_, _) -> {
174+
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(databaseContext, new BibEntryTypesManager(), (_, _) -> {
174175
});
175176
try (Writer writer = new OutputStreamWriter(Files.newOutputStream(csvFile));
176177
BibliographyConsistencyCheckResultCsvWriter paperConsistencyCheckResultCsvWriter = new BibliographyConsistencyCheckResultCsvWriter(result, writer, true)) {

jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckResultTxtWriterTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void checkSimpleLibrary(@TempDir Path tempDir) throws IOException {
4444

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

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

9191
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck()
92-
.check(bibContext, (_, _) -> {
92+
.check(bibContext, new BibEntryTypesManager(), (_, _) -> {
9393
});
9494

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

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

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

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

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

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

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

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

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

304304
Path txtFile = tempDir.resolve("checkLibraryWithoutIssues-result.txt");
@@ -315,7 +315,7 @@ void checkManualInput() throws IOException {
315315
Path file = Path.of("C:\\TEMP\\JabRef\\biblio-anon.bib");
316316
Path txtFile = file.resolveSibling("biblio-cited.txt");
317317
BibDatabaseContext databaseContext = importer.importDatabase(file).getDatabaseContext();
318-
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(databaseContext, (_, _) -> {
318+
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(databaseContext, new BibEntryTypesManager(), (_, _) -> {
319319
});
320320
try (Writer writer = new OutputStreamWriter(Files.newOutputStream(txtFile));
321321
BibliographyConsistencyCheckResultTxtWriter txtWriter = new BibliographyConsistencyCheckResultTxtWriter(result, writer, true)) {

jablib/src/test/java/org/jabref/logic/quality/consistency/BibliographyConsistencyCheckTest.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,30 @@
99
import org.jabref.model.database.BibDatabaseContext;
1010
import org.jabref.model.database.BibDatabaseMode;
1111
import org.jabref.model.entry.BibEntry;
12+
import org.jabref.model.entry.BibEntryTypesManager;
1213
import org.jabref.model.entry.field.Field;
1314
import org.jabref.model.entry.field.SpecialField;
1415
import org.jabref.model.entry.field.StandardField;
1516
import org.jabref.model.entry.field.UnknownField;
1617
import org.jabref.model.entry.field.UserSpecificCommentField;
1718
import org.jabref.model.entry.types.StandardEntryType;
1819

20+
import org.junit.jupiter.api.BeforeEach;
1921
import org.junit.jupiter.api.Test;
2022
import org.junit.jupiter.api.io.TempDir;
2123

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

2426
class BibliographyConsistencyCheckTest {
2527

28+
private BibEntryTypesManager entryTypesManager;
29+
30+
@BeforeEach
31+
void setUp() {
32+
// TODO: add some custom entry types for this manager and test with it
33+
entryTypesManager = new BibEntryTypesManager();
34+
}
35+
2636
@Test
2737
void checkSimpleLibrary(@TempDir Path tempDir) {
2838
BibEntry first = new BibEntry(StandardEntryType.Article, "first")
@@ -34,7 +44,7 @@ void checkSimpleLibrary(@TempDir Path tempDir) {
3444
BibDatabase database = new BibDatabase(List.of(first, second));
3545
BibDatabaseContext bibContext = new BibDatabaseContext(database);
3646
bibContext.setMode(BibDatabaseMode.BIBTEX);
37-
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (count, total) -> {
47+
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, entryTypesManager, (count, total) -> {
3848
});
3949

4050
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) {
5565
BibDatabase bibDatabase = new BibDatabase(List.of(first, second));
5666
BibDatabaseContext bibContext = new BibDatabaseContext(bibDatabase);
5767
bibContext.setMode(BibDatabaseMode.BIBTEX);
58-
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (_, _) -> {
68+
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, entryTypesManager, (_, _) -> {
5969
});
6070

6171
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) {
8898
BibDatabase bibDatabase = new BibDatabase(List.of(first, second, third, fourth, fifth));
8999
BibDatabaseContext bibContext = new BibDatabaseContext(bibDatabase);
90100

91-
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (_, _) -> {
101+
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, entryTypesManager, (_, _) -> {
92102
});
93103

94104
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) {
111121
BibDatabase bibDatabase = new BibDatabase(List.of(first, second));
112122
BibDatabaseContext bibContext = new BibDatabaseContext(bibDatabase);
113123

114-
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, (_, _) -> {
124+
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(bibContext, entryTypesManager, (_, _) -> {
115125
});
116126

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

135145
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck()
136-
.check(bibContext, (_, _) -> {
146+
.check(bibContext, entryTypesManager, (_, _) -> {
137147
});
138148

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

152162
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck()
153-
.check(bibContext, (_, _) -> {
163+
.check(bibContext, entryTypesManager, (_, _) -> {
154164
});
155165

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

176186
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck()
177-
.check(bibContext, (_, _) -> {
187+
.check(bibContext, entryTypesManager, (_, _) -> {
178188
});
179189

180190
BibliographyConsistencyCheck.EntryTypeResult typeResult =
@@ -198,7 +208,7 @@ void unsetFieldsReportedInBibtexMode() {
198208
BibDatabaseContext bibContext = new BibDatabaseContext(bibDatabase);
199209
bibContext.setMode(BibDatabaseMode.BIBTEX);
200210
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck()
201-
.check(bibContext, (_, _) -> {
211+
.check(bibContext, entryTypesManager, (_, _) -> {
202212
});
203213
BibliographyConsistencyCheck.EntryTypeResult typeResult =
204214
result.entryTypeToResultMap().get(StandardEntryType.Online);
@@ -268,7 +278,7 @@ void checkComplexLibraryWithAdditionalEntry(@TempDir Path tempDir) {
268278
BibDatabase bibDatabase = new BibDatabase(List.of(first, second, third, fourth, fifth, sixth));
269279
BibDatabaseContext bibContext = new BibDatabaseContext(bibDatabase);
270280

271-
BibliographyConsistencyCheck.Result actualResult = new BibliographyConsistencyCheck().check(bibContext, (_, _) -> {
281+
BibliographyConsistencyCheck.Result actualResult = new BibliographyConsistencyCheck().check(bibContext, entryTypesManager, (_, _) -> {
272282
});
273283

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

0 commit comments

Comments
 (0)