Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor BibEntry deprecated method #4554

Merged
merged 14 commits into from
Dec 28, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.util.StandardFileType;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexEntryTypes;
import org.jabref.model.entry.FieldName;

/**
Expand Down Expand Up @@ -255,7 +256,7 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException {
if (!comments.isEmpty()) { // set comment if present
hm.put(FieldName.COMMENT, String.join(";", comments));
}
BibEntry b = new BibEntry(bibtexType);
BibEntry b = new BibEntry(BibtexEntryTypes.getType(bibtexType).get());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may throw a NPE, because BibtexEntryTypes.getType(bibtexType) may return an empty optional. To handle such cases, one would need to add code similar to how you did it in BibtexParser. In order to reuse this code, I would propose to add a getTypeOrDefault method to BibtexEntryTypes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. There are still a few other places where the new method should be used, this is also the reason why some of the unit tests fail at the moment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Fixed several tests all related with this NPE problem but some are failing. I switch to master and noticed that these are also failing on master so I believe these are non related.

b.setField(hm);
bibItems.add(b);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.jabref.model.database.KeyCollisionException;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexString;
import org.jabref.model.entry.BibtexEntryTypes;
import org.jabref.model.entry.CustomEntryType;
import org.jabref.model.entry.EntryType;
import org.jabref.model.entry.FieldName;
Expand Down Expand Up @@ -504,7 +505,7 @@ private String parsePreamble() throws IOException {
}

private BibEntry parseEntry(String entryType) throws IOException {
BibEntry result = new BibEntry(entryType);
BibEntry result = new BibEntry(BibtexEntryTypes.getType(entryType).get());
skipWhitespace();
consume('{', '(');
int character = peek();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.util.StandardFileType;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexEntryTypes;
import org.jabref.model.entry.FieldName;

/**
Expand Down Expand Up @@ -98,7 +99,7 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException {

// Copac does not contain enough information on the type of the
// document. A book is assumed.
BibEntry b = new BibEntry("book");
BibEntry b = new BibEntry(BibtexEntryTypes.BOOK);

String[] lines = entry.split("\n");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.jabref.logic.util.StandardFileType;
import org.jabref.model.entry.AuthorList;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexEntryTypes;
import org.jabref.model.entry.FieldName;

/**
Expand Down Expand Up @@ -253,7 +254,7 @@ else if ("P".equals(prefix)) {
hm.put(FieldName.PAGES, artnum);
}

BibEntry b = new BibEntry(type);
BibEntry b = new BibEntry(BibtexEntryTypes.getType(type).get());
b.setField(hm);
if (!b.getFieldNames().isEmpty()) {
bibitems.add(b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.jabref.logic.importer.ParseException;
import org.jabref.logic.importer.Parser;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexEntryTypes;
import org.jabref.model.entry.FieldName;

import com.google.common.base.Strings;
Expand Down Expand Up @@ -360,7 +361,7 @@ private BibEntry parseEntry(Element e) {
* dann @incollection annehmen, wenn weder ISBN noch
* ZDB-ID vorhanden sind.
*/
BibEntry result = new BibEntry(entryType);
BibEntry result = new BibEntry(BibtexEntryTypes.getType(entryType).get());

