Skip to content

Commit

Permalink
remove Deprecated getText() (JabRef#11841)
Browse files Browse the repository at this point in the history
* remove Deprecated getText()

remove Deprecated getText()

* Fix the code to pass unit test

Maybe I should add more unit test based on my change?

* Add more unit test to support my change

1. more unit test
2. rollback logic to follow old code which assumed that databaseContext is never null

* change to @ParameterizedTest test

change to @ParameterizedTest test

* update follow requested change

1. remove test prefix
2. remove unnecessary inline the helper method
3. fix a type
4. two minor refactor in both files.

* remove unused import

remove import org.jspecify.annotations.Nullable;

---------

Co-authored-by: Oliver Kopp <kopp.dev@gmail.com>
  • Loading branch information
2 people authored and ExrosZ-Alt committed Oct 17, 2024
1 parent 7ca3685 commit e02ebb5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 37 deletions.
36 changes: 21 additions & 15 deletions src/main/java/org/jabref/logic/layout/LayoutEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ public void setPostFormatter(LayoutFormatter formatter) {
this.postFormatter = formatter;
}

public String doLayout(BibEntry bibtex, BibDatabase database) {
public String doLayout(BibEntry bibEntry, BibDatabase database) {
switch (type) {
case LayoutHelper.IS_LAYOUT_TEXT:
return text;
case LayoutHelper.IS_SIMPLE_COMMAND:
String value = bibtex.getResolvedFieldOrAlias(FieldFactory.parseField(text), database).orElse("");
String value = bibEntry.getResolvedFieldOrAlias(FieldFactory.parseField(text), database).orElse("");

// If a post formatter has been set, call it:
if (postFormatter != null) {
Expand All @@ -214,22 +214,31 @@ public String doLayout(BibEntry bibtex, BibDatabase database) {
return value;
case LayoutHelper.IS_FIELD_START:
case LayoutHelper.IS_GROUP_START:
return handleFieldOrGroupStart(bibtex, database);
case LayoutHelper.IS_FIELD_END:
case LayoutHelper.IS_GROUP_END:
return "";
return handleFieldOrGroupStart(bibEntry, database);
case LayoutHelper.IS_OPTION_FIELD:
return handleOptionField(bibtex, database);
return handleOptionField(bibEntry, database);
case LayoutHelper.IS_ENCODING_NAME:
// Printing the encoding name is not supported in entry layouts, only
// in begin/end layouts. This prevents breakage if some users depend
// on a field called "encoding". We simply return this field instead:
return bibtex.getResolvedFieldOrAlias(new UnknownField("encoding"), database).orElse(null);
return bibEntry.getResolvedFieldOrAlias(new UnknownField("encoding"), database).orElse(null);
default:
return "";
}
}

private String resolveFieldEntry(BibEntry bidEntry, BibDatabase database) {
// resolve field (recognized by leading backslash) or text
if (text.startsWith("\\")) {
return bidEntry.getResolvedFieldOrAlias(FieldFactory.parseField(text.substring(1)), database)
.orElse("");
}
if (database == null) {
return text;
}
return database.resolveForStrings(text);
}

private String handleOptionField(BibEntry bibtex, BibDatabase database) {
String fieldEntry;

Expand All @@ -239,12 +248,7 @@ private String handleOptionField(BibEntry bibtex, BibDatabase database) {
LOGGER.warn("'{}' is an obsolete name for the entry type. Please update your layout to use '{}' instead.", InternalField.OBSOLETE_TYPE_HEADER, InternalField.TYPE_HEADER);
fieldEntry = bibtex.getType().getDisplayName();
} else {
// changed section begin - arudert
// resolve field (recognized by leading backslash) or text
fieldEntry = text.startsWith("\\") ? bibtex
.getResolvedFieldOrAlias(FieldFactory.parseField(text.substring(1)), database)
.orElse("") : BibDatabase.getText(text, database);
// changed section end - arudert
fieldEntry = resolveFieldEntry(bibtex, database);
}

if (option != null) {
Expand Down Expand Up @@ -360,7 +364,9 @@ public String doLayout(BibDatabaseContext databaseContext, Charset encoding) {
throw new UnsupportedOperationException("field and group ends not allowed in begin or end layout");

case LayoutHelper.IS_OPTION_FIELD:
String field = BibDatabase.getText(text, databaseContext.getDatabase());
String field = Optional.ofNullable(databaseContext.getDatabase())
.map(db -> db.resolveForStrings(text))
.orElse(text);
if (option != null) {
for (LayoutFormatter anOption : option) {
field = anOption.format(field);
Expand Down
17 changes: 0 additions & 17 deletions src/main/java/org/jabref/model/database/BibDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import org.jspecify.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -85,22 +84,6 @@ public BibDatabase() {
this.registerListener(new KeyChangeListener(this));
}

/**
* Returns a text with references resolved according to an optionally given database.
*
* @param toResolve The text to resolve.
* @param database The database to use for resolving the text.
* @return The resolved text or the original text if either the text or the database are null
* @deprecated use {@link BibDatabase#resolveForStrings(String)}
*/
@Deprecated
public static String getText(@Nullable String toResolve, @Nullable BibDatabase database) {
if ((toResolve != null) && (database != null)) {
return database.resolveForStrings(toResolve);
}
return toResolve;
}

/**
* Returns the number of entries.
*/
Expand Down
47 changes: 42 additions & 5 deletions src/test/java/org/jabref/logic/layout/LayoutEntryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@

import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.SpecialField;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.field.UnknownField;
import org.jabref.model.metadata.MetaData;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;

/**
Expand All @@ -23,20 +31,17 @@
* instance of LayoutEntry will be instantiated via Layout and LayoutHelper. With these instance the doLayout() Method
* is called several times for each test case. To simulate a search, a BibEntry will be created, which will be used by
* LayoutEntry.
*
* There are five test cases: - The shown result text has no words which should be highlighted. - There is one word
* which will be highlighted ignoring case sensitivity. - There are two words which will be highlighted ignoring case
* sensitivity. - There is one word which will be highlighted case sensitivity. - There are more words which will be
* highlighted case sensitivity.
*/

public class LayoutEntryTest {

private BibEntry mBTE;
class LayoutEntryTest {

@BeforeEach
void setUp() {
mBTE = new BibEntry();
BibEntry mBTE = new BibEntry();
mBTE.setField(StandardField.ABSTRACT, "In this paper, we initiate a formal study of security on Android: Google's new open-source platform for mobile devices. Tags: Paper android google Open-Source Devices");
// Specifically, we present a core typed language to describe Android applications, and to reason about their data-flow security properties. Our operational semantics and type system provide some necessary foundations to help both users and developers of Android applications deal with their security concerns.
mBTE.setField(StandardField.KEYWORDS, "android, mobile devices, security");
Expand Down Expand Up @@ -95,4 +100,36 @@ void parseMethodCalls() {
assertEquals("test", (LayoutEntry.parseMethodsCalls("bla(test),foo(fark)").getFirst()).get(1));
assertEquals("fark", (LayoutEntry.parseMethodsCalls("bla(test),foo(fark)").get(1)).get(1));
}

@ParameterizedTest
@CsvSource({
"2",
"3",
"4",
"6",
"7"
})
void unsupportedOperationTypes(int type) {
List<StringInt> parsedEntries = List.of(new StringInt("place_holder", 0),
new StringInt("testString", 0));
BibDatabaseContext bibDatabaseContext = new BibDatabaseContext(new BibDatabase(), new MetaData(), null);
LayoutEntry layoutEntry = new LayoutEntry(parsedEntries, type, null, null, null);
assertThrows(UnsupportedOperationException.class, () -> layoutEntry.doLayout(bibDatabaseContext, StandardCharsets.UTF_8));
}

@ParameterizedTest
@CsvSource({
"1, testString",
"5, testString",
"8, UTF-8",
"9, ''",
"10, ''"
})
void layoutResult(int type, String expectedValue) {
List<StringInt> parsedEntries = List.of(new StringInt("place_holder", 0),
new StringInt("testString", 0));
BibDatabaseContext bibDatabaseContext = new BibDatabaseContext(new BibDatabase(), new MetaData(), null);
LayoutEntry layoutEntry = new LayoutEntry(parsedEntries, type, null, null, null);
assertEquals(expectedValue, layoutEntry.doLayout(bibDatabaseContext, StandardCharsets.UTF_8));
}
}

0 comments on commit e02ebb5

Please sign in to comment.