Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Opening a file removes duplicate keywords and triggers lib changed #9216

Merged
merged 8 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 13 additions & 1 deletion src/main/java/org/jabref/model/entry/BibEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -773,8 +773,20 @@ public KeywordList getResolvedKeywords(Character delimiter, BibDatabase database

public Optional<FieldChange> removeKeywords(KeywordList keywordsToRemove, Character keywordDelimiter) {
KeywordList keywordList = getKeywords(keywordDelimiter);

// We need to fix "file has changed on disk" for duplicate keywords
// The input of the set may contain duplicate keywords (which are present as single keyword in the set)
// Even if no "keywordsToRemove" is contained, the method "putKeywords" will return a change, because the duplicate keywords will have been removed
int oldSize = keywordList.size();
keywordList.removeAll(keywordsToRemove);
return putKeywords(keywordList, keywordDelimiter);
// claim 1: The size of a set is the same, if no element is removed
// claim 2: The size of a set is different if an element was removed
// With claim 1, we can assume no change if there is no change on the size
if (oldSize == keywordList.size()) {
return Optional.empty();
} else {
return putKeywords(keywordList, keywordDelimiter);
}
}

public Optional<FieldChange> replaceKeywords(KeywordList keywordsToReplace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,37 @@ void writeEntry() throws Exception {
stringWriter.toString());
}

@Test
void writeEntryWithDuplicateKeywords() throws Exception {
BibEntry entry = new BibEntry();
entry.setType(StandardEntryType.Article);
entry.setField(StandardField.KEYWORDS, "asdf,asdf,asdf");
database.insertEntry(entry);

databaseWriter.savePartOfDatabase(bibtexContext, Collections.singletonList(entry));

assertEquals("@Article{," + OS.NEWLINE
+ " keywords = {asdf,asdf,asdf}," + OS.NEWLINE
+ "}" + OS.NEWLINE,
stringWriter.toString());
}

@Test
void putKeyWordsRemovesDuplicateKeywordsIsVisibleDuringWrite() throws Exception {
BibEntry entry = new BibEntry();
entry.setType(StandardEntryType.Article);
entry.putKeywords(List.of("asdf", "asdf", "asdf"), ',');

database.insertEntry(entry);

databaseWriter.savePartOfDatabase(bibtexContext, Collections.singletonList(entry));

assertEquals("@Article{," + OS.NEWLINE
+ " keywords = {asdf}," + OS.NEWLINE
+ "}" + OS.NEWLINE,
stringWriter.toString());
}

@Test
void writeEncodingAndEntry() throws Exception {
BibEntry entry = new BibEntry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1923,4 +1923,39 @@ void bibTeXConstantAprilIsParsedAsStringMonthAprilWhenReadingTheField() throws P

assertEquals(Optional.of("#apr#"), result.get().getField(StandardField.MONTH));
}

@Test
void parseDuplicateKeywordsWithOnlyOneEntry() throws ParseException {
Optional<BibEntry> result = parser.parseSingleEntry("@Article{,\n"
+ "Keywords={asdf,asdf,asdf},\n"
+ "}\n"
+ "");

BibEntry expectedEntry = new BibEntry(StandardEntryType.Article)
.withField(StandardField.KEYWORDS, "asdf,asdf,asdf");

assertEquals(Optional.of(expectedEntry), result);
}

@Test
void parseDuplicateKeywordsWithTwoEntries() throws Exception {
BibEntry expectedEntryFirst = new BibEntry(StandardEntryType.Article)
.withField(StandardField.KEYWORDS, "bbb")
.withCitationKey("Test2017");

BibEntry expectedEntrySecond = new BibEntry(StandardEntryType.Article)
.withField(StandardField.KEYWORDS, "asdf,asdf,asdf");

String entries = """
@Article{Test2017,
keywords = {bbb},
}

@Article{,
keywords = {asdf,asdf,asdf},
},
""";
ParserResult result = parser.parse(new StringReader(entries));
assertEquals(List.of(expectedEntryFirst, expectedEntrySecond), result.getDatabase().getEntries());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ public void noKewordToMigrate() {
assertEquals(expected, entry);
}

@Test
public void noKeywordToMigrateButDuplicateKeywords() {
BibEntry entry = new BibEntry().withField(StandardField.AUTHOR, "JabRef")
.withField(StandardField.KEYWORDS, "asdf, asdf, asdf");
BibEntry expected = new BibEntry().withField(StandardField.AUTHOR, "JabRef")
.withField(StandardField.KEYWORDS, "asdf, asdf, asdf");

new SpecialFieldsToSeparateFields(',').performMigration(new ParserResult(List.of(entry)));

assertEquals(expected, entry);
}

@Test
public void migrateMultipleSpecialFields() {
BibEntry entry = new BibEntry().withField(StandardField.AUTHOR, "JabRef")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
class BibDatabaseTest {

private BibDatabase database;
private BibtexString bibtexString = new BibtexString("DSP", "Digital Signal Processing");
private final BibtexString bibtexString = new BibtexString("DSP", "Digital Signal Processing");

@BeforeEach
void setUp() {
Expand Down