diff --git a/src/main/java/org/jabref/gui/fieldeditors/UrlEditor.java b/src/main/java/org/jabref/gui/fieldeditors/UrlEditor.java index 08d22698a8c..270770a2ea1 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/UrlEditor.java +++ b/src/main/java/org/jabref/gui/fieldeditors/UrlEditor.java @@ -12,7 +12,7 @@ import org.jabref.gui.DialogService; import org.jabref.gui.autocompleter.AutoCompleteSuggestionProvider; import org.jabref.gui.fieldeditors.contextmenu.EditorMenus; -import org.jabref.logic.formatter.bibtexfields.CleanupURLFormatter; +import org.jabref.logic.formatter.bibtexfields.CleanupUrlFormatter; import org.jabref.logic.formatter.bibtexfields.TrimWhitespaceFormatter; import org.jabref.logic.integrity.FieldCheckers; import org.jabref.model.entry.BibEntry; @@ -34,12 +34,11 @@ public UrlEditor(Field field, DialogService dialogService, AutoCompleteSuggestio .load(); textArea.textProperty().bindBidirectional(viewModel.textProperty()); - Supplier> contextMenuSupplier = EditorMenus.getCleanupURLMenu(textArea); + Supplier> contextMenuSupplier = EditorMenus.getCleanupUrlMenu(textArea); textArea.addToContextMenu(contextMenuSupplier); - // init paste handler for URLEditor to format pasted url link in textArea - textArea.setPasteActionHandler(() -> textArea.setText(new CleanupURLFormatter().format(new TrimWhitespaceFormatter().format(textArea.getText())))); - + // init paste handler for UrlEditor to format pasted url link in textArea + textArea.setPasteActionHandler(() -> textArea.setText(new CleanupUrlFormatter().format(new TrimWhitespaceFormatter().format(textArea.getText())))); new EditorValidator(preferences).configureValidation(viewModel.getFieldValidator().getValidationStatus(), textArea); } diff --git a/src/main/java/org/jabref/gui/fieldeditors/contextmenu/EditorMenus.java b/src/main/java/org/jabref/gui/fieldeditors/contextmenu/EditorMenus.java index abdb9700909..b6b8df62bc3 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/contextmenu/EditorMenus.java +++ b/src/main/java/org/jabref/gui/fieldeditors/contextmenu/EditorMenus.java @@ -16,7 +16,7 @@ import org.jabref.gui.actions.ActionFactory; import org.jabref.gui.actions.StandardActions; import org.jabref.gui.edit.CopyDoiUrlAction; -import org.jabref.logic.formatter.bibtexfields.CleanupURLFormatter; +import org.jabref.logic.formatter.bibtexfields.CleanupUrlFormatter; import org.jabref.logic.formatter.bibtexfields.NormalizeNamesFormatter; import org.jabref.logic.l10n.Localization; @@ -91,11 +91,11 @@ public static Supplier> getDOIMenu(TextArea textArea) { * @param textArea text-area that this menu will be connected to * @return menu containing items of the default menu and an item to cleanup a URL */ - public static Supplier> getCleanupURLMenu(TextArea textArea) { + public static Supplier> getCleanupUrlMenu(TextArea textArea) { return () -> { CustomMenuItem cleanupURL = new CustomMenuItem(new Label(Localization.lang("Cleanup URL link"))); cleanupURL.setDisable(textArea.textProperty().isEmpty().get()); - cleanupURL.setOnAction(event -> textArea.setText(new CleanupURLFormatter().format(textArea.getText()))); + cleanupURL.setOnAction(event -> textArea.setText(new CleanupUrlFormatter().format(textArea.getText()))); List menuItems = new ArrayList<>(); menuItems.add(cleanupURL); diff --git a/src/main/java/org/jabref/logic/formatter/Formatters.java b/src/main/java/org/jabref/logic/formatter/Formatters.java index 7aea0a024a6..55a91f7f86c 100644 --- a/src/main/java/org/jabref/logic/formatter/Formatters.java +++ b/src/main/java/org/jabref/logic/formatter/Formatters.java @@ -6,7 +6,7 @@ import java.util.Objects; import java.util.Optional; -import org.jabref.logic.formatter.bibtexfields.CleanupURLFormatter; +import org.jabref.logic.formatter.bibtexfields.CleanupUrlFormatter; import org.jabref.logic.formatter.bibtexfields.ClearFormatter; import org.jabref.logic.formatter.bibtexfields.EscapeUnderscoresFormatter; import org.jabref.logic.formatter.bibtexfields.HtmlToLatexFormatter; @@ -58,7 +58,7 @@ public static List getCaseChangers() { public static List getOthers() { return Arrays.asList( new ClearFormatter(), - new CleanupURLFormatter(), + new CleanupUrlFormatter(), new LatexCleanupFormatter(), new MinifyNameListFormatter(), new NormalizeDateFormatter(), diff --git a/src/main/java/org/jabref/logic/formatter/bibtexfields/CleanupURLFormatter.java b/src/main/java/org/jabref/logic/formatter/bibtexfields/CleanupUrlFormatter.java similarity index 91% rename from src/main/java/org/jabref/logic/formatter/bibtexfields/CleanupURLFormatter.java rename to src/main/java/org/jabref/logic/formatter/bibtexfields/CleanupUrlFormatter.java index e14b717dd64..1821760ab0a 100644 --- a/src/main/java/org/jabref/logic/formatter/bibtexfields/CleanupURLFormatter.java +++ b/src/main/java/org/jabref/logic/formatter/bibtexfields/CleanupUrlFormatter.java @@ -15,9 +15,9 @@ /** * Cleanup URL link */ -public class CleanupURLFormatter extends Formatter { +public class CleanupUrlFormatter extends Formatter { - private static final Log LOGGER = LogFactory.getLog(CleanupURLFormatter.class); + private static final Log LOGGER = LogFactory.getLog(CleanupUrlFormatter.class); // This regexp find "url=" or "to=" parameter in full link and get text after them private static final Pattern PATTERN_URL = Pattern.compile("(?:url|to)=([^&]*)"); @@ -38,15 +38,14 @@ public String format(String value) { Matcher matcher = PATTERN_URL.matcher(value); if (matcher.find()) { - toDecode = matcher.group(1); - + toDecode = matcher.group(1); } try { decodedLink = URLDecoder.decode(toDecode, StandardCharsets.UTF_8.name()); - } - catch (UnsupportedEncodingException e) { + } catch (UnsupportedEncodingException e) { LOGGER.warn("Used unsupported character encoding", e); } + return decodedLink; } @@ -61,6 +60,5 @@ public String getExampleInput() { "rja&uact=8&ved=0ahUKEwjg3ZrB_ZPXAhVGuhoKHYdOBOg4ChAWCCYwAA&url=" + "http%3A%2F%2Fwww.focus.de%2Fgesundheit%2Fratgeber%2Fherz%2Ftest%2" + "Flebenserwartung-werden-sie-100-jahre-alt_aid_363828.html" + "&usg=AOvVaw1G6m2jf-pTHYkXceii4hXU"; - } - + } } diff --git a/src/main/java/org/jabref/logic/formatter/bibtexfields/LatexCleanupFormatter.java b/src/main/java/org/jabref/logic/formatter/bibtexfields/LatexCleanupFormatter.java index 032914146b6..c9bf179d201 100644 --- a/src/main/java/org/jabref/logic/formatter/bibtexfields/LatexCleanupFormatter.java +++ b/src/main/java/org/jabref/logic/formatter/bibtexfields/LatexCleanupFormatter.java @@ -5,6 +5,9 @@ import org.jabref.logic.l10n.Localization; import org.jabref.model.cleanup.Formatter; +/** + * Simplifies LaTeX syntax. {@see org.jabref.logic.layout.format.RemoveLatexCommandsFormatter} for a formatter removing LaTeX commands completely. + */ public class LatexCleanupFormatter extends Formatter { private static final Pattern REMOVE_REDUNDANT = Pattern diff --git a/src/main/java/org/jabref/logic/formatter/bibtexfields/ShortenDOIFormatter.java b/src/main/java/org/jabref/logic/formatter/bibtexfields/ShortenDOIFormatter.java index 75b9ba9a2ab..e689cc962da 100644 --- a/src/main/java/org/jabref/logic/formatter/bibtexfields/ShortenDOIFormatter.java +++ b/src/main/java/org/jabref/logic/formatter/bibtexfields/ShortenDOIFormatter.java @@ -1,7 +1,6 @@ package org.jabref.logic.formatter.bibtexfields; import java.util.Objects; -import java.util.Optional; import org.jabref.logic.importer.util.ShortDOIService; import org.jabref.logic.importer.util.ShortDOIServiceException; @@ -29,22 +28,15 @@ public String getKey() { @Override public String format(String value) { Objects.requireNonNull(value); - - ShortDOIService shortDOIService = new ShortDOIService(); - - Optional doi = Optional.empty(); - - try { - doi = DOI.parse(value); - - if (doi.isPresent()) { - return shortDOIService.getShortDOI(doi.get()).getDOI(); - } - } catch (ShortDOIServiceException e) { - LOGGER.error(e.getMessage(), e); - } - - return value; + return DOI.parse(value) + .map(doi -> { + try { + return new ShortDOIService().getShortDOI(doi).getDOI(); + } catch (ShortDOIServiceException e) { + LOGGER.error(e.getMessage(), e); + return value; + } + }).orElse(value); } @Override diff --git a/src/main/java/org/jabref/logic/importer/fetcher/DBLPFetcher.java b/src/main/java/org/jabref/logic/importer/fetcher/DBLPFetcher.java index d7515a638c4..2e419a8271d 100644 --- a/src/main/java/org/jabref/logic/importer/fetcher/DBLPFetcher.java +++ b/src/main/java/org/jabref/logic/importer/fetcher/DBLPFetcher.java @@ -3,6 +3,7 @@ import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; +import java.util.List; import java.util.Objects; import java.util.Optional; @@ -14,9 +15,13 @@ import org.jabref.logic.importer.Parser; import org.jabref.logic.importer.SearchBasedParserFetcher; import org.jabref.logic.importer.fileformat.BibtexParser; +import org.jabref.logic.layout.LayoutFormatterBasedFormatter; +import org.jabref.logic.layout.format.RemoveLatexCommandsFormatter; import org.jabref.model.cleanup.FieldFormatterCleanup; +import org.jabref.model.cleanup.FieldFormatterCleanups; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.field.InternalField; +import org.jabref.model.entry.field.StandardField; import org.jabref.model.util.DummyFileUpdateMonitor; import org.apache.http.client.utils.URIBuilder; @@ -57,13 +62,15 @@ public Parser getParser() { @Override public void doPostCleanup(BibEntry entry) { DoiCleanup doiCleaner = new DoiCleanup(); - - FieldFormatterCleanup clearTimestampFormatter = new FieldFormatterCleanup(InternalField.TIMESTAMP, - new ClearFormatter()); - doiCleaner.cleanup(entry); - clearTimestampFormatter.cleanup(entry); + FieldFormatterCleanups cleanups = new FieldFormatterCleanups(true, + List.of( + new FieldFormatterCleanup(InternalField.TIMESTAMP, new ClearFormatter()), + // unescape the the contents of the URL field, e.g., some\_url\_part becomes some_url_part + new FieldFormatterCleanup(StandardField.URL, new LayoutFormatterBasedFormatter(new RemoveLatexCommandsFormatter())) + )); + cleanups.applySaveActions(entry); } @Override @@ -75,5 +82,4 @@ public String getName() { public Optional getHelpPage() { return Optional.of(HelpFile.FETCHER_DBLP); } - } diff --git a/src/main/java/org/jabref/logic/layout/Layout.java b/src/main/java/org/jabref/logic/layout/Layout.java index 873fb396de4..25ed99711d1 100644 --- a/src/main/java/org/jabref/logic/layout/Layout.java +++ b/src/main/java/org/jabref/logic/layout/Layout.java @@ -12,9 +12,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * Main class for formatting DOCUMENT ME! - */ public class Layout { private static final Logger LOGGER = LoggerFactory.getLogger(Layout.class); diff --git a/src/main/java/org/jabref/logic/layout/LayoutFormatter.java b/src/main/java/org/jabref/logic/layout/LayoutFormatter.java index 47184094402..25b701b3d12 100644 --- a/src/main/java/org/jabref/logic/layout/LayoutFormatter.java +++ b/src/main/java/org/jabref/logic/layout/LayoutFormatter.java @@ -2,14 +2,12 @@ /** * The LayoutFormatter is used for a Filter design-pattern. - * + *

* Implementing classes have to accept a String and returned a formatted version of it. - * + *

* Example: - * - * "John von Neumann" => "von Neumann, John" - * - * @version 1.2 - Documentation CO + *

+ * "John von Neumann" => "von Neumann, John" */ @FunctionalInterface public interface LayoutFormatter { @@ -17,14 +15,12 @@ public interface LayoutFormatter { /** * Failure Mode: *

- * Formatters should be robust in the sense that they always return some - * relevant string. + * Formatters should be robust in the sense that they always return some relevant string. *

- * If the formatter can detect an invalid input it should return the - * original string otherwise it may simply return a wrong output. + * If the formatter can detect an invalid input it should return the original string otherwise it may simply return + * a wrong output. * - * @param fieldText - * The text to layout. + * @param fieldText The text to layout. * @return The layouted text. */ String format(String fieldText); diff --git a/src/main/java/org/jabref/logic/layout/LayoutFormatterBasedFormatter.java b/src/main/java/org/jabref/logic/layout/LayoutFormatterBasedFormatter.java new file mode 100644 index 00000000000..72762fc8142 --- /dev/null +++ b/src/main/java/org/jabref/logic/layout/LayoutFormatterBasedFormatter.java @@ -0,0 +1,40 @@ +package org.jabref.logic.layout; + +import org.jabref.model.cleanup.Formatter; + +/** + * When having to use a LayoutFormatter as Formatter, this class is helpful. One usecase is {@link org.jabref.model.cleanup.FieldFormatterCleanup} + */ +public class LayoutFormatterBasedFormatter extends Formatter { + + private final LayoutFormatter layoutFormatter; + + public LayoutFormatterBasedFormatter(LayoutFormatter layoutFormatter) { + this.layoutFormatter = layoutFormatter; + } + + @Override + public String getName() { + return layoutFormatter.getClass().getName(); + } + + @Override + public String getKey() { + return layoutFormatter.getClass().getName(); + } + + @Override + public String format(String value) { + return layoutFormatter.format(value); + } + + @Override + public String getDescription() { + return layoutFormatter.getClass().getName(); + } + + @Override + public String getExampleInput() { + return layoutFormatter.getClass().getName(); + } +} diff --git a/src/main/java/org/jabref/logic/layout/LayoutHelper.java b/src/main/java/org/jabref/logic/layout/LayoutHelper.java index 2502b285c7f..dde7ad47e24 100644 --- a/src/main/java/org/jabref/logic/layout/LayoutHelper.java +++ b/src/main/java/org/jabref/logic/layout/LayoutHelper.java @@ -67,27 +67,24 @@ public static void setCurrentGroup(String newGroup) { private void doBracketedField(final int field) throws IOException { StringBuilder buffer = null; - int c; + int currentCharacter; boolean start = false; while (!endOfFile) { - c = read(); + currentCharacter = read(); - if (c == -1) { + if (currentCharacter == -1) { endOfFile = true; - if (buffer != null) { parsedEntries.add(new StringInt(buffer.toString(), field)); } - return; } - if ((c == '{') || (c == '}')) { - if (c == '}') { + if ((currentCharacter == '{') || (currentCharacter == '}')) { + if (currentCharacter == '}') { if (buffer != null) { parsedEntries.add(new StringInt(buffer.toString(), field)); - return; } } else { @@ -98,16 +95,13 @@ private void doBracketedField(final int field) throws IOException { buffer = new StringBuilder(100); } - if (start && (c != '}')) { - buffer.append((char) c); + if (start && (currentCharacter != '}')) { + buffer.append((char) currentCharacter); } } } } - /** - * - */ private void doBracketedOptionField() throws IOException { StringBuilder buffer = null; int c; diff --git a/src/main/java/org/jabref/logic/layout/format/RemoveLatexCommandsFormatter.java b/src/main/java/org/jabref/logic/layout/format/RemoveLatexCommandsFormatter.java index a538bdaf13e..e9681c4d698 100644 --- a/src/main/java/org/jabref/logic/layout/format/RemoveLatexCommandsFormatter.java +++ b/src/main/java/org/jabref/logic/layout/format/RemoveLatexCommandsFormatter.java @@ -7,51 +7,61 @@ public class RemoveLatexCommandsFormatter implements LayoutFormatter { @Override public String format(String field) { - StringBuilder sb = new StringBuilder(""); + StringBuilder cleanedField = new StringBuilder(); StringBuilder currentCommand = null; - char c; + char currentCharacter; boolean escaped = false; boolean incommand = false; - int i; - for (i = 0; i < field.length(); i++) { - c = field.charAt(i); - if (escaped && (c == '\\')) { - sb.append('\\'); + int currentFieldPosition; + for (currentFieldPosition = 0; currentFieldPosition < field.length(); currentFieldPosition++) { + currentCharacter = field.charAt(currentFieldPosition); + if (escaped && (currentCharacter == '\\')) { + cleanedField.append('\\'); escaped = false; - } else if (c == '\\') { + // \\ --> first \ begins the command, second \ ends the command + // \latexommand\\ -> \latexcommand is the command, terminated by \, which begins a new command + incommand = false; + } else if (currentCharacter == '\\') { escaped = true; incommand = true; currentCommand = new StringBuilder(); - } else if (!incommand && ((c == '{') || (c == '}'))) { + } else if (!incommand && ((currentCharacter == '{') || (currentCharacter == '}'))) { // Swallow the brace. - } else if (Character.isLetter(c) || StringUtil.SPECIAL_COMMAND_CHARS.contains(String.valueOf(c))) { + } else if (Character.isLetter(currentCharacter) || StringUtil.SPECIAL_COMMAND_CHARS.contains(String.valueOf(currentCharacter))) { escaped = false; if (incommand) { - currentCommand.append(c); + currentCommand.append(currentCharacter); if ((currentCommand.length() == 1) && StringUtil.SPECIAL_COMMAND_CHARS.contains(currentCommand.toString())) { // This indicates that we are in a command of the type \^o or \~{n} incommand = false; escaped = false; - } } else { - sb.append(c); + cleanedField.append(currentCharacter); } - } else if (Character.isLetter(c)) { + } else if (Character.isLetter(currentCharacter)) { escaped = false; if (incommand) { // We are in a command, and should not keep the letter. - currentCommand.append(c); + currentCommand.append(currentCharacter); } else { - sb.append(c); + cleanedField.append(currentCharacter); } } else { - if (!incommand || (!Character.isWhitespace(c) && (c != '{'))) { - sb.append(c); + if (!incommand || (!Character.isWhitespace(currentCharacter) && (currentCharacter != '{'))) { + cleanedField.append(currentCharacter); } else { - if (c != '{') { - sb.append(c); + if (!Character.isWhitespace(currentCharacter) && (currentCharacter != '{')) { + // do not append the opening brace of a command parameter + // do not append the whitespace character + cleanedField.append(currentCharacter); + } + if (incommand) { + // eat up all whitespace characters + while ((currentFieldPosition + 1 < field.length() && Character.isWhitespace(field.charAt(currentFieldPosition + 1)))) { + currentFieldPosition++; + } } } incommand = false; @@ -59,7 +69,6 @@ public String format(String field) { } } - return sb.toString(); + return cleanedField.toString(); } - } diff --git a/src/main/java/org/jabref/model/cleanup/FieldFormatterCleanups.java b/src/main/java/org/jabref/model/cleanup/FieldFormatterCleanups.java index ffae762c0dc..b0e43f0e756 100644 --- a/src/main/java/org/jabref/model/cleanup/FieldFormatterCleanups.java +++ b/src/main/java/org/jabref/model/cleanup/FieldFormatterCleanups.java @@ -62,7 +62,7 @@ public List applySaveActions(BibEntry entry) { if (enabled) { return applyAllActions(entry); } else { - return new ArrayList<>(); + return Collections.emptyList(); } } diff --git a/src/main/java/org/jabref/model/cleanup/Formatter.java b/src/main/java/org/jabref/model/cleanup/Formatter.java index 17eb2d262e2..fa149b720c3 100644 --- a/src/main/java/org/jabref/model/cleanup/Formatter.java +++ b/src/main/java/org/jabref/model/cleanup/Formatter.java @@ -1,13 +1,12 @@ package org.jabref.model.cleanup; /** - * The Formatter is used for a Filter design-pattern. Extending classes have to accept a String and returned a - * formatted version of it. Implementations have to reside in the logic package. - * + * The Formatter is used for a Filter design-pattern. Extending classes have to accept a String and returned a formatted + * version of it. Implementations have to reside in the logic package. + *

* Example: - * + *

* "John von Neumann" => "von Neumann, John" - * */ public abstract class Formatter { @@ -20,13 +19,14 @@ public abstract class Formatter { /** * Returns a unique key for the formatter that can be used for its identification + * * @return the key of the formatter, always not null */ public abstract String getKey(); /** * Formats a field value by with a particular formatter transformation. - * + *

* Calling this method with a null argument results in a NullPointerException. * * @param value the input String @@ -42,8 +42,8 @@ public abstract class Formatter { public abstract String getDescription(); /** - * Returns an example input string of the formatter. - * This example is used as input to the formatter to demonstrate its functionality + * Returns an example input string of the formatter. This example is used as input to the formatter to demonstrate + * its functionality * * @return the example input string, always non empty */ @@ -68,7 +68,7 @@ public int hashCode() { @Override public boolean equals(Object obj) { if (obj instanceof Formatter) { - return getKey().equals(((Formatter)obj).getKey()); + return getKey().equals(((Formatter) obj).getKey()); } else { return false; } diff --git a/src/test/java/org/jabref/logic/formatter/bibtexfields/CleanupUrlFormatterTest.java b/src/test/java/org/jabref/logic/formatter/bibtexfields/CleanupUrlFormatterTest.java index 46e4bea3508..0fb47acf98e 100644 --- a/src/test/java/org/jabref/logic/formatter/bibtexfields/CleanupUrlFormatterTest.java +++ b/src/test/java/org/jabref/logic/formatter/bibtexfields/CleanupUrlFormatterTest.java @@ -10,11 +10,11 @@ */ class CleanupUrlFormatterTest { - private CleanupURLFormatter formatter; + private CleanupUrlFormatter formatter; @BeforeEach void setUp() { - formatter = new CleanupURLFormatter(); + formatter = new CleanupUrlFormatter(); } @Test @@ -29,6 +29,17 @@ void extractURLFormLink() { formatter.format("away.php?to=http%3A%2F%2Fwikipedia.org&a=snippet")); } + @Test + void latexCommandsNotRemoved() { + assertEquals("http://pi.informatik.uni-siegen.de/stt/36\\_2/./03\\_Technische\\_Beitraege/ZEUS2016/beitrag\\_2.pdf", formatter.format("http://pi.informatik.uni-siegen.de/stt/36\\_2/./03\\_Technische\\_Beitraege/ZEUS2016/beitrag\\_2.pdf")); + } + + @Test + void urlencodedSlashesAreAlsoConverted() { + // the caller has to pay attention that this does not happen + assertEquals("jabref.org/test/test", formatter.format("jabref.org/test%2Ftest")); + } + @Test void formatExample() { assertEquals("http://www.focus.de/" + diff --git a/src/test/java/org/jabref/logic/formatter/bibtexfields/ShortenDOIFormatterTest.java b/src/test/java/org/jabref/logic/formatter/bibtexfields/ShortenDOIFormatterTest.java index 1191014af41..1da025cdb4b 100644 --- a/src/test/java/org/jabref/logic/formatter/bibtexfields/ShortenDOIFormatterTest.java +++ b/src/test/java/org/jabref/logic/formatter/bibtexfields/ShortenDOIFormatterTest.java @@ -21,4 +21,9 @@ public void setUp() { public void formatDoi() { assertEquals("10/adc", formatter.format("10.1006/jmbi.1998.2354")); } + + @Test + public void invalidDoiIsKept() { + assertEquals("invalid-doi", formatter.format("invalid-doi")); + } } diff --git a/src/test/java/org/jabref/logic/layout/format/RemoveBracketsTest.java b/src/test/java/org/jabref/logic/layout/format/RemoveBracketsTest.java index 48c904c40bc..018d5354786 100644 --- a/src/test/java/org/jabref/logic/layout/format/RemoveBracketsTest.java +++ b/src/test/java/org/jabref/logic/layout/format/RemoveBracketsTest.java @@ -16,10 +16,22 @@ public void setUp() { } @Test - public void testFormat() throws Exception { + public void bracePairCorrectlyRemoved() throws Exception { assertEquals("some text", formatter.format("{some text}")); + } + + @Test + public void singleOpeningBraceCorrectlyRemoved() throws Exception { assertEquals("some text", formatter.format("{some text")); + } + + @Test + public void singleClosingBraceCorrectlyRemoved() throws Exception { assertEquals("some text", formatter.format("some text}")); + } + + @Test + public void bracePairWithEscapedBackslashCorrectlyRemoved() throws Exception { assertEquals("\\some text\\", formatter.format("\\{some text\\}")); } } diff --git a/src/test/java/org/jabref/logic/layout/format/RemoveLatexCommandsFormatterTest.java b/src/test/java/org/jabref/logic/layout/format/RemoveLatexCommandsFormatterTest.java new file mode 100644 index 00000000000..9eab6df5985 --- /dev/null +++ b/src/test/java/org/jabref/logic/layout/format/RemoveLatexCommandsFormatterTest.java @@ -0,0 +1,61 @@ +package org.jabref.logic.layout.format; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class RemoveLatexCommandsFormatterTest { + + private RemoveLatexCommandsFormatter formatter; + + @BeforeEach + public void setUp() { + formatter = new RemoveLatexCommandsFormatter(); + } + + @Test + public void withoutLatexCommandsUnmodified() { + assertEquals("some text", formatter.format("some text")); + } + + @Test + public void singleCommandWiped() { + assertEquals("", formatter.format("\\sometext")); + } + + @Test + public void singleSpaceAfterCommandRemoved() { + assertEquals("text", formatter.format("\\some text")); + } + + @Test + public void multipleSpacesAfterCommandRemoved() { + assertEquals("text", formatter.format("\\some text")); + } + + @Test + public void escapedBackslashBecomesBackslash() { + assertEquals("\\", formatter.format("\\\\")); + } + + @Test + public void escapedBackslashFollowedByTextBecomesBackslashFollowedByText() { + assertEquals("\\some text", formatter.format("\\\\some text")); + } + + @Test + public void escapedBackslashKept() { + assertEquals("\\some text\\", formatter.format("\\\\some text\\\\")); + } + + @Test + public void escapedUnderscoreReplaces() { + assertEquals("some_text", formatter.format("some\\_text")); + } + + @Test + public void exampleUrlCorrectlyCleaned() { + assertEquals("http://pi.informatik.uni-siegen.de/stt/36_2/./03_Technische_Beitraege/ZEUS2016/beitrag_2.pdf", formatter.format("http://pi.informatik.uni-siegen.de/stt/36\\_2/./03\\_Technische\\_Beitraege/ZEUS2016/beitrag\\_2.pdf")); + } +}