From 8f54393798f35feced3da92595655837ac7f3c9b Mon Sep 17 00:00:00 2001 From: subhramit Date: Tue, 13 Aug 2024 00:51:57 +0530 Subject: [PATCH 01/16] Fix authorsAlpha --- .../citationkeypattern/BracketedPattern.java | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java b/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java index 826361fa990..1b08913dd46 100644 --- a/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java +++ b/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java @@ -846,15 +846,7 @@ static String allAuthors(AuthorList authorList) { */ static String authorsAlpha(AuthorList authorList) { StringBuilder alphaStyle = new StringBuilder(); - int maxAuthors; - final boolean maxAuthorsExceeded; - if (authorList.getNumberOfAuthors() <= MAX_ALPHA_AUTHORS) { - maxAuthors = authorList.getNumberOfAuthors(); - maxAuthorsExceeded = false; - } else { - maxAuthors = MAX_ALPHA_AUTHORS - 1; - maxAuthorsExceeded = true; - } + int maxAuthors = Math.min(authorList.getNumberOfAuthors(), MAX_ALPHA_AUTHORS); if (authorList.getNumberOfAuthors() == 1) { String[] firstAuthor = authorList.getAuthor(0).getNamePrefixAndFamilyName() @@ -865,7 +857,7 @@ static String authorsAlpha(AuthorList authorList) { } // append last part of last name completely alphaStyle.append(firstAuthor[firstAuthor.length - 1], 0, - Math.min(3, firstAuthor[firstAuthor.length - 1].length())); + Math.min(4, firstAuthor[firstAuthor.length - 1].length())); } else { boolean andOthersPresent = authorList.getAuthor(maxAuthors - 1).equals(Author.OTHERS); if (andOthersPresent) { @@ -874,7 +866,7 @@ static String authorsAlpha(AuthorList authorList) { List vonAndLastNames = authorList.getAuthors().stream() .limit(maxAuthors) .map(Author::getNamePrefixAndFamilyName) - .collect(Collectors.toList()); + .toList(); for (String vonAndLast : vonAndLastNames) { // replace all whitespaces by " " // split the lastname at " " @@ -884,9 +876,6 @@ static String authorsAlpha(AuthorList authorList) { alphaStyle.append(part, 0, 1); } } - if (andOthersPresent || maxAuthorsExceeded) { - alphaStyle.append("+"); - } } return alphaStyle.toString(); } From cfc3c7f513823caf0476f08cba005c28ecd1cf0d Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Mon, 12 Aug 2024 21:37:36 +0200 Subject: [PATCH 02/16] Add more test cases --- .../jabref/logic/citationkeypattern/BracketedPatternTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java b/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java index f2893707f0e..47e007cc9eb 100644 --- a/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java +++ b/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java @@ -80,6 +80,9 @@ static Stream authorsAlpha() { Arguments.of("A+", "Aachen and others"), Arguments.of("AB+", "Aachen and Berlin and others"), Arguments.of("ABC+", "Aachen and Berlin and Chemnitz and others"), + Arguments.of("Aach", "Aachen"), + Arguments.of("AB", "Aachen and Berlin"), + Arguments.of("ABC", "Aachen and Berlin and Chemnitz"), Arguments.of("ABCD", "Aachen and Berlin and Chemnitz and Düsseldorf"), Arguments.of("ABC+", "Aachen and Berlin and Chemnitz and Düsseldorf and others"), Arguments.of("ABC+", "Aachen and Berlin and Chemnitz and Düsseldorf and Essen"), From e879e315c44c8f12af44ce43514a39e360afa8cc Mon Sep 17 00:00:00 2001 From: subhramit Date: Tue, 13 Aug 2024 20:38:39 +0530 Subject: [PATCH 03/16] Fix authorsAlpha - 2 --- .../citationkeypattern/BracketedPattern.java | 50 +++++++++---------- .../BracketedPatternTest.java | 46 ++++++++++++++--- 2 files changed, 61 insertions(+), 35 deletions(-) diff --git a/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java b/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java index 1b08913dd46..8d51c50e697 100644 --- a/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java +++ b/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java @@ -846,40 +846,36 @@ static String allAuthors(AuthorList authorList) { */ static String authorsAlpha(AuthorList authorList) { StringBuilder alphaStyle = new StringBuilder(); - int maxAuthors = Math.min(authorList.getNumberOfAuthors(), MAX_ALPHA_AUTHORS); - - if (authorList.getNumberOfAuthors() == 1) { - String[] firstAuthor = authorList.getAuthor(0).getNamePrefixAndFamilyName() - .replaceAll("\\s+", " ").trim().split(" "); - // take first letter of any "prefixes" (e.g. van der Aalst -> vd) - for (int j = 0; j < (firstAuthor.length - 1); j++) { - alphaStyle.append(firstAuthor[j], 0, 1); - } - // append last part of last name completely - alphaStyle.append(firstAuthor[firstAuthor.length - 1], 0, - Math.min(4, firstAuthor[firstAuthor.length - 1].length())); + int numberOfAuthors = authorList.getNumberOfAuthors(); + boolean andOthersPresent = numberOfAuthors > 1 && + authorList.getAuthor(numberOfAuthors - 1).equals(Author.OTHERS); + + if (numberOfAuthors == 1 || andOthersPresent) { + // Single author or "and others" case + String lastName = getLastName(authorList.getAuthor(0)); + alphaStyle.append(lastName, 0, Math.min(2, lastName.length())); } else { - boolean andOthersPresent = authorList.getAuthor(maxAuthors - 1).equals(Author.OTHERS); - if (andOthersPresent) { - maxAuthors--; - } - List vonAndLastNames = authorList.getAuthors().stream() - .limit(maxAuthors) - .map(Author::getNamePrefixAndFamilyName) - .toList(); - for (String vonAndLast : vonAndLastNames) { - // replace all whitespaces by " " - // split the lastname at " " - String[] nameParts = vonAndLast.replaceAll("\\s+", " ").trim().split(" "); - for (String part : nameParts) { - // use first character of each part of lastname - alphaStyle.append(part, 0, 1); + int maxAuthors = Math.min(numberOfAuthors, MAX_ALPHA_AUTHORS); + for (int i = 0; i < maxAuthors; i++) { + String lastName = getLastName(authorList.getAuthor(i)); + alphaStyle.append(lastName, 0, 1); + if (alphaStyle.length() >= 4) { + // Stop after 4 authors + break; } } } return alphaStyle.toString(); } + static String getLastName(Author author) { + String[] nameParts = author.getNamePrefixAndFamilyName() + .replaceAll("\\s+", " ") + .trim() + .split(" "); + return nameParts[nameParts.length - 1]; + } + /** * Creates a string with all last names separated by a `delimiter`. If the number of authors are larger than * `maxAuthors`, replace all excess authors with `suffix`. diff --git a/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java b/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java index 47e007cc9eb..ca40eca2626 100644 --- a/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java +++ b/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java @@ -3,6 +3,7 @@ import java.util.stream.Stream; import org.jabref.model.database.BibDatabase; +import org.jabref.model.entry.Author; import org.jabref.model.entry.AuthorList; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.BibtexString; @@ -76,17 +77,22 @@ void allAuthors(String expected, AuthorList list) { static Stream authorsAlpha() { return Stream.of( - Arguments.of("A+", "Alexander Artemenko and others"), - Arguments.of("A+", "Aachen and others"), - Arguments.of("AB+", "Aachen and Berlin and others"), - Arguments.of("ABC+", "Aachen and Berlin and Chemnitz and others"), - Arguments.of("Aach", "Aachen"), + Arguments.of("Ar", "Alexander Artemenko and others"), + Arguments.of("Aa", "Aachen and others"), + Arguments.of("Aa", "Aachen and Berlin and others"), + Arguments.of("Aa", "Aachen and Berlin and Chemnitz and others"), Arguments.of("AB", "Aachen and Berlin"), Arguments.of("ABC", "Aachen and Berlin and Chemnitz"), Arguments.of("ABCD", "Aachen and Berlin and Chemnitz and Düsseldorf"), - Arguments.of("ABC+", "Aachen and Berlin and Chemnitz and Düsseldorf and others"), - Arguments.of("ABC+", "Aachen and Berlin and Chemnitz and Düsseldorf and Essen"), - Arguments.of("ABC+", "Aachen and Berlin and Chemnitz and Düsseldorf and Essen and others")); + Arguments.of("Aa", "Aachen and Berlin and Chemnitz and Düsseldorf and others"), + Arguments.of("ABCD", "Aachen and Berlin and Chemnitz and Düsseldorf and Essen"), + Arguments.of("Aa", "Aachen and Berlin and Chemnitz and Düsseldorf and Essen and others"), + Arguments.of("AB", "Abel, K.; Bibel, U."), + Arguments.of("ABC", "Abraham, N.; Bibel, U.; Corleone, P."), + Arguments.of("Az", "Azubi, L. et.al."), + Arguments.of("Ez", "Ezgarani, O."), + Arguments.of("GI", "GI, Gesellschaft für Informatik e.V."), + Arguments.of("Gl", "Glück, H. I.")); } @ParameterizedTest @@ -95,6 +101,30 @@ void authorsAlpha(String expected, AuthorList list) { assertEquals(expected, BracketedPattern.authorsAlpha(list)); } + static Stream getLastName() { + return Stream.of( + Arguments.of("Artemenko", "Alexander Artemenko"), + Arguments.of("Aachen", "Aachen"), + Arguments.of("Berlin", "Berlin"), + Arguments.of("Chemnitz", "Chemnitz"), + Arguments.of("Düsseldorf", "Düsseldorf"), + Arguments.of("Essen", "Essen"), + Arguments.of("Abel", "Abel, K."), + Arguments.of("Bibel", "Bibel, U."), + Arguments.of("Abraham", "Abraham, N."), + Arguments.of("Corleone", "Corleone, P."), + Arguments.of("Azubi", "Azubi, L."), + Arguments.of("Ezgarani", "Ezgarani, O."), + Arguments.of("e.V.", "GI, Gesellschaft für Informatik e.V."), + Arguments.of("Glück", "Glück, H. I.")); + } + + @ParameterizedTest + @MethodSource + void getLastName(String expected, Author fullName) { + assertEquals(expected, BracketedPattern.getLastName(fullName)); + } + /** * Tests [authorIni] */ From 0131b9fec2c90edbf6e290f50f6c5a302ff3a0b8 Mon Sep 17 00:00:00 2001 From: subhramit Date: Tue, 13 Aug 2024 20:47:52 +0530 Subject: [PATCH 04/16] Fix tests --- .../logic/citationkeypattern/BracketedPatternTest.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java b/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java index ca40eca2626..5edf64a041b 100644 --- a/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java +++ b/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java @@ -115,14 +115,16 @@ static Stream getLastName() { Arguments.of("Corleone", "Corleone, P."), Arguments.of("Azubi", "Azubi, L."), Arguments.of("Ezgarani", "Ezgarani, O."), - Arguments.of("e.V.", "GI, Gesellschaft für Informatik e.V."), + Arguments.of("GI", "GI, Gesellschaft für Informatik e.V."), Arguments.of("Glück", "Glück, H. I.")); } @ParameterizedTest @MethodSource - void getLastName(String expected, Author fullName) { - assertEquals(expected, BracketedPattern.getLastName(fullName)); + void getLastName(String expected, String fullName) { + AuthorList authorList = AuthorList.parse(fullName); + Author firstAuthor = authorList.getAuthor(0); + assertEquals(expected, BracketedPattern.getLastName(firstAuthor)); } /** From 4680f3cd3ff3ecaaad2e13c1dc6ac09f0533bead Mon Sep 17 00:00:00 2001 From: subhramit Date: Wed, 14 Aug 2024 03:40:42 +0530 Subject: [PATCH 05/16] Add tests for "van der" and other titles --- .../BracketedPatternTest.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java b/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java index 5edf64a041b..6574aa1bc0d 100644 --- a/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java +++ b/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java @@ -3,7 +3,6 @@ import java.util.stream.Stream; import org.jabref.model.database.BibDatabase; -import org.jabref.model.entry.Author; import org.jabref.model.entry.AuthorList; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.BibtexString; @@ -92,7 +91,10 @@ static Stream authorsAlpha() { Arguments.of("Az", "Azubi, L. et.al."), Arguments.of("Ez", "Ezgarani, O."), Arguments.of("GI", "GI, Gesellschaft für Informatik e.V."), - Arguments.of("Gl", "Glück, H. I.")); + Arguments.of("Gl", "Glück, H. I."), + Arguments.of("Go", "von Goethe"), + Arguments.of("Aa", "van der Aalst"), + Arguments.of("AW", "van der Aalst and Weske")); } @ParameterizedTest @@ -119,13 +121,13 @@ static Stream getLastName() { Arguments.of("Glück", "Glück, H. I.")); } - @ParameterizedTest - @MethodSource - void getLastName(String expected, String fullName) { - AuthorList authorList = AuthorList.parse(fullName); - Author firstAuthor = authorList.getAuthor(0); - assertEquals(expected, BracketedPattern.getLastName(firstAuthor)); - } +// @ParameterizedTest +// @MethodSource +// void getLastName(String expected, String fullName) { +// AuthorList authorList = AuthorList.parse(fullName); +// Author firstAuthor = authorList.getAuthor(0); +// assertEquals(expected, BracketedPattern.getLastName(firstAuthor)); +// } /** * Tests [authorIni] From d4b54593c514cd5ef807510ae678237b8fc928c6 Mon Sep 17 00:00:00 2001 From: subhramit Date: Wed, 14 Aug 2024 03:42:18 +0530 Subject: [PATCH 06/16] Uncomment getLastName tests --- .../citationkeypattern/BracketedPatternTest.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java b/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java index 6574aa1bc0d..91db3ecda8b 100644 --- a/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java +++ b/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java @@ -3,6 +3,7 @@ import java.util.stream.Stream; import org.jabref.model.database.BibDatabase; +import org.jabref.model.entry.Author; import org.jabref.model.entry.AuthorList; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.BibtexString; @@ -121,13 +122,13 @@ static Stream getLastName() { Arguments.of("Glück", "Glück, H. I.")); } -// @ParameterizedTest -// @MethodSource -// void getLastName(String expected, String fullName) { -// AuthorList authorList = AuthorList.parse(fullName); -// Author firstAuthor = authorList.getAuthor(0); -// assertEquals(expected, BracketedPattern.getLastName(firstAuthor)); -// } + @ParameterizedTest + @MethodSource + void getLastName(String expected, String fullName) { + AuthorList authorList = AuthorList.parse(fullName); + Author firstAuthor = authorList.getAuthor(0); + assertEquals(expected, BracketedPattern.getLastName(firstAuthor)); + } /** * Tests [authorIni] From 01259836a6b3ed9c651a5b8c6465ab5e410966c2 Mon Sep 17 00:00:00 2001 From: subhramit Date: Wed, 14 Aug 2024 03:56:25 +0530 Subject: [PATCH 07/16] Add javadoc --- .../citationkeypattern/BracketedPatternTest.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java b/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java index 91db3ecda8b..1ebd9c2e7ff 100644 --- a/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java +++ b/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java @@ -98,13 +98,17 @@ static Stream authorsAlpha() { Arguments.of("AW", "van der Aalst and Weske")); } + /** + * Precondition: This test assumes that the method {@link BracketedPattern#getLastName(Author) getLastName} works properly. + * See {@link BracketedPatternTest#getLastNameTest(String, String) getLastNameTest}. + */ @ParameterizedTest @MethodSource void authorsAlpha(String expected, AuthorList list) { assertEquals(expected, BracketedPattern.authorsAlpha(list)); } - static Stream getLastName() { + static Stream getLastNameTest() { return Stream.of( Arguments.of("Artemenko", "Alexander Artemenko"), Arguments.of("Aachen", "Aachen"), @@ -119,12 +123,14 @@ static Stream getLastName() { Arguments.of("Azubi", "Azubi, L."), Arguments.of("Ezgarani", "Ezgarani, O."), Arguments.of("GI", "GI, Gesellschaft für Informatik e.V."), - Arguments.of("Glück", "Glück, H. I.")); + Arguments.of("Glück", "Glück, H. I."), + Arguments.of("Goethe", "von Goethe"), + Arguments.of("Aalst", "van der Aalst")); } @ParameterizedTest @MethodSource - void getLastName(String expected, String fullName) { + void getLastNameTest(String expected, String fullName) { AuthorList authorList = AuthorList.parse(fullName); Author firstAuthor = authorList.getAuthor(0); assertEquals(expected, BracketedPattern.getLastName(firstAuthor)); From b889366d1df6ac868bbf1e728a848fff734022b9 Mon Sep 17 00:00:00 2001 From: subhramit Date: Wed, 14 Aug 2024 04:52:47 +0530 Subject: [PATCH 08/16] Fix CitationKeyGeneratorTest --- .../citationkeypattern/CitationKeyGeneratorTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/jabref/logic/citationkeypattern/CitationKeyGeneratorTest.java b/src/test/java/org/jabref/logic/citationkeypattern/CitationKeyGeneratorTest.java index 2afd2990190..c84f92c3019 100644 --- a/src/test/java/org/jabref/logic/citationkeypattern/CitationKeyGeneratorTest.java +++ b/src/test/java/org/jabref/logic/citationkeypattern/CitationKeyGeneratorTest.java @@ -505,14 +505,14 @@ void allAuthors() { static Stream authorsAlpha() { return Stream.of( - Arguments.of("New", AUTHOR_FIRSTNAME_INITIAL_LASTNAME_FULL_COUNT_1, AUTHORSALPHA), + Arguments.of("Ne", AUTHOR_FIRSTNAME_INITIAL_LASTNAME_FULL_COUNT_1, AUTHORSALPHA), Arguments.of("NM", AUTHOR_FIRSTNAME_INITIAL_LASTNAME_FULL_COUNT_2, AUTHORSALPHA), Arguments.of("NME", AUTHOR_FIRSTNAME_INITIAL_LASTNAME_FULL_COUNT_3, AUTHORSALPHA), Arguments.of("NMEB", AUTHOR_FIRSTNAME_INITIAL_LASTNAME_FULL_COUNT_4, AUTHORSALPHA), - Arguments.of("NME+", AUTHOR_FIRSTNAME_INITIAL_LASTNAME_FULL_COUNT_5, AUTHORSALPHA), - Arguments.of("vdAal", AUTHOR_FIRSTNAME_FULL_LASTNAME_FULL_WITH_VAN_COUNT_1, AUTHORSALPHA), - Arguments.of("vdAvL", AUTHOR_FIRSTNAME_FULL_LASTNAME_FULL_WITH_VAN_COUNT_2, AUTHORSALPHA), - Arguments.of("NM+", AUTHOR_FIRSTNAME_FULL_LASTNAME_FULL_AND_OTHERS_COUNT_3, AUTHORSALPHA) + Arguments.of("NMEB", AUTHOR_FIRSTNAME_INITIAL_LASTNAME_FULL_COUNT_5, AUTHORSALPHA), + Arguments.of("Aa", AUTHOR_FIRSTNAME_FULL_LASTNAME_FULL_WITH_VAN_COUNT_1, AUTHORSALPHA), + Arguments.of("AL", AUTHOR_FIRSTNAME_FULL_LASTNAME_FULL_WITH_VAN_COUNT_2, AUTHORSALPHA), + Arguments.of("Ne", AUTHOR_FIRSTNAME_FULL_LASTNAME_FULL_AND_OTHERS_COUNT_3, AUTHORSALPHA) ); } From 603b9e3ee43d0cbb0b425a772e7a251cc51baeb4 Mon Sep 17 00:00:00 2001 From: subhramit Date: Wed, 14 Aug 2024 05:08:23 +0530 Subject: [PATCH 09/16] Fix BracketedPatternTest --- .../AbstractCitationKeyPatternsTest.java | 4 +- .../BracketedPatternTest.java | 2 +- .../org/jabref/model/entry/BibEntryTest.java | 38 +++++++++---------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/test/java/org/jabref/logic/citationkeypattern/AbstractCitationKeyPatternsTest.java b/src/test/java/org/jabref/logic/citationkeypattern/AbstractCitationKeyPatternsTest.java index f050f0fdc05..47c2666610b 100644 --- a/src/test/java/org/jabref/logic/citationkeypattern/AbstractCitationKeyPatternsTest.java +++ b/src/test/java/org/jabref/logic/citationkeypattern/AbstractCitationKeyPatternsTest.java @@ -12,7 +12,7 @@ class AbstractCitationKeyPatternsTest { @Test - void AbstractCitationKeyPatternParse() throws Exception { + void AbstractCitationKeyPatternParse() { AbstractCitationKeyPatterns pattern = mock(AbstractCitationKeyPatterns.class, Mockito.CALLS_REAL_METHODS); pattern.setDefaultValue("[field1]spacer1[field2]spacer2[field3]"); @@ -21,7 +21,7 @@ void AbstractCitationKeyPatternParse() throws Exception { } @Test - void AbstractCitationKeyPatternParseEmptySpacer() throws Exception { + void AbstractCitationKeyPatternParseEmptySpacer() { AbstractCitationKeyPatterns pattern = mock(AbstractCitationKeyPatterns.class, Mockito.CALLS_REAL_METHODS); pattern.setDefaultValue("[field1][field2]spacer2[field3]"); diff --git a/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java b/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java index 1ebd9c2e7ff..7ac74275386 100644 --- a/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java +++ b/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java @@ -349,7 +349,7 @@ void authShort(String expected, AuthorList list) { "'New', '[auth3]', 'Isaac Newton'", "'New', '[auth3_1]', 'Isaac Newton'", "'Newton', '[authshort]', 'Isaac Newton'", - "'New', '[authorsAlpha]', 'Isaac Newton'", + "'Ne', '[authorsAlpha]', 'Isaac Newton'", "'Newton', '[authorLast]', 'Isaac Newton'", "'I', '[authorLastForeIni]', 'Isaac Newton'", diff --git a/src/test/java/org/jabref/model/entry/BibEntryTest.java b/src/test/java/org/jabref/model/entry/BibEntryTest.java index 25ed76d7633..0f81a583472 100644 --- a/src/test/java/org/jabref/model/entry/BibEntryTest.java +++ b/src/test/java/org/jabref/model/entry/BibEntryTest.java @@ -59,13 +59,13 @@ void setNullFieldThrowsNPE() { } @Test - void getFieldIsCaseInsensitive() throws Exception { + void getFieldIsCaseInsensitive() { entry.setField(new UnknownField("TeSt"), "value"); assertEquals(Optional.of("value"), entry.getField(new UnknownField("tEsT"))); } @Test - void getFieldWorksWithBibFieldAsWell() throws Exception { + void getFieldWorksWithBibFieldAsWell() { entry.setField(StandardField.AUTHOR, "value"); assertEquals(Optional.of("value"), entry.getField(new BibField(StandardField.AUTHOR, FieldPriority.IMPORTANT).field())); } @@ -76,80 +76,80 @@ void newBibEntryIsUnchanged() { } @Test - void setFieldLeadsToAChangedEntry() throws Exception { + void setFieldLeadsToAChangedEntry() { entry.setField(StandardField.AUTHOR, "value"); assertTrue(entry.hasChanged()); } @Test - void setFieldWorksWithBibFieldAsWell() throws Exception { + void setFieldWorksWithBibFieldAsWell() { entry.setField(new BibField(StandardField.AUTHOR, FieldPriority.IMPORTANT).field(), "value"); assertEquals(Optional.of("value"), entry.getField(StandardField.AUTHOR)); } @Test - void clonedBibEntryHasUniqueID() throws Exception { + void clonedBibEntryHasUniqueID() { BibEntry entryClone = (BibEntry) entry.clone(); assertNotEquals(entry.getId(), entryClone.getId()); } @Test - void clonedBibEntryWithMiscTypeHasOriginalChangedFlag() throws Exception { + void clonedBibEntryWithMiscTypeHasOriginalChangedFlag() { BibEntry entryClone = (BibEntry) entry.clone(); assertFalse(entryClone.hasChanged()); } @Test - void clonedBibEntryWithBookTypeAndOneFieldHasOriginalChangedFlag() throws Exception { + void clonedBibEntryWithBookTypeAndOneFieldHasOriginalChangedFlag() { entry = new BibEntry(StandardEntryType.Book).withField(StandardField.AUTHOR, "value"); BibEntry entryClone = (BibEntry) entry.clone(); assertFalse(entryClone.hasChanged()); } @Test - void setAndGetAreConsistentForMonth() throws Exception { + void setAndGetAreConsistentForMonth() { entry.setField(StandardField.MONTH, "may"); assertEquals(Optional.of("may"), entry.getField(StandardField.MONTH)); } @Test - void setAndGetAreConsistentForCapitalizedMonth() throws Exception { + void setAndGetAreConsistentForCapitalizedMonth() { entry.setField(StandardField.MONTH, "May"); assertEquals(Optional.of("May"), entry.getField(StandardField.MONTH)); } @Test - void setAndGetAreConsistentForMonthString() throws Exception { + void setAndGetAreConsistentForMonthString() { entry.setField(StandardField.MONTH, "#may#"); assertEquals(Optional.of("#may#"), entry.getField(StandardField.MONTH)); } @Test - void monthCorrectlyReturnedForMonth() throws Exception { + void monthCorrectlyReturnedForMonth() { entry.setField(StandardField.MONTH, "may"); assertEquals(Optional.of(Month.MAY), entry.getMonth()); } @Test - void monthCorrectlyReturnedForCapitalizedMonth() throws Exception { + void monthCorrectlyReturnedForCapitalizedMonth() { entry.setField(StandardField.MONTH, "May"); assertEquals(Optional.of(Month.MAY), entry.getMonth()); } @Test - void monthCorrectlyReturnedForMonthString() throws Exception { + void monthCorrectlyReturnedForMonthString() { entry.setField(StandardField.MONTH, "#may#"); assertEquals(Optional.of(Month.MAY), entry.getMonth()); } @Test - void monthCorrectlyReturnedForMonthMay() throws Exception { + void monthCorrectlyReturnedForMonthMay() { entry.setMonth(Month.MAY); assertEquals(Optional.of(Month.MAY), entry.getMonth()); } @Test - void monthFieldCorrectlyReturnedForMonthMay() throws Exception { + void monthFieldCorrectlyReturnedForMonthMay() { entry.setMonth(Month.MAY); assertEquals(Optional.of("#may#"), entry.getField(StandardField.MONTH)); } @@ -362,23 +362,23 @@ void isEmptyCiteKey() { } @Test - void identicObjectsareEqual() throws Exception { + void identicObjectsareEqual() { BibEntry otherEntry = entry; assertEquals(entry, otherEntry); } @Test - void compareToNullObjectIsFalse() throws Exception { + void compareToNullObjectIsFalse() { assertNotEquals(null, entry); } @Test - void compareToDifferentClassIsFalse() throws Exception { + void compareToDifferentClassIsFalse() { assertNotEquals(entry, new Object()); } @Test - void compareIsTrueWhenIdAndFieldsAreEqual() throws Exception { + void compareIsTrueWhenIdAndFieldsAreEqual() { entry.setId("1"); entry.setField(new UnknownField("key"), "value"); BibEntry otherEntry = new BibEntry(); From 1d1bc0ec715422b5bea61b0259164537c85c1be6 Mon Sep 17 00:00:00 2001 From: subhramit Date: Fri, 16 Aug 2024 08:40:22 +0530 Subject: [PATCH 10/16] Use Authors#getLastName --- .../citationkeypattern/BracketedPattern.java | 12 ++----- .../BracketedPatternTest.java | 33 ------------------- 2 files changed, 2 insertions(+), 43 deletions(-) diff --git a/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java b/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java index fada7599b6e..2aa64f80503 100644 --- a/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java +++ b/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java @@ -852,12 +852,12 @@ public static String authorsAlpha(AuthorList authorList) { if (numberOfAuthors == 1 || andOthersPresent) { // Single author or "and others" case - String lastName = getLastName(authorList.getAuthor(0)); + String lastName = authorList.getAuthor(0).getFamilyName().orElse(""); alphaStyle.append(lastName, 0, Math.min(2, lastName.length())); } else { int maxAuthors = Math.min(numberOfAuthors, MAX_ALPHA_AUTHORS); for (int i = 0; i < maxAuthors; i++) { - String lastName = getLastName(authorList.getAuthor(i)); + String lastName = authorList.getAuthor(i).getFamilyName().orElse(""); alphaStyle.append(lastName, 0, 1); if (alphaStyle.length() >= 4) { // Stop after 4 authors @@ -868,14 +868,6 @@ public static String authorsAlpha(AuthorList authorList) { return alphaStyle.toString(); } - static String getLastName(Author author) { - String[] nameParts = author.getNamePrefixAndFamilyName() - .replaceAll("\\s+", " ") - .trim() - .split(" "); - return nameParts[nameParts.length - 1]; - } - /** * Creates a string with all last names separated by a `delimiter`. If the number of authors are larger than * `maxAuthors`, replace all excess authors with `suffix`. diff --git a/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java b/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java index 7ac74275386..693b0b2f223 100644 --- a/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java +++ b/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java @@ -3,7 +3,6 @@ import java.util.stream.Stream; import org.jabref.model.database.BibDatabase; -import org.jabref.model.entry.Author; import org.jabref.model.entry.AuthorList; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.BibtexString; @@ -98,44 +97,12 @@ static Stream authorsAlpha() { Arguments.of("AW", "van der Aalst and Weske")); } - /** - * Precondition: This test assumes that the method {@link BracketedPattern#getLastName(Author) getLastName} works properly. - * See {@link BracketedPatternTest#getLastNameTest(String, String) getLastNameTest}. - */ @ParameterizedTest @MethodSource void authorsAlpha(String expected, AuthorList list) { assertEquals(expected, BracketedPattern.authorsAlpha(list)); } - static Stream getLastNameTest() { - return Stream.of( - Arguments.of("Artemenko", "Alexander Artemenko"), - Arguments.of("Aachen", "Aachen"), - Arguments.of("Berlin", "Berlin"), - Arguments.of("Chemnitz", "Chemnitz"), - Arguments.of("Düsseldorf", "Düsseldorf"), - Arguments.of("Essen", "Essen"), - Arguments.of("Abel", "Abel, K."), - Arguments.of("Bibel", "Bibel, U."), - Arguments.of("Abraham", "Abraham, N."), - Arguments.of("Corleone", "Corleone, P."), - Arguments.of("Azubi", "Azubi, L."), - Arguments.of("Ezgarani", "Ezgarani, O."), - Arguments.of("GI", "GI, Gesellschaft für Informatik e.V."), - Arguments.of("Glück", "Glück, H. I."), - Arguments.of("Goethe", "von Goethe"), - Arguments.of("Aalst", "van der Aalst")); - } - - @ParameterizedTest - @MethodSource - void getLastNameTest(String expected, String fullName) { - AuthorList authorList = AuthorList.parse(fullName); - Author firstAuthor = authorList.getAuthor(0); - assertEquals(expected, BracketedPattern.getLastName(firstAuthor)); - } - /** * Tests [authorIni] */ From fd59be0ac618d9a1d3112e40cf97be1b7522de45 Mon Sep 17 00:00:00 2001 From: subhramit Date: Fri, 16 Aug 2024 13:37:20 +0530 Subject: [PATCH 11/16] save --- src/main/resources/csl-locales | 2 +- src/main/resources/csl-styles | 2 +- .../jabref/logic/citationkeypattern/BracketedPatternTest.java | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/resources/csl-locales b/src/main/resources/csl-locales index 242640a0e00..e631a52dcea 160000 --- a/src/main/resources/csl-locales +++ b/src/main/resources/csl-locales @@ -1 +1 @@ -Subproject commit 242640a0e00972f48c417530f9c2b9518504c0b6 +Subproject commit e631a52dcea396be20d031b6456e91dba7772224 diff --git a/src/main/resources/csl-styles b/src/main/resources/csl-styles index d9989a28ed4..bf2926b71a9 160000 --- a/src/main/resources/csl-styles +++ b/src/main/resources/csl-styles @@ -1 +1 @@ -Subproject commit d9989a28ed474d87dbb600fc233fb37e773b59a8 +Subproject commit bf2926b71a969644ce735760977d1246aed1f2e2 diff --git a/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java b/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java index 693b0b2f223..ad6d1797732 100644 --- a/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java +++ b/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java @@ -94,7 +94,8 @@ static Stream authorsAlpha() { Arguments.of("Gl", "Glück, H. I."), Arguments.of("Go", "von Goethe"), Arguments.of("Aa", "van der Aalst"), - Arguments.of("AW", "van der Aalst and Weske")); + Arguments.of("AW", "van der Aalst and Weske"), + Arguments.of("GI", "{Gesellschaft für Informatik e.V.}")); } @ParameterizedTest From 274934a41e66c132c3b808582dbd90e4f0c2e0d1 Mon Sep 17 00:00:00 2001 From: subhramit Date: Fri, 16 Aug 2024 13:52:47 +0530 Subject: [PATCH 12/16] Handle organization names --- .../citationkeypattern/BracketedPattern.java | 19 ++++++++++++++++++- .../BracketedPatternTest.java | 3 ++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java b/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java index 2aa64f80503..b231ed0b87f 100644 --- a/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java +++ b/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java @@ -853,7 +853,11 @@ public static String authorsAlpha(AuthorList authorList) { if (numberOfAuthors == 1 || andOthersPresent) { // Single author or "and others" case String lastName = authorList.getAuthor(0).getFamilyName().orElse(""); - alphaStyle.append(lastName, 0, Math.min(2, lastName.length())); + if (lastName.startsWith("{") && lastName.endsWith("}")) { + alphaStyle.append(getOrganizationInitials(lastName)); + } else { + alphaStyle.append(lastName, 0, Math.min(2, lastName.length())); + } } else { int maxAuthors = Math.min(numberOfAuthors, MAX_ALPHA_AUTHORS); for (int i = 0; i < maxAuthors; i++) { @@ -868,6 +872,19 @@ public static String authorsAlpha(AuthorList authorList) { return alphaStyle.toString(); } + private static String getOrganizationInitials(String orgName) { + // Remove the curly braces + String name = orgName.substring(1, orgName.length() - 1).trim(); + // Get the initials of the organization + StringBuilder initials = new StringBuilder(); + for (String part : name.split("\\s+")) { + if (!part.isEmpty() && Character.isUpperCase(part.charAt(0))) { + initials.append(part.charAt(0)); + } + } + return initials.toString(); + } + /** * Creates a string with all last names separated by a `delimiter`. If the number of authors are larger than * `maxAuthors`, replace all excess authors with `suffix`. diff --git a/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java b/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java index ad6d1797732..cc466d6b90e 100644 --- a/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java +++ b/src/test/java/org/jabref/logic/citationkeypattern/BracketedPatternTest.java @@ -95,7 +95,8 @@ static Stream authorsAlpha() { Arguments.of("Go", "von Goethe"), Arguments.of("Aa", "van der Aalst"), Arguments.of("AW", "van der Aalst and Weske"), - Arguments.of("GI", "{Gesellschaft für Informatik e.V.}")); + Arguments.of("GI", "{Gesellschaft für Informatik e.V.}"), + Arguments.of("AF", "{Apache Foundation}")); } @ParameterizedTest From 45eb781bac5e3b9cd5864a576aa72216f03c0dc2 Mon Sep 17 00:00:00 2001 From: subhramit Date: Fri, 16 Aug 2024 14:09:34 +0530 Subject: [PATCH 13/16] fix submodules part 1 --- src/main/resources/csl-styles | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/csl-styles b/src/main/resources/csl-styles index bf2926b71a9..df939b87950 160000 --- a/src/main/resources/csl-styles +++ b/src/main/resources/csl-styles @@ -1 +1 @@ -Subproject commit bf2926b71a969644ce735760977d1246aed1f2e2 +Subproject commit df939b87950c9f22a98e7a8188bbeabbc307bb42 From a68d1c89d9c4f725a73ba4f3e405a0c14ff180a5 Mon Sep 17 00:00:00 2001 From: subhramit Date: Fri, 16 Aug 2024 14:11:00 +0530 Subject: [PATCH 14/16] fix submodules part 1 --- src/main/resources/csl-styles | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/csl-styles b/src/main/resources/csl-styles index df939b87950..d9989a28ed4 160000 --- a/src/main/resources/csl-styles +++ b/src/main/resources/csl-styles @@ -1 +1 @@ -Subproject commit df939b87950c9f22a98e7a8188bbeabbc307bb42 +Subproject commit d9989a28ed474d87dbb600fc233fb37e773b59a8 From 2cf576dd3a1dd2eb748bcdd1f80397fb5e430f7b Mon Sep 17 00:00:00 2001 From: subhramit Date: Fri, 16 Aug 2024 14:11:15 +0530 Subject: [PATCH 15/16] fix submodules part 2 --- src/main/resources/csl-locales | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/csl-locales b/src/main/resources/csl-locales index e631a52dcea..242640a0e00 160000 --- a/src/main/resources/csl-locales +++ b/src/main/resources/csl-locales @@ -1 +1 @@ -Subproject commit e631a52dcea396be20d031b6456e91dba7772224 +Subproject commit 242640a0e00972f48c417530f9c2b9518504c0b6 From 79923916ea49eb5b5581ffbc63faa92b16b6324e Mon Sep 17 00:00:00 2001 From: subhramit Date: Fri, 16 Aug 2024 14:34:44 +0530 Subject: [PATCH 16/16] Use Braces Formatter --- .../logic/citationkeypattern/BracketedPattern.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java b/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java index b231ed0b87f..2407cf0fe7f 100644 --- a/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java +++ b/src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java @@ -21,6 +21,7 @@ import org.jabref.logic.cleanup.Formatter; import org.jabref.logic.formatter.Formatters; +import org.jabref.logic.formatter.bibtexfields.RemoveEnclosingBracesFormatter; import org.jabref.logic.formatter.casechanger.Word; import org.jabref.logic.layout.format.RemoveLatexCommandsFormatter; import org.jabref.model.database.BibDatabase; @@ -84,6 +85,8 @@ public class BracketedPattern { private static final Pattern WHITESPACE = Pattern.compile("\\p{javaWhitespace}"); + private static final RemoveEnclosingBracesFormatter ENCLOSING_BRACES_FORMATTER = new RemoveEnclosingBracesFormatter(); + private enum Institution { SCHOOL, DEPARTMENT, @@ -853,8 +856,10 @@ public static String authorsAlpha(AuthorList authorList) { if (numberOfAuthors == 1 || andOthersPresent) { // Single author or "and others" case String lastName = authorList.getAuthor(0).getFamilyName().orElse(""); - if (lastName.startsWith("{") && lastName.endsWith("}")) { - alphaStyle.append(getOrganizationInitials(lastName)); + String formattedName = ENCLOSING_BRACES_FORMATTER.format(lastName); + if (!formattedName.equals(lastName)) { + // Inequality => braces were removed, indicating an organization + alphaStyle.append(getOrganizationInitials(formattedName)); } else { alphaStyle.append(lastName, 0, Math.min(2, lastName.length())); } @@ -873,11 +878,8 @@ public static String authorsAlpha(AuthorList authorList) { } private static String getOrganizationInitials(String orgName) { - // Remove the curly braces - String name = orgName.substring(1, orgName.length() - 1).trim(); - // Get the initials of the organization StringBuilder initials = new StringBuilder(); - for (String part : name.split("\\s+")) { + for (String part : orgName.split("\\s+")) { if (!part.isEmpty() && Character.isUpperCase(part.charAt(0))) { initials.append(part.charAt(0)); }