From d6d67215a77738ed4d133b7c06b8aa287b6a1bdc Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Fri, 3 Jun 2016 14:27:46 +0200 Subject: [PATCH 01/34] Add failing test with preceding comment --- .../logic/bibtex/BibEntryWriterTest.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java b/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java index 3721ae7650e..4ab27c912e8 100644 --- a/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java +++ b/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java @@ -403,4 +403,38 @@ public void trimFieldContents() throws IOException { assertEquals(expected, actual); } + + @Test + public void roundTripWithPrecedingCommentTest() throws IOException { + // @formatter:off + String bibtexEntry = "% Some random comment that should stay here" + Globals.NEWLINE + + "@Article{test," + Globals.NEWLINE + + " Author = {Foo Bar}," + Globals.NEWLINE + + " Journal = {International Journal of Something}," + Globals.NEWLINE + + " Note = {some note}," + Globals.NEWLINE + + " Number = {1}" + Globals.NEWLINE + + "}"; + // @formatter:on + + // read in bibtex string + ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry)); + + Collection entries = result.getDatabase().getEntries(); + assertEquals(1, entries.size()); + + BibEntry entry = entries.iterator().next(); + assertEquals("test", entry.getCiteKey()); + assertEquals(5, entry.getFieldNames().size()); + Set fields = entry.getFieldNames(); + assertTrue(fields.contains("author")); + assertEquals("Foo Bar", entry.getField("author")); + + //write out bibtex string + StringWriter stringWriter = new StringWriter(); + writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX); + String actual = stringWriter.toString(); + + assertEquals(bibtexEntry, actual); + } + } From cb85443d0fe3f529591661c52018471bc99bc61d Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Fri, 3 Jun 2016 14:32:11 +0200 Subject: [PATCH 02/34] Add failing parser test --- .../importer/fileformat/BibtexParserTest.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/test/java/net/sf/jabref/importer/fileformat/BibtexParserTest.java b/src/test/java/net/sf/jabref/importer/fileformat/BibtexParserTest.java index fc2e8a76fed..30badb8bcb3 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/BibtexParserTest.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/BibtexParserTest.java @@ -10,6 +10,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; @@ -1486,4 +1487,31 @@ public void parseReturnsEntriesInSameOrder() throws IOException { assertEquals(expected, result.getDatabase().getEntries()); } + + @Test + public void parsePrecedingComment () throws IOException { + // @formatter:off + String bibtexEntry = "% Some random comment that should stay here" + Globals.NEWLINE + + "@Article{test," + Globals.NEWLINE + + " Author = {Foo Bar}," + Globals.NEWLINE + + " Journal = {International Journal of Something}," + Globals.NEWLINE + + " Note = {some note}," + Globals.NEWLINE + + " Number = {1}" + Globals.NEWLINE + + "}"; + // @formatter:on + + // read in bibtex string + ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry)); + + Collection entries = result.getDatabase().getEntries(); + assertEquals(1, entries.size()); + + BibEntry entry = entries.iterator().next(); + assertEquals("test", entry.getCiteKey()); + assertEquals(5, entry.getFieldNames().size()); + Set fields = entry.getFieldNames(); + assertTrue(fields.contains("author")); + assertEquals("Foo Bar", entry.getField("author")); + assertEquals(bibtexEntry, entry.getParsedSerialization()); + } } From b50c3d6dc45a54f77b0d367665fa59aa65a1c81e Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Fri, 3 Jun 2016 14:37:20 +0200 Subject: [PATCH 03/34] Improve test naming --- .../net/sf/jabref/importer/fileformat/BibtexParserTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/net/sf/jabref/importer/fileformat/BibtexParserTest.java b/src/test/java/net/sf/jabref/importer/fileformat/BibtexParserTest.java index 30badb8bcb3..7fa85509748 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/BibtexParserTest.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/BibtexParserTest.java @@ -1254,7 +1254,7 @@ public void parseSavesNewlinesBeforeEntryInParsedSerialization() throws IOExcept } @Test - public void parseSavesOnlyRealNewlinesBeforeEntryInParsedSerialization() throws IOException { + public void parseRemovesEncodingLineInParsedSerialization() throws IOException { String testEntry = "@article{test,author={Ed von Test}}"; ParserResult result = BibtexParser.parse( new StringReader("%Encoding: no" + Globals.NEWLINE + Globals.NEWLINE + Globals.NEWLINE + testEntry)); From 2c39c89fa4f5fc9a7c0142fbcc9af5611d341c7d Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Fri, 3 Jun 2016 15:41:30 +0200 Subject: [PATCH 04/34] Reuse Globals.ENCODING_PREFIX in test --- .../net/sf/jabref/importer/fileformat/BibtexParserTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/net/sf/jabref/importer/fileformat/BibtexParserTest.java b/src/test/java/net/sf/jabref/importer/fileformat/BibtexParserTest.java index 7fa85509748..18e44b74990 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/BibtexParserTest.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/BibtexParserTest.java @@ -1257,7 +1257,7 @@ public void parseSavesNewlinesBeforeEntryInParsedSerialization() throws IOExcept public void parseRemovesEncodingLineInParsedSerialization() throws IOException { String testEntry = "@article{test,author={Ed von Test}}"; ParserResult result = BibtexParser.parse( - new StringReader("%Encoding: no" + Globals.NEWLINE + Globals.NEWLINE + Globals.NEWLINE + testEntry)); + new StringReader(Globals.ENCODING_PREFIX + Globals.NEWLINE + Globals.NEWLINE + Globals.NEWLINE + testEntry)); Collection c = result.getDatabase().getEntries(); assertEquals(1, c.size()); From b547703fc6f151d1fa2b8a9b45c58c737c5c5c97 Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Fri, 3 Jun 2016 15:41:49 +0200 Subject: [PATCH 05/34] Check explicitly for encoding line and purge it --- .../importer/fileformat/BibtexParser.java | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/sf/jabref/importer/fileformat/BibtexParser.java b/src/main/java/net/sf/jabref/importer/fileformat/BibtexParser.java index f4c78b0c2f0..500c37ec916 100644 --- a/src/main/java/net/sf/jabref/importer/fileformat/BibtexParser.java +++ b/src/main/java/net/sf/jabref/importer/fileformat/BibtexParser.java @@ -28,6 +28,7 @@ import java.util.Map; import java.util.Objects; +import net.sf.jabref.Globals; import net.sf.jabref.MetaData; import net.sf.jabref.importer.ParserResult; import net.sf.jabref.logic.bibtex.FieldContentParser; @@ -322,29 +323,23 @@ private String dumpTextReadSoFarToString() { // if there is no entry found, simply return the content (necessary to parse text remaining after the last entry) if (indexOfAt == -1) { return purgeEOFCharacters(result); - } else { - - //skip all text except newlines and whitespaces before first @. This is necessary to remove the file header - int runningIndex = indexOfAt - 1; - while (runningIndex >= 0) { - if (!Character.isWhitespace(result.charAt(runningIndex))) { + } else if(result.contains(Globals.ENCODING_PREFIX)) { + // purge the encoding line if it exists + int runningIndex = result.indexOf(Globals.ENCODING_PREFIX); + while(runningIndex < indexOfAt) { + if(result.charAt(runningIndex) == '\n') { + break; + } else if(result.charAt(runningIndex) == '\r') { + if(result.charAt(runningIndex + 1) == '\n') { + runningIndex++; + } break; } - runningIndex--; - } - - if(runningIndex > -1) { - // We have to ignore some text at the beginning - // so we view the first line break as the end of the previous text and don't store it - if(result.charAt(runningIndex + 1) == '\r') { - runningIndex++; - } - if(result.charAt(runningIndex + 1) == '\n') { - runningIndex++; - } + runningIndex++; } - return result.substring(runningIndex + 1); + } else { + return result; } } From ce56e23fa22114b77717de840f6f1ac01df87243 Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Fri, 3 Jun 2016 15:46:25 +0200 Subject: [PATCH 06/34] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b73aebea0e1..88b067fdeb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# ## [Unreleased] ### Changed +- JabRef does no longer delete user comments outside of BibTeX entries [#1026] ### Fixed - Fixed [#405](https://github.com/JabRef/jabref/issues/405): Added more {} around capital letters in Unicode/HTML to LaTeX conversion to preserve them From fe79b417b519d805c06f762388e5298fad8c9043 Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Fri, 3 Jun 2016 16:03:28 +0200 Subject: [PATCH 07/34] Write out user comments also for modified entries --- .../jabref/logic/bibtex/BibEntryWriter.java | 31 ++++++++++--- .../logic/bibtex/BibEntryWriterTest.java | 45 +++++++++++++++++++ 2 files changed, 69 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/sf/jabref/logic/bibtex/BibEntryWriter.java b/src/main/java/net/sf/jabref/logic/bibtex/BibEntryWriter.java index 629c8c1d32b..4c9ee6e95c7 100644 --- a/src/main/java/net/sf/jabref/logic/bibtex/BibEntryWriter.java +++ b/src/main/java/net/sf/jabref/logic/bibtex/BibEntryWriter.java @@ -39,10 +39,10 @@ public void write(BibEntry entry, Writer out, BibDatabaseMode bibDatabaseMode) t /** * Writes the given BibEntry using the given writer * - * @param entry The entry to write - * @param out The writer to use + * @param entry The entry to write + * @param out The writer to use * @param bibDatabaseMode The database mode (bibtex or biblatex) - * @param reformat Should the entry be in any case, even if no change occurred? + * @param reformat Should the entry be in any case, even if no change occurred? */ public void write(BibEntry entry, Writer out, BibDatabaseMode bibDatabaseMode, Boolean reformat) throws IOException { // if the entry has not been modified, write it as it was @@ -50,11 +50,28 @@ public void write(BibEntry entry, Writer out, BibDatabaseMode bibDatabaseMode, B out.write(entry.getParsedSerialization()); return; } + + writeUserComments(entry, out); out.write(Globals.NEWLINE); writeRequiredFieldsFirstRemainingFieldsSecond(entry, out, bibDatabaseMode); out.write(Globals.NEWLINE); } + private void writeUserComments(BibEntry entry, Writer out) throws IOException { + String parsedSerialization = entry.getParsedSerialization(); + + if(parsedSerialization != null) { + // get the text before the entry + String prolog = entry.getParsedSerialization().substring(0, entry.getParsedSerialization().indexOf('@')); + + prolog = prolog.trim(); + // if there is any non whitespace text, write it + if (prolog.length() > 0) { + out.write(prolog + Globals.NEWLINE); + } + } + } + public void writeWithoutPrependedNewlines(BibEntry entry, Writer out, BibDatabaseMode bibDatabaseMode) throws IOException { // if the entry has not been modified, write it as it was if (!entry.hasChanged()) { @@ -73,7 +90,7 @@ public void writeWithoutPrependedNewlines(BibEntry entry, Writer out, BibDatabas * @throws IOException */ private void writeRequiredFieldsFirstRemainingFieldsSecond(BibEntry entry, Writer out, - BibDatabaseMode bibDatabaseMode) throws IOException { + BibDatabaseMode bibDatabaseMode) throws IOException { // Write header with type and bibtex-key. TypedBibEntry typedEntry = new TypedBibEntry(entry, Optional.empty(), bibDatabaseMode); out.write('@' + typedEntry.getTypeForDisplay() + '{'); @@ -129,9 +146,9 @@ private void writeKeyField(BibEntry entry, Writer out) throws IOException { /** * Write a single field, if it has any content. * - * @param entry the entry to write - * @param out the target of the write - * @param name The field name + * @param entry the entry to write + * @param out the target of the write + * @param name The field name * @throws IOException In case of an IO error */ private void writeField(BibEntry entry, Writer out, String name, int indentation) throws IOException { diff --git a/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java b/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java index 4ab27c912e8..f286a337493 100644 --- a/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java +++ b/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java @@ -437,4 +437,49 @@ public void roundTripWithPrecedingCommentTest() throws IOException { assertEquals(bibtexEntry, actual); } + @Test + public void roundTripWithPrecedingCommentAndModificationTest() throws IOException { + // @formatter:off + String bibtexEntry = "% Some random comment that should stay here" + Globals.NEWLINE + + "@Article{test," + Globals.NEWLINE + + " Author = {Foo Bar}," + Globals.NEWLINE + + " Journal = {International Journal of Something}," + Globals.NEWLINE + + " Note = {some note}," + Globals.NEWLINE + + " Number = {1}" + Globals.NEWLINE + + "}"; + // @formatter:on + + // read in bibtex string + ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry)); + + Collection entries = result.getDatabase().getEntries(); + assertEquals(1, entries.size()); + + BibEntry entry = entries.iterator().next(); + assertEquals("test", entry.getCiteKey()); + assertEquals(5, entry.getFieldNames().size()); + Set fields = entry.getFieldNames(); + assertTrue(fields.contains("author")); + assertEquals("Foo Bar", entry.getField("author")); + + // change the entry + entry.setField("author", "John Doe"); + + //write out bibtex string + StringWriter stringWriter = new StringWriter(); + writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX); + String actual = stringWriter.toString(); + // @formatter:off + String expected = "% Some random comment that should stay here" + Globals.NEWLINE + Globals.NEWLINE + + "@Article{test," + Globals.NEWLINE + + " author = {John Doe}," + Globals.NEWLINE + + " journal = {International Journal of Something}," + Globals.NEWLINE + + " number = {1}," + Globals.NEWLINE + + " note = {some note}," + Globals.NEWLINE + + "}" + Globals.NEWLINE; + // @formatter:on + + assertEquals(expected, actual); + } + } From ec589e80d402cce929c3e9935d4e95242d4c6c69 Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Fri, 3 Jun 2016 16:08:56 +0200 Subject: [PATCH 08/34] Add test to check preservation of ENCODING_PREFIX inside an entry --- .../importer/fileformat/BibtexParserTest.java | 146 ++++++++++-------- 1 file changed, 79 insertions(+), 67 deletions(-) diff --git a/src/test/java/net/sf/jabref/importer/fileformat/BibtexParserTest.java b/src/test/java/net/sf/jabref/importer/fileformat/BibtexParserTest.java index 18e44b74990..0e633578097 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/BibtexParserTest.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/BibtexParserTest.java @@ -313,10 +313,10 @@ public void parseRecognizesMultipleEntries() throws IOException { ParserResult result = BibtexParser .parse(new StringReader( "@article{canh05," - + " author = {Crowston, K. and Annabi, H.},\n" - + " title = {Title A}}\n" - + "@inProceedings{foo," - + " author={Norton Bar}}")); + + " author = {Crowston, K. and Annabi, H.},\n" + + " title = {Title A}}\n" + + "@inProceedings{foo," + + " author={Norton Bar}}")); List parsed = result.getDatabase().getEntries(); List expected = new ArrayList<>(); @@ -431,28 +431,28 @@ public void parseCombinesMultipleKeywordsFields() throws IOException { public void parseRecognizesHeaderButIgnoresEncoding() throws IOException { ParserResult result = BibtexParser.parse(new StringReader( "This file was created with JabRef 2.1 beta 2." - + "\n" - + "Encoding: Cp1252" - + "\n" - + "" - + "\n" - + "@INPROCEEDINGS{CroAnnHow05," - + "\n" - + " author = {Crowston, K. and Annabi, H. and Howison, J. and Masango, C.}," - + "\n" - + " title = {Effective work practices for floss development: A model and propositions}," - + "\n" - + " booktitle = {Hawaii International Conference On System Sciences (HICSS)}," - + "\n" - + " year = {2005}," - + "\n" - + " owner = {oezbek}," - + "\n" - + " timestamp = {2006.05.29}," - + "\n" - + " url = {http://james.howison.name/publications.html}" - + "\n" - + "}))")); + + "\n" + + "Encoding: Cp1252" + + "\n" + + "" + + "\n" + + "@INPROCEEDINGS{CroAnnHow05," + + "\n" + + " author = {Crowston, K. and Annabi, H. and Howison, J. and Masango, C.}," + + "\n" + + " title = {Effective work practices for floss development: A model and propositions}," + + "\n" + + " booktitle = {Hawaii International Conference On System Sciences (HICSS)}," + + "\n" + + " year = {2005}," + + "\n" + + " owner = {oezbek}," + + "\n" + + " timestamp = {2006.05.29}," + + "\n" + + " url = {http://james.howison.name/publications.html}" + + "\n" + + "}))")); assertEquals(Globals.prefs.getDefaultEncoding(), result.getMetaData().getEncoding()); Collection c = result.getDatabase().getEntries(); @@ -594,20 +594,20 @@ public void parseReturnsEmptyListIfNoEntryRecognized() throws IOException { ParserResult result = BibtexParser .parse(new StringReader( " author = {Crowston, K. and Annabi, H. and Howison, J. and Masango, C.}," - + "\n" - + " title = {Effective work practices for floss development: A model and propositions}," - + "\n" - + " booktitle = {Hawaii International Conference On System Sciences (HICSS)}," - + "\n" - + " year = {2005}," - + "\n" - + " owner = {oezbek}," - + "\n" - + " timestamp = {2006.05.29}," - + "\n" - + " url = {http://james.howison.name/publications.html}" - + "\n" - + "}))")); + + "\n" + + " title = {Effective work practices for floss development: A model and propositions}," + + "\n" + + " booktitle = {Hawaii International Conference On System Sciences (HICSS)}," + + "\n" + + " year = {2005}," + + "\n" + + " owner = {oezbek}," + + "\n" + + " timestamp = {2006.05.29}," + + "\n" + + " url = {http://james.howison.name/publications.html}" + + "\n" + + "}))")); Collection c = result.getDatabase().getEntries(); assertEquals(0, c.size()); } @@ -953,16 +953,16 @@ public void parseRecognizesMultipleStrings() throws IOException { public void parseRecognizesStringAndEntry() throws IOException { ParserResult result = BibtexParser.parse(new StringReader( - "" - + "@string{bourdieu = {Bourdieu, Pierre}}" - + "@book{bourdieu-2002-questions-sociologie, " - + " Address = {Paris}," - + " Author = bourdieu," - + " Isbn = 2707318256," - + " Publisher = {Minuit}," - + " Title = {Questions de sociologie}," - + " Year = 2002" - + "}")); + "" + + "@string{bourdieu = {Bourdieu, Pierre}}" + + "@book{bourdieu-2002-questions-sociologie, " + + " Address = {Paris}," + + " Author = bourdieu," + + " Isbn = 2707318256," + + " Publisher = {Minuit}," + + " Title = {Questions de sociologie}," + + " Year = 2002" + + "}")); assertEquals(1, result.getDatabase().getStringCount()); BibtexString s = result.getDatabase().getStringValues().iterator().next(); @@ -1379,7 +1379,7 @@ public void integrationTestSaveOrderConfig() throws IOException { Optional saveOrderConfig = result.getMetaData().getSaveOrderConfig(); assertEquals(new SaveOrderConfig(false, new SaveOrderConfig.SortCriterion("author", false), - new SaveOrderConfig.SortCriterion("year", true), new SaveOrderConfig.SortCriterion("abstract", false)), + new SaveOrderConfig.SortCriterion("year", true), new SaveOrderConfig.SortCriterion("abstract", false)), saveOrderConfig.get()); } @@ -1388,8 +1388,8 @@ public void integrationTestCustomKeyPattern() throws IOException { ParserResult result = BibtexParser .parse(new StringReader( "@comment{jabref-meta: keypattern_article:articleTest;}" - + Globals.NEWLINE - + "@comment{jabref-meta: keypatterndefault:test;}")); + + Globals.NEWLINE + + "@comment{jabref-meta: keypatterndefault:test;}")); AbstractLabelPattern labelPattern = result.getMetaData().getLabelPattern(); @@ -1413,17 +1413,17 @@ public void integrationTestBiblatexMode() throws IOException { public void integrationTestGroupTree() throws IOException, ParseException { ParserResult result = BibtexParser.parse(new StringReader( "@comment{jabref-meta: groupsversion:3;}" - + Globals.NEWLINE + - "@comment{jabref-meta: groupstree:" - + Globals.NEWLINE - + "0 AllEntriesGroup:;" - + Globals.NEWLINE - + "1 KeywordGroup:Fréchet\\;0\\;keywords\\;FrechetSpace\\;0\\;1\\;;" - + Globals.NEWLINE - + "1 KeywordGroup:Invariant theory\\;0\\;keywords\\;GIT\\;0\\;0\\;;" - + Globals.NEWLINE - + "1 ExplicitGroup:TestGroup\\;0\\;Key1\\;Key2\\;;" - + "}")); + + Globals.NEWLINE + + "@comment{jabref-meta: groupstree:" + + Globals.NEWLINE + + "0 AllEntriesGroup:;" + + Globals.NEWLINE + + "1 KeywordGroup:Fréchet\\;0\\;keywords\\;FrechetSpace\\;0\\;1\\;;" + + Globals.NEWLINE + + "1 KeywordGroup:Invariant theory\\;0\\;keywords\\;GIT\\;0\\;0\\;;" + + Globals.NEWLINE + + "1 ExplicitGroup:TestGroup\\;0\\;Key1\\;Key2\\;;" + + "}")); GroupTreeNode root = result.getMetaData().getGroups(); @@ -1435,7 +1435,7 @@ public void integrationTestGroupTree() throws IOException, ParseException { assertEquals( new KeywordGroup("Invariant theory", "keywords", "GIT", false, false, GroupHierarchyType.INDEPENDENT), root.getChildren().get(1).getGroup()); - assertEquals(Arrays.asList("Key1", "Key2"), ((ExplicitGroup)root.getChildren().get(2).getGroup()).getLegacyEntryKeys()); + assertEquals(Arrays.asList("Key1", "Key2"), ((ExplicitGroup) root.getChildren().get(2).getGroup()).getLegacyEntryKeys()); } @Test @@ -1458,7 +1458,7 @@ public void integrationTestFileDirectories() throws IOException { ParserResult result = BibtexParser .parse(new StringReader( "@comment{jabref-meta: fileDirectory:\\\\Literature\\\\;}" - + "@comment{jabref-meta: fileDirectory-defaultOwner-user:D:\\\\Documents;}")); + + "@comment{jabref-meta: fileDirectory-defaultOwner-user:D:\\\\Documents;}")); assertEquals("\\Literature\\", result.getMetaData().getDefaultFileDirectory().get()); assertEquals("D:\\Documents", result.getMetaData().getUserFileDirectory("defaultOwner-user").get()); @@ -1489,7 +1489,7 @@ public void parseReturnsEntriesInSameOrder() throws IOException { } @Test - public void parsePrecedingComment () throws IOException { + public void parsePrecedingComment() throws IOException { // @formatter:off String bibtexEntry = "% Some random comment that should stay here" + Globals.NEWLINE + "@Article{test," + Globals.NEWLINE + @@ -1514,4 +1514,16 @@ public void parsePrecedingComment () throws IOException { assertEquals("Foo Bar", entry.getField("author")); assertEquals(bibtexEntry, entry.getParsedSerialization()); } + + @Test + public void preserveEncodingPrefixInsideEntry() { + List parsed = BibtexParser.fromString("@article{test,author={" + Globals.ENCODING_PREFIX + "}}"); + + BibEntry expected = new BibEntry(); + expected.setType("article"); + expected.setCiteKey("test"); + expected.setField("author", Globals.ENCODING_PREFIX); + assertEquals(Collections.singletonList(expected), parsed); + } + } From c7f46941b76cb3a3161049aa3c3281f5313b9c92 Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Fri, 3 Jun 2016 16:15:53 +0200 Subject: [PATCH 09/34] Make BibEntryWriter more robust when dealing with faulty parsed serializations --- .../sf/jabref/logic/bibtex/BibEntryWriter.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/sf/jabref/logic/bibtex/BibEntryWriter.java b/src/main/java/net/sf/jabref/logic/bibtex/BibEntryWriter.java index 4c9ee6e95c7..5c1442fbeac 100644 --- a/src/main/java/net/sf/jabref/logic/bibtex/BibEntryWriter.java +++ b/src/main/java/net/sf/jabref/logic/bibtex/BibEntryWriter.java @@ -61,13 +61,18 @@ private void writeUserComments(BibEntry entry, Writer out) throws IOException { String parsedSerialization = entry.getParsedSerialization(); if(parsedSerialization != null) { - // get the text before the entry - String prolog = entry.getParsedSerialization().substring(0, entry.getParsedSerialization().indexOf('@')); - prolog = prolog.trim(); - // if there is any non whitespace text, write it - if (prolog.length() > 0) { - out.write(prolog + Globals.NEWLINE); + try { + // get the text before the entry + String prolog = entry.getParsedSerialization().substring(0, entry.getParsedSerialization().indexOf('@')); + + prolog = prolog.trim(); + // if there is any non whitespace text, write it + if (prolog.length() > 0) { + out.write(prolog + Globals.NEWLINE); + } + } catch(StringIndexOutOfBoundsException ignore) { + // if this occurs a broken parsed serialization has been set, so just do nothing } } } From 39576805ecebd02cc68a2d6b64bd705b62964c0e Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Wed, 8 Jun 2016 11:14:21 +0200 Subject: [PATCH 10/34] Add test with user comment in file --- .../exporter/BibDatabaseWriterTest.java | 16 +++++++ .../resources/testbib/bibWithUserComments.bib | 43 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 src/test/resources/testbib/bibWithUserComments.bib diff --git a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java index bfaa1c0535b..94613bafdef 100644 --- a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java +++ b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java @@ -291,6 +291,22 @@ public void roundtrip() throws IOException { } } + @Test + public void roundtripWithUserComment() throws IOException { + Path testBibtexFile = Paths.get("src/test/resources/testbib/bibWithUserComments.bib"); + Charset encoding = StandardCharsets.UTF_8; + ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); + + SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true); + BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), + new Defaults(BibDatabaseMode.BIBTEX)); + + databaseWriter.writePartOfDatabase(stringWriter, context, result.getDatabase().getEntries(), preferences); + try (Scanner scanner = new Scanner(testBibtexFile,encoding.name())) { + assertEquals(scanner.useDelimiter("\\A").next(), stringWriter.toString()); + } + } + @Test public void writeSavedSerializationOfEntryIfUnchanged() throws IOException { BibEntry entry = new BibEntry(); diff --git a/src/test/resources/testbib/bibWithUserComments.bib b/src/test/resources/testbib/bibWithUserComments.bib new file mode 100644 index 00000000000..8148c3b6d63 --- /dev/null +++ b/src/test/resources/testbib/bibWithUserComments.bib @@ -0,0 +1,43 @@ +% Encoding: UTF-8 + +@Preamble{preamble} + +@String{firstString = {my first string}} +@String{secondString = {}} + +@ARTICLE{1102917, + author = {E. Bardram}, + title = {The trouble with login: on usability and computer security in ubiquitous + computing}, + journal = {Personal Ubiquitous Comput.}, + year = {2005}, + volume = {9}, + pages = {357--367}, + number = {6}, + address = {London, UK}, + bdsk-url-1 = {http://dx.doi.org/10.1007/s00779-005-0347-6}, + doi = {http://dx.doi.org/10.1007/s00779-005-0347-6}, + issn = {1617-4909}, + publisher = {Springer-Verlag} +} + +This is some arbitrary user comment that should be preserved + +@INPROCEEDINGS{1137631, + author = {Gustav Bostr\"{o}m and Jaana W\"{a}yrynen and Marine Bod\'{e}n and + Konstantin Beznosov and Philippe Kruchten}, + title = {Extending XP practices to support security requirements engineering}, + booktitle = {SESS '06: Proceedings of the 2006 international workshop on Software + engineering for secure systems}, + year = {2006}, + pages = {11--18}, + address = {New York, NY, USA}, + publisher = {ACM}, + bdsk-url-1 = {http://doi.acm.org/10.1145/1137627.1137631}, + doi = {http://doi.acm.org/10.1145/1137627.1137631}, + file = {:/Volumes/iDisk/Freie Universität Berlin/Semester 9/Softwareprozesse/p11-bostrom.pdf:PDF}, + isbn = {1-59593-411-1}, + location = {Shanghai, China} +} + +@Comment{jabref-meta: databaseType:bibtex;} From 399bcb925b727277de7ccdc364df8787f0714ec4 Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Wed, 8 Jun 2016 11:19:37 +0200 Subject: [PATCH 11/34] Add test that changes and reformats an entry with a user comment --- .../exporter/BibDatabaseWriterTest.java | 20 ++++++++++ .../bibWithUserCommentAndEntryChange.bib | 40 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/test/resources/testbib/bibWithUserCommentAndEntryChange.bib diff --git a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java index 94613bafdef..5a8bfddb59c 100644 --- a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java +++ b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java @@ -307,6 +307,26 @@ public void roundtripWithUserComment() throws IOException { } } + @Test + public void roundtripWithUserCommentAndEntryChange() throws IOException { + Path testBibtexFile = Paths.get("src/test/resources/testbib/bibWithUserComments.bib"); + Charset encoding = StandardCharsets.UTF_8; + ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); + + BibEntry entry = result.getDatabase().getEntryByKey("1137631"); + entry.setField("author", "Mr. Author"); + + SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true); + BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), + new Defaults(BibDatabaseMode.BIBTEX)); + + databaseWriter.writePartOfDatabase(stringWriter, context, result.getDatabase().getEntries(), preferences); + + try (Scanner scanner = new Scanner(Paths.get("src/test/resources/testbib/bibWithUserCommentAndEntryChange.bib"),encoding.name())) { + assertEquals(scanner.useDelimiter("\\A").next(), stringWriter.toString()); + } + } + @Test public void writeSavedSerializationOfEntryIfUnchanged() throws IOException { BibEntry entry = new BibEntry(); diff --git a/src/test/resources/testbib/bibWithUserCommentAndEntryChange.bib b/src/test/resources/testbib/bibWithUserCommentAndEntryChange.bib new file mode 100644 index 00000000000..680bc063838 --- /dev/null +++ b/src/test/resources/testbib/bibWithUserCommentAndEntryChange.bib @@ -0,0 +1,40 @@ +% Encoding: UTF-8 + +@Preamble{preamble} + +@String{firstString = {my first string}} +@String{secondString = {}} + +@ARTICLE{1102917, + author = {E. Bardram}, + title = {The trouble with login: on usability and computer security in ubiquitous + computing}, + journal = {Personal Ubiquitous Comput.}, + year = {2005}, + volume = {9}, + pages = {357--367}, + number = {6}, + address = {London, UK}, + bdsk-url-1 = {http://dx.doi.org/10.1007/s00779-005-0347-6}, + doi = {http://dx.doi.org/10.1007/s00779-005-0347-6}, + issn = {1617-4909}, + publisher = {Springer-Verlag} +} +This is some arbitrary user comment that should be preserved + +@InProceedings{1137631, + author = {Mr. Author}, + title = {Extending XP practices to support security requirements engineering}, + booktitle = {SESS '06: Proceedings of the 2006 international workshop on Software engineering for secure systems}, + year = {2006}, + pages = {11--18}, + address = {New York, NY, USA}, + publisher = {ACM}, + bdsk-url-1 = {http://doi.acm.org/10.1145/1137627.1137631}, + doi = {http://doi.acm.org/10.1145/1137627.1137631}, + file = {:/Volumes/iDisk/Freie Universität Berlin/Semester 9/Softwareprozesse/p11-bostrom.pdf:PDF}, + isbn = {1-59593-411-1}, + location = {Shanghai, China}, +} + +@Comment{jabref-meta: databaseType:bibtex;} From 14db4bfd5d59bb146ea8391260831f5ac9de531a Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Wed, 8 Jun 2016 11:22:56 +0200 Subject: [PATCH 12/34] Add test with user comment before String --- .../exporter/BibDatabaseWriterTest.java | 16 +++++++ .../bibWithUserCommentBeforeString.bib | 43 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 src/test/resources/testbib/bibWithUserCommentBeforeString.bib diff --git a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java index 5a8bfddb59c..591e3333301 100644 --- a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java +++ b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java @@ -327,6 +327,22 @@ public void roundtripWithUserCommentAndEntryChange() throws IOException { } } + @Test + public void roundtripWithUserCommentBeforeString() throws IOException { + Path testBibtexFile = Paths.get("src/test/resources/testbib/bibWithUserCommentBeforeString.bib"); + Charset encoding = StandardCharsets.UTF_8; + ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); + + SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true); + BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), + new Defaults(BibDatabaseMode.BIBTEX)); + + databaseWriter.writePartOfDatabase(stringWriter, context, result.getDatabase().getEntries(), preferences); + try (Scanner scanner = new Scanner(testBibtexFile,encoding.name())) { + assertEquals(scanner.useDelimiter("\\A").next(), stringWriter.toString()); + } + } + @Test public void writeSavedSerializationOfEntryIfUnchanged() throws IOException { BibEntry entry = new BibEntry(); diff --git a/src/test/resources/testbib/bibWithUserCommentBeforeString.bib b/src/test/resources/testbib/bibWithUserCommentBeforeString.bib new file mode 100644 index 00000000000..f4b675a5171 --- /dev/null +++ b/src/test/resources/testbib/bibWithUserCommentBeforeString.bib @@ -0,0 +1,43 @@ +% Encoding: UTF-8 + +@Preamble{preamble} + +This is some arbitrary user comment that should be preserved + +@String{firstString = {my first string}} +@String{secondString = {}} + +@ARTICLE{1102917, + author = {E. Bardram}, + title = {The trouble with login: on usability and computer security in ubiquitous + computing}, + journal = {Personal Ubiquitous Comput.}, + year = {2005}, + volume = {9}, + pages = {357--367}, + number = {6}, + address = {London, UK}, + bdsk-url-1 = {http://dx.doi.org/10.1007/s00779-005-0347-6}, + doi = {http://dx.doi.org/10.1007/s00779-005-0347-6}, + issn = {1617-4909}, + publisher = {Springer-Verlag} +} + +@INPROCEEDINGS{1137631, + author = {Gustav Bostr\"{o}m and Jaana W\"{a}yrynen and Marine Bod\'{e}n and + Konstantin Beznosov and Philippe Kruchten}, + title = {Extending XP practices to support security requirements engineering}, + booktitle = {SESS '06: Proceedings of the 2006 international workshop on Software + engineering for secure systems}, + year = {2006}, + pages = {11--18}, + address = {New York, NY, USA}, + publisher = {ACM}, + bdsk-url-1 = {http://doi.acm.org/10.1145/1137627.1137631}, + doi = {http://doi.acm.org/10.1145/1137627.1137631}, + file = {:/Volumes/iDisk/Freie Universität Berlin/Semester 9/Softwareprozesse/p11-bostrom.pdf:PDF}, + isbn = {1-59593-411-1}, + location = {Shanghai, China} +} + +@Comment{jabref-meta: databaseType:bibtex;} From 392864ec4c42ce4103a48822d9092864de03e858 Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Wed, 8 Jun 2016 11:37:35 +0200 Subject: [PATCH 13/34] Preserve newlines also when used with bibtex strings --- .../sf/jabref/exporter/BibDatabaseWriter.java | 22 +++++++++++++++++++ .../exporter/BibDatabaseWriterTest.java | 20 +++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java b/src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java index 72925e078ff..dfc8a1d8810 100644 --- a/src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java +++ b/src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java @@ -317,6 +317,8 @@ private void writeString(Writer fw, BibtexString bs, Map r return; } + writeUserCommentsForString(bs, fw); + if(isFirstStringInType) { fw.write(Globals.NEWLINE); } @@ -354,6 +356,26 @@ private void writeString(Writer fw, BibtexString bs, Map r fw.write("}" + Globals.NEWLINE); } + private void writeUserCommentsForString(BibtexString string, Writer out) throws IOException { + String parsedSerialization = string.getParsedSerialization(); + + if(parsedSerialization != null) { + + try { + // get the text before the string + String prolog = string.getParsedSerialization().substring(0, string.getParsedSerialization().indexOf('@')); + + prolog = prolog.trim(); + // if there is any non whitespace text, write it with proper line separation + if (prolog.length() > 0) { + out.write(Globals.NEWLINE + prolog + Globals.NEWLINE); + } + } catch(StringIndexOutOfBoundsException ignore) { + // if this occurs a broken parsed serialization has been set, so just do nothing + } + } + } + /** * Write all strings in alphabetical order, modified to produce a safe (for * BibTeX) order of the strings if they reference each other. diff --git a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java index 591e3333301..ef131a49722 100644 --- a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java +++ b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java @@ -343,6 +343,26 @@ public void roundtripWithUserCommentBeforeString() throws IOException { } } + @Test + public void roundtripWithUserCommentBeforeStringAndChange() throws IOException { + Path testBibtexFile = Paths.get("src/test/resources/testbib/bibWithUserCommentBeforeString.bib"); + Charset encoding = StandardCharsets.UTF_8; + ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); + + BibtexString string = result.getDatabase().getString("00000000"); + string.setContent("my first string"); + + SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true); + BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), + new Defaults(BibDatabaseMode.BIBTEX)); + + databaseWriter.writePartOfDatabase(stringWriter, context, result.getDatabase().getEntries(), preferences); + + try (Scanner scanner = new Scanner(testBibtexFile,encoding.name())) { + assertEquals(scanner.useDelimiter("\\A").next(), stringWriter.toString()); + } + } + @Test public void writeSavedSerializationOfEntryIfUnchanged() throws IOException { BibEntry entry = new BibEntry(); From 9c23e942d8129738b33549ef447f89d7a7c1cf5d Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Wed, 8 Jun 2016 11:43:04 +0200 Subject: [PATCH 14/34] Add test for serialization of epilog --- .../exporter/BibDatabaseWriterTest.java | 16 +++++++ src/test/resources/testbib/bibWithEpilog.bib | 43 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 src/test/resources/testbib/bibWithEpilog.bib diff --git a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java index ef131a49722..f0f44c63074 100644 --- a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java +++ b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java @@ -615,4 +615,20 @@ public void writeEntriesInOriginalOrderWhenNoSaveOrderConfigIsSetInMetadata() th + Globals.NEWLINE , stringWriter.toString()); } + + @Test + public void writeEpilog() throws IOException { + Path testBibtexFile = Paths.get("src/test/resources/testbib/bibWithEpilog.bib"); + Charset encoding = StandardCharsets.UTF_8; + ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); + + SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true); + BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), + new Defaults(BibDatabaseMode.BIBTEX)); + + databaseWriter.writePartOfDatabase(stringWriter, context, result.getDatabase().getEntries(), preferences); + try (Scanner scanner = new Scanner(testBibtexFile,encoding.name())) { + assertEquals(scanner.useDelimiter("\\A").next(), stringWriter.toString()); + } + } } diff --git a/src/test/resources/testbib/bibWithEpilog.bib b/src/test/resources/testbib/bibWithEpilog.bib new file mode 100644 index 00000000000..2f2d315b27e --- /dev/null +++ b/src/test/resources/testbib/bibWithEpilog.bib @@ -0,0 +1,43 @@ +% Encoding: UTF-8 + +@Preamble{preamble} + +@String{firstString = {my first string}} +@String{secondString = {}} + +@ARTICLE{1102917, + author = {E. Bardram}, + title = {The trouble with login: on usability and computer security in ubiquitous + computing}, + journal = {Personal Ubiquitous Comput.}, + year = {2005}, + volume = {9}, + pages = {357--367}, + number = {6}, + address = {London, UK}, + bdsk-url-1 = {http://dx.doi.org/10.1007/s00779-005-0347-6}, + doi = {http://dx.doi.org/10.1007/s00779-005-0347-6}, + issn = {1617-4909}, + publisher = {Springer-Verlag} +} + +@INPROCEEDINGS{1137631, + author = {Gustav Bostr\"{o}m and Jaana W\"{a}yrynen and Marine Bod\'{e}n and + Konstantin Beznosov and Philippe Kruchten}, + title = {Extending XP practices to support security requirements engineering}, + booktitle = {SESS '06: Proceedings of the 2006 international workshop on Software + engineering for secure systems}, + year = {2006}, + pages = {11--18}, + address = {New York, NY, USA}, + publisher = {ACM}, + bdsk-url-1 = {http://doi.acm.org/10.1145/1137627.1137631}, + doi = {http://doi.acm.org/10.1145/1137627.1137631}, + file = {:/Volumes/iDisk/Freie Universität Berlin/Semester 9/Softwareprozesse/p11-bostrom.pdf:PDF}, + isbn = {1-59593-411-1}, + location = {Shanghai, China} +} + +@Comment{jabref-meta: databaseType:bibtex;} + +And here comes some arbitrary text that should just be appended again as it is standing here. From 21eac95ea48f0dd209149053561dfca2fdf8e919 Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Wed, 8 Jun 2016 11:50:16 +0200 Subject: [PATCH 15/34] Fix string detection in test --- .../java/net/sf/jabref/exporter/BibDatabaseWriterTest.java | 4 +++- src/test/resources/testbib/bibWithUserCommentBeforeString.bib | 3 +-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java index f0f44c63074..d0043e7db35 100644 --- a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java +++ b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java @@ -9,6 +9,7 @@ import java.nio.file.Paths; import java.util.Arrays; import java.util.Collections; +import java.util.Iterator; import java.util.Scanner; import net.sf.jabref.BibDatabaseContext; @@ -349,7 +350,8 @@ public void roundtripWithUserCommentBeforeStringAndChange() throws IOException { Charset encoding = StandardCharsets.UTF_8; ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); - BibtexString string = result.getDatabase().getString("00000000"); + Iterator strings = result.getDatabase().getStringValues().iterator(); + BibtexString string = result.getDatabase().getStringValues().iterator().next(); string.setContent("my first string"); SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true); diff --git a/src/test/resources/testbib/bibWithUserCommentBeforeString.bib b/src/test/resources/testbib/bibWithUserCommentBeforeString.bib index f4b675a5171..de20f29181b 100644 --- a/src/test/resources/testbib/bibWithUserCommentBeforeString.bib +++ b/src/test/resources/testbib/bibWithUserCommentBeforeString.bib @@ -4,8 +4,7 @@ @Preamble{preamble This is some arbitrary user comment that should be preserved -@String{firstString = {my first string}} -@String{secondString = {}} +@String{firstString = {my first string}} @ARTICLE{1102917, author = {E. Bardram}, From 3206b29723161eb16f4ecaadecb12045c3cc1fce Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Wed, 8 Jun 2016 17:36:13 +0200 Subject: [PATCH 16/34] In case of change, only remove trailing whitespaces between user comments and entry --- src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java | 5 +++-- src/main/java/net/sf/jabref/logic/bibtex/BibEntryWriter.java | 4 +++- .../resources/testbib/bibWithUserCommentAndEntryChange.bib | 1 + 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java b/src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java index dfc8a1d8810..ac6761a958f 100644 --- a/src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java +++ b/src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java @@ -365,10 +365,11 @@ private void writeUserCommentsForString(BibtexString string, Writer out) throws // get the text before the string String prolog = string.getParsedSerialization().substring(0, string.getParsedSerialization().indexOf('@')); - prolog = prolog.trim(); + // delete trailing whitespaces (between string and text) + prolog = prolog.replaceFirst("\\s+$", ""); // if there is any non whitespace text, write it with proper line separation if (prolog.length() > 0) { - out.write(Globals.NEWLINE + prolog + Globals.NEWLINE); + out.write(prolog + Globals.NEWLINE); } } catch(StringIndexOutOfBoundsException ignore) { // if this occurs a broken parsed serialization has been set, so just do nothing diff --git a/src/main/java/net/sf/jabref/logic/bibtex/BibEntryWriter.java b/src/main/java/net/sf/jabref/logic/bibtex/BibEntryWriter.java index 5c1442fbeac..83aad3deec0 100644 --- a/src/main/java/net/sf/jabref/logic/bibtex/BibEntryWriter.java +++ b/src/main/java/net/sf/jabref/logic/bibtex/BibEntryWriter.java @@ -66,7 +66,9 @@ private void writeUserComments(BibEntry entry, Writer out) throws IOException { // get the text before the entry String prolog = entry.getParsedSerialization().substring(0, entry.getParsedSerialization().indexOf('@')); - prolog = prolog.trim(); + // delete trailing whitespaces (between entry and text) + prolog = prolog.replaceFirst("\\s+$", ""); + // if there is any non whitespace text, write it if (prolog.length() > 0) { out.write(prolog + Globals.NEWLINE); diff --git a/src/test/resources/testbib/bibWithUserCommentAndEntryChange.bib b/src/test/resources/testbib/bibWithUserCommentAndEntryChange.bib index 680bc063838..ea17f6aaae5 100644 --- a/src/test/resources/testbib/bibWithUserCommentAndEntryChange.bib +++ b/src/test/resources/testbib/bibWithUserCommentAndEntryChange.bib @@ -20,6 +20,7 @@ @ARTICLE{1102917 issn = {1617-4909}, publisher = {Springer-Verlag} } + This is some arbitrary user comment that should be preserved @InProceedings{1137631, From 678c83ee7fca52c15d99dde2ece082630ffa9bd3 Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Tue, 14 Jun 2016 09:52:16 +0200 Subject: [PATCH 17/34] Remove unused variable --- .../java/net/sf/jabref/exporter/BibDatabaseWriterTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java index d0043e7db35..f3b284625e3 100644 --- a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java +++ b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java @@ -349,8 +349,7 @@ public void roundtripWithUserCommentBeforeStringAndChange() throws IOException { Path testBibtexFile = Paths.get("src/test/resources/testbib/bibWithUserCommentBeforeString.bib"); Charset encoding = StandardCharsets.UTF_8; ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); - - Iterator strings = result.getDatabase().getStringValues().iterator(); + BibtexString string = result.getDatabase().getStringValues().iterator().next(); string.setContent("my first string"); From 7a3522abda673e6f1a8f3a4984912602e8bb6a7c Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Tue, 14 Jun 2016 10:06:29 +0200 Subject: [PATCH 18/34] Remove unused import --- .../java/net/sf/jabref/exporter/BibDatabaseWriterTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java index f3b284625e3..1d2c314300c 100644 --- a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java +++ b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java @@ -9,7 +9,6 @@ import java.nio.file.Paths; import java.util.Arrays; import java.util.Collections; -import java.util.Iterator; import java.util.Scanner; import net.sf.jabref.BibDatabaseContext; @@ -349,7 +348,7 @@ public void roundtripWithUserCommentBeforeStringAndChange() throws IOException { Path testBibtexFile = Paths.get("src/test/resources/testbib/bibWithUserCommentBeforeString.bib"); Charset encoding = StandardCharsets.UTF_8; ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); - + BibtexString string = result.getDatabase().getStringValues().iterator().next(); string.setContent("my first string"); From 51d82a2effb4c5894ddae5e351bedf9c0b209968 Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Fri, 8 Jul 2016 14:20:49 +0200 Subject: [PATCH 19/34] Reformat changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 681db48983d..a780d41e1f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# ## [Unreleased] ### Changed -- JabRef does no longer delete user comments outside of BibTeX entries [#1026] +- [#1026](https://github.com/JabRef/jabref/issues/1026) JabRef does no longer delete user comments outside of BibTeX entries and strings - [#1485](https://github.com/JabRef/jabref/issues/1485) Biblatex field shorttitle is now exported/imported as standard field ShortTitle to Word bibliography ### Fixed From 1dca1a7bcebe26922aa8fc6ad646369c28ec0a87 Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Fri, 8 Jul 2016 14:29:02 +0200 Subject: [PATCH 20/34] Move comment detection logic to BibtexString --- .../sf/jabref/exporter/BibDatabaseWriter.java | 19 ++------------- .../sf/jabref/model/entry/BibtexString.java | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java b/src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java index ac6761a958f..cd5c072a575 100644 --- a/src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java +++ b/src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java @@ -357,23 +357,8 @@ private void writeString(Writer fw, BibtexString bs, Map r } private void writeUserCommentsForString(BibtexString string, Writer out) throws IOException { - String parsedSerialization = string.getParsedSerialization(); - - if(parsedSerialization != null) { - - try { - // get the text before the string - String prolog = string.getParsedSerialization().substring(0, string.getParsedSerialization().indexOf('@')); - - // delete trailing whitespaces (between string and text) - prolog = prolog.replaceFirst("\\s+$", ""); - // if there is any non whitespace text, write it with proper line separation - if (prolog.length() > 0) { - out.write(prolog + Globals.NEWLINE); - } - } catch(StringIndexOutOfBoundsException ignore) { - // if this occurs a broken parsed serialization has been set, so just do nothing - } + if(!string.getUserComments().isEmpty()) { + out.write(string.getUserComments() + Globals.NEWLINE); } } diff --git a/src/main/java/net/sf/jabref/model/entry/BibtexString.java b/src/main/java/net/sf/jabref/model/entry/BibtexString.java index 95eb37c3dcd..125161e2eb4 100644 --- a/src/main/java/net/sf/jabref/model/entry/BibtexString.java +++ b/src/main/java/net/sf/jabref/model/entry/BibtexString.java @@ -152,4 +152,28 @@ public String getParsedSerialization() { public boolean hasChanged(){ return hasChanged; } + + /* + * Returns user comments (arbitrary text before the string) if there are any. If not returns the empty string + */ + public String getUserComments() { + if(parsedSerialization != null) { + + try { + // get the text before the string + String prolog = parsedSerialization.substring(0, parsedSerialization.indexOf('@')); + + // delete trailing whitespaces (between string and text) + prolog = prolog.replaceFirst("\\s+$", ""); + // if there is any non whitespace text, write it with proper line separation + if (prolog.length() > 0) { + return prolog; + } + } catch(StringIndexOutOfBoundsException ignore) { + // if this occurs a broken parsed serialization has been set, so just do nothing + } + } + + return ""; + } } From cee181fe567368dc5352f41759cc55df340b2a13 Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Fri, 8 Jul 2016 14:44:50 +0200 Subject: [PATCH 21/34] Compute user comment in string only once --- src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java b/src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java index cd5c072a575..66c7741df53 100644 --- a/src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java +++ b/src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java @@ -357,8 +357,10 @@ private void writeString(Writer fw, BibtexString bs, Map r } private void writeUserCommentsForString(BibtexString string, Writer out) throws IOException { - if(!string.getUserComments().isEmpty()) { - out.write(string.getUserComments() + Globals.NEWLINE); + String userComments = string.getUserComments(); + + if(!userComments.isEmpty()) { + out.write(userComments + Globals.NEWLINE); } } From 6496190bbbed5a67d7a040049b270cced50b1e67 Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Fri, 8 Jul 2016 14:45:16 +0200 Subject: [PATCH 22/34] Move user comment computation to BibEntry --- .../jabref/logic/bibtex/BibEntryWriter.java | 20 +++------------ .../net/sf/jabref/model/entry/BibEntry.java | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/main/java/net/sf/jabref/logic/bibtex/BibEntryWriter.java b/src/main/java/net/sf/jabref/logic/bibtex/BibEntryWriter.java index 83aad3deec0..1de857c3af3 100644 --- a/src/main/java/net/sf/jabref/logic/bibtex/BibEntryWriter.java +++ b/src/main/java/net/sf/jabref/logic/bibtex/BibEntryWriter.java @@ -58,24 +58,10 @@ public void write(BibEntry entry, Writer out, BibDatabaseMode bibDatabaseMode, B } private void writeUserComments(BibEntry entry, Writer out) throws IOException { - String parsedSerialization = entry.getParsedSerialization(); + String userComments = entry.getUserComments(); - if(parsedSerialization != null) { - - try { - // get the text before the entry - String prolog = entry.getParsedSerialization().substring(0, entry.getParsedSerialization().indexOf('@')); - - // delete trailing whitespaces (between entry and text) - prolog = prolog.replaceFirst("\\s+$", ""); - - // if there is any non whitespace text, write it - if (prolog.length() > 0) { - out.write(prolog + Globals.NEWLINE); - } - } catch(StringIndexOutOfBoundsException ignore) { - // if this occurs a broken parsed serialization has been set, so just do nothing - } + if(!userComments.isEmpty()) { + out.write(userComments + Globals.NEWLINE); } } diff --git a/src/main/java/net/sf/jabref/model/entry/BibEntry.java b/src/main/java/net/sf/jabref/model/entry/BibEntry.java index 7a14564f812..565ab1eebef 100644 --- a/src/main/java/net/sf/jabref/model/entry/BibEntry.java +++ b/src/main/java/net/sf/jabref/model/entry/BibEntry.java @@ -615,4 +615,29 @@ public BibEntry withField(String field, String value) { setField(field, value); return this; } + + /* + * Returns user comments (arbitrary text before the entry), if they exist. If not, returns the empty String + */ + public String getUserComments() { + + if(parsedSerialization != null) { + + try { + // get the text before the entry + String prolog = parsedSerialization.substring(0, parsedSerialization.indexOf('@')); + + // delete trailing whitespaces (between entry and text) + prolog = prolog.replaceFirst("\\s+$", ""); + + // if there is any non whitespace text, write it + if (prolog.length() > 0) { + return prolog; + } + } catch(StringIndexOutOfBoundsException ignore) { + // if this occurs a broken parsed serialization has been set, so just do nothing + } + } + return ""; + } } From afea9b0a93b107e7ae6ac0e5b78a1e2e654f1440 Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Fri, 8 Jul 2016 14:56:59 +0200 Subject: [PATCH 23/34] Add test for comment in the same line as BibEntry --- .../importer/fileformat/BibtexParserTest.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/test/java/net/sf/jabref/importer/fileformat/BibtexParserTest.java b/src/test/java/net/sf/jabref/importer/fileformat/BibtexParserTest.java index 0e633578097..adc87ca2342 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/BibtexParserTest.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/BibtexParserTest.java @@ -1515,6 +1515,32 @@ public void parsePrecedingComment() throws IOException { assertEquals(bibtexEntry, entry.getParsedSerialization()); } + @Test + public void parseCommentAndEntryInOneLine() throws IOException { + // @formatter:off + String bibtexEntry = "Some random comment that should stay here @Article{test," + Globals.NEWLINE + + " Author = {Foo Bar}," + Globals.NEWLINE + + " Journal = {International Journal of Something}," + Globals.NEWLINE + + " Note = {some note}," + Globals.NEWLINE + + " Number = {1}" + Globals.NEWLINE + + "}"; + // @formatter:on + + // read in bibtex string + ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry)); + + Collection entries = result.getDatabase().getEntries(); + assertEquals(1, entries.size()); + + BibEntry entry = entries.iterator().next(); + assertEquals("test", entry.getCiteKey()); + assertEquals(5, entry.getFieldNames().size()); + Set fields = entry.getFieldNames(); + assertTrue(fields.contains("author")); + assertEquals("Foo Bar", entry.getField("author")); + assertEquals(bibtexEntry, entry.getParsedSerialization()); + } + @Test public void preserveEncodingPrefixInsideEntry() { List parsed = BibtexParser.fromString("@article{test,author={" + Globals.ENCODING_PREFIX + "}}"); From ccddef0e824d3557ca44da603446fe2a38284597 Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Fri, 8 Jul 2016 15:28:00 +0200 Subject: [PATCH 24/34] Remove unnecessary epilog test --- .../exporter/BibDatabaseWriterTest.java | 15 ------- src/test/resources/testbib/bibWithEpilog.bib | 43 ------------------- 2 files changed, 58 deletions(-) delete mode 100644 src/test/resources/testbib/bibWithEpilog.bib diff --git a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java index 1d2c314300c..5f9fe6c7d91 100644 --- a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java +++ b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java @@ -616,19 +616,4 @@ public void writeEntriesInOriginalOrderWhenNoSaveOrderConfigIsSetInMetadata() th , stringWriter.toString()); } - @Test - public void writeEpilog() throws IOException { - Path testBibtexFile = Paths.get("src/test/resources/testbib/bibWithEpilog.bib"); - Charset encoding = StandardCharsets.UTF_8; - ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); - - SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true); - BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), - new Defaults(BibDatabaseMode.BIBTEX)); - - databaseWriter.writePartOfDatabase(stringWriter, context, result.getDatabase().getEntries(), preferences); - try (Scanner scanner = new Scanner(testBibtexFile,encoding.name())) { - assertEquals(scanner.useDelimiter("\\A").next(), stringWriter.toString()); - } - } } diff --git a/src/test/resources/testbib/bibWithEpilog.bib b/src/test/resources/testbib/bibWithEpilog.bib deleted file mode 100644 index 2f2d315b27e..00000000000 --- a/src/test/resources/testbib/bibWithEpilog.bib +++ /dev/null @@ -1,43 +0,0 @@ -% Encoding: UTF-8 - -@Preamble{preamble} - -@String{firstString = {my first string}} -@String{secondString = {}} - -@ARTICLE{1102917, - author = {E. Bardram}, - title = {The trouble with login: on usability and computer security in ubiquitous - computing}, - journal = {Personal Ubiquitous Comput.}, - year = {2005}, - volume = {9}, - pages = {357--367}, - number = {6}, - address = {London, UK}, - bdsk-url-1 = {http://dx.doi.org/10.1007/s00779-005-0347-6}, - doi = {http://dx.doi.org/10.1007/s00779-005-0347-6}, - issn = {1617-4909}, - publisher = {Springer-Verlag} -} - -@INPROCEEDINGS{1137631, - author = {Gustav Bostr\"{o}m and Jaana W\"{a}yrynen and Marine Bod\'{e}n and - Konstantin Beznosov and Philippe Kruchten}, - title = {Extending XP practices to support security requirements engineering}, - booktitle = {SESS '06: Proceedings of the 2006 international workshop on Software - engineering for secure systems}, - year = {2006}, - pages = {11--18}, - address = {New York, NY, USA}, - publisher = {ACM}, - bdsk-url-1 = {http://doi.acm.org/10.1145/1137627.1137631}, - doi = {http://doi.acm.org/10.1145/1137627.1137631}, - file = {:/Volumes/iDisk/Freie Universität Berlin/Semester 9/Softwareprozesse/p11-bostrom.pdf:PDF}, - isbn = {1-59593-411-1}, - location = {Shanghai, China} -} - -@Comment{jabref-meta: databaseType:bibtex;} - -And here comes some arbitrary text that should just be appended again as it is standing here. From 6d1ac659d8fded54934d98fe3d4a368b0f474c2c Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Fri, 8 Jul 2016 15:35:30 +0200 Subject: [PATCH 25/34] Remove redundant test bib file --- .../exporter/BibDatabaseWriterTest.java | 10 +++-- .../bibWithUserCommentBeforeString.bib | 42 ------------------- src/test/resources/testbib/complex.bib | 2 + 3 files changed, 9 insertions(+), 45 deletions(-) delete mode 100644 src/test/resources/testbib/bibWithUserCommentBeforeString.bib diff --git a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java index 5f9fe6c7d91..0495c679845 100644 --- a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java +++ b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java @@ -329,7 +329,7 @@ public void roundtripWithUserCommentAndEntryChange() throws IOException { @Test public void roundtripWithUserCommentBeforeString() throws IOException { - Path testBibtexFile = Paths.get("src/test/resources/testbib/bibWithUserCommentBeforeString.bib"); + Path testBibtexFile = Paths.get("src/test/resources/testbib/complex.bib"); Charset encoding = StandardCharsets.UTF_8; ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); @@ -345,12 +345,16 @@ public void roundtripWithUserCommentBeforeString() throws IOException { @Test public void roundtripWithUserCommentBeforeStringAndChange() throws IOException { - Path testBibtexFile = Paths.get("src/test/resources/testbib/bibWithUserCommentBeforeString.bib"); + Path testBibtexFile = Paths.get("src/test/resources/testbib/complex.bib"); Charset encoding = StandardCharsets.UTF_8; ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); BibtexString string = result.getDatabase().getStringValues().iterator().next(); - string.setContent("my first string"); + if(string.getContent().isEmpty()) { + // do nothing + } else { + string.setContent("my first string"); + } SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true); BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), diff --git a/src/test/resources/testbib/bibWithUserCommentBeforeString.bib b/src/test/resources/testbib/bibWithUserCommentBeforeString.bib deleted file mode 100644 index de20f29181b..00000000000 --- a/src/test/resources/testbib/bibWithUserCommentBeforeString.bib +++ /dev/null @@ -1,42 +0,0 @@ -% Encoding: UTF-8 - -@Preamble{preamble} - -This is some arbitrary user comment that should be preserved - -@String{firstString = {my first string}} - -@ARTICLE{1102917, - author = {E. Bardram}, - title = {The trouble with login: on usability and computer security in ubiquitous - computing}, - journal = {Personal Ubiquitous Comput.}, - year = {2005}, - volume = {9}, - pages = {357--367}, - number = {6}, - address = {London, UK}, - bdsk-url-1 = {http://dx.doi.org/10.1007/s00779-005-0347-6}, - doi = {http://dx.doi.org/10.1007/s00779-005-0347-6}, - issn = {1617-4909}, - publisher = {Springer-Verlag} -} - -@INPROCEEDINGS{1137631, - author = {Gustav Bostr\"{o}m and Jaana W\"{a}yrynen and Marine Bod\'{e}n and - Konstantin Beznosov and Philippe Kruchten}, - title = {Extending XP practices to support security requirements engineering}, - booktitle = {SESS '06: Proceedings of the 2006 international workshop on Software - engineering for secure systems}, - year = {2006}, - pages = {11--18}, - address = {New York, NY, USA}, - publisher = {ACM}, - bdsk-url-1 = {http://doi.acm.org/10.1145/1137627.1137631}, - doi = {http://doi.acm.org/10.1145/1137627.1137631}, - file = {:/Volumes/iDisk/Freie Universität Berlin/Semester 9/Softwareprozesse/p11-bostrom.pdf:PDF}, - isbn = {1-59593-411-1}, - location = {Shanghai, China} -} - -@Comment{jabref-meta: databaseType:bibtex;} diff --git a/src/test/resources/testbib/complex.bib b/src/test/resources/testbib/complex.bib index 09ecb967c41..ee64be35dc5 100644 --- a/src/test/resources/testbib/complex.bib +++ b/src/test/resources/testbib/complex.bib @@ -2,6 +2,8 @@ @Preamble{preamble} +This is some arbitrary user comment that should be preserved + @String{firstString = {my first string}} @String{secondString = {}} From 0972b51d5a5aa775c75be66bc5316253f12fab50 Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Fri, 8 Jul 2016 15:46:00 +0200 Subject: [PATCH 26/34] Remove redundant asserts --- .../logic/bibtex/BibEntryWriterTest.java | 41 ++----------------- 1 file changed, 4 insertions(+), 37 deletions(-) diff --git a/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java b/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java index f286a337493..56cfd05fb3a 100644 --- a/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java +++ b/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java @@ -117,14 +117,8 @@ public void roundTripWithPrependingNewlines() throws IOException { ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry)); Collection entries = result.getDatabase().getEntries(); - assertEquals(1, entries.size()); BibEntry entry = entries.iterator().next(); - assertEquals("test", entry.getCiteKey()); - assertEquals(5, entry.getFieldNames().size()); - Set fields = entry.getFieldNames(); - assertTrue(fields.contains("author")); - assertEquals("Foo Bar", entry.getField("author")); //write out bibtex string StringWriter stringWriter = new StringWriter(); @@ -149,18 +143,15 @@ public void roundTripWithModification() throws IOException { ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry)); Collection entries = result.getDatabase().getEntries(); - assertEquals(1, entries.size()); BibEntry entry = entries.iterator().next(); - assertEquals("test", entry.getCiteKey()); - assertEquals(5, entry.getFieldNames().size()); // Modify entry entry.setField("author", "BlaBla"); Set fields = entry.getFieldNames(); assertTrue(fields.contains("author")); - assertEquals("BlaBla", entry.getField("author")); + assertEquals("BlaBla", entry.getFieldOptional("author").get()); // write out bibtex string StringWriter stringWriter = new StringWriter(); @@ -194,18 +185,15 @@ public void roundTripWithCamelCasingInTheOriginalEntryAndResultInLowerCase() thr ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry)); Collection entries = result.getDatabase().getEntries(); - assertEquals(1, entries.size()); BibEntry entry = entries.iterator().next(); - assertEquals("test", entry.getCiteKey()); - assertEquals(6, entry.getFieldNames().size()); // modify entry entry.setField("author", "BlaBla"); Set fields = entry.getFieldNames(); assertTrue(fields.contains("author")); - assertEquals("BlaBla", entry.getField("author")); + assertEquals("BlaBla", entry.getFieldOptional("author").get()); //write out bibtex string StringWriter stringWriter = new StringWriter(); @@ -240,14 +228,8 @@ public void roundTripWithAppendedNewlines() throws IOException { ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry)); Collection entries = result.getDatabase().getEntries(); - assertEquals(1, entries.size()); BibEntry entry = entries.iterator().next(); - assertEquals("test", entry.getCiteKey()); - assertEquals(5, entry.getFieldNames().size()); - Set fields = entry.getFieldNames(); - assertTrue(fields.contains("author")); - assertEquals("Foo Bar", entry.getField("author")); //write out bibtex string StringWriter stringWriter = new StringWriter(); @@ -313,14 +295,11 @@ public void monthFieldSpecialSyntax() throws IOException { ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry)); Collection entries = result.getDatabase().getEntries(); - assertEquals(1, entries.size()); BibEntry entry = entries.iterator().next(); - assertEquals("test", entry.getCiteKey()); - assertEquals(4, entry.getFieldNames().size()); Set fields = entry.getFieldNames(); assertTrue(fields.contains("month")); - assertEquals("#mar#", entry.getField("month")); + assertEquals("#mar#", entry.getFieldOptional("month").get()); //write out bibtex string StringWriter stringWriter = new StringWriter(); @@ -420,14 +399,8 @@ public void roundTripWithPrecedingCommentTest() throws IOException { ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry)); Collection entries = result.getDatabase().getEntries(); - assertEquals(1, entries.size()); BibEntry entry = entries.iterator().next(); - assertEquals("test", entry.getCiteKey()); - assertEquals(5, entry.getFieldNames().size()); - Set fields = entry.getFieldNames(); - assertTrue(fields.contains("author")); - assertEquals("Foo Bar", entry.getField("author")); //write out bibtex string StringWriter stringWriter = new StringWriter(); @@ -453,17 +426,11 @@ public void roundTripWithPrecedingCommentAndModificationTest() throws IOExceptio ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry)); Collection entries = result.getDatabase().getEntries(); - assertEquals(1, entries.size()); - BibEntry entry = entries.iterator().next(); - assertEquals("test", entry.getCiteKey()); - assertEquals(5, entry.getFieldNames().size()); - Set fields = entry.getFieldNames(); - assertTrue(fields.contains("author")); - assertEquals("Foo Bar", entry.getField("author")); // change the entry entry.setField("author", "John Doe"); + assertEquals("John Doe", entry.getFieldOptional("author").get()); //write out bibtex string StringWriter stringWriter = new StringWriter(); From b4b288a87f7f2a606b025fd8b3d3b00b150346fb Mon Sep 17 00:00:00 2001 From: Stefan Kolb Date: Wed, 13 Jul 2016 13:51:59 +0200 Subject: [PATCH 27/34] Elevate registry actions --- jabref.install4j | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jabref.install4j b/jabref.install4j index d1bc2f25767..14265b42010 100644 --- a/jabref.install4j +++ b/jabref.install4j @@ -369,7 +369,7 @@ return true; context.getBooleanVariable("addToDockAction") - + From 67a48859b5b2973f01bb41ffddce802e8825f3ac Mon Sep 17 00:00:00 2001 From: Stefan Kolb Date: Wed, 13 Jul 2016 14:21:15 +0200 Subject: [PATCH 28/34] Delete stuff --- jabref.install4j | 76 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 12 deletions(-) diff --git a/jabref.install4j b/jabref.install4j index 14265b42010..a6bc9763025 100644 --- a/jabref.install4j +++ b/jabref.install4j @@ -116,7 +116,16 @@ - + + + + + + + + + + @@ -148,14 +157,6 @@ context.getBooleanVariable("sys.confirmedUpdateInstallation") - - - - - - - - @@ -163,7 +164,7 @@ - if (Util.hasFullAdminRights()) { + if (Util.hasFullAdminRights() || Util.isAdminGroup()) { context.setInstallationDirectory(context.getInstallationDirectory()); } else { if (Util.isAtLeastWindowsVista()) { @@ -398,7 +399,7 @@ return true; - Util.hasFullAdminRights() + Util.hasFullAdminRights() || Util.isAdminGroup() @@ -422,7 +423,7 @@ return true; - !Util.hasFullAdminRights() + !(Util.hasFullAdminRights() || Util.isAdminGroup()) @@ -612,6 +613,57 @@ return true; + + + + + + + + + + + + + SOFTWARE\JabRef + + + false + + + + com.install4j.api.windows.RegistryRoot + HKEY_LOCAL_MACHINE + + + + + + Util.hasFullAdminRights() || Util.isAdminGroup() + + + + + + + SOFTWARE\JabRef + + + false + + + + com.install4j.api.windows.RegistryRoot + HKEY_CURRENT_USER + + + + + + !(Util.hasFullAdminRights() || Util.isAdminGroup()) + + + From 9000f1c025f1d0686096c6223dd668415640c5ae Mon Sep 17 00:00:00 2001 From: Stefan Kolb Date: Wed, 13 Jul 2016 15:54:34 +0200 Subject: [PATCH 29/34] Revert "Elevate registry actions" This reverts commit b4b288a87f7f2a606b025fd8b3d3b00b150346fb. --- jabref.install4j | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jabref.install4j b/jabref.install4j index a6bc9763025..8efb4377bb7 100644 --- a/jabref.install4j +++ b/jabref.install4j @@ -370,7 +370,7 @@ return true; context.getBooleanVariable("addToDockAction") - + From d970a4c1e3580688246cbb4aa91db0106f59ac8d Mon Sep 17 00:00:00 2001 From: Stefan Kolb Date: Wed, 13 Jul 2016 15:54:47 +0200 Subject: [PATCH 30/34] Revert "Delete stuff" This reverts commit 67a48859b5b2973f01bb41ffddce802e8825f3ac. --- jabref.install4j | 76 ++++++++---------------------------------------- 1 file changed, 12 insertions(+), 64 deletions(-) diff --git a/jabref.install4j b/jabref.install4j index 8efb4377bb7..d1bc2f25767 100644 --- a/jabref.install4j +++ b/jabref.install4j @@ -116,16 +116,7 @@ - - - - - - - - - - + @@ -157,6 +148,14 @@ context.getBooleanVariable("sys.confirmedUpdateInstallation") + + + + + + + + @@ -164,7 +163,7 @@ - if (Util.hasFullAdminRights() || Util.isAdminGroup()) { + if (Util.hasFullAdminRights()) { context.setInstallationDirectory(context.getInstallationDirectory()); } else { if (Util.isAtLeastWindowsVista()) { @@ -399,7 +398,7 @@ return true; - Util.hasFullAdminRights() || Util.isAdminGroup() + Util.hasFullAdminRights() @@ -423,7 +422,7 @@ return true; - !(Util.hasFullAdminRights() || Util.isAdminGroup()) + !Util.hasFullAdminRights() @@ -613,57 +612,6 @@ return true; - - - - - - - - - - - - - SOFTWARE\JabRef - - - false - - - - com.install4j.api.windows.RegistryRoot - HKEY_LOCAL_MACHINE - - - - - - Util.hasFullAdminRights() || Util.isAdminGroup() - - - - - - - SOFTWARE\JabRef - - - false - - - - com.install4j.api.windows.RegistryRoot - HKEY_CURRENT_USER - - - - - - !(Util.hasFullAdminRights() || Util.isAdminGroup()) - - - From 98cbe985c7361eddd3b527dabc94b23a433b0281 Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Thu, 14 Jul 2016 13:33:32 +0200 Subject: [PATCH 31/34] Use optional in assert --- .../java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java b/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java index 56cfd05fb3a..a23e7379855 100644 --- a/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java +++ b/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java @@ -4,6 +4,7 @@ import java.io.StringReader; import java.io.StringWriter; import java.util.Collection; +import java.util.Optional; import java.util.Set; import net.sf.jabref.Globals; @@ -151,7 +152,7 @@ public void roundTripWithModification() throws IOException { Set fields = entry.getFieldNames(); assertTrue(fields.contains("author")); - assertEquals("BlaBla", entry.getFieldOptional("author").get()); + assertEquals(Optional.of("BlaBla"), entry.getFieldOptional("author")); // write out bibtex string StringWriter stringWriter = new StringWriter(); From 72de4ce8bf15cd8759bf4af5bf1e19f32340bd5a Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Thu, 14 Jul 2016 13:49:58 +0200 Subject: [PATCH 32/34] Remove duplicate test --- .../jabref/exporter/BibDatabaseWriterTest.java | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java index 0d1a7457a41..454bbc8e0a7 100644 --- a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java +++ b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java @@ -326,23 +326,7 @@ public void roundtripWithUserCommentAndEntryChange() throws IOException { assertEquals(scanner.useDelimiter("\\A").next(), stringWriter.toString()); } } - - @Test - public void roundtripWithUserCommentBeforeString() throws IOException { - Path testBibtexFile = Paths.get("src/test/resources/testbib/complex.bib"); - Charset encoding = StandardCharsets.UTF_8; - ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); - - SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true); - BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), - new Defaults(BibDatabaseMode.BIBTEX)); - - databaseWriter.writePartOfDatabase(stringWriter, context, result.getDatabase().getEntries(), preferences); - try (Scanner scanner = new Scanner(testBibtexFile,encoding.name())) { - assertEquals(scanner.useDelimiter("\\A").next(), stringWriter.toString()); - } - } - + @Test public void roundtripWithUserCommentBeforeStringAndChange() throws IOException { Path testBibtexFile = Paths.get("src/test/resources/testbib/complex.bib"); From e5e5c4481e6e441eceb8fc957ca9ec1973ad7e68 Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Thu, 14 Jul 2016 13:59:38 +0200 Subject: [PATCH 33/34] Remove unnecessary asserts --- .../exporter/BibDatabaseWriterTest.java | 2 +- .../logic/bibtex/BibEntryWriterTest.java | 42 +------------------ 2 files changed, 3 insertions(+), 41 deletions(-) diff --git a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java index 454bbc8e0a7..57fef45bc27 100644 --- a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java +++ b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java @@ -326,7 +326,7 @@ public void roundtripWithUserCommentAndEntryChange() throws IOException { assertEquals(scanner.useDelimiter("\\A").next(), stringWriter.toString()); } } - + @Test public void roundtripWithUserCommentBeforeStringAndChange() throws IOException { Path testBibtexFile = Paths.get("src/test/resources/testbib/complex.bib"); diff --git a/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java b/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java index a23e7379855..50938917de9 100644 --- a/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java +++ b/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java @@ -84,16 +84,8 @@ public void roundTripTest() throws IOException { // read in bibtex string ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry)); - Collection entries = result.getDatabase().getEntries(); - assertEquals(1, entries.size()); - BibEntry entry = entries.iterator().next(); - assertEquals("test", entry.getCiteKey()); - assertEquals(5, entry.getFieldNames().size()); - Set fields = entry.getFieldNames(); - assertTrue(fields.contains("author")); - assertEquals("Foo Bar", entry.getField("author")); //write out bibtex string StringWriter stringWriter = new StringWriter(); @@ -116,9 +108,7 @@ public void roundTripWithPrependingNewlines() throws IOException { // read in bibtex string ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry)); - Collection entries = result.getDatabase().getEntries(); - BibEntry entry = entries.iterator().next(); //write out bibtex string @@ -142,18 +132,12 @@ public void roundTripWithModification() throws IOException { // read in bibtex string ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry)); - Collection entries = result.getDatabase().getEntries(); - BibEntry entry = entries.iterator().next(); // Modify entry entry.setField("author", "BlaBla"); - Set fields = entry.getFieldNames(); - assertTrue(fields.contains("author")); - assertEquals(Optional.of("BlaBla"), entry.getFieldOptional("author")); - // write out bibtex string StringWriter stringWriter = new StringWriter(); writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX); @@ -184,21 +168,14 @@ public void roundTripWithCamelCasingInTheOriginalEntryAndResultInLowerCase() thr // read in bibtex string ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry)); - Collection entries = result.getDatabase().getEntries(); - BibEntry entry = entries.iterator().next(); // modify entry entry.setField("author", "BlaBla"); - Set fields = entry.getFieldNames(); - assertTrue(fields.contains("author")); - assertEquals("BlaBla", entry.getFieldOptional("author").get()); - //write out bibtex string StringWriter stringWriter = new StringWriter(); - writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX); String actual = stringWriter.toString(); @@ -227,9 +204,7 @@ public void roundTripWithAppendedNewlines() throws IOException { // read in bibtex string ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry)); - Collection entries = result.getDatabase().getEntries(); - BibEntry entry = entries.iterator().next(); //write out bibtex string @@ -262,16 +237,8 @@ public void multipleWritesWithoutModification() throws IOException { private String testSingleWrite(String bibtexEntry) throws IOException { // read in bibtex string ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry)); - Collection entries = result.getDatabase().getEntries(); - assertEquals(1, entries.size()); - BibEntry entry = entries.iterator().next(); - assertEquals("test", entry.getCiteKey()); - assertEquals(5, entry.getFieldNames().size()); - Set fields = entry.getFieldNames(); - assertTrue(fields.contains("author")); - assertEquals("Foo Bar", entry.getField("author")); //write out bibtex string StringWriter stringWriter = new StringWriter(); @@ -294,10 +261,10 @@ public void monthFieldSpecialSyntax() throws IOException { // read in bibtex string ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry)); - Collection entries = result.getDatabase().getEntries(); - BibEntry entry = entries.iterator().next(); + + // modify month field Set fields = entry.getFieldNames(); assertTrue(fields.contains("month")); assertEquals("#mar#", entry.getFieldOptional("month").get()); @@ -331,7 +298,6 @@ public void addFieldWithLongerLength() throws IOException { //write out bibtex string StringWriter stringWriter = new StringWriter(); - writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX); String actual = stringWriter.toString(); @@ -398,9 +364,7 @@ public void roundTripWithPrecedingCommentTest() throws IOException { // read in bibtex string ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry)); - Collection entries = result.getDatabase().getEntries(); - BibEntry entry = entries.iterator().next(); //write out bibtex string @@ -425,13 +389,11 @@ public void roundTripWithPrecedingCommentAndModificationTest() throws IOExceptio // read in bibtex string ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry)); - Collection entries = result.getDatabase().getEntries(); BibEntry entry = entries.iterator().next(); // change the entry entry.setField("author", "John Doe"); - assertEquals("John Doe", entry.getFieldOptional("author").get()); //write out bibtex string StringWriter stringWriter = new StringWriter(); From a8a7b202795887b93fb123c71046b1a3012a5181 Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Thu, 14 Jul 2016 14:06:43 +0200 Subject: [PATCH 34/34] Remove unused import of Optional --- src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java b/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java index 50938917de9..be74d915a9f 100644 --- a/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java +++ b/src/test/java/net/sf/jabref/logic/bibtex/BibEntryWriterTest.java @@ -4,7 +4,6 @@ import java.io.StringReader; import java.io.StringWriter; import java.util.Collection; -import java.util.Optional; import java.util.Set; import net.sf.jabref.Globals;