Skip to content

Commit

Permalink
Added Branch Coverage Tool for addDotIfAbbreviation()
Browse files Browse the repository at this point in the history
* Added our own branch coverage tool to the function addDotIfAbbreviation
  • Loading branch information
christinerosquist committed Feb 18, 2020
1 parent faa2bd7 commit 0824b92
Showing 1 changed file with 79 additions and 1 deletion.
80 changes: 79 additions & 1 deletion src/main/java/org/jabref/model/entry/Author.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.jabref.model.entry;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Objects;
import java.util.Optional;

Expand All @@ -15,6 +18,7 @@
* all other methods are provided for completeness.
*/
public class Author {
static boolean[] visited = new boolean[33];

private final String firstPart;

Expand Down Expand Up @@ -55,90 +59,164 @@ public Author(String first, String firstabbr, String von, String last, String jr
jrPart = removeStartAndEndBraces(jr);
}

@SuppressWarnings("checkstyle:WhitespaceAround")
public static String addDotIfAbbreviation(String name) {
if ((name == null) || name.isEmpty()) {
visited[0] = true;
getBranchCoverage(visited);
return name;
} else {
visited[1] = true;
}
// If only one character (uppercase letter), add a dot and return immediately:
if ((name.length() == 1) && Character.isLetter(name.charAt(0)) &&
Character.isUpperCase(name.charAt(0))) {
visited[2] = true;
getBranchCoverage(visited);
return name + ".";
} else {
visited[3] = true;
}

StringBuilder sb = new StringBuilder();
char lastChar = name.charAt(0);
for (int i = 0; i < name.length(); i++) {
visited[4] = true;
if (i > 0) {
visited[5] = true;
lastChar = name.charAt(i - 1);
} else {
visited[6] = true;
}
char currentChar = name.charAt(i);
sb.append(currentChar);

if (currentChar == '.') {
visited[7] = true;
// A.A. -> A. A.
if (((i + 1) < name.length()) && Character.isUpperCase(name.charAt(i + 1))) {
visited[8] = true;
sb.append(' ');
}
else {
visited[9] = true;
}
} else {
visited[10] = true;
}

boolean currentIsUppercaseLetter = Character.isLetter(currentChar) && Character.isUpperCase(currentChar);
if (!currentIsUppercaseLetter) {
visited[11] = true;
// No uppercase letter, hence nothing to do
continue;
} else {
visited[12] = true;
}

boolean lastIsLowercaseLetter = Character.isLetter(lastChar) && Character.isLowerCase(lastChar);
if (lastIsLowercaseLetter) {
visited[13] = true;
// previous character was lowercase (probably an acronym like JabRef) -> don't change anything
continue;
} else {
visited[14] = true;
}

if ((i + 1) >= name.length()) {
visited[15] = true;
// Current character is last character in input, so append dot
sb.append('.');
continue;
} else {
visited[16] = true;
}

char nextChar = name.charAt(i + 1);
if ('-' == nextChar) {
visited[17] = true;
// A-A -> A.-A.
sb.append(".");
continue;
} else {
visited[18] = true;
}
if ('.' == nextChar) {
visited[19] = true;
// Dot already there, so nothing to do
continue;
} else {
visited[20] = true;
}

// AA -> A. A.
// Only append ". " if the rest of the 'word' is uppercase
boolean nextWordIsUppercase = true;
char furtherChar = Character.MIN_VALUE;
for (int j = i + 1; j < name.length(); j++) {
visited[21] = true;
furtherChar = name.charAt(j);
if (Character.isWhitespace(furtherChar) || (furtherChar == '-') || (furtherChar == '~') || (furtherChar == '.')) {
visited[22] = true;
// end of word
break;
} else {
visited[23] = true;
}

boolean furtherIsUppercaseLetter = Character.isLetter(furtherChar) && Character.isUpperCase(furtherChar);
if (!furtherIsUppercaseLetter) {
visited[24] = true;
nextWordIsUppercase = false;
break;
} else {
visited[25] = true;
}

}
if (!(name.length() > i + 1)) {
visited[31] = true;
}
if (nextWordIsUppercase) {
visited[26] = true;
if (Character.isWhitespace(furtherChar)) {
visited[27] = true;
sb.append(".");
} else {
visited[28] = true;
sb.append(". ");
}
} else {
visited[29] = true;
}
}

if (name.length() == 0) {
visited[32] = true;
}
getBranchCoverage(visited);
return sb.toString().trim();
}

private static void getBranchCoverage(boolean[] visited) {
try {
File directory = new File("/Temp");
if (!directory.exists()) {
directory.mkdir();
}
File f = new File(directory + "/addDotIfAbbreviation.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("ye");
}
}

@Override
public int hashCode() {
return Objects.hash(firstAbbr, firstPart, jrPart, lastPart, vonPart);
Expand Down

0 comments on commit 0824b92

Please sign in to comment.