diff --git a/handleArgument.txt b/handleArgument.txt new file mode 100644 index 00000000000..1cb5c4b6b2b --- /dev/null +++ b/handleArgument.txt @@ -0,0 +1,43 @@ +branch 0 was visited. +branch 1 was not visited. +branch 2 was visited. +branch 3 was not visited. +branch 4 was not visited. +branch 5 was visited. +branch 6 was visited. +branch 7 was visited. +branch 8 was not visited. +branch 9 was visited. +branch 10 was visited. +branch 11 was not visited. +branch 12 was not visited. +branch 13 was visited. +branch 14 was not visited. +branch 15 was not visited. +branch 16 was not visited. +branch 17 was visited. +branch 18 was not visited. +branch 19 was visited. +branch 20 was visited. +branch 21 was visited. +branch 22 was visited. +branch 23 was visited. +branch 24 was visited. +branch 25 was visited. +branch 26 was not visited. +branch 27 was not visited. +branch 28 was not visited. +branch 29 was visited. +branch 30 was not visited. +branch 31 was visited. +branch 32 was visited. +branch 33 was visited. +branch 34 was not visited. +branch 35 was not visited. +branch 36 was not visited. +branch 37 was visited. +branch 38 was visited. +branch 39 was visited. +branch 40 was visited. +branch 41 was not visited. +0.5714285714285714 \ No newline at end of file diff --git a/handleArgumentRefactor.txt b/handleArgumentRefactor.txt new file mode 100644 index 00000000000..e8cf6dd4378 --- /dev/null +++ b/handleArgumentRefactor.txt @@ -0,0 +1,9 @@ +branch 0 was visited. +branch 1 was visited. +branch 2 was visited. +branch 3 was visited. +branch 4 was visited. +branch 5 was visited. +branch 6 was visited. +branch 7 was not visited. +0.875 \ No newline at end of file diff --git a/handleArgument_NEW.txt b/handleArgument_NEW.txt new file mode 100644 index 00000000000..11f5c2ddf36 --- /dev/null +++ b/handleArgument_NEW.txt @@ -0,0 +1,43 @@ +branch 0 was visited. +branch 1 was visited. +branch 2 was visited. +branch 3 was not visited. +branch 4 was not visited. +branch 5 was visited. +branch 6 was visited. +branch 7 was visited. +branch 8 was visited. +branch 9 was visited. +branch 10 was visited. +branch 11 was not visited. +branch 12 was not visited. +branch 13 was visited. +branch 14 was not visited. +branch 15 was not visited. +branch 16 was not visited. +branch 17 was visited. +branch 18 was not visited. +branch 19 was visited. +branch 20 was visited. +branch 21 was visited. +branch 22 was visited. +branch 23 was visited. +branch 24 was visited. +branch 25 was visited. +branch 26 was visited. +branch 27 was visited. +branch 28 was visited. +branch 29 was visited. +branch 30 was not visited. +branch 31 was visited. +branch 32 was visited. +branch 33 was visited. +branch 34 was not visited. +branch 35 was not visited. +branch 36 was not visited. +branch 37 was visited. +branch 38 was visited. +branch 39 was visited. +branch 40 was visited. +branch 41 was not visited. +0.6904761904761905 \ No newline at end of file diff --git a/src/main/java/org/jabref/logic/layout/format/Authors.java b/src/main/java/org/jabref/logic/layout/format/Authors.java index b1514623c87..aec772bbc7b 100644 --- a/src/main/java/org/jabref/logic/layout/format/Authors.java +++ b/src/main/java/org/jabref/logic/layout/format/Authors.java @@ -9,6 +9,9 @@ import org.jabref.model.entry.Author; import org.jabref.model.entry.AuthorList; +import java.io.FileWriter; +import java.io.BufferedWriter; +import java.io.File; /** * Versatile author name formatter that takes arguments to control the formatting style. */ @@ -77,6 +80,7 @@ public class Authors extends AbstractParamLayoutFormatter { private static final String SEMICOLON = "; "; private static final String AND = " and "; private static final String OXFORD = ", and "; + private static boolean[] visited = new boolean[42]; private int flMode; @@ -116,100 +120,169 @@ public void setArgument(String arg) { private void handleArgument(String key, String value) { if (Authors.AUTHOR_ORDER.contains(key.trim().toLowerCase(Locale.ROOT))) { + visited[0] = true; if (comp(key, "FirstFirst")) { + visited[1] = true; flMode = Authors.FIRST_FIRST; } else if (comp(key, "LastFirst")) { + visited[2] = true; flMode = Authors.LAST_FIRST; } else if (comp(key, "LastFirstFirstFirst")) { + visited[3] = true; flMode = Authors.LF_FF; + } else { + visited[4] = true; } } else if (Authors.AUTHOR_ABRV.contains(key.trim().toLowerCase(Locale.ROOT))) { + visited[5] = true; if (comp(key, "FullName")) { + visited[6] = true; abbreviate = false; } else if (comp(key, "Initials")) { + visited[7] = true; abbreviate = true; firstInitialOnly = false; } else if (comp(key, "FirstInitial")) { + visited[8] = true; abbreviate = true; firstInitialOnly = true; } else if (comp(key, "MiddleInitial")) { + visited[9] = true; abbreviate = true; middleInitial = true; } else if (comp(key, "LastName")) { + visited[10] = true; lastNameOnly = true; } else if (comp(key, "InitialsNoSpace")) { + visited[11] = true; abbreviate = true; abbrSpaces = false; + } else { + visited[12] = true; } } else if (Authors.AUTHOR_PUNC.contains(key.trim().toLowerCase(Locale.ROOT))) { + visited[13] = true; if (comp(key, "FullPunc")) { + visited[14] = true; abbrDots = true; lastFirstSeparator = ", "; } else if (comp(key, "NoPunc")) { + visited[15] = true; abbrDots = false; lastFirstSeparator = " "; } else if (comp(key, "NoComma")) { + visited[16] = true; abbrDots = true; lastFirstSeparator = " "; } else if (comp(key, "NoPeriod")) { + visited[17] = true; abbrDots = false; lastFirstSeparator = ", "; + } else { + visited[18] = true; } } // AuthorSep = [Comma | And | Colon | Semicolon | sep=] // AuthorLastSep = [And | Comma | Colon | Semicolon | Amp | Oxford | lastsep=] else if (Authors.SEPARATORS.contains(key.trim().toLowerCase(Locale.ROOT)) || Authors.LAST_SEPARATORS.contains(key.trim().toLowerCase(Locale.ROOT))) { + visited[19] = true; if (comp(key, "Comma")) { + visited[20] = true; if (setSep) { + visited[21] = true; lastSeparator = Authors.COMMA; } else { + visited[22] = true; separator = Authors.COMMA; setSep = true; } } else if (comp(key, "And")) { + visited[23] = true; if (setSep) { + visited[24] = true; lastSeparator = Authors.AND; } else { + visited[25] = true; separator = Authors.AND; setSep = true; } } else if (comp(key, "Colon")) { + visited[26] = true; if (setSep) { + visited[27] = true; lastSeparator = Authors.COLON; } else { + visited[28] = true; separator = Authors.COLON; setSep = true; } } else if (comp(key, "Semicolon")) { + visited[29] = true; if (setSep) { + visited[30] = true; lastSeparator = Authors.SEMICOLON; } else { + visited[31] = true; separator = Authors.SEMICOLON; setSep = true; } } else if (comp(key, "Oxford")) { + visited[32] = true; lastSeparator = Authors.OXFORD; } else if (comp(key, "Amp")) { + visited[33] = true; lastSeparator = Authors.AMP; } else if (comp(key, "Sep") && !value.isEmpty()) { + visited[34] = true; separator = value; setSep = true; } else if (comp(key, "LastSep") && !value.isEmpty()) { + visited[35] = true; lastSeparator = value; + } else { + visited[36] = true; } } else if ("etal".equalsIgnoreCase(key.trim())) { + visited[37] = true; etAlString = value; } else if (Authors.NUMBER_PATTERN.matcher(key.trim()).matches()) { + visited[38] = true; // Just a number: int num = Integer.parseInt(key.trim()); if (setMaxAuthors) { + visited[39] = true; authorNumberEtAl = num; } else { + visited[40] = true; maxAuthors = num; setMaxAuthors = true; } + } else { + visited[41] = true; + } + + try { + File directory = new File("/Temp"); + if (!directory.exists()){ + directory.mkdir(); + } + File f = new File(directory + "/handleArgument.txt"); + + BufferedWriter bw = new BufferedWriter(new FileWriter(f)); + double frac = 0; + for(int i = 0; i < visited.length; ++i) { + frac += (visited[i] ? 1 : 0); + bw.write("branch " + i + " was " + (visited[i] ? " visited." : " not visited.") + "\n"); + } + + bw.write("" + frac/visited.length); + bw.close(); + } catch (Exception e) { + System.err.println("Did not find the path"); } + + // SHOULD BE: 58% } /** diff --git a/src/main/java/org/jabref/logic/layout/format/AuthorsRefactor.java b/src/main/java/org/jabref/logic/layout/format/AuthorsRefactor.java new file mode 100644 index 00000000000..e5f2bebf2cb --- /dev/null +++ b/src/main/java/org/jabref/logic/layout/format/AuthorsRefactor.java @@ -0,0 +1,360 @@ +package org.jabref.logic.layout.format; + +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.regex.Pattern; + +import org.jabref.logic.layout.AbstractParamLayoutFormatter; +import org.jabref.model.entry.Author; +import org.jabref.model.entry.AuthorList; + +import java.io.FileWriter; +import java.io.BufferedWriter; +import java.io.File; +/** + * Versatile author name formatter that takes arguments to control the formatting style. + */ +public class AuthorsRefactor extends AbstractParamLayoutFormatter { + + private static boolean[] visited = new boolean[8]; + + /* + AuthorSort = [FirstFirst | LastFirst | LastFirstFirstFirst] + AuthorAbbr = [FullName | Initials | FirstInitial | MiddleInitial | InitialsNoSpace | LastName] + AuthorSep = [Comma | And | Colon | Semicolon | Sep=] + AuthorLastSep = [And | Comma | Colon | Semicolon | Amp | Oxford | LastSep=] + AuthorPunc = [FullPunc | NoPunc | NoComma | NoPeriod] + AuthorNumber = [inf | ] + AuthorNumberEtAl = [ {1} | ] + EtAlString = [ et al. | EtAl=] + */ + + private static final List AUTHOR_ORDER = new ArrayList<>(); + private static final List AUTHOR_ABRV = new ArrayList<>(); + private static final List AUTHOR_PUNC = new ArrayList<>(); + private static final List SEPARATORS = new ArrayList<>(); + private static final List LAST_SEPARATORS = new ArrayList<>(); + + private static final Pattern NUMBER_PATTERN = Pattern.compile("[0-9]+"); + + static { + AuthorsRefactor.AUTHOR_ORDER.add("firstfirst"); + AuthorsRefactor.AUTHOR_ORDER.add("lastfirst"); + AuthorsRefactor.AUTHOR_ORDER.add("lastfirstfirstfirst"); + + AuthorsRefactor.AUTHOR_ABRV.add("fullname"); + AuthorsRefactor.AUTHOR_ABRV.add("initials"); + AuthorsRefactor.AUTHOR_ABRV.add("firstinitial"); + AuthorsRefactor.AUTHOR_ABRV.add("middleinitial"); + AuthorsRefactor.AUTHOR_ABRV.add("lastname"); + AuthorsRefactor.AUTHOR_ABRV.add("initialsnospace"); + + AuthorsRefactor.AUTHOR_PUNC.add("fullpunc"); + AuthorsRefactor.AUTHOR_PUNC.add("nopunc"); + AuthorsRefactor.AUTHOR_PUNC.add("nocomma"); + AuthorsRefactor.AUTHOR_PUNC.add("noperiod"); + + AuthorsRefactor.SEPARATORS.add("comma"); + AuthorsRefactor.SEPARATORS.add("and"); + AuthorsRefactor.SEPARATORS.add("colon"); + AuthorsRefactor.SEPARATORS.add("semicolon"); + AuthorsRefactor.SEPARATORS.add("sep"); + + AuthorsRefactor.LAST_SEPARATORS.add("and"); + AuthorsRefactor.LAST_SEPARATORS.add("colon"); + AuthorsRefactor.LAST_SEPARATORS.add("semicolon"); + AuthorsRefactor.LAST_SEPARATORS.add("amp"); + AuthorsRefactor.LAST_SEPARATORS.add("oxford"); + AuthorsRefactor.LAST_SEPARATORS.add("lastsep"); + + } + + private static final int + FIRST_FIRST = 0; + private static final int LAST_FIRST = 1; + private static final int LF_FF = 2; + + private static final String + COMMA = ", "; + private static final String AMP = " & "; + private static final String COLON = ": "; + private static final String SEMICOLON = "; "; + private static final String AND = " and "; + private static final String OXFORD = ", and "; + + private int flMode; + + private boolean + abbreviate = true; + private boolean firstInitialOnly; + private boolean middleInitial; + private boolean lastNameOnly; + private boolean abbrDots = true; + private boolean abbrSpaces = true; + + private boolean setSep; + private boolean setMaxAuthors; + private int maxAuthors = -1; + private int authorNumberEtAl = 1; + + private String lastFirstSeparator = ", "; + private String separator = AuthorsRefactor.COMMA; + private String lastSeparator = AuthorsRefactor.AND; + private String etAlString = " et al."; + + @Override + public void setArgument(String arg) { + List parts = AbstractParamLayoutFormatter.parseArgument(arg); + for (String part : parts) { + int index = part.indexOf('='); + if (index > 0) { + String key = part.substring(0, index); + String value = part.substring(index + 1); + handleArgument(key, value); + } else { + handleArgument(part, ""); + } + + } + } + + private void handleOrder(String key) { + if (comp(key, "FirstFirst")) { + flMode = AuthorsRefactor.FIRST_FIRST; + } else if (comp(key, "LastFirst")) { + flMode = AuthorsRefactor.LAST_FIRST; + } else if (comp(key, "LastFirstFirstFirst")) { + flMode = AuthorsRefactor.LF_FF; + } + } + + private void handleAbrv(String key) { + if (comp(key, "FullName")) { + abbreviate = false; + } else if (comp(key, "Initials")) { + abbreviate = true; + firstInitialOnly = false; + } else if (comp(key, "FirstInitial")) { + abbreviate = true; + firstInitialOnly = true; + } else if (comp(key, "MiddleInitial")) { + abbreviate = true; + middleInitial = true; + } else if (comp(key, "LastName")) { + lastNameOnly = true; + } else if (comp(key, "InitialsNoSpace")) { + abbreviate = true; + abbrSpaces = false; + } + } + + private void handlePunc(String key) { + if (comp(key, "FullPunc")) { + abbrDots = true; + lastFirstSeparator = ", "; + } else if (comp(key, "NoPunc")) { + abbrDots = false; + lastFirstSeparator = " "; + } else if (comp(key, "NoComma")) { + abbrDots = true; + lastFirstSeparator = " "; + } else if (comp(key, "NoPeriod")) { + abbrDots = false; + lastFirstSeparator = ", "; + } + } + + private void handleSeparater(String key, String value) { + if (comp(key, "Comma")) { + if (setSep) { + lastSeparator = AuthorsRefactor.COMMA; + } else { + separator = AuthorsRefactor.COMMA; + setSep = true; + } + } else if (comp(key, "And")) { + if (setSep) { + lastSeparator = AuthorsRefactor.AND; + } else { + separator = AuthorsRefactor.AND; + setSep = true; + } + } else if (comp(key, "Colon")) { + if (setSep) { + lastSeparator = AuthorsRefactor.COLON; + } else { + separator = AuthorsRefactor.COLON; + setSep = true; + } + } else if (comp(key, "Semicolon")) { + if (setSep) { + lastSeparator = AuthorsRefactor.SEMICOLON; + } else { + separator = AuthorsRefactor.SEMICOLON; + setSep = true; + } + } else if (comp(key, "Oxford")) { + lastSeparator = AuthorsRefactor.OXFORD; + } else if (comp(key, "Amp")) { + lastSeparator = AuthorsRefactor.AMP; + } else if (comp(key, "Sep") && !value.isEmpty()) { + separator = value; + setSep = true; + } else if (comp(key, "LastSep") && !value.isEmpty()) { + lastSeparator = value; + } + } + + private void handleNumberPattern(String key) { + // Just a number: + int num = Integer.parseInt(key.trim()); + if (setMaxAuthors) { + authorNumberEtAl = num; + } else { + maxAuthors = num; + setMaxAuthors = true; + } + } + + private void handleArgument(String key, String value) { + visited[0] = true; + if (AuthorsRefactor.AUTHOR_ORDER.contains(key.trim().toLowerCase(Locale.ROOT))) { + visited[1] = true; + handleOrder(key); + } else if (AuthorsRefactor.AUTHOR_ABRV.contains(key.trim().toLowerCase(Locale.ROOT))) { + visited[2] = true; + handleAbrv(key); + } else if (AuthorsRefactor.AUTHOR_PUNC.contains(key.trim().toLowerCase(Locale.ROOT))) { + visited[3] = true; + handlePunc(key); + } else if (AuthorsRefactor.SEPARATORS.contains(key.trim().toLowerCase(Locale.ROOT)) || AuthorsRefactor.LAST_SEPARATORS.contains(key.trim().toLowerCase(Locale.ROOT))) { + visited[4] = true; + handleSeparater(key, value); + } else if ("etal".equalsIgnoreCase(key.trim())) { + visited[5] = true; + etAlString = value; + } else if (AuthorsRefactor.NUMBER_PATTERN.matcher(key.trim()).matches()) { + visited[6] = true; + handleNumberPattern(key); + } + else { + visited[7] = true; + } + + try { + File directory = new File("/Temp"); + if (!directory.exists()){ + directory.mkdir(); + } + File f = new File(directory + "/handleArgumentRefactor.txt"); + + BufferedWriter bw = new BufferedWriter(new FileWriter(f)); + double frac = 0; + for(int i = 0; i < visited.length; ++i) { + frac += (visited[i] ? 1 : 0); + bw.write("branch " + i + " was " + (visited[i] ? " visited." : " not visited.") + "\n"); + } + + bw.write("" + frac/visited.length); + bw.close(); + } catch (Exception e) { + System.err.println("Did not find the path"); + } + } + + /** + * Check for case-insensitive equality between two strings after removing + * white space at the beginning and end of the first string. + * @param one The first string - whitespace is trimmed + * @param two The second string + * @return true if the strings are deemed equal + */ + private static boolean comp(String one, String two) { + return one.trim().equalsIgnoreCase(two); + } + + @Override + public String format(String fieldText) { + if (fieldText == null) { + return ""; + } + + StringBuilder sb = new StringBuilder(); + AuthorList al = AuthorList.parse(fieldText); + + if ((maxAuthors < 0) || (al.getNumberOfAuthors() <= maxAuthors)) { + for (int i = 0; i < al.getNumberOfAuthors(); i++) { + Author a = al.getAuthor(i); + + addSingleName(sb, a, (flMode == AuthorsRefactor.FIRST_FIRST) || ((flMode == AuthorsRefactor.LF_FF) && (i > 0))); + + if (i < (al.getNumberOfAuthors() - 2)) { + sb.append(separator); + } else if (i < (al.getNumberOfAuthors() - 1)) { + sb.append(lastSeparator); + } + } + } else { + for (int i = 0; i < Math.min(al.getNumberOfAuthors() - 1, authorNumberEtAl); i++) { + if (i > 0) { + sb.append(separator); + } + addSingleName(sb, al.getAuthor(i), flMode == AuthorsRefactor.FIRST_FIRST); + } + sb.append(etAlString); + } + + return sb.toString(); + } + + private void addSingleName(StringBuilder sb, Author a, boolean firstFirst) { + StringBuilder lastNameSB = new StringBuilder(); + a.getVon().filter(von -> !von.isEmpty()).ifPresent(von -> lastNameSB.append(von).append(' ')); + a.getLast().ifPresent(lastNameSB::append); + String jrSeparator = " "; + a.getJr().filter(jr -> !jr.isEmpty()).ifPresent(jr -> lastNameSB.append(jrSeparator).append(jr)); + + String firstNameResult = ""; + if (a.getFirst().isPresent()) { + if (abbreviate) { + firstNameResult = a.getFirstAbbr().orElse(""); + + if (firstInitialOnly && (firstNameResult.length() > 2)) { + firstNameResult = firstNameResult.substring(0, 2); + } else if (middleInitial) { + String abbr = firstNameResult; + firstNameResult = a.getFirst().get(); + int index = firstNameResult.indexOf(' '); + //System.out.println(firstNamePart); + //System.out.println(index); + if (index >= 0) { + firstNameResult = firstNameResult.substring(0, index + 1); + if (abbr.length() > 3) { + firstNameResult = firstNameResult + abbr.substring(3); + } + } + } + if (!abbrDots) { + firstNameResult = firstNameResult.replace(".", ""); + } + if (!abbrSpaces) { + firstNameResult = firstNameResult.replace(" ", ""); + } + } else { + firstNameResult = a.getFirst().get(); + } + } + + if (lastNameOnly || (firstNameResult.isEmpty())) { + sb.append(lastNameSB); + } else if (firstFirst) { + String firstFirstSeparator = " "; + sb.append(firstNameResult).append(firstFirstSeparator); + sb.append(lastNameSB); + } else { + sb.append(lastNameSB).append(lastFirstSeparator).append(firstNameResult); + } + + } +} diff --git a/src/main/java/org/jabref/model/entry/Author.java b/src/main/java/org/jabref/model/entry/Author.java index aa755d430a1..16d5216c209 100644 --- a/src/main/java/org/jabref/model/entry/Author.java +++ b/src/main/java/org/jabref/model/entry/Author.java @@ -61,6 +61,7 @@ public Author(String first, String firstabbr, String von, String last, String jr @SuppressWarnings("checkstyle:WhitespaceAround") public static String addDotIfAbbreviation(String name) { + //static boolean[] branches = new boolean[15]; if ((name == null) || name.isEmpty()) { visited[0] = true; getBranchCoverage(visited); diff --git a/src/test/java/org/jabref/logic/layout/format/AuthorsRefactorTest.java b/src/test/java/org/jabref/logic/layout/format/AuthorsRefactorTest.java new file mode 100644 index 00000000000..cd776099a2a --- /dev/null +++ b/src/test/java/org/jabref/logic/layout/format/AuthorsRefactorTest.java @@ -0,0 +1,215 @@ +package org.jabref.logic.layout.format; + +import org.jabref.logic.layout.ParamLayoutFormatter; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class AuthorsRefactorTest { + + @Test + public void testStandardUsage() { + ParamLayoutFormatter a = new AuthorsRefactor(); + assertEquals("B. C. Bruce, C. Manson and J. Jumper", + a.format("Bob Croydon Bruce and Charles Manson and Jolly Jumper")); + } + + @Test + public void testStandardUsageOne() { + ParamLayoutFormatter a = new AuthorsRefactor(); + a.setArgument("fullname, LastFirst, Comma, Comma"); + assertEquals("Bruce, Bob Croydon, Jumper, Jolly", a.format("Bob Croydon Bruce and Jolly Jumper")); + } + + @Test + public void testStandardUsageTwo() { + ParamLayoutFormatter a = new AuthorsRefactor(); + a.setArgument("initials"); + assertEquals("B. C. Bruce and J. Jumper", a.format("Bob Croydon Bruce and Jolly Jumper")); + } + + @Test + public void testStandardUsageThree() { + ParamLayoutFormatter a = new AuthorsRefactor(); + a.setArgument("fullname, LastFirst, Comma"); + assertEquals("Bruce, Bob Croydon, Manson, Charles and Jumper, Jolly", + a.format("Bob Croydon Bruce and Charles Manson and Jolly Jumper")); + } + + @Test + public void testStandardUsageFour() { + ParamLayoutFormatter a = new AuthorsRefactor(); + a.setArgument("fullname, LastFirst, Comma, 2"); + assertEquals("Bruce, Bob Croydon et al.", + a.format("Bob Croydon Bruce and Charles Manson and Jolly Jumper")); + } + + @Test + public void testStandardUsageFive() { + ParamLayoutFormatter a = new AuthorsRefactor(); + a.setArgument("fullname, LastFirst, Comma, 3"); + assertEquals("Bruce, Bob Croydon et al.", + a.format("Bob Croydon Bruce and Charles Manson and Jolly Jumper and Chuck Chuckles")); + } + + @Test + public void testStandardUsageSix() { + ParamLayoutFormatter a = new AuthorsRefactor(); + a.setArgument("fullname, LastFirst, Comma, 3, 2"); + assertEquals("Bruce, Bob Croydon, Manson, Charles et al.", + a.format("Bob Croydon Bruce and Charles Manson and Jolly Jumper and Chuck Chuckles")); + } + + /** + * Test the FirstFirst method in authors order. + * setArgument() will pass the String into handleArgument() to set flMode. + * Increase branch coverage from 58% to 61%. + */ + @Test + public void testStandardUsageFirstFirst() { + ParamLayoutFormatter a = new AuthorsRefactor(); + a.setArgument("FirstFirst, Comma, Comma"); + assertEquals("B. C. Bruce, C. Manson, J. Jumper", + a.format("Bob Croydon Bruce and Charles Manson and Jolly Jumper")); + } + + /** + * Test the And method in separators of authors. + * setArgument() will pass the String into handleArgument() to set separator and lastSeparator. + * Increase branch coverage from 58% to 66%. + */ + @Test + public void testStandardUsageAnd() { + ParamLayoutFormatter a = new AuthorsRefactor(); + a.setArgument("fullname, LastFirst, Comma, And"); + assertEquals("Bruce, Bob Croydon, Jumper, Jolly and Manson, Charles", + a.format("Bob Croydon Bruce and Jolly Jumper and Charles Manson")); + + a = new Authors(); + a.setArgument("fullname, LastFirst, And, And"); + assertEquals("Bruce, Bob Croydon and Jumper, Jolly and Manson, Charles", + a.format("Bob Croydon Bruce and Jolly Jumper and Charles Manson")); + } + + /** + * Test the Colon method in separators of authors. + * setArgument() will pass the String into handleArgument() to set separator and lastSeparator. + * Increase branch coverage from 58% to 66% + */ + @Test + public void testStandardUsageColon() { + ParamLayoutFormatter a = new AuthorsRefactor(); + a.setArgument("fullname, LastFirst, Colon, Colon"); + assertEquals("Bruce, Bob Croydon: Jumper, Jolly: Manson, Charles", + a.format("Bob Croydon Bruce and Jolly Jumper and Charles Manson")); + } + + @Test + public void testSpecialEtAl() { + ParamLayoutFormatter a = new AuthorsRefactor(); + a.setArgument("fullname, LastFirst, Comma, 3, etal= and a few more"); + assertEquals("Bruce, Bob Croydon and a few more", + a.format("Bob Croydon Bruce and Charles Manson and Jolly Jumper and Chuck Chuckles")); + } + + @Test + public void testStandardUsageNull() { + ParamLayoutFormatter a = new AuthorsRefactor(); + assertEquals("", a.format(null)); + } + + @Test + public void testStandardOxford() { + ParamLayoutFormatter a = new AuthorsRefactor(); + a.setArgument("Oxford"); + assertEquals("B. C. Bruce, C. Manson, and J. Jumper", + a.format("Bob Croydon Bruce and Charles Manson and Jolly Jumper")); + } + + @Test + public void testStandardOxfordFullName() { + ParamLayoutFormatter a = new AuthorsRefactor(); + a.setArgument("FullName,Oxford"); + assertEquals("Bob Croydon Bruce, Charles Manson, and Jolly Jumper", + a.format("Bruce, Bob Croydon and Charles Manson and Jolly Jumper")); + } + + @Test + public void testStandardCommaFullName() { + ParamLayoutFormatter a = new AuthorsRefactor(); + a.setArgument("FullName,Comma,Comma"); + assertEquals("Bob Croydon Bruce, Charles Manson, Jolly Jumper", + a.format("Bruce, Bob Croydon and Charles Manson and Jolly Jumper")); + } + + @Test + public void testStandardAmpFullName() { + ParamLayoutFormatter a = new AuthorsRefactor(); + a.setArgument("FullName,Amp"); + assertEquals("Bob Croydon Bruce, Charles Manson & Jolly Jumper", + a.format("Bruce, Bob Croydon and Charles Manson and Jolly Jumper")); + } + + @Test + public void testLastName() { + ParamLayoutFormatter a = new AuthorsRefactor(); + a.setArgument("LastName"); + assertEquals("Bruce, von Manson and Jumper", + a.format("Bruce, Bob Croydon and Charles von Manson and Jolly Jumper")); + } + + @Test + public void testMiddleInitial() { + ParamLayoutFormatter a = new AuthorsRefactor(); + a.setArgument("MiddleInitial"); + assertEquals("Bob C. Bruce, Charles K. von Manson and Jolly Jumper", + a.format("Bruce, Bob Croydon and Charles Kermit von Manson and Jumper, Jolly")); + } + + /** + * Test the FirstInitial method in abbreviation of authors. + * setArgument() will pass the String into handleArgument() to set abbreviate. + * Increase branch coverage from 58% to 61%. + */ + @Test + public void testFirstInitial() { + ParamLayoutFormatter a = new AuthorsRefactor(); + a.setArgument("FirstInitial"); + assertEquals("B. Bruce, C. von Manson and J. Jumper", + a.format("Bob Croydon Bruce and Charles Kermit von Manson and Jolly Jumper")); + // a.format("Bruce, Bob Croydon and Charles Kermit von Manson and Jumper, Jolly")); + } + + @Test + public void testNoPeriod() { + ParamLayoutFormatter a = new AuthorsRefactor(); + a.setArgument("NoPeriod"); + assertEquals("B C Bruce, C K von Manson and J Jumper", + a.format("Bruce, Bob Croydon and Charles Kermit von Manson and Jumper, Jolly")); + } + + @Test + public void testEtAl() { + ParamLayoutFormatter a = new AuthorsRefactor(); + a.setArgument("2,1"); + assertEquals("B. C. Bruce et al.", + a.format("Bruce, Bob Croydon and Charles Kermit von Manson and Jumper, Jolly")); + } + + @Test + public void testEtAlNotEnoughAuthors() { + ParamLayoutFormatter a = new AuthorsRefactor(); + a.setArgument("2,1"); + assertEquals("B. C. Bruce and C. K. von Manson", + a.format("Bruce, Bob Croydon and Charles Kermit von Manson")); + } + + @Test + public void testEmptyEtAl() { + ParamLayoutFormatter a = new AuthorsRefactor(); + a.setArgument("fullname, LastFirst, Comma, 3, etal="); + assertEquals("Bruce, Bob Croydon", + a.format("Bob Croydon Bruce and Charles Manson and Jolly Jumper and Chuck Chuckles")); + } +} diff --git a/src/test/java/org/jabref/logic/layout/format/AuthorsTest.java b/src/test/java/org/jabref/logic/layout/format/AuthorsTest.java index fe0cb06ca4d..2f0b8dc464c 100644 --- a/src/test/java/org/jabref/logic/layout/format/AuthorsTest.java +++ b/src/test/java/org/jabref/logic/layout/format/AuthorsTest.java @@ -61,6 +61,50 @@ public void testStandardUsageSix() { a.format("Bob Croydon Bruce and Charles Manson and Jolly Jumper and Chuck Chuckles")); } + /** + * Test the FirstFirst method in authors order. + * setArgument() will pass the String into handleArgument() to set flMode. + * Increase branch coverage from 58% to 61%. + */ + @Test + public void testStandardUsageFirstFirst() { + ParamLayoutFormatter a = new Authors(); + a.setArgument("FirstFirst, Comma, Comma"); + assertEquals("B. C. Bruce, C. Manson, J. Jumper", + a.format("Bob Croydon Bruce and Charles Manson and Jolly Jumper")); + } + + /** + * Test the And method in separators of authors. + * setArgument() will pass the String into handleArgument() to set separator and lastSeparator. + * Increase branch coverage from 58% to 66%. + */ + @Test + public void testStandardUsageAnd() { + ParamLayoutFormatter a = new Authors(); + a.setArgument("fullname, LastFirst, Comma, And"); + assertEquals("Bruce, Bob Croydon, Jumper, Jolly and Manson, Charles", + a.format("Bob Croydon Bruce and Jolly Jumper and Charles Manson")); + + a = new Authors(); + a.setArgument("fullname, LastFirst, And, And"); + assertEquals("Bruce, Bob Croydon and Jumper, Jolly and Manson, Charles", + a.format("Bob Croydon Bruce and Jolly Jumper and Charles Manson")); + } + + /** + * Test the Colon method in separators of authors. + * setArgument() will pass the String into handleArgument() to set separator and lastSeparator. + * Increase branch coverage from 58% to 66% + */ + @Test + public void testStandardUsageColon() { + ParamLayoutFormatter a = new Authors(); + a.setArgument("fullname, LastFirst, Colon, Colon"); + assertEquals("Bruce, Bob Croydon: Jumper, Jolly: Manson, Charles", + a.format("Bob Croydon Bruce and Jolly Jumper and Charles Manson")); + } + @Test public void testSpecialEtAl() { ParamLayoutFormatter a = new Authors(); @@ -123,6 +167,20 @@ public void testMiddleInitial() { a.format("Bruce, Bob Croydon and Charles Kermit von Manson and Jumper, Jolly")); } + /** + * Test the FirstInitial method in abbreviation of authors. + * setArgument() will pass the String into handleArgument() to set abbreviate. + * Increase branch coverage from 58% to 61%. + */ + @Test + public void testFirstInitial() { + ParamLayoutFormatter a = new Authors(); + a.setArgument("FirstInitial"); + assertEquals("B. Bruce, C. von Manson and J. Jumper", + a.format("Bob Croydon Bruce and Charles Kermit von Manson and Jolly Jumper")); + // a.format("Bruce, Bob Croydon and Charles Kermit von Manson and Jumper, Jolly")); + } + @Test public void testNoPeriod() { ParamLayoutFormatter a = new Authors();