// Zuordnung der Felder in Abhängigkeit vom Dokumenttyp
if (author != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.jabref.logic.util.StandardFileType;
import org.jabref.model.entry.AuthorList;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexEntryTypes;
import org.jabref.model.entry.FieldName;

/**
Expand Down Expand Up @@ -120,7 +121,7 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException {
}
}
}
BibEntry b = new BibEntry(type);
BibEntry b = new BibEntry(BibtexEntryTypes.getType(type).get());
b.setField(h);

bibitems.add(b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.util.StandardFileType;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexEntryTypes;
import org.jabref.model.entry.FieldName;
import org.jabref.model.entry.Month;

Expand Down Expand Up @@ -301,7 +302,7 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException {
continue;
}

BibEntry b = new BibEntry(Type);
BibEntry b = new BibEntry(BibtexEntryTypes.getType(Type).get());
// id assumes an existing database so don't

// Remove empty fields:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import org.jabref.logic.importer.fileformat.medline.Text;
import org.jabref.logic.util.StandardFileType;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexEntryTypes;
import org.jabref.model.entry.FieldName;
import org.jabref.model.strings.StringUtil;

Expand Down Expand Up @@ -262,7 +263,7 @@ private void parseBookArticle(PubmedBookArticle currentArticle, List<BibEntry> b
putIfValueNotNull(fields, "pubstatus", bookData.getPublicationStatus());
}

BibEntry entry = new BibEntry("article");
BibEntry entry = new BibEntry(BibtexEntryTypes.ARTICLE);
entry.setField(fields);

bibItems.add(entry);
Expand Down Expand Up @@ -410,7 +411,7 @@ private void parseArticle(PubmedArticle article, List<BibEntry> bibItems) {
}
}

BibEntry entry = new BibEntry("article");
BibEntry entry = new BibEntry(BibtexEntryTypes.ARTICLE);
entry.setField(fields);

bibItems.add(entry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.jabref.logic.util.StandardFileType;
import org.jabref.model.entry.AuthorList;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexEntryTypes;
import org.jabref.model.entry.FieldName;

/**
Expand Down Expand Up @@ -208,7 +209,7 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException {
fields.put(FieldName.COMMENT, comment);
}

BibEntry b = new BibEntry(type);
BibEntry b = new BibEntry(BibtexEntryTypes.getType(type).get());

// Remove empty fields:
fields.entrySet().stream().filter(n -> n.getValue().trim().isEmpty()).forEach(fields::remove);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.jabref.logic.util.StandardFileType;
import org.jabref.model.entry.AuthorList;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexEntryTypes;
import org.jabref.model.entry.FieldName;

/**
Expand Down Expand Up @@ -207,7 +208,7 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException {
// Move the "chaptertitle" to just "title":
h.put(FieldName.TITLE, h.remove("chaptertitle"));
}
BibEntry b = new BibEntry(entryType);
BibEntry b = new BibEntry(BibtexEntryTypes.getType(entryType).get());
b.setField(h);

bibitems.add(b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.jabref.logic.util.StandardFileType;
import org.jabref.model.entry.AuthorList;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexEntryTypes;
import org.jabref.model.entry.FieldName;
import org.jabref.model.entry.Month;

Expand Down Expand Up @@ -265,7 +266,7 @@ else if ("AV".equals(tag)) {

// create one here
// type is set in the loop above
BibEntry entry = new BibEntry(type);
BibEntry entry = new BibEntry(BibtexEntryTypes.getType(type).get());
entry.setField(fields);
// month has a special treatment as we use the separate method "setMonth" of BibEntry instead of directly setting the value
month.ifPresent(entry::setMonth);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.jabref.logic.util.StandardFileType;
import org.jabref.model.entry.AuthorList;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexEntryTypes;
import org.jabref.model.entry.FieldName;

/**
Expand Down Expand Up @@ -180,7 +181,7 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException {

}

BibEntry b = new BibEntry(type);
BibEntry b = new BibEntry(BibtexEntryTypes.getType(type).get());
// create one here
b.setField(h);

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/logic/msbib/BibTeXConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.stream.Collectors;

import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexEntryTypes;
import org.jabref.model.entry.FieldName;
import org.jabref.model.entry.Month;

Expand All @@ -29,7 +30,7 @@ public static BibEntry convert(MSBibEntry entry) {
Map<String, String> fieldValues = new HashMap<>();

String bibTexEntryType = MSBibMapping.getBiblatexEntryType(entry.getType());
result = new BibEntry(bibTexEntryType);
result = new BibEntry(BibtexEntryTypes.getType(bibTexEntryType).get());

// add String fields
for (Map.Entry<String, String> field : entry.fields.entrySet()) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/logic/util/TestEntry.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.logic.util;

import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexEntryTypes;
import org.jabref.model.entry.FieldName;

public class TestEntry {
Expand All @@ -10,7 +11,7 @@ private TestEntry() {

public static BibEntry getTestEntry() {

BibEntry entry = new BibEntry("article");
BibEntry entry = new BibEntry(BibtexEntryTypes.ARTICLE);
entry.setCiteKey("Smith2016");
entry.setField(FieldName.AUTHOR, "Smith, Bill and Jones, Bob and Williams, Jeff");
entry.setField(FieldName.EDITOR, "Taylor, Phil");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/model/entry/BibEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private BibEntry(String id, String type) {
* Constructs a new BibEntry. The internal ID is set to IdGenerator.next()
*/
public BibEntry(EntryType type) {
this(type.getName());
this(IdGenerator.next(),type.getName());
}

public Optional<FieldChange> setMonth(Month parsedMonth) {
Expand Down Expand Up @@ -544,7 +544,7 @@ private boolean atLeastOnePresent(String[] fieldsToCheck, BibDatabase database)
*/
@Override
public Object clone() {
BibEntry clone = new BibEntry(type.getValue());
BibEntry clone = new BibEntry(IdGenerator.next(),type.getValue());
clone.fields = FXCollections.observableMap(new ConcurrentHashMap<>(fields));
return clone;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.jabref.logic.util.io.AutoLinkPreferences;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexEntryTypes;
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.metadata.FilePreferences;

Expand All @@ -30,7 +31,7 @@ public class AutoSetFileLinksUtilTest {
private final AutoLinkPreferences autoLinkPrefs = new AutoLinkPreferences(false, "", true, ';');
private final BibDatabaseContext databaseContext = mock(BibDatabaseContext.class);
private final ExternalFileTypes externalFileTypes = mock(ExternalFileTypes.class);
private final BibEntry entry = new BibEntry("article");
private final BibEntry entry = new BibEntry(BibtexEntryTypes.ARTICLE);

@BeforeEach
public void setUp(@TempDirectory.TempDir Path folder) throws Exception {
Expand Down
11 changes: 6 additions & 5 deletions src/test/java/org/jabref/logic/TypedBibEntryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexEntryTypes;

import org.jabref.model.entry.CustomEntryType;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -14,7 +15,7 @@ public class TypedBibEntryTest {

@Test
public void hasAllRequiredFieldsFail() {
BibEntry e = new BibEntry(BibtexEntryTypes.ARTICLE.getName());
BibEntry e = new BibEntry(BibtexEntryTypes.ARTICLE);
e.setField("author", "abc");
e.setField("title", "abc");
e.setField("journal", "abc");
Expand All @@ -25,7 +26,7 @@ public void hasAllRequiredFieldsFail() {

@Test
public void hasAllRequiredFields() {
BibEntry e = new BibEntry(BibtexEntryTypes.ARTICLE.getName());
BibEntry e = new BibEntry(BibtexEntryTypes.ARTICLE);
e.setField("author", "abc");
e.setField("title", "abc");
e.setField("journal", "abc");
Expand All @@ -37,23 +38,23 @@ public void hasAllRequiredFields() {

@Test
public void hasAllRequiredFieldsForUnknownTypeReturnsTrue() {
BibEntry e = new BibEntry("articlllleeeee");
BibEntry e = new BibEntry(new CustomEntryType("articlllleeeee","required","optional"));

TypedBibEntry typedEntry = new TypedBibEntry(e, BibDatabaseMode.BIBTEX);
assertTrue(typedEntry.hasAllRequiredFields());
}

@Test
public void getTypeForDisplayReturnsTypeName() {
BibEntry e = new BibEntry(BibtexEntryTypes.INPROCEEDINGS.getName());
BibEntry e = new BibEntry(BibtexEntryTypes.INPROCEEDINGS);

TypedBibEntry typedEntry = new TypedBibEntry(e, BibDatabaseMode.BIBTEX);
assertEquals("InProceedings", typedEntry.getTypeForDisplay());
}

@Test
public void getTypeForDisplayForUnknownTypeCapitalizeFirstLetter() {
BibEntry e = new BibEntry("articlllleeeee");
BibEntry e = new BibEntry(new CustomEntryType("articlllleeeee","required","optional"));

TypedBibEntry typedEntry = new TypedBibEntry(e, BibDatabaseMode.BIBTEX);
assertEquals("Articlllleeeee", typedEntry.getTypeForDisplay());
Expand Down
12 changes: 5 additions & 7 deletions src/test/java/org/jabref/logic/bibtex/BibEntryWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
import org.jabref.logic.importer.fileformat.BibtexParser;
import org.jabref.logic.util.OS;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BiblatexEntryTypes;
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.entry.*;
import org.jabref.model.util.DummyFileUpdateMonitor;
import org.jabref.model.util.FileUpdateMonitor;

Expand Down Expand Up @@ -43,7 +41,7 @@ public void setUpWriter() {
public void testSerialization() throws IOException {
StringWriter stringWriter = new StringWriter();

BibEntry entry = new BibEntry("article");
BibEntry entry = new BibEntry(BibtexEntryTypes.ARTICLE);
//set a required field
entry.setField("author", "Foo Bar");
entry.setField("journal", "International Journal of Something");
Expand Down Expand Up @@ -405,7 +403,7 @@ public void addFieldWithLongerLength() throws IOException {
public void doNotWriteEmptyFields() throws IOException {
StringWriter stringWriter = new StringWriter();

BibEntry entry = new BibEntry("article");
BibEntry entry = new BibEntry(BibtexEntryTypes.ARTICLE);
entry.setField("author", " ");
entry.setField("note", "some note");

Expand All @@ -424,7 +422,7 @@ public void doNotWriteEmptyFields() throws IOException {
public void trimFieldContents() throws IOException {
StringWriter stringWriter = new StringWriter();

BibEntry entry = new BibEntry("article");
BibEntry entry = new BibEntry(BibtexEntryTypes.ARTICLE);
entry.setField("note", " some note \t");

writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX);
Expand All @@ -442,7 +440,7 @@ public void trimFieldContents() throws IOException {
public void writeThrowsErrorIfFieldContainsUnbalancedBraces() {
StringWriter stringWriter = new StringWriter();

BibEntry entry = new BibEntry("article");
BibEntry entry = new BibEntry(BibtexEntryTypes.ARTICLE);
entry.setField("note", "some text with unbalanced { braces");

assertThrows(IOException.class, () -> writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX));
Expand Down
Loading