Skip to content

Commit

Permalink
Add test for alphabetic serialization of fields in BibEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Oct 26, 2019
1 parent 2d92872 commit 9fd49bc
Showing 1 changed file with 56 additions and 19 deletions.
75 changes: 56 additions & 19 deletions src/test/java/org/jabref/logic/bibtex/BibEntryWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;

public class BibEntryWriterTest {
class BibEntryWriterTest {

private static ImportFormatPreferences importFormatPreferences;
private BibEntryWriter writer;
private final FileUpdateMonitor fileMonitor = new DummyFileUpdateMonitor();

@BeforeEach
public void setUpWriter() {
void setUpWriter() {
importFormatPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS);
LatexFieldFormatterPreferences latexFieldFormatterPreferences = mock(LatexFieldFormatterPreferences.class, Answers.RETURNS_DEEP_STUBS);
writer = new BibEntryWriter(new LatexFieldFormatter(latexFieldFormatterPreferences), new BibEntryTypesManager());
}

@Test
public void testSerialization() throws IOException {
void testSerialization() throws IOException {
StringWriter stringWriter = new StringWriter();

BibEntry entry = new BibEntry(StandardEntryType.Article);
Expand Down Expand Up @@ -72,7 +72,7 @@ public void testSerialization() throws IOException {
}

@Test
public void writeOtherTypeTest() throws Exception {
void writeOtherTypeTest() throws Exception {
String expected = OS.NEWLINE + "@Other{test," + OS.NEWLINE +
" comment = {testentry}," + OS.NEWLINE +
"}" + OS.NEWLINE;
Expand Down Expand Up @@ -165,7 +165,7 @@ void writeEntryWithOrFieldBothFieldsPresent() throws Exception {
}

@Test
public void writeReallyUnknownTypeTest() throws Exception {
void writeReallyUnknownTypeTest() throws Exception {
String expected = OS.NEWLINE + "@Reallyunknowntype{test," + OS.NEWLINE +
" comment = {testentry}," + OS.NEWLINE +
"}" + OS.NEWLINE;
Expand All @@ -184,7 +184,7 @@ public void writeReallyUnknownTypeTest() throws Exception {
}

@Test
public void roundTripTest() throws IOException {
void roundTripTest() throws IOException {
// @formatter:off
String bibtexEntry = "@Article{test," + OS.NEWLINE +
" Author = {Foo Bar}," + OS.NEWLINE +
Expand All @@ -208,7 +208,7 @@ public void roundTripTest() throws IOException {
}

@Test
public void roundTripWithPrependingNewlines() throws IOException {
void roundTripWithPrependingNewlines() throws IOException {
// @formatter:off
String bibtexEntry = "\r\n@Article{test," + OS.NEWLINE +
" Author = {Foo Bar}," + OS.NEWLINE +
Expand All @@ -232,7 +232,7 @@ public void roundTripWithPrependingNewlines() throws IOException {
}

@Test
public void roundTripWithModification() throws IOException {
void roundTripWithModification() throws IOException {
// @formatter:off
String bibtexEntry = OS.NEWLINE + "@Article{test," + OS.NEWLINE +
" Author = {Foo Bar}," + OS.NEWLINE +
Expand Down Expand Up @@ -267,7 +267,7 @@ public void roundTripWithModification() throws IOException {
}

@Test
public void roundTripWithCamelCasingInTheOriginalEntryAndResultInLowerCase() throws IOException {
void roundTripWithCamelCasingInTheOriginalEntryAndResultInLowerCase() throws IOException {
// @formatter:off
String bibtexEntry = OS.NEWLINE + "@Article{test," + OS.NEWLINE +
" Author = {Foo Bar}," + OS.NEWLINE +
Expand Down Expand Up @@ -304,7 +304,7 @@ public void roundTripWithCamelCasingInTheOriginalEntryAndResultInLowerCase() thr
}

@Test
public void testEntryTypeChange() throws IOException {
void testEntryTypeChange() throws IOException {
// @formatter:off
String expected = OS.NEWLINE + "@Article{test," + OS.NEWLINE +
" author = {BlaBla}," + OS.NEWLINE +
Expand Down Expand Up @@ -341,7 +341,7 @@ public void testEntryTypeChange() throws IOException {
}

@Test
public void roundTripWithAppendedNewlines() throws IOException {
void roundTripWithAppendedNewlines() throws IOException {
// @formatter:off
String bibtexEntry = "@Article{test," + OS.NEWLINE +
" Author = {Foo Bar}," + OS.NEWLINE +
Expand All @@ -366,7 +366,7 @@ public void roundTripWithAppendedNewlines() throws IOException {
}

@Test
public void multipleWritesWithoutModification() throws IOException {
void multipleWritesWithoutModification() throws IOException {
// @formatter:off
String bibtexEntry = "@Article{test," + OS.NEWLINE +
" Author = {Foo Bar}," + OS.NEWLINE +
Expand Down Expand Up @@ -399,7 +399,7 @@ private String testSingleWrite(String bibtexEntry) throws IOException {
}

@Test
public void monthFieldSpecialSyntax() throws IOException {
void monthFieldSpecialSyntax() throws IOException {
// @formatter:off
String bibtexEntry = "@Article{test," + OS.NEWLINE +
" Author = {Foo Bar}," + OS.NEWLINE +
Expand Down Expand Up @@ -427,7 +427,7 @@ public void monthFieldSpecialSyntax() throws IOException {
}

@Test
public void addFieldWithLongerLength() throws IOException {
void addFieldWithLongerLength() throws IOException {
// @formatter:off
String bibtexEntry = OS.NEWLINE + OS.NEWLINE + "@Article{test," + OS.NEWLINE +
" author = {BlaBla}," + OS.NEWLINE +
Expand Down Expand Up @@ -463,7 +463,7 @@ public void addFieldWithLongerLength() throws IOException {
}

@Test
public void doNotWriteEmptyFields() throws IOException {
void doNotWriteEmptyFields() throws IOException {
StringWriter stringWriter = new StringWriter();

BibEntry entry = new BibEntry(StandardEntryType.Article);
Expand All @@ -482,7 +482,7 @@ public void doNotWriteEmptyFields() throws IOException {
}

@Test
public void trimFieldContents() throws IOException {
void trimFieldContents() throws IOException {
StringWriter stringWriter = new StringWriter();

BibEntry entry = new BibEntry(StandardEntryType.Article);
Expand All @@ -500,7 +500,7 @@ public void trimFieldContents() throws IOException {
}

@Test
public void writeThrowsErrorIfFieldContainsUnbalancedBraces() {
void writeThrowsErrorIfFieldContainsUnbalancedBraces() {
StringWriter stringWriter = new StringWriter();

BibEntry entry = new BibEntry(StandardEntryType.Article);
Expand All @@ -510,7 +510,7 @@ public void writeThrowsErrorIfFieldContainsUnbalancedBraces() {
}

@Test
public void roundTripWithPrecedingCommentTest() throws IOException {
void roundTripWithPrecedingCommentTest() throws IOException {
// @formatter:off
String bibtexEntry = "% Some random comment that should stay here" + OS.NEWLINE +
"@Article{test," + OS.NEWLINE +
Expand All @@ -535,7 +535,7 @@ public void roundTripWithPrecedingCommentTest() throws IOException {
}

@Test
public void roundTripWithPrecedingCommentAndModificationTest() throws IOException {
void roundTripWithPrecedingCommentAndModificationTest() throws IOException {
// @formatter:off
String bibtexEntry = "% Some random comment that should stay here" + OS.NEWLINE +
"@Article{test," + OS.NEWLINE +
Expand Down Expand Up @@ -570,4 +570,41 @@ public void roundTripWithPrecedingCommentAndModificationTest() throws IOExceptio

assertEquals(expected, actual);
}

@Test
void alphabeticSerialization() throws IOException {
StringWriter stringWriter = new StringWriter();

BibEntry entry = new BibEntry(StandardEntryType.Article);
//required fields
entry.setField(StandardField.AUTHOR, "Foo Bar");
entry.setField(StandardField.JOURNALTITLE, "International Journal of Something");
entry.setField(StandardField.TITLE, "Title");
entry.setField(StandardField.DATE, "2019-10-16");
//optional fields
entry.setField(StandardField.NUMBER, "1");
entry.setField(StandardField.NOTE, "some note");
//unknown fields
entry.setField(StandardField.YEAR, "2019");
entry.setField(StandardField.CHAPTER, "chapter");

writer.write(entry, stringWriter, BibDatabaseMode.BIBLATEX);

String actual = stringWriter.toString();

// @formatter:off
String expected = OS.NEWLINE + "@Article{," + OS.NEWLINE +
" author = {Foo Bar}," + OS.NEWLINE +
" date = {2019-10-16}," + OS.NEWLINE +
" journaltitle = {International Journal of Something}," + OS.NEWLINE +
" title = {Title}," + OS.NEWLINE +
" note = {some note}," + OS.NEWLINE +
" number = {1}," + OS.NEWLINE +
" chapter = {chapter}," + OS.NEWLINE +
" year = {2019}," + OS.NEWLINE +
"}";

This comment has been minimized.

Copy link
@martinacat

martinacat Oct 26, 2019

Contributor

I believe this might need another new line at the end of the file to pass

This comment has been minimized.

Copy link
@tobiasdiez

tobiasdiez Oct 26, 2019

Author Member

Thanks for pointing this out! Is fixed now. Shouldn't have pushed to master directly...

// @formatter:on

assertEquals(expected, actual);
}
}

0 comments on commit 9fd49bc

Please sign in to comment.