From aa60dd67223cb3cfdf5ffe8446f8baf3f329c1c2 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Mon, 28 Jun 2021 00:29:51 +0200 Subject: [PATCH] Fix for entering a backslash in the custom entry preview dialog (#7851) --- CHANGELOG.md | 1 + .../org/jabref/logic/layout/LayoutHelper.java | 14 ++++--- .../jabref/logic/layout/LayoutHelperTest.java | 39 +++++++++++++++++++ 3 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 src/test/java/org/jabref/logic/layout/LayoutHelperTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 036cc5ce61d..df5afe7f63d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -111,6 +111,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve - We fixed an issue where the RFC fetcher is not compatible with the draft [7305](https://github.com/JabRef/jabref/issues/7305) - We fixed an issue where duplicate files (both file names and contents are the same) is downloaded and add to linked files [#6197](https://github.com/JabRef/jabref/issues/6197) - We fixed an issue where changing the appearance of the preview tab did not trigger a restart warning. [#5464](https://github.com/JabRef/jabref/issues/5464) +- We fixed an issue where editing "Custom preview style" triggers exception. [#7526](https://github.com/JabRef/jabref/issues/7526) - We fixed an issue where a title with multiple applied formattings in EndNote was not imported correctly [forum#2734](https://discourse.jabref.org/t/importing-endnote-label-field-to-jabref-from-xml-file/2734) - We fixed an issue where a `report` in EndNote was imported as `article` [forum#2734](https://discourse.jabref.org/t/importing-endnote-label-field-to-jabref-from-xml-file/2734) - We fixed an issue where the field `publisher` in EndNote was not imported in JabRef [forum#2734](https://discourse.jabref.org/t/importing-endnote-label-field-to-jabref-from-xml-file/2734) diff --git a/src/main/java/org/jabref/logic/layout/LayoutHelper.java b/src/main/java/org/jabref/logic/layout/LayoutHelper.java index da8c7390e65..1e47272b149 100644 --- a/src/main/java/org/jabref/logic/layout/LayoutHelper.java +++ b/src/main/java/org/jabref/logic/layout/LayoutHelper.java @@ -188,7 +188,7 @@ private void doBracketedOptionField() throws IOException { } } - private void parse() throws IOException, StringIndexOutOfBoundsException { + private void parse() throws IOException { skipWhitespace(); int c; @@ -254,11 +254,15 @@ private void parseField() throws IOException { if (name.isEmpty()) { StringBuilder lastFive = new StringBuilder(10); - for (StringInt entry : parsedEntries.subList(Math.max(0, parsedEntries.size() - 6), - parsedEntries.size() - 1)) { - lastFive.append(entry.s); + if (parsedEntries.isEmpty()) { + lastFive.append("unknown"); + } else { + for (StringInt entry : parsedEntries.subList(Math.max(0, parsedEntries.size() - 6), + parsedEntries.size() - 1)) { + lastFive.append(entry.s); + } } - throw new StringIndexOutOfBoundsException( + throw new IOException( "Backslash parsing error near \'" + lastFive.toString().replace("\n", " ") + '\''); } diff --git a/src/test/java/org/jabref/logic/layout/LayoutHelperTest.java b/src/test/java/org/jabref/logic/layout/LayoutHelperTest.java new file mode 100644 index 00000000000..f78b71b92b0 --- /dev/null +++ b/src/test/java/org/jabref/logic/layout/LayoutHelperTest.java @@ -0,0 +1,39 @@ +package org.jabref.logic.layout; + +import java.io.IOException; +import java.io.StringReader; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; + +class LayoutHelperTest { + + @Test + public void backslashDoesNotTriggerException() { + StringReader stringReader = new StringReader("\\"); + LayoutFormatterPreferences layoutFormatterPreferences = mock(LayoutFormatterPreferences.class); + LayoutHelper layoutHelper = new LayoutHelper(stringReader, layoutFormatterPreferences); + assertThrows(IOException.class, () -> layoutHelper.getLayoutFromText()); + } + + @Test + public void unbalancedBeginEndIsParsed() throws Exception { + StringReader stringReader = new StringReader("\\begin{doi}, DOI: \\doi"); + LayoutFormatterPreferences layoutFormatterPreferences = mock(LayoutFormatterPreferences.class); + LayoutHelper layoutHelper = new LayoutHelper(stringReader, layoutFormatterPreferences); + Layout layout = layoutHelper.getLayoutFromText(); + assertNotNull(layout); + } + + @Test + public void minimalExampleWithDoiGetsParsed() throws Exception { + StringReader stringReader = new StringReader("\\begin{doi}, DOI: \\doi\\end{doi}"); + LayoutFormatterPreferences layoutFormatterPreferences = mock(LayoutFormatterPreferences.class); + LayoutHelper layoutHelper = new LayoutHelper(stringReader, layoutFormatterPreferences); + Layout layout = layoutHelper.getLayoutFromText(); + assertNotNull(layout); + } +